Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 922 Lines • ▼ Show 20 Lines | |||||
* serialization, it may be worthwhile to implement a specialized version for | * serialization, it may be worthwhile to implement a specialized version for | ||||
* CSizeComputer, which uses the s.seek() method to record bytes that would | * CSizeComputer, which uses the s.seek() method to record bytes that would | ||||
* be written instead. | * be written instead. | ||||
*/ | */ | ||||
class CSizeComputer { | class CSizeComputer { | ||||
protected: | protected: | ||||
size_t nSize; | size_t nSize; | ||||
const int nType; | |||||
const int nVersion; | const int nVersion; | ||||
public: | public: | ||||
CSizeComputer(int nTypeIn, int nVersionIn) | explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {} | ||||
: nSize(0), nType(nTypeIn), nVersion(nVersionIn) {} | |||||
void write(const char *psz, size_t _nSize) { this->nSize += _nSize; } | void write(const char *psz, size_t _nSize) { this->nSize += _nSize; } | ||||
/** Pretend _nSize bytes are written, without specifying them. */ | /** Pretend _nSize bytes are written, without specifying them. */ | ||||
void seek(size_t _nSize) { this->nSize += _nSize; } | void seek(size_t _nSize) { this->nSize += _nSize; } | ||||
template <typename T> CSizeComputer &operator<<(const T &obj) { | template <typename T> CSizeComputer &operator<<(const T &obj) { | ||||
::Serialize(*this, obj); | ::Serialize(*this, obj); | ||||
return (*this); | return (*this); | ||||
} | } | ||||
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; } | |||||
}; | }; | ||||
template <typename Stream> void SerializeMany(Stream &s) {} | template <typename Stream> void SerializeMany(Stream &s) {} | ||||
template <typename Stream, typename Arg, typename... Args> | template <typename Stream, typename Arg, typename... Args> | ||||
void SerializeMany(Stream &s, const Arg &arg, const Args &... args) { | void SerializeMany(Stream &s, const Arg &arg, const Args &... args) { | ||||
::Serialize(s, arg); | ::Serialize(s, arg); | ||||
::SerializeMany(s, args...); | ::SerializeMany(s, args...); | ||||
Show All 22 Lines | |||||
template <typename I> inline void WriteVarInt(CSizeComputer &s, I n) { | template <typename I> inline void WriteVarInt(CSizeComputer &s, I n) { | ||||
s.seek(GetSizeOfVarInt<I>(n)); | s.seek(GetSizeOfVarInt<I>(n)); | ||||
} | } | ||||
inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize) { | inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize) { | ||||
s.seek(GetSizeOfCompactSize(nSize)); | s.seek(GetSizeOfCompactSize(nSize)); | ||||
} | } | ||||
template <typename T> | template <typename T> size_t GetSerializeSize(const T &t, int nVersion = 0) { | ||||
size_t GetSerializeSize(const T &t, int nType, int nVersion) { | return (CSizeComputer(nVersion) << t).size(); | ||||
return (CSizeComputer(nType, nVersion) << t).size(); | |||||
} | } | ||||
template <typename S, typename T> | template <typename S, typename T> | ||||
size_t GetSerializeSize(const S &s, const T &t) { | size_t GetSerializeSize(const S &s, const T &t) { | ||||
return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size(); | return (CSizeComputer(s.GetVersion()) << t).size(); | ||||
} | } | ||||
#endif // BITCOIN_SERIALIZE_H | #endif // BITCOIN_SERIALIZE_H |