diff --git a/src/serialize.h b/src/serialize.h index fd0a20e346f..75d6b521541 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -723,6 +723,20 @@ inline void Unserialize(Stream& is, T&& a) a.Unserialize(is); } +/** Default formatter. Serializes objects as themselves. + * + * The vector/prevector serialization code passes this to VectorFormatter + * to enable reusing that logic. It shouldn't be needed elsewhere. + */ +struct DefaultFormatter +{ + template + static void Ser(Stream& s, const T& t) { Serialize(s, t); } + + template + static void Unser(Stream& s, T& t) { Unserialize(s, t); } +}; + @@ -763,9 +777,7 @@ void Serialize_impl(Stream& os, const prevector& v, const unsigned char&) template void Serialize_impl(Stream& os, const prevector& v, const V&) { - WriteCompactSize(os, v.size()); - for (typename prevector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - ::Serialize(os, (*vi)); + Serialize(os, Using>(v)); } template @@ -794,19 +806,7 @@ void Unserialize_impl(Stream& is, prevector& v, const unsigned char&) template void Unserialize_impl(Stream& is, prevector& v, const V&) { - v.clear(); - unsigned int nSize = ReadCompactSize(is); - unsigned int i = 0; - unsigned int nMid = 0; - while (nMid < nSize) - { - nMid += MAX_VECTOR_ALLOCATE / sizeof(T); - if (nMid > nSize) - nMid = nSize; - v.resize_uninitialized(nMid); - for (; i < nMid; ++i) - Unserialize(is, v[i]); - } + Unserialize(is, Using>(v)); } template @@ -843,9 +843,7 @@ void Serialize_impl(Stream& os, const std::vector& v, const bool&) template void Serialize_impl(Stream& os, const std::vector& v, const V&) { - WriteCompactSize(os, v.size()); - for (typename std::vector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - ::Serialize(os, (*vi)); + Serialize(os, Using>(v)); } template @@ -874,19 +872,7 @@ void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&) template void Unserialize_impl(Stream& is, std::vector& v, const V&) { - v.clear(); - unsigned int nSize = ReadCompactSize(is); - unsigned int i = 0; - unsigned int nMid = 0; - while (nMid < nSize) - { - nMid += MAX_VECTOR_ALLOCATE / sizeof(T); - if (nMid > nSize) - nMid = nSize; - v.resize(nMid); - for (; i < nMid; i++) - Unserialize(is, v[i]); - } + Unserialize(is, Using>(v)); } template