Skip to content

Commit 8c74c09

Browse files
sipafurszy
authored andcommitted
Convert LimitedString to formatter
1 parent f021897 commit 8c74c09

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/serialize.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T&
532532
#define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
533533
#define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
534534
#define COMPACTSIZE(obj) Using<CompactSizeFormatter>(obj)
535-
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
535+
#define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
536536

537537
/** Serialization wrapper class for integers in VarInt format. */
538538
template<VarIntMode Mode>
@@ -616,33 +616,24 @@ struct CompactSizeFormatter
616616
}
617617
};
618618

619-
template <size_t Limit>
620-
class LimitedString
619+
template<size_t Limit>
620+
struct LimitedStringFormatter
621621
{
622-
protected:
623-
std::string& string;
624-
625-
public:
626-
LimitedString(std::string& _string) : string(_string) {}
627-
628-
template <typename Stream>
629-
void Unserialize(Stream& s)
622+
template<typename Stream>
623+
void Unser(Stream& s, std::string& v)
630624
{
631625
size_t size = ReadCompactSize(s);
632626
if (size > Limit) {
633627
throw std::ios_base::failure("String length limit exceeded");
634628
}
635-
string.resize(size);
636-
if (size != 0)
637-
s.read((char*)string.data(), size);
629+
v.resize(size);
630+
if (size != 0) s.read((char*)v.data(), size);
638631
}
639632

640-
template <typename Stream>
641-
void Serialize(Stream& s) const
633+
template<typename Stream>
634+
void Ser(Stream& s, const std::string& v)
642635
{
643-
WriteCompactSize(s, string.size());
644-
if (!string.empty())
645-
s.write((char*)string.data(), string.size());
636+
s << v;
646637
}
647638
};
648639

0 commit comments

Comments
 (0)