Skip to content

Commit fd29a50

Browse files
ryanofskyfurszy
authored andcommitted
Make VectorFormatter support stateful formatters
1 parent 4e2afad commit fd29a50

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
@@ -422,15 +422,20 @@ class prevector {
422422
return first;
423423
}
424424

425-
void push_back(const T& value) {
425+
template<typename... Args>
426+
void emplace_back(Args&&... args) {
426427
size_type new_size = size() + 1;
427428
if (capacity() < new_size) {
428429
change_capacity(new_size + (new_size >> 1));
429430
}
430-
new(item_ptr(size())) T(value);
431+
new(item_ptr(size())) T(std::forward<Args>(args)...);
431432
_size++;
432433
}
433434

435+
void push_back(const T& value) {
436+
emplace_back(value);
437+
}
438+
434439
void pop_back() {
435440
erase(end() - 1, end());
436441
}

src/serialize.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,23 +643,25 @@ BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
643643
* as a vector of VarInt-encoded integers.
644644
*
645645
* V is not required to be an std::vector type. It works for any class that
646-
* exposes a value_type, size, reserve, push_back, and const iterators.
646+
* exposes a value_type, size, reserve, emplace_back, back, and const iterators.
647647
*/
648648
template<class Formatter>
649649
struct VectorFormatter
650650
{
651651
template<typename Stream, typename V>
652652
void Ser(Stream& s, const V& v)
653653
{
654+
Formatter formatter;
654655
WriteCompactSize(s, v.size());
655656
for (const typename V::value_type& elem : v) {
656-
s << Using<Formatter>(elem);
657+
formatter.Ser(s, elem);
657658
}
658659
}
659660

660661
template<typename Stream, typename V>
661662
void Unser(Stream& s, V& v)
662663
{
664+
Formatter formatter;
663665
v.clear();
664666
size_t size = ReadCompactSize(s);
665667
size_t allocated = 0;
@@ -671,9 +673,8 @@ struct VectorFormatter
671673
allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
672674
v.reserve(allocated);
673675
while (v.size() < allocated) {
674-
typename V::value_type val;
675-
s >> Using<Formatter>(val);
676-
v.push_back(std::move(val));
676+
v.emplace_back();
677+
formatter.Unser(s, v.back());
677678
}
678679
}
679680
};

0 commit comments

Comments
 (0)