7 #ifndef RAVEN_SERIALIZE_H 8 #define RAVEN_SERIALIZE_H 27 static const unsigned int MAX_SIZE = 0x02000000;
48 inline T&
REF(
const T& val)
50 return const_cast<T&
>(val);
60 return const_cast<T*
>(val);
69 s.write((
char*)&obj, 1);
74 s.write((
char*)&obj, 2);
79 s.write((
char*)&obj, 4);
84 s.write((
char*)&obj, 4);
89 s.write((
char*)&obj, 8);
94 s.read((
char*)&obj, 1);
100 s.read((
char*)&obj, 2);
106 s.read((
char*)&obj, 4);
112 s.read((
char*)&obj, 4);
118 s.read((
char*)&obj, 8);
123 union {
double x; uint64_t y; } tmp;
129 union {
float x; uint32_t y; } tmp;
135 union {
double x; uint64_t y; } tmp;
141 union {
float x; uint32_t y; } tmp;
163 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action)) 164 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) 172 #define ADD_SERIALIZE_METHODS \ 173 template<typename Stream> \ 174 void Serialize(Stream& s) const { \ 175 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \ 177 template<typename Stream> \ 178 void Unserialize(Stream& s) { \ 179 SerializationOp(s, CSerActionUnserialize()); \ 223 if (nSize < 253)
return sizeof(
unsigned char);
224 else if (nSize <= std::numeric_limits<unsigned short>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned short);
225 else if (nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
226 else return sizeof(
unsigned char) +
sizeof(uint64_t);
231 template<
typename Stream>
238 else if (nSize <= std::numeric_limits<unsigned short>::max())
243 else if (nSize <= std::numeric_limits<unsigned int>::max())
256 template<
typename Stream>
260 uint64_t nSizeRet = 0;
265 else if (chSize == 253)
269 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
271 else if (chSize == 254)
274 if (nSizeRet < 0x10000u)
275 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
280 if (nSizeRet < 0x100000000ULL)
281 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
283 if (nSizeRet > (uint64_t)MAX_SIZE)
284 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
328 template<
typename Stream,
typename I>
331 unsigned char tmp[(
sizeof(n)*8+6)/7];
334 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
345 template<
typename Stream,
typename I>
351 if (n > (std::numeric_limits<I>::max() >> 7)) {
352 throw std::ios_base::failure(
"ReadVarInt(): size too large");
354 n = (n << 7) | (chData & 0x7F);
356 if (n == std::numeric_limits<I>::max()) {
357 throw std::ios_base::failure(
"ReadVarInt(): size too large");
366 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) 367 #define VARINT(obj) REF(WrapVarInt(REF(obj))) 368 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj))) 369 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj))) 380 CFlatData(
void* pbeginIn,
void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
381 template <
class T,
class TAl>
384 pbegin = (
char*)v.data();
385 pend = (
char*)(v.data() + v.size());
387 template <
unsigned int N,
typename T,
typename S,
typename D>
390 pbegin = (
char*)v.
data();
391 pend = (
char*)(v.
data() + v.
size());
394 const char*
begin()
const {
return pbegin; }
395 char*
end() {
return pend; }
396 const char*
end()
const {
return pend; }
398 template<
typename Stream>
401 s.write(pbegin, pend - pbegin);
404 template<
typename Stream>
407 s.read(pbegin, pend - pbegin);
419 template<
typename Stream>
421 WriteVarInt<Stream,I>(s, n);
424 template<
typename Stream>
426 n = ReadVarInt<Stream,I>(s);
437 template<
typename Stream>
439 WriteCompactSize<Stream>(s, n);
442 template<
typename Stream>
444 n = ReadCompactSize<Stream>(s);
448 template<
size_t Limit>
456 template<
typename Stream>
461 throw std::ios_base::failure(
"String length limit exceeded");
465 s.read((
char*)
string.data(), size);
468 template<
typename Stream>
473 s.write((
char*)
string.data(),
string.size());
487 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
488 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
505 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
506 template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
507 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
508 template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
509 template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
510 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
515 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
516 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
521 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
522 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
527 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
528 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
533 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
534 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
539 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
540 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
547 template<
typename Stream,
typename T>
553 template<
typename Stream,
typename T>
566 template<
typename Stream,
typename C>
567 void Serialize(Stream& os,
const std::basic_string<C>& str)
571 os.write((
char*)str.data(), str.size() *
sizeof(C));
574 template<
typename Stream,
typename C>
580 is.read((
char*)str.data(), nSize *
sizeof(C));
588 template<
typename Stream,
unsigned int N,
typename T>
593 os.write((
char*)v.
data(), v.
size() *
sizeof(T));
596 template<
typename Stream,
unsigned int N,
typename T,
typename V>
604 template<
typename Stream,
unsigned int N,
typename T>
611 template<
typename Stream,
unsigned int N,
typename T>
620 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(T)));
622 is.read((
char*)&v[i], blk *
sizeof(T));
627 template<
typename Stream,
unsigned int N,
typename T,
typename V>
633 unsigned int nMid = 0;
636 nMid += 5000000 /
sizeof(T);
640 for (; i < nMid; i++)
645 template<
typename Stream,
unsigned int N,
typename T>
656 template<
typename Stream,
typename T,
typename A>
657 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&)
661 os.write((
char*)v.data(), v.size() *
sizeof(T));
664 template<
typename Stream,
typename T,
typename A,
typename V>
668 for (
typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
672 template<
typename Stream,
typename T,
typename A>
673 inline void Serialize(Stream& os,
const std::vector<T, A>& v)
679 template<
typename Stream,
typename T,
typename A>
688 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(T)));
690 is.read((
char*)&v[i], blk *
sizeof(T));
695 template<
typename Stream,
typename T,
typename A,
typename V>
701 unsigned int nMid = 0;
704 nMid += 5000000 /
sizeof(T);
708 for (; i < nMid; i++)
713 template<
typename Stream,
typename T,
typename A>
724 template<
typename Stream,
typename K,
typename T>
731 template<
typename Stream,
typename K,
typename T>
743 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
744 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
747 for (
typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
751 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
756 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
757 for (
unsigned int i = 0; i < nSize; i++)
759 std::pair<K, T> item;
761 mi = m.insert(mi, item);
770 template<
typename Stream,
typename K,
typename Pred,
typename A>
771 void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
774 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
778 template<
typename Stream,
typename K,
typename Pred,
typename A>
783 typename std::set<K, Pred, A>::iterator it = m.begin();
784 for (
unsigned int i = 0; i < nSize; i++)
788 it = m.insert(it, key);
797 template<
typename Stream,
typename T>
void 798 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
803 template<
typename Stream,
typename T>
814 template<
typename Stream,
typename T>
void 815 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
820 template<
typename Stream,
typename T>
833 constexpr
bool ForRead()
const {
return false; }
837 constexpr
bool ForRead()
const {
return true; }
840 template<
typename Stream,
typename T>
846 template<
typename Stream,
typename T>
879 CSizeComputer(
int nTypeIn,
int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
881 void write(
const char *psz,
size_t _nSize)
883 this->nSize += _nSize;
889 this->nSize += _nSize;
907 template<
typename Stream>
912 template<
typename Stream,
typename Arg>
918 template<
typename Stream,
typename Arg,
typename... Args>
925 template<
typename Stream>
930 template<
typename Stream,
typename Arg>
936 template<
typename Stream,
typename Arg,
typename... Args>
943 template<
typename Stream,
typename... Args>
949 template<
typename Stream,
typename... Args>
958 s.
seek(GetSizeOfVarInt<I>(n));
966 template <
typename T>
972 template <
typename S,
typename T>
975 return (
CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
978 #endif // RAVEN_SERIALIZE_H uint64_t ser_double_to_uint64(double x)
void resize(size_type new_size)
uint32_t ser_float_to_uint32(float x)
void Serialize(Stream &s) const
uint8_t ser_readdata8(Stream &s)
void ser_writedata64(Stream &s, uint64_t obj)
uint64_t ReadCompactSize(Stream &is)
void WriteVarInt(CSizeComputer &os, I n)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, Args &&... args)
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
void ser_writedata32(Stream &s, uint32_t obj)
constexpr deserialize_type deserialize
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
CCompactSize(uint64_t &nIn)
CSizeComputer(int nTypeIn, int nVersionIn)
void Unserialize(Stream &s)
uint32_t be32toh(uint32_t big_endian_32bits)
void UnserializeMany(Stream &s)
uint32_t htole32(uint32_t host_32bits)
Dummy data type to identify deserializing constructors.
void Serialize(Stream &s) const
void Serialize(Stream &s, char a)
uint32_t htobe32(uint32_t host_32bits)
void Unserialize(Stream &s)
void Unserialize(Stream &s)
CSizeComputer & operator<<(const T &obj)
void Serialize(Stream &s) const
uint16_t ser_readdata16(Stream &s)
uint32_t ser_readdata32(Stream &s)
void Serialize(Stream &s) const
void ser_writedata16(Stream &s, uint16_t obj)
void SerReadWrite(Stream &s, const T &obj, CSerActionSerialize ser_action)
void Unserialize(Stream &s)
#define S(x0, x1, x2, x3, cb, r)
constexpr bool ForRead() const
uint16_t le16toh(uint16_t little_endian_16bits)
void ser_writedata32be(Stream &s, uint32_t obj)
double ser_uint64_to_double(uint64_t y)
unsigned int GetSizeOfVarInt(I n)
Variable-length integers: bytes are a MSB base-128 encoding of the number.
Support for ADD_SERIALIZE_METHODS and READWRITE macro.
constexpr bool ForRead() const
void SerializeMany(Stream &s)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
uint64_t ser_readdata64(Stream &s)
const char * begin() const
CFlatData(void *pbeginIn, void *pendIn)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
uint16_t htole16(uint16_t host_16bits)
uint64_t le64toh(uint64_t little_endian_64bits)
CFlatData(prevector< N, T, S, D > &v)
uint64_t htole64(uint64_t host_64bits)
void write(const char *psz, size_t _nSize)
uint32_t ser_readdata32be(Stream &s)
void Unserialize(Stream &s, char &a)
CFlatData(std::vector< T, TAl > &v)
void Serialize_impl(Stream &os, const prevector< N, T > &v, const unsigned char &)
prevector prevectors of unsigned char are a special case and are intended to be serialized as a singl...
T * NCONST_PTR(const T *val)
Used to acquire a non-const pointer "this" to generate bodies of const serialization operations from ...
void Unserialize_impl(Stream &is, prevector< N, T > &v, const unsigned char &)
uint32_t le32toh(uint32_t little_endian_32bits)
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
LimitedString(std::string &_string)
CVarInt< I > WrapVarInt(I &n)
void ser_writedata8(Stream &s, uint8_t obj)
Wrapper for serializing arrays and POD.
float ser_uint32_to_float(uint32_t y)