From 3ca574cef0b4423f21b2c3efd8f5c9f71d52f219 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 3 Feb 2020 19:49:10 -0800 Subject: Convert CCompactSize to proper formatter --- src/serialize.h | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index cee7225bcb..e0e29bcf4a 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -495,7 +495,7 @@ static inline Wrapper Using(T&& t) { return Wrapper>(obj) #define VARINT(obj) Using>(obj) -#define COMPACTSIZE(obj) CCompactSize(REF(obj)) +#define COMPACTSIZE(obj) Using(obj) #define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj)) /** Serialization wrapper class for integers in VarInt format. */ @@ -547,21 +547,26 @@ public: } }; -class CCompactSize +/** Formatter for integers in CompactSize format. */ +struct CompactSizeFormatter { -protected: - uint64_t &n; -public: - explicit CCompactSize(uint64_t& nIn) : n(nIn) { } - - template - void Serialize(Stream &s) const { - WriteCompactSize(s, n); + template + void Unser(Stream& s, I& v) + { + uint64_t n = ReadCompactSize(s); + if (n < std::numeric_limits::min() || n > std::numeric_limits::max()) { + throw std::ios_base::failure("CompactSize exceeds limit of type"); + } + v = n; } - template - void Unserialize(Stream& s) { - n = ReadCompactSize(s); + template + void Ser(Stream& s, I v) + { + static_assert(std::is_unsigned::value, "CompactSize only supported for unsigned integers"); + static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "CompactSize only supports 64-bit integers and below"); + + WriteCompactSize(s, v); } }; -- cgit v1.2.3