Skip to content

Commit

Permalink
Make std::vector and prevector reuse the VectorFormatter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and furszy committed Jul 3, 2021
1 parent 1dfddce commit a926ba3
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,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<typename Stream, typename T>
static void Ser(Stream& s, const T& t) { Serialize(s, t); }

template<typename Stream, typename T>
static void Unser(Stream& s, T& t) { Unserialize(s, t); }
};


/**
* string
Expand Down Expand Up @@ -903,9 +917,7 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
template<typename Stream, unsigned int N, typename T, typename V>
void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
{
WriteCompactSize(os, v.size());
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
::Serialize(os, (*vi));
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
}

template<typename Stream, unsigned int N, typename T>
Expand Down Expand Up @@ -934,19 +946,7 @@ void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
template<typename Stream, unsigned int N, typename T, typename V>
void Unserialize_impl(Stream& is, prevector<N, T>& 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<VectorFormatter<DefaultFormatter>>(v));
}

template<typename Stream, unsigned int N, typename T>
Expand All @@ -971,9 +971,7 @@ void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&
template <typename Stream, typename T, typename A, typename V>
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
{
WriteCompactSize(os, v.size());
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
::Serialize(os, (*vi));
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
}

template <typename Stream, typename T, typename A>
Expand Down Expand Up @@ -1001,18 +999,7 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
template <typename Stream, typename T, typename A, typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& 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<VectorFormatter<DefaultFormatter>>(v));
}

template <typename Stream, typename T, typename A>
Expand Down

0 comments on commit a926ba3

Please sign in to comment.