diff --git a/src/compressor.h b/src/compressor.h index 65ea6be0dd76b..a2ced07df35d2 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -10,6 +10,7 @@ #include "primitives/transaction.h" #include "script/script.h" #include "serialize.h" +#include "span.h" class CKeyID; class CPubKey; @@ -63,12 +64,12 @@ class CScriptCompressor { std::vector compr; if (Compress(compr)) { - s << CFlatData(compr); + s << MakeSpan(compr); return; } unsigned int nSize = script.size() + nSpecialScripts; s << VARINT(nSize); - s << CFlatData(script); + s << MakeSpan(script); } template @@ -78,7 +79,7 @@ class CScriptCompressor s >> VARINT(nSize); if (nSize < nSpecialScripts) { std::vector vch(GetSpecialSize(nSize), 0x00); - s >> CFlatData(vch); + s >> MakeSpan(vch); Decompress(nSize, vch); return; } @@ -89,7 +90,7 @@ class CScriptCompressor s.ignore(nSize); } else { script.resize(nSize); - s >> CFlatData(script); + s >> MakeSpan(script); } } }; diff --git a/src/netaddress.h b/src/netaddress.h index e5f3ef6e19a52..f3dfc6fc1fd7f 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -12,6 +12,7 @@ #include "compat.h" #include "serialize.h" +#include "span.h" #include #include @@ -171,10 +172,13 @@ class CService : public CNetAddr inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(ip); + + // TODO: introduce native support for BE serialization in serialize.h unsigned short portN = htons(port); - READWRITE(FLATDATA(portN)); - if (ser_action.ForRead()) + READWRITE(Span((unsigned char*)&portN, 2)); + if (ser_action.ForRead()) { port = ntohs(portN); + } } }; diff --git a/src/serialize.h b/src/serialize.h index dde4883be959a..5fc811a2b6c5d 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -27,6 +27,7 @@ #include "libzerocoin/SpendType.h" #include "optional.h" #include "prevector.h" +#include "span.h" #include "sporkid.h" class CScript; @@ -49,7 +50,7 @@ constexpr deserialize_type deserialize {}; /** * Used to bypass the rule against non-const reference to temporary - * where it makes sense with wrappers such as CFlatData or CTxDB + * where it makes sense with wrappers. */ template inline T& REF(const T& val) @@ -203,6 +204,8 @@ template inline void Serialize(Stream& s, float a ) { ser_wri template inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); } template inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); } template inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); } +template inline void Serialize(Stream& s, const Span& span) { s.write(CharCast(span.data()), span.size()); } +template inline void Serialize(Stream& s, const Span& span) { s.write(CharCast(span.data()), span.size()); } template inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char template inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } @@ -217,6 +220,7 @@ template inline void Unserialize(Stream& s, float& a ) { a = template inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); } template inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); } template inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); } +template inline void Unserialize(Stream& s, Span& span) { s.read(CharCast(span.data()), span.size()); } template inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); } template inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; } @@ -433,52 +437,10 @@ I ReadVarInt(Stream& is) } } -#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)) #define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj)) #define COMPACTSIZE(obj) CCompactSize(REF(obj)) #define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj)) -/** - * Wrapper for serializing arrays and POD. - */ -class CFlatData -{ -protected: - char* pbegin; - char* pend; - -public: - CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) {} - template - explicit CFlatData(std::vector& v) - { - pbegin = (char*)v.data(); - pend = (char*)(v.data() + v.size()); - } - template - explicit CFlatData(prevector &v) - { - pbegin = (char*)v.data(); - pend = (char*)(v.data() + v.size()); - } - char* begin() { return pbegin; } - const char* begin() const { return pbegin; } - char* end() { return pend; } - const char* end() const { return pend; } - - template - void Serialize(Stream& s) const - { - s.write(pbegin, pend - pbegin); - } - - template - void Unserialize(Stream& s) - { - s.read(pbegin, pend - pbegin); - } -}; - template class CVarInt {