diff --git a/src/serialize.h b/src/serialize.h --- a/src/serialize.h +++ b/src/serialize.h @@ -855,6 +855,23 @@ 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); + } +}; + /** * string */ @@ -888,10 +905,7 @@ template void Serialize_impl(Stream &os, const prevector &v, const V &) { - WriteCompactSize(os, v.size()); - for (const T &i : v) { - ::Serialize(os, i); - } + Serialize(os, Using>(v)); } template @@ -915,20 +929,7 @@ template void Unserialize_impl(Stream &is, prevector &v, const V &) { - v.clear(); - size_t nSize = ReadCompactSize(is); - size_t i = 0; - size_t 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 @@ -960,10 +961,7 @@ template void Serialize_impl(Stream &os, const std::vector &v, const V &) { - WriteCompactSize(os, v.size()); - for (const T &i : v) { - ::Serialize(os, i); - } + Serialize(os, Using>(v)); } template @@ -987,20 +985,7 @@ template void Unserialize_impl(Stream &is, std::vector &v, const V &) { - v.clear(); - size_t nSize = ReadCompactSize(is); - size_t i = 0; - size_t 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