aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorRussell Yanofsky <russ@yanofsky.org>2020-02-15 19:09:09 -0800
committerPieter Wuille <pieter.wuille@gmail.com>2020-02-15 19:49:24 -0800
commit56dd9f04c701aa3ac340e95065bf83de20373c8b (patch)
treed8515059063517e9b2388efb54e365152af1a77b /src/serialize.h
parent3ca574cef0b4423f21b2c3efd8f5c9f71d52f219 (diff)
Make VectorFormatter support stateful formatters
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/serialize.h b/src/serialize.h
index e0e29bcf4a..d7d1be7c4b 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -613,7 +613,7 @@ BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
* as a vector of VarInt-encoded integers.
*
* V is not required to be an std::vector type. It works for any class that
- * exposes a value_type, size, reserve, push_back, and const iterators.
+ * exposes a value_type, size, reserve, emplace_back, back, and const iterators.
*/
template<class Formatter>
struct VectorFormatter
@@ -621,15 +621,17 @@ struct VectorFormatter
template<typename Stream, typename V>
void Ser(Stream& s, const V& v)
{
+ Formatter formatter;
WriteCompactSize(s, v.size());
for (const typename V::value_type& elem : v) {
- s << Using<Formatter>(elem);
+ formatter.Ser(s, elem);
}
}
template<typename Stream, typename V>
void Unser(Stream& s, V& v)
{
+ Formatter formatter;
v.clear();
size_t size = ReadCompactSize(s);
size_t allocated = 0;
@@ -641,9 +643,8 @@ struct VectorFormatter
allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
v.reserve(allocated);
while (v.size() < allocated) {
- typename V::value_type val;
- s >> Using<Formatter>(val);
- v.push_back(std::move(val));
+ v.emplace_back();
+ formatter.Unser(s, v.back());
}
}
};