Bitcoin ABC 0.26.3
P2P Digital Currency
|
#include <compat/endian.h>
#include <prevector.h>
#include <rcu.h>
#include <span.h>
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <ios>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
Go to the source code of this file.
Classes | |
struct | deserialize_type |
Dummy data type to identify deserializing constructors. More... | |
struct | CheckVarIntMode< Mode, I > |
class | Wrapper< Formatter, T > |
Simple wrapper class to serialize objects using a formatter; used by Using(). More... | |
struct | VarIntFormatter< Mode > |
Serialization wrapper class for integers in VarInt format. More... | |
struct | CustomUintFormatter< Bytes, BigEndian > |
Serialization wrapper class for custom integers and enums. More... | |
struct | CompactSizeFormatter< RangeCheck > |
Formatter for integers in CompactSize format. More... | |
struct | ChronoFormatter< U, LOSSY > |
struct | LimitedStringFormatter< Limit > |
struct | VectorFormatter< Formatter > |
Formatter to serialize/deserialize vector elements using another formatter. More... | |
class | DifferenceFormatter |
Helper for differentially encoded Compact Size integers in lists. More... | |
struct | DifferentialIndexedItemFormatter |
Helper for a list of items containing a differentially encoded index as their first member. More... | |
struct | DefaultFormatter |
Default formatter. More... | |
struct | CSerActionSerialize |
Support for SERIALIZE_METHODS and READWRITE macro. More... | |
struct | CSerActionUnserialize |
class | CSizeComputer |
GetSerializeSize implementations More... | |
Macros | |
#define | READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) |
#define | READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj))) |
#define | SER_READ(obj, code) |
#define | SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream &s, const Type &obj) { code; }) |
#define | FORMATTER_METHODS(cls, obj) |
Implement the Ser and Unser methods needed for implementing a formatter (see Using below). | |
#define | SERIALIZE_METHODS(cls, obj) |
Implement the Serialize and Unserialize methods by delegating to a single templated static method that takes the to-be-(de)serialized object as a parameter. | |
#define | VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj) |
#define | VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj) |
#define | COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj) |
#define | LIMITED_STRING(obj, n) Using<LimitedStringFormatter<n>>(obj) |
Typedefs | |
template<int Bytes> | |
using | BigEndianFormatter = CustomUintFormatter< Bytes, true > |
template<typename U > | |
using | LossyChronoFormatter = ChronoFormatter< U, true > |
Enumerations | |
enum | { SER_NETWORK = (1 << 0) , SER_DISK = (1 << 1) , SER_GETHASH = (1 << 2) } |
enum class | VarIntMode { DEFAULT , NONNEGATIVE_SIGNED } |
Variable-length integers: bytes are a MSB base-128 encoding of the number. More... | |
Variables | |
static constexpr uint64_t | MAX_SIZE = 0x02000000 |
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size is encoded as CompactSize. | |
static const unsigned int | MAX_VECTOR_ALLOCATE = 5000000 |
Maximum amount of memory (in bytes) to allocate at once when deserializing vectors. | |
constexpr deserialize_type | deserialize {} |
Definition at line 580 of file serialize.h.
Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
Both Ser and Unser are delegated to a single static method SerializationOps, which is polymorphic in the serialized/deserialized type (allowing it to be const when serializing, and non-const when deserializing).
Example use: struct FooFormatter { FORMATTER_METHODS(Class, obj) { READWRITE(obj.val1, VARINT(obj.val2)); } } would define a class FooFormatter that defines a serialization of Class objects consisting of serializing its val1 member using the default serialization, and its val2 member using VARINT serialization. That FooFormatter can then be used in statements like READWRITE(Using<FooFormatter>(obj.bla)).
Definition at line 194 of file serialize.h.
#define LIMITED_STRING | ( | obj, | |
n | |||
) | Using<LimitedStringFormatter<n>>(obj) |
Definition at line 581 of file serialize.h.
#define READWRITE | ( | ... | ) | (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) |
Definition at line 166 of file serialize.h.
#define READWRITEAS | ( | type, | |
obj | |||
) | (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj))) |
Definition at line 167 of file serialize.h.
Definition at line 169 of file serialize.h.
#define SER_WRITE | ( | obj, | |
code | |||
) | ::SerWrite(s, ser_action, obj, [&](Stream &s, const Type &obj) { code; }) |
Definition at line 173 of file serialize.h.
Implement the Serialize and Unserialize methods by delegating to a single templated static method that takes the to-be-(de)serialized object as a parameter.
This approach has the advantage that the constness of the object becomes a template parameter, and thus allows a single implementation that sees the object as const for serializing and non-const for deserializing, without casts.
Definition at line 213 of file serialize.h.
#define VARINT | ( | obj | ) | Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj) |
Definition at line 579 of file serialize.h.
#define VARINT_MODE | ( | obj, | |
mode | |||
) | Using<VarIntFormatter<mode>>(obj) |
Definition at line 578 of file serialize.h.
using BigEndianFormatter = CustomUintFormatter<Bytes, true> |
Definition at line 644 of file serialize.h.
Definition at line 683 of file serialize.h.
Enumerator | |
---|---|
SER_NETWORK | |
SER_DISK | |
SER_GETHASH |
Definition at line 150 of file serialize.h.
|
strong |
Variable-length integers: bytes are a MSB base-128 encoding of the number.
The high bit in each byte signifies whether another digit follows. To make sure the encoding is one-to-one, one is subtracted from all but the last digit. Thus, the byte sequence a[] with length len, where all but the last byte has bit 128 set, encodes the number:
(a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
Properties:
0: [0x00] 256: [0x81 0x00] 1: [0x01] 16383: [0xFE 0x7F] 127: [0x7F] 16384: [0xFF 0x00] 128: [0x80 0x00] 16511: [0xFF 0x7F] 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F] 2^32: [0x8E 0xFE 0xFE 0xFF 0x00] Mode for encoding VarInts.
Currently there is no support for signed encodings. The default mode will not compile with signed values, and the legacy "nonnegative signed" mode will accept signed values, but improperly encode and decode them if they are negative. In the future, the DEFAULT mode could be extended to support negative numbers in a backwards compatible way, and additional modes could be added to support different varint formats (e.g. zigzag encoding).
Enumerator | |
---|---|
DEFAULT | |
NONNEGATIVE_SIGNED |
Definition at line 474 of file serialize.h.
Definition at line 1258 of file serialize.h.
Definition at line 1263 of file serialize.h.
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 bytes (254 + 4 bytes) size > UINT_MAX – 9 bytes (255 + 8 bytes)
Definition at line 373 of file serialize.h.
Decode a CompactSize-encoded variable-length integer.
As these are primarily used to encode the size of vector-like serializations, by default a range check is performed. When used as a generic number encoding, range_check should be set to false.
Definition at line 413 of file serialize.h.
const X & ReadWriteAsHelper | ( | const X & | x | ) |
Definition at line 162 of file serialize.h.
X & ReadWriteAsHelper | ( | X & | x | ) |
Convert the reference base type to X, without changing constness or reference type.
Definition at line 159 of file serialize.h.
Definition at line 113 of file serialize.h.
Definition at line 120 of file serialize.h.
Definition at line 88 of file serialize.h.
Definition at line 98 of file serialize.h.
Definition at line 108 of file serialize.h.
Definition at line 83 of file serialize.h.
Definition at line 59 of file serialize.h.
Definition at line 69 of file serialize.h.
Definition at line 79 of file serialize.h.
Lowest-level serialization and conversion.
Definition at line 55 of file serialize.h.
void Serialize | ( | Stream & | os, |
const std::basic_string< C > & | str | ||
) |
Forward declarations.
string
Definition at line 939 of file serialize.h.
void Serialize | ( | Stream & | os, |
const std::shared_ptr< const T > & | p | ||
) |
void Serialize | ( | Stream & | os, |
const std::unique_ptr< const T > & | p | ||
) |
|
inline |
If none of the specialized versions above matched, default to calling member function.
Definition at line 909 of file serialize.h.
|
inline |
|
inline |
|
inline |
prevector prevectors of uint8_t are a special case and are intended to be serialized as a single opaque blob.
prevector
Definition at line 959 of file serialize.h.
void Serialize_impl | ( | Stream & | os, |
const std::vector< T, A > & | v, | ||
const bool & | |||
) |
void Serialize_impl | ( | Stream & | os, |
const std::vector< T, A > & | v, | ||
const uint8_t & | |||
) |
vector vectors of uint8_t are a special case and are intended to be serialized as a single opaque blob.
vector
Definition at line 1004 of file serialize.h.
void Serialize_impl | ( | Stream & | os, |
const std::vector< T, A > & | v, | ||
const V & | |||
) |
|
inline |
Definition at line 1231 of file serialize.h.
|
inline |
|
inline |
Definition at line 1219 of file serialize.h.
|
inline |
|
inline |
|
inline |
Definition at line 1247 of file serialize.h.
void Unserialize | ( | Stream & | is, |
std::basic_string< C > & | str | ||
) |
|
inline |
void Unserialize | ( | Stream & | os, |
std::shared_ptr< const T > & | p | ||
) |
void Unserialize | ( | Stream & | os, |
std::unique_ptr< const T > & | p | ||
) |
|
inline |
|
inline |
|
inline |
Definition at line 977 of file serialize.h.
void Unserialize_impl | ( | Stream & | is, |
std::vector< T, A > & | v, | ||
const uint8_t & | |||
) |
void Unserialize_impl | ( | Stream & | is, |
std::vector< T, A > & | v, | ||
const V & | |||
) |
Cause serialization/deserialization of an object to be done using a specified formatter class.
To use this, you need a class Formatter that has public functions Ser(stream, const object&) for serialization, and Unser(stream, object&) for deserialization. Serialization routines (inside READWRITE, or directly with << and >> operators), can then use Using<Formatter>(object).
This works by constructing a Wrapper<Formatter, T>-wrapped version of object, where T is const during serialization, and non-const during deserialization, which maintains const correctness.
Definition at line 574 of file serialize.h.
|
inline |
Definition at line 1254 of file serialize.h.
|
inline |
|
constexpr |
Definition at line 50 of file serialize.h.
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size is encoded as CompactSize.
Definition at line 31 of file serialize.h.
Maximum amount of memory (in bytes) to allocate at once when deserializing vectors.
Definition at line 37 of file serialize.h.