6 #ifndef FABCOIN_SERIALIZE_H 7 #define FABCOIN_SERIALIZE_H 27 static const unsigned int MAX_SIZE = 0x10000000;
51 return const_cast<T&
>(val);
61 return const_cast<T*
>(val);
70 s.write((
char*)&obj, 1);
75 s.write((
char*)&obj, 2);
80 s.write((
char*)&obj, 4);
87 s.write((
char*)&obj, 4);
94 s.write((
char*)&obj, 8);
99 s.read((
char*)&obj, 1);
105 s.read((
char*)&obj, 2);
111 s.read((
char*)&obj, 4);
118 s.read((
char*)&obj, 4);
125 s.read((
char*)&obj, 32);
134 s.read((
char*)&obj, 8);
139 union {
double x; uint64_t y; } tmp;
145 union {
float x; uint32_t y; } tmp;
151 union {
double x; uint64_t y; } tmp;
157 union {
float x; uint32_t y; } tmp;
179 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action)) 180 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) 188 #define ADD_SERIALIZE_METHODS \ 189 template<typename Stream> \ 190 void Serialize(Stream& s) const { \ 191 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \ 193 template<typename Stream> \ 194 void Unserialize(Stream& s) { \ 195 SerializationOp(s, CSerActionUnserialize()); \ 239 if (nSize < 253)
return sizeof(
unsigned char);
242 else return sizeof(
unsigned char) +
sizeof(uint64_t);
247 template<
typename Stream>
272 template<
typename Stream>
276 uint64_t nSizeRet = 0;
281 else if (chSize == 253)
285 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
287 else if (chSize == 254)
290 if (nSizeRet < 0x10000u)
291 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
296 if (nSizeRet < 0x100000000ULL)
297 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
299 if (nSizeRet > (uint64_t)MAX_SIZE)
300 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
304 template<
typename Stream>
350 template<
typename Stream,
typename I>
353 unsigned char tmp[(
sizeof(n)*8+6)/7];
356 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
367 template<
typename Stream,
typename I>
374 throw std::ios_base::failure(
"ReadVarInt(): size too large");
376 n = (n << 7) | (chData & 0x7F);
379 throw std::ios_base::failure(
"ReadVarInt(): size too large");
388 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) 389 #define VARINT(obj) REF(WrapVarInt(REF(obj))) 390 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj))) 391 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj))) 402 CFlatData(
void* pbeginIn,
void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
403 template <
class T,
class TAl>
406 pbegin = (
char*)v.data();
407 pend = (
char*)(v.data() + v.size());
409 template <
unsigned int N,
typename T,
typename S,
typename D>
412 pbegin = (
char*)v.
data();
413 pend = (
char*)(v.
data() + v.
size());
416 const char*
begin()
const {
return pbegin; }
417 char*
end() {
return pend; }
418 const char*
end()
const {
return pend; }
420 template<
typename Stream>
423 s.write(pbegin, pend - pbegin);
426 template<
typename Stream>
429 s.read(pbegin, pend - pbegin);
441 template<
typename Stream>
443 WriteVarInt<Stream,I>(s, n);
446 template<
typename Stream>
448 n = ReadVarInt<Stream,I>(s);
459 template<
typename Stream>
461 WriteCompactSize<Stream>(s, n);
464 template<
typename Stream>
466 n = ReadCompactSize<Stream>(s);
470 template<
size_t Limit>
478 template<
typename Stream>
483 throw std::ios_base::failure(
"String length limit exceeded");
487 s.read((
char*)
string.
data(), size);
490 template<
typename Stream>
495 s.write((
char*)
string.
data(),
string.
size());
509 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
510 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
527 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
528 template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
529 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
530 template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
531 template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
532 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
537 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
538 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
543 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
544 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
549 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
550 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
555 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
556 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
561 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
562 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
569 template<
typename Stream,
typename T>
575 template<
typename Stream,
typename T>
588 template<
typename Stream,
typename C>
589 void Serialize(Stream& os,
const std::basic_string<C>& str)
593 os.write((
char*)str.data(), str.size() *
sizeof(C));
596 template<
typename Stream,
typename C>
602 is.read((
char*)str.data(), nSize *
sizeof(C));
610 template<
typename Stream,
unsigned int N,
typename T>
615 os.write((
char*)v.
data(), v.
size() *
sizeof(
T));
618 template<
typename Stream,
unsigned int N,
typename T,
typename V>
626 template<
typename Stream,
unsigned int N,
typename T>
633 template<
typename Stream,
unsigned int N,
typename T>
642 unsigned int blk =
std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
644 is.read((
char*)&v[i], blk *
sizeof(
T));
649 template<
typename Stream,
unsigned int N,
typename T,
typename V>
655 unsigned int nMid = 0;
658 nMid += 5000000 /
sizeof(
T);
662 for (; i < nMid; i++)
667 template<
typename Stream,
unsigned int N,
typename T>
678 template<
typename Stream,
typename T,
typename A>
679 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&)
683 os.write((
char*)v.data(), v.size() *
sizeof(
T));
686 template<
typename Stream,
typename T,
typename A,
typename V>
690 for (
typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
694 template<
typename Stream,
typename T,
typename A>
695 inline void Serialize(Stream& os,
const std::vector<T, A>& v)
701 template<
typename Stream,
typename T,
typename A>
710 unsigned int blk =
std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
712 is.read((
char*)&v[i], blk *
sizeof(
T));
717 template<
typename Stream,
typename T,
typename A,
typename V>
723 unsigned int nMid = 0;
726 nMid += 5000000 /
sizeof(
T);
730 for (; i < nMid; i++)
735 template<
typename Stream,
typename T,
typename A>
746 template<
typename Stream,
typename K,
typename T>
753 template<
typename Stream,
typename K,
typename T>
765 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
766 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
769 for (
typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
773 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
778 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
779 for (
unsigned int i = 0; i < nSize; i++)
781 std::pair<K, T> item;
783 mi = m.insert(mi, item);
792 template<
typename Stream,
typename K,
typename Pred,
typename A>
793 void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
796 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
800 template<
typename Stream,
typename K,
typename Pred,
typename A>
805 typename std::set<K, Pred, A>::iterator it = m.begin();
806 for (
unsigned int i = 0; i < nSize; i++)
810 it = m.insert(it, key);
819 template<
typename Stream,
typename T>
void 820 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
825 template<
typename Stream,
typename T>
836 template<
typename Stream,
typename T>
void 837 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
842 template<
typename Stream,
typename T>
855 constexpr
bool ForRead()
const {
return false; }
859 constexpr
bool ForRead()
const {
return true; }
862 template<
typename Stream,
typename T>
868 template<
typename Stream,
typename T>
901 CSizeComputer(
int nTypeIn,
int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
903 void write(
const char *psz,
size_t _nSize)
905 this->nSize += _nSize;
911 this->nSize += _nSize;
929 template<
typename Stream>
934 template<
typename Stream,
typename Arg>
940 template<
typename Stream,
typename Arg,
typename... Args>
947 template<
typename Stream>
952 template<
typename Stream,
typename Arg>
958 template<
typename Stream,
typename Arg,
typename... Args>
965 template<
typename Stream,
typename... Args>
971 template<
typename Stream,
typename... Args>
980 s.
seek(GetSizeOfVarInt<I>(n));
988 template <
typename T>
994 template <
typename S,
typename T>
997 return (
CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
1000 #endif // FABCOIN_SERIALIZE_H uint32_t htobe32(uint32_t host_32bits)
uint64_t ser_double_to_uint64(double x)
void resize(size_type new_size)
uint32_t ser_float_to_uint32(float x)
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 Serialize(Stream &s) const
void Serialize(Stream &s) const
uint64_t htole64(uint64_t host_64bits)
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 SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, Args &&...args)
void Unserialize(Stream &s)
void UnserializeMany(Stream &s)
Dummy data type to identify deserializing constructors.
void Serialize(Stream &s, char a)
void Unserialize(Stream &s)
void Unserialize(Stream &s)
CSizeComputer & operator<<(const T &obj)
uint32_t le32toh(uint32_t little_endian_32bits)
uint16_t ser_readdata16(Stream &s)
constexpr bool ForRead() const
const char * begin() const
uint32_t ser_readdata32(Stream &s)
void ser_writedata16(Stream &s, uint16_t obj)
uint256 ReadUint256(Stream &is)
void SerReadWrite(Stream &s, const T &obj, CSerActionSerialize ser_action)
void Unserialize(Stream &s)
uint64_t le64toh(uint64_t little_endian_64bits)
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.
uint16_t le16toh(uint16_t little_endian_16bits)
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)
void Serialize(Stream &s) const
CFlatData(void *pbeginIn, void *pendIn)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
constexpr bool ForRead() const
void Serialize(Stream &s) const
uint8_t const size_t const size
uint256 ser_readdata256(Stream &s)
CFlatData(prevector< N, T, S, D > &v)
void write(const char *psz, size_t _nSize)
uint32_t ser_readdata32be(Stream &s)
void Unserialize(Stream &s, char &a)
uint16_t htole16(uint16_t host_16bits)
uint32_t htole32(uint32_t host_32bits)
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 &)
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)
uint32_t be32toh(uint32_t big_endian_32bits)