Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
* template <typename Stream> T::T(deserialize_type, Stream& s) | * template <typename Stream> T::T(deserialize_type, Stream& s) | ||||
* | * | ||||
* is a deserializing constructor, which builds the type by deserializing it | * is a deserializing constructor, which builds the type by deserializing it | ||||
* from s. If T contains const fields, this is likely the only way to do so. | * from s. If T contains const fields, this is likely the only way to do so. | ||||
*/ | */ | ||||
struct deserialize_type {}; | struct deserialize_type {}; | ||||
constexpr deserialize_type deserialize{}; | constexpr deserialize_type deserialize{}; | ||||
/** | |||||
* Used to bypass the rule against non-const reference to temporary | |||||
* where it makes sense with wrappers. | |||||
*/ | |||||
template <typename T> inline T &REF(const T &val) { | |||||
return const_cast<T &>(val); | |||||
} | |||||
/** | |||||
* Used to acquire a non-const pointer "this" to generate bodies of const | |||||
* serialization operations from a template | |||||
*/ | |||||
template <typename T> inline T *NCONST_PTR(const T *val) { | |||||
return const_cast<T *>(val); | |||||
} | |||||
//! Safely convert odd char pointer types to standard ones. | //! Safely convert odd char pointer types to standard ones. | ||||
inline char *CharCast(char *c) { | inline char *CharCast(char *c) { | ||||
return c; | return c; | ||||
} | } | ||||
inline char *CharCast(uint8_t *c) { | inline char *CharCast(uint8_t *c) { | ||||
return (char *)c; | return (char *)c; | ||||
} | } | ||||
inline const char *CharCast(const char *c) { | inline const char *CharCast(const char *c) { | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | |||||
#define SER_READ(obj, code) \ | #define SER_READ(obj, code) \ | ||||
::SerRead( \ | ::SerRead( \ | ||||
s, ser_action, obj, \ | s, ser_action, obj, \ | ||||
[&](Stream &s, typename std::remove_const<Type>::type &obj) { code; }) | [&](Stream &s, typename std::remove_const<Type>::type &obj) { code; }) | ||||
#define SER_WRITE(obj, code) \ | #define SER_WRITE(obj, code) \ | ||||
::SerWrite(s, ser_action, obj, [&](Stream &s, const Type &obj) { code; }) | ::SerWrite(s, ser_action, obj, [&](Stream &s, const Type &obj) { code; }) | ||||
/** | /** | ||||
* Implement three methods for serializable objects. These are actually wrappers | |||||
* over "SerializationOp" template, which implements the body of each class' | |||||
* serialization code. Adding "ADD_SERIALIZE_METHODS" in the body of the class | |||||
* causes these wrappers to be added as members. | |||||
*/ | |||||
#define ADD_SERIALIZE_METHODS \ | |||||
template <typename Stream> void Serialize(Stream &s) const { \ | |||||
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \ | |||||
} \ | |||||
template <typename Stream> void Unserialize(Stream &s) { \ | |||||
SerializationOp(s, CSerActionUnserialize()); \ | |||||
} | |||||
/** | |||||
* Implement the Ser and Unser methods needed for implementing a formatter | * Implement the Ser and Unser methods needed for implementing a formatter | ||||
* (see Using below). | * (see Using below). | ||||
* | * | ||||
* Both Ser and Unser are delegated to a single static method SerializationOps, | * Both Ser and Unser are delegated to a single static method SerializationOps, | ||||
* which is polymorphic in the serialized/deserialized type (allowing it to be | * which is polymorphic in the serialized/deserialized type (allowing it to be | ||||
* const when serializing, and non-const when deserializing). | * const when serializing, and non-const when deserializing). | ||||
* | * | ||||
* Example use: | * Example use: | ||||
▲ Show 20 Lines • Show All 875 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
template <typename Stream, typename T> | template <typename Stream, typename T> | ||||
void Unserialize(Stream &is, std::shared_ptr<const T> &p) { | void Unserialize(Stream &is, std::shared_ptr<const T> &p) { | ||||
p = std::make_shared<const T>(deserialize, is); | p = std::make_shared<const T>(deserialize, is); | ||||
} | } | ||||
/** | /** | ||||
* Support for ADD_SERIALIZE_METHODS and READWRITE macro | * Support for SERIALIZE_METHODS and READWRITE macro. | ||||
*/ | */ | ||||
struct CSerActionSerialize { | struct CSerActionSerialize { | ||||
constexpr bool ForRead() const { return false; } | constexpr bool ForRead() const { return false; } | ||||
}; | }; | ||||
struct CSerActionUnserialize { | struct CSerActionUnserialize { | ||||
constexpr bool ForRead() const { return true; } | constexpr bool ForRead() const { return true; } | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 104 Lines • Show Last 20 Lines |