diff --git a/src/serialize.h b/src/serialize.h index ef3053b717..14e56baef4 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -787,23 +787,14 @@ template void Unserialize(Stream& is, std::basic_st * prevector * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ -template void Serialize_impl(Stream& os, const prevector& v, const unsigned char&); -template void Serialize_impl(Stream& os, const prevector& v, const V&); template inline void Serialize(Stream& os, const prevector& v); -template void Unserialize_impl(Stream& is, prevector& v, const unsigned char&); -template void Unserialize_impl(Stream& is, prevector& v, const V&); template inline void Unserialize(Stream& is, prevector& v); /** * vector * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ -template void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&); -template void Serialize_impl(Stream& os, const std::vector& v, const bool&); -template void Serialize_impl(Stream& os, const std::vector& v, const V&); template inline void Serialize(Stream& os, const std::vector& v); -template void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&); -template void Unserialize_impl(Stream& is, std::vector& v, const V&); template inline void Unserialize(Stream& is, std::vector& v); /** @@ -961,122 +952,82 @@ void Unserialize(Stream& is, std::basic_string_view& str) /** * prevector */ -template -void Serialize_impl(Stream& os, const prevector& v, const unsigned char&) +template +void Serialize(Stream& os, const prevector& v) { - WriteCompactSize(os, v.size()); - if (!v.empty()) - os.write(MakeByteSpan(v)); -} - -template -void Serialize_impl(Stream& os, const prevector& v, const V&) -{ - Serialize(os, Using>(v)); -} - -template -inline void Serialize(Stream& os, const prevector& v) -{ - Serialize_impl(os, v, T()); -} - - -template -void Unserialize_impl(Stream& is, prevector& v, const unsigned char&) -{ - // Limit size per read so bogus size value won't cause out of memory - v.clear(); - unsigned int nSize = ReadCompactSize(is); - unsigned int i = 0; - while (i < nSize) - { - unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); - v.resize_uninitialized(i + blk); - is.read(AsWritableBytes(Span{&v[i], blk})); - i += blk; + if constexpr (std::is_same_v) { + WriteCompactSize(os, v.size()); + if (!v.empty()) + os.write(MakeByteSpan(v)); + } else { + Serialize(os, Using>(v)); } } -template -void Unserialize_impl(Stream& is, prevector& v, const V&) -{ - Unserialize(is, Using>(v)); -} -template -inline void Unserialize(Stream& is, prevector& v) +template +void Unserialize(Stream& is, prevector& v) { - Unserialize_impl(is, v, T()); + if constexpr (std::is_same_v) { + // Limit size per read so bogus size value won't cause out of memory + v.clear(); + unsigned int nSize = ReadCompactSize(is); + unsigned int i = 0; + while (i < nSize) { + unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); + v.resize_uninitialized(i + blk); + is.read(AsWritableBytes(Span{&v[i], blk})); + i += blk; + } + } else { + Unserialize(is, Using>(v)); + } } - /** * vector */ -template -void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&) +template +void Serialize(Stream& os, const std::vector& v) { - WriteCompactSize(os, v.size()); - if (!v.empty()) - os.write(MakeByteSpan(v)); -} - -template -void Serialize_impl(Stream& os, const std::vector& v, const bool&) -{ - // A special case for std::vector, as dereferencing - // std::vector::const_iterator does not result in a const bool& - // due to std::vector's special casing for bool arguments. - WriteCompactSize(os, v.size()); - for (bool elem : v) { - ::Serialize(os, elem); + if constexpr (std::is_same_v) { + WriteCompactSize(os, v.size()); + if (!v.empty()) + os.write(MakeByteSpan(v)); + } else if constexpr (std::is_same_v) { + // A special case for std::vector, as dereferencing + // std::vector::const_iterator does not result in a const bool& + // due to std::vector's special casing for bool arguments. + WriteCompactSize(os, v.size()); + for (bool elem : v) { + ::Serialize(os, elem); + } + } else { + Serialize(os, Using>(v)); } } -template -void Serialize_impl(Stream& os, const std::vector& v, const V&) -{ - Serialize(os, Using>(v)); -} -template -inline void Serialize(Stream& os, const std::vector& v) +template +void Unserialize(Stream& is, std::vector& v) { - Serialize_impl(os, v, T()); -} - - -template -void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&) -{ - // Limit size per read so bogus size value won't cause out of memory - v.clear(); - unsigned int nSize = ReadCompactSize(is); - unsigned int i = 0; - while (i < nSize) - { - unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); - v.resize(i + blk); - is.read(AsWritableBytes(Span{&v[i], blk})); - i += blk; + if constexpr (std::is_same_v) { + // Limit size per read so bogus size value won't cause out of memory + v.clear(); + unsigned int nSize = ReadCompactSize(is); + unsigned int i = 0; + while (i < nSize) { + unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); + v.resize(i + blk); + is.read(AsWritableBytes(Span{&v[i], blk})); + i += blk; + } + } else { + Unserialize(is, Using>(v)); } } -template -void Unserialize_impl(Stream& is, std::vector& v, const V&) -{ - Unserialize(is, Using>(v)); -} - -template -inline void Unserialize(Stream& is, std::vector& v) -{ - Unserialize_impl(is, v, T()); -} - - /** * pair @@ -1386,28 +1337,16 @@ public: int GetVersion() const { return nVersion; } }; -template -void SerializeMany(Stream& s) +template +void SerializeMany(Stream& s, const Args&... args) { + (::Serialize(s, args), ...); } -template -void SerializeMany(Stream& s, const Arg& arg, const Args&... args) +template +inline void UnserializeMany(Stream& s, Args&&... args) { - ::Serialize(s, arg); - ::SerializeMany(s, args...); -} - -template -inline void UnserializeMany(Stream& s) -{ -} - -template -inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args) -{ - ::Unserialize(s, arg); - ::UnserializeMany(s, args...); + (::Unserialize(s, args), ...); } template