From 3903c3b934c04e763cafd88aa70180bf58de4df0 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Tue, 16 May 2017 15:46:02 -0400 Subject: [PATCH] sstring: add denc() support Signed-off-by: Casey Bodley Signed-off-by: Kefu Chai --- src/common/sstring.hh | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/common/sstring.hh b/src/common/sstring.hh index 8d2c92cc4de9f..954c01d193fcd 100644 --- a/src/common/sstring.hh +++ b/src/common/sstring.hh @@ -38,6 +38,9 @@ #include #include +#include "include/buffer.h" +#include "include/denc.h" + template class basic_sstring; @@ -647,6 +650,60 @@ inline string_type to_sstring(T value) { return sstring::to_sstring(value); } + +// encode/decode +template +struct denc_traits> { +private: + using value_type = basic_sstring; +public: + static constexpr bool supported = true; + static constexpr bool featured = false; + static constexpr bool bounded = false; + + static void bound_encode(const value_type& s, size_t& p, uint64_t f=0) { + p += sizeof(Size) + s.size(); + } + + static void encode_nohead(const value_type& s, + buffer::list::contiguous_appender& p) + { + auto len = s.size(); + if (len) { + p.append(reinterpret_cast(s.c_str()), len); + } + } + + static void decode_nohead(size_t len, value_type& s, + buffer::ptr::iterator& p) + { + s.reset(); + if (len) { + s.append(reinterpret_cast(p.get_pos_add(len)), len); + } + } + + static void encode(const value_type& s, + buffer::list::contiguous_appender& p, + uint64_t f=0) + { + Size len = (Size)(s.size()); + ::denc(len, p); + if (len) { + p.append(reinterpret_cast(s.c_str()), len); + } + } + + static void decode(value_type& s, + buffer::ptr::iterator& p, + uint64_t f=0) + { + Size len; + ::denc(len, p); + decode_nohead(len, s, p); + } +}; + #if 0 /* XXX conflicts w/Ceph types.h */ template inline -- 2.39.5