serialize: Drop nVersion from [C]SizeComputer

Protocol version is no longer needed to work out the serialized size
of objects so drop that information from CSizeComputer and rename the
class to SizeComputer.
This commit is contained in:
Anthony Towns 2023-11-01 22:20:10 +10:00
parent 0aa014d5a3
commit efa9eb6d7c
1 changed files with 12 additions and 15 deletions

View File

@ -124,7 +124,7 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
// i.e. anything that supports .read(Span<std::byte>) and .write(Span<const std::byte>)
//
class CSizeComputer;
class SizeComputer;
enum
{
@ -324,7 +324,7 @@ constexpr inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
else return sizeof(unsigned char) + sizeof(uint64_t);
}
inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
inline void WriteCompactSize(SizeComputer& os, uint64_t nSize);
template<typename Stream>
void WriteCompactSize(Stream& os, uint64_t nSize)
@ -450,7 +450,7 @@ inline unsigned int GetSizeOfVarInt(I n)
}
template<typename I>
inline void WriteVarInt(CSizeComputer& os, I n);
inline void WriteVarInt(SizeComputer& os, I n);
template<typename Stream, VarIntMode Mode, typename I>
void WriteVarInt(Stream& os, I n)
@ -1070,22 +1070,21 @@ struct ActionUnserialize {
/* ::GetSerializeSize implementations
*
* Computing the serialized size of objects is done through a special stream
* object of type CSizeComputer, which only records the number of bytes written
* object of type SizeComputer, which only records the number of bytes written
* to it.
*
* If your Serialize or SerializationOp method has non-trivial overhead for
* serialization, it may be worthwhile to implement a specialized version for
* CSizeComputer, which uses the s.seek() method to record bytes that would
* SizeComputer, which uses the s.seek() method to record bytes that would
* be written instead.
*/
class CSizeComputer
class SizeComputer
{
protected:
size_t nSize{0};
const int nVersion;
public:
explicit CSizeComputer(int nVersionIn) : nVersion(nVersionIn) {}
SizeComputer() {}
void write(Span<const std::byte> src)
{
@ -1099,7 +1098,7 @@ public:
}
template<typename T>
CSizeComputer& operator<<(const T& obj)
SizeComputer& operator<<(const T& obj)
{
::Serialize(*this, obj);
return (*this);
@ -1108,17 +1107,15 @@ public:
size_t size() const {
return nSize;
}
int GetVersion() const { return nVersion; }
};
template<typename I>
inline void WriteVarInt(CSizeComputer &s, I n)
inline void WriteVarInt(SizeComputer &s, I n)
{
s.seek(GetSizeOfVarInt<I>(n));
}
inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
inline void WriteCompactSize(SizeComputer &s, uint64_t nSize)
{
s.seek(GetSizeOfCompactSize(nSize));
}
@ -1126,13 +1123,13 @@ inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
template <typename T>
size_t GetSerializeSize(const T& t, int nVersion = 0)
{
return (CSizeComputer(nVersion) << t).size();
return (SizeComputer() << t).size();
}
template <typename... T>
size_t GetSerializeSizeMany(int nVersion, const T&... t)
{
CSizeComputer sc(nVersion);
SizeComputer sc;
SerializeMany(sc, t...);
return sc.size();
}