mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-29 02:11:24 +02:00
Make std::vector and prevector reuse the VectorFormatter logic
This commit is contained in:
@ -723,6 +723,20 @@ inline void Unserialize(Stream& is, T&& a)
|
|||||||
a.Unserialize(is);
|
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); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -763,9 +777,7 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
|
|||||||
template<typename Stream, unsigned int N, typename T, typename V>
|
template<typename Stream, unsigned int N, typename T, typename V>
|
||||||
void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
|
void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
|
||||||
::Serialize(os, (*vi));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
@ -794,19 +806,7 @@ void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
|
|||||||
template<typename Stream, unsigned int N, typename T, typename V>
|
template<typename Stream, unsigned int N, typename T, typename V>
|
||||||
void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
|
void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
|
||||||
{
|
{
|
||||||
v.clear();
|
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
@ -843,9 +843,7 @@ void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
|
|||||||
template<typename Stream, typename T, typename A, typename V>
|
template<typename Stream, typename T, typename A, typename V>
|
||||||
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
|
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
|
||||||
::Serialize(os, (*vi));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
@ -874,19 +872,7 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
|
|||||||
template<typename Stream, typename T, typename A, typename V>
|
template<typename Stream, typename T, typename A, typename V>
|
||||||
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
|
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
|
||||||
{
|
{
|
||||||
v.clear();
|
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
|
Reference in New Issue
Block a user