mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-28 03:36:28 +02:00
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:
@@ -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>)
|
// i.e. anything that supports .read(Span<std::byte>) and .write(Span<const std::byte>)
|
||||||
//
|
//
|
||||||
|
|
||||||
class CSizeComputer;
|
class SizeComputer;
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
@@ -324,7 +324,7 @@ constexpr inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
|
|||||||
else return sizeof(unsigned char) + sizeof(uint64_t);
|
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>
|
template<typename Stream>
|
||||||
void WriteCompactSize(Stream& os, uint64_t nSize)
|
void WriteCompactSize(Stream& os, uint64_t nSize)
|
||||||
@@ -450,7 +450,7 @@ inline unsigned int GetSizeOfVarInt(I n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename I>
|
template<typename I>
|
||||||
inline void WriteVarInt(CSizeComputer& os, I n);
|
inline void WriteVarInt(SizeComputer& os, I n);
|
||||||
|
|
||||||
template<typename Stream, VarIntMode Mode, typename I>
|
template<typename Stream, VarIntMode Mode, typename I>
|
||||||
void WriteVarInt(Stream& os, I n)
|
void WriteVarInt(Stream& os, I n)
|
||||||
@@ -1070,22 +1070,21 @@ struct ActionUnserialize {
|
|||||||
/* ::GetSerializeSize implementations
|
/* ::GetSerializeSize implementations
|
||||||
*
|
*
|
||||||
* Computing the serialized size of objects is done through a special stream
|
* 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.
|
* to it.
|
||||||
*
|
*
|
||||||
* If your Serialize or SerializationOp method has non-trivial overhead for
|
* If your Serialize or SerializationOp method has non-trivial overhead for
|
||||||
* serialization, it may be worthwhile to implement a specialized version 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.
|
* be written instead.
|
||||||
*/
|
*/
|
||||||
class CSizeComputer
|
class SizeComputer
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
size_t nSize{0};
|
size_t nSize{0};
|
||||||
|
|
||||||
const int nVersion;
|
|
||||||
public:
|
public:
|
||||||
explicit CSizeComputer(int nVersionIn) : nVersion(nVersionIn) {}
|
SizeComputer() {}
|
||||||
|
|
||||||
void write(Span<const std::byte> src)
|
void write(Span<const std::byte> src)
|
||||||
{
|
{
|
||||||
@@ -1099,7 +1098,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CSizeComputer& operator<<(const T& obj)
|
SizeComputer& operator<<(const T& obj)
|
||||||
{
|
{
|
||||||
::Serialize(*this, obj);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
@@ -1108,17 +1107,15 @@ public:
|
|||||||
size_t size() const {
|
size_t size() const {
|
||||||
return nSize;
|
return nSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetVersion() const { return nVersion; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename I>
|
template<typename I>
|
||||||
inline void WriteVarInt(CSizeComputer &s, I n)
|
inline void WriteVarInt(SizeComputer &s, I n)
|
||||||
{
|
{
|
||||||
s.seek(GetSizeOfVarInt<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));
|
s.seek(GetSizeOfCompactSize(nSize));
|
||||||
}
|
}
|
||||||
@@ -1126,13 +1123,13 @@ inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
size_t GetSerializeSize(const T& t, int nVersion = 0)
|
size_t GetSerializeSize(const T& t, int nVersion = 0)
|
||||||
{
|
{
|
||||||
return (CSizeComputer(nVersion) << t).size();
|
return (SizeComputer() << t).size();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
size_t GetSerializeSizeMany(int nVersion, const T&... t)
|
size_t GetSerializeSizeMany(int nVersion, const T&... t)
|
||||||
{
|
{
|
||||||
CSizeComputer sc(nVersion);
|
SizeComputer sc;
|
||||||
SerializeMany(sc, t...);
|
SerializeMany(sc, t...);
|
||||||
return sc.size();
|
return sc.size();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user