Convert VARINT to the formatter/Using approach

This commit is contained in:
Pieter Wuille 2020-01-08 09:05:44 -08:00
parent ca62563df3
commit 2f1b2f4ed0

View file

@ -468,26 +468,22 @@ public:
template<typename Formatter, typename T>
static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T&>(t); }
#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
#define VARINT(obj, ...) Using<VarIntFormatter<__VA_ARGS__>>(obj)
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
template<VarIntMode Mode, typename I>
class CVarInt
/** Serialization wrapper class for integers in VarInt format. */
template<VarIntMode Mode=VarIntMode::DEFAULT>
struct VarIntFormatter
{
protected:
I &n;
public:
explicit CVarInt(I& nIn) : n(nIn) { }
template<typename Stream>
void Serialize(Stream &s) const {
WriteVarInt<Stream,Mode,I>(s, n);
template<typename Stream, typename I> void Ser(Stream &s, I v)
{
WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
}
template<typename Stream>
void Unserialize(Stream& s) {
n = ReadVarInt<Stream,Mode,I>(s);
template<typename Stream, typename I> void Unser(Stream& s, I& v)
{
v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
}
};
@ -572,9 +568,6 @@ public:
}
};
template<VarIntMode Mode=VarIntMode::DEFAULT, typename I>
CVarInt<Mode, I> WrapVarInt(I& n) { return CVarInt<Mode, I>{n}; }
template<typename I>
BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }