diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2020-05-20 11:13:06 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2020-05-24 10:35:00 -0700 |
commit | 92beff15d3ae2646c00bd78146d7592a7097ce9c (patch) | |
tree | bb88450e1500034b00d626e1e2babeb8bc977d5a /src/serialize.h | |
parent | ef17c03e074b6c3f185afa4eff572ba687c2a171 (diff) |
Convert LimitedString to formatter
Diffstat (limited to 'src/serialize.h')
-rw-r--r-- | src/serialize.h | 22 |
1 files changed, 7 insertions, 15 deletions
diff --git a/src/serialize.h b/src/serialize.h index af75c50ff9..c2e863d97a 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -503,7 +503,7 @@ static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T& #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj) #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj) #define COMPACTSIZE(obj) Using<CompactSizeFormatter>(obj) -#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj)) +#define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj) /** Serialization wrapper class for integers in VarInt format. */ template<VarIntMode Mode> @@ -588,31 +588,23 @@ struct CompactSizeFormatter }; template<size_t Limit> -class LimitedString +struct LimitedStringFormatter { -protected: - std::string& string; -public: - explicit LimitedString(std::string& _string) : string(_string) {} - template<typename Stream> - void Unserialize(Stream& s) + void Unser(Stream& s, std::string& v) { size_t size = ReadCompactSize(s); if (size > Limit) { throw std::ios_base::failure("String length limit exceeded"); } - string.resize(size); - if (size != 0) - s.read((char*)string.data(), size); + v.resize(size); + if (size != 0) s.read((char*)v.data(), size); } template<typename Stream> - void Serialize(Stream& s) const + void Ser(Stream& s, const std::string& v) { - WriteCompactSize(s, string.size()); - if (!string.empty()) - s.write((char*)string.data(), string.size()); + s << v; } }; |