Skip to content

Commit 3cd8ab9

Browse files
committed
Make std::vector and prevector reuse the VectorFormatter logic
1 parent abf8624 commit 3cd8ab9

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

src/serialize.h

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,20 @@ inline void Unserialize(Stream& is, T&& a)
723723
a.Unserialize(is);
724724
}
725725

726+
/** Default formatter. Serializes objects as themselves.
727+
*
728+
* The vector/prevector serialization code passes this to VectorFormatter
729+
* to enable reusing that logic. It shouldn't be needed elsewhere.
730+
*/
731+
struct DefaultFormatter
732+
{
733+
template<typename Stream, typename T>
734+
static void Ser(Stream& s, const T& t) { Serialize(s, t); }
735+
736+
template<typename Stream, typename T>
737+
static void Unser(Stream& s, T& t) { Unserialize(s, t); }
738+
};
739+
726740

727741

728742

@@ -763,9 +777,7 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
763777
template<typename Stream, unsigned int N, typename T, typename V>
764778
void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
765779
{
766-
WriteCompactSize(os, v.size());
767-
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
768-
::Serialize(os, (*vi));
780+
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
769781
}
770782

771783
template<typename Stream, unsigned int N, typename T>
@@ -794,19 +806,7 @@ void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
794806
template<typename Stream, unsigned int N, typename T, typename V>
795807
void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
796808
{
797-
v.clear();
798-
unsigned int nSize = ReadCompactSize(is);
799-
unsigned int i = 0;
800-
unsigned int nMid = 0;
801-
while (nMid < nSize)
802-
{
803-
nMid += MAX_VECTOR_ALLOCATE / sizeof(T);
804-
if (nMid > nSize)
805-
nMid = nSize;
806-
v.resize_uninitialized(nMid);
807-
for (; i < nMid; ++i)
808-
Unserialize(is, v[i]);
809-
}
809+
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
810810
}
811811

812812
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&)
843843
template<typename Stream, typename T, typename A, typename V>
844844
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
845845
{
846-
WriteCompactSize(os, v.size());
847-
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
848-
::Serialize(os, (*vi));
846+
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
849847
}
850848

851849
template<typename Stream, typename T, typename A>
@@ -874,19 +872,7 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
874872
template<typename Stream, typename T, typename A, typename V>
875873
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
876874
{
877-
v.clear();
878-
unsigned int nSize = ReadCompactSize(is);
879-
unsigned int i = 0;
880-
unsigned int nMid = 0;
881-
while (nMid < nSize)
882-
{
883-
nMid += MAX_VECTOR_ALLOCATE / sizeof(T);
884-
if (nMid > nSize)
885-
nMid = nSize;
886-
v.resize(nMid);
887-
for (; i < nMid; i++)
888-
Unserialize(is, v[i]);
889-
}
875+
Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
890876
}
891877

892878
template<typename Stream, typename T, typename A>

0 commit comments

Comments
 (0)