Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 619 Lines • ▼ Show 20 Lines | |||||
template <typename Formatter, typename T> | template <typename Formatter, typename T> | ||||
static inline Wrapper<Formatter, T &> Using(T &&t) { | static inline Wrapper<Formatter, T &> Using(T &&t) { | ||||
return Wrapper<Formatter, T &>(t); | return Wrapper<Formatter, T &>(t); | ||||
} | } | ||||
#define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj) | #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj) | ||||
#define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj) | #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj) | ||||
#define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj) | #define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj) | ||||
#define LIMITED_STRING(obj, n) LimitedString<n>(REF(obj)) | #define LIMITED_STRING(obj, n) Using<LimitedStringFormatter<n>>(obj) | ||||
/** | /** | ||||
* Serialization wrapper class for integers in VarInt format. | * Serialization wrapper class for integers in VarInt format. | ||||
*/ | */ | ||||
template <VarIntMode Mode> struct VarIntFormatter { | template <VarIntMode Mode> struct VarIntFormatter { | ||||
template <typename Stream, typename I> void Ser(Stream &s, I v) { | template <typename Stream, typename I> void Ser(Stream &s, I v) { | ||||
WriteVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s, v); | WriteVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s, v); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | template <typename Stream, typename I> void Ser(Stream &s, I v) { | ||||
static_assert(std::numeric_limits<I>::max() <= | static_assert(std::numeric_limits<I>::max() <= | ||||
std::numeric_limits<uint64_t>::max(), | std::numeric_limits<uint64_t>::max(), | ||||
"CompactSize only supports 64-bit integers and below"); | "CompactSize only supports 64-bit integers and below"); | ||||
WriteCompactSize<Stream>(s, v); | WriteCompactSize<Stream>(s, v); | ||||
} | } | ||||
}; | }; | ||||
template <size_t Limit> class LimitedString { | template <size_t Limit> struct LimitedStringFormatter { | ||||
protected: | template <typename Stream> void Unser(Stream &s, std::string &v) { | ||||
std::string &string; | |||||
public: | |||||
explicit LimitedString(std::string &_string) : string(_string) {} | |||||
template <typename Stream> void Unserialize(Stream &s) { | |||||
size_t size = ReadCompactSize(s); | size_t size = ReadCompactSize(s); | ||||
if (size > Limit) { | if (size > Limit) { | ||||
throw std::ios_base::failure("String length limit exceeded"); | throw std::ios_base::failure("String length limit exceeded"); | ||||
} | } | ||||
string.resize(size); | v.resize(size); | ||||
if (size != 0) { | if (size != 0) { | ||||
s.read((char *)string.data(), size); | s.read((char *)v.data(), size); | ||||
} | } | ||||
} | } | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Ser(Stream &s, const std::string &v) { | ||||
WriteCompactSize(s, string.size()); | s << v; | ||||
if (!string.empty()) { | |||||
s.write((char *)string.data(), string.size()); | |||||
} | |||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Formatter to serialize/deserialize vector elements using another formatter | * Formatter to serialize/deserialize vector elements using another formatter | ||||
* | * | ||||
* Example: | * Example: | ||||
* struct X { | * struct X { | ||||
▲ Show 20 Lines • Show All 487 Lines • Show Last 20 Lines |