Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | |||||
enum { | enum { | ||||
// primary actions | // primary actions | ||||
SER_NETWORK = (1 << 0), | SER_NETWORK = (1 << 0), | ||||
SER_DISK = (1 << 1), | SER_DISK = (1 << 1), | ||||
SER_GETHASH = (1 << 2), | SER_GETHASH = (1 << 2), | ||||
}; | }; | ||||
#define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action)) | #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) | ||||
#define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) | |||||
/** | /** | ||||
* Implement three methods for serializable objects. These are actually wrappers | * Implement three methods for serializable objects. These are actually wrappers | ||||
* over "SerializationOp" template, which implements the body of each class' | * over "SerializationOp" template, which implements the body of each class' | ||||
* serialization code. Adding "ADD_SERIALIZE_METHODS" in the body of the class | * serialization code. Adding "ADD_SERIALIZE_METHODS" in the body of the class | ||||
* causes these wrappers to be added as members. | * causes these wrappers to be added as members. | ||||
*/ | */ | ||||
#define ADD_SERIALIZE_METHODS \ | #define ADD_SERIALIZE_METHODS \ | ||||
▲ Show 20 Lines • Show All 655 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
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; } | ||||
}; | }; | ||||
template <typename Stream, typename T> | |||||
inline void SerReadWrite(Stream &s, const T &obj, | |||||
CSerActionSerialize ser_action) { | |||||
::Serialize(s, obj); | |||||
} | |||||
template <typename Stream, typename T> | |||||
inline void SerReadWrite(Stream &s, T &obj, CSerActionUnserialize ser_action) { | |||||
::Unserialize(s, obj); | |||||
} | |||||
/** | /** | ||||
* ::GetSerializeSize implementations | * ::GetSerializeSize implementations | ||||
* | * | ||||
* Computing the serialized size of objects is done through a special stream | * Computing the serialized size of objects is done through a special stream | ||||
* object of type CSizeComputer, which only records the number of bytes written | * object of type CSizeComputer, which only records the number of bytes written | ||||
* to it. | * to it. | ||||
* | * | ||||
* If your Serialize or SerializationOp method has non-trivial overhead for | * If your Serialize or SerializationOp method has non-trivial overhead for | ||||
Show All 25 Lines | public: | ||||
size_t size() const { return nSize; } | size_t size() const { return nSize; } | ||||
int GetVersion() const { return nVersion; } | int GetVersion() const { return nVersion; } | ||||
int GetType() const { return nType; } | int GetType() const { return nType; } | ||||
}; | }; | ||||
template <typename Stream> void SerializeMany(Stream &s) {} | template <typename Stream> void SerializeMany(Stream &s) {} | ||||
template <typename Stream, typename Arg> | |||||
void SerializeMany(Stream &s, Arg &&arg) { | |||||
::Serialize(s, std::forward<Arg>(arg)); | |||||
} | |||||
template <typename Stream, typename Arg, typename... Args> | template <typename Stream, typename Arg, typename... Args> | ||||
void SerializeMany(Stream &s, Arg &&arg, Args &&... args) { | void SerializeMany(Stream &s, Arg &&arg, Args &&... args) { | ||||
::Serialize(s, std::forward<Arg>(arg)); | ::Serialize(s, std::forward<Arg>(arg)); | ||||
::SerializeMany(s, std::forward<Args>(args)...); | ::SerializeMany(s, std::forward<Args>(args)...); | ||||
} | } | ||||
template <typename Stream> inline void UnserializeMany(Stream &s) {} | template <typename Stream> inline void UnserializeMany(Stream &s) {} | ||||
template <typename Stream, typename Arg> | |||||
inline void UnserializeMany(Stream &s, Arg &arg) { | |||||
::Unserialize(s, arg); | |||||
} | |||||
template <typename Stream, typename Arg, typename... Args> | template <typename Stream, typename Arg, typename... Args> | ||||
inline void UnserializeMany(Stream &s, Arg &arg, Args &... args) { | inline void UnserializeMany(Stream &s, Arg &arg, Args &... args) { | ||||
::Unserialize(s, arg); | ::Unserialize(s, arg); | ||||
::UnserializeMany(s, args...); | ::UnserializeMany(s, args...); | ||||
} | } | ||||
template <typename Stream, typename... Args> | template <typename Stream, typename... Args> | ||||
inline void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, | inline void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, | ||||
Show All 29 Lines |