Skip to content

Commit 56dd9f0

Browse files
ryanofskysipa
authored andcommitted
Make VectorFormatter support stateful formatters
1 parent 3ca574c commit 56dd9f0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/prevector.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,20 @@ class prevector {
424424
return first;
425425
}
426426

427-
void push_back(const T& value) {
427+
template<typename... Args>
428+
void emplace_back(Args&&... args) {
428429
size_type new_size = size() + 1;
429430
if (capacity() < new_size) {
430431
change_capacity(new_size + (new_size >> 1));
431432
}
432-
new(item_ptr(size())) T(value);
433+
new(item_ptr(size())) T(std::forward<Args>(args)...);
433434
_size++;
434435
}
435436

437+
void push_back(const T& value) {
438+
emplace_back(value);
439+
}
440+
436441
void pop_back() {
437442
erase(end() - 1, end());
438443
}

src/serialize.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,23 +613,25 @@ BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
613613
* as a vector of VarInt-encoded integers.
614614
*
615615
* V is not required to be an std::vector type. It works for any class that
616-
* exposes a value_type, size, reserve, push_back, and const iterators.
616+
* exposes a value_type, size, reserve, emplace_back, back, and const iterators.
617617
*/
618618
template<class Formatter>
619619
struct VectorFormatter
620620
{
621621
template<typename Stream, typename V>
622622
void Ser(Stream& s, const V& v)
623623
{
624+
Formatter formatter;
624625
WriteCompactSize(s, v.size());
625626
for (const typename V::value_type& elem : v) {
626-
s << Using<Formatter>(elem);
627+
formatter.Ser(s, elem);
627628
}
628629
}
629630

630631
template<typename Stream, typename V>
631632
void Unser(Stream& s, V& v)
632633
{
634+
Formatter formatter;
633635
v.clear();
634636
size_t size = ReadCompactSize(s);
635637
size_t allocated = 0;
@@ -641,9 +643,8 @@ struct VectorFormatter
641643
allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
642644
v.reserve(allocated);
643645
while (v.size() < allocated) {
644-
typename V::value_type val;
645-
s >> Using<Formatter>(val);
646-
v.push_back(std::move(val));
646+
v.emplace_back();
647+
formatter.Unser(s, v.back());
647648
}
648649
}
649650
};

0 commit comments

Comments
 (0)