diff options
author | Casey Bodley <cbodley@redhat.com> | 2017-05-16 21:46:02 +0200 |
---|---|---|
committer | Casey Bodley <cbodley@redhat.com> | 2017-05-18 20:46:59 +0200 |
commit | 3903c3b934c04e763cafd88aa70180bf58de4df0 (patch) | |
tree | 3c0c226aa15592297a8aee908f8aa67d27ae12da /src/common | |
parent | denc: add reserve() to boost::flat_set/map decode (diff) | |
download | ceph-3903c3b934c04e763cafd88aa70180bf58de4df0.tar.xz ceph-3903c3b934c04e763cafd88aa70180bf58de4df0.zip |
sstring: add denc() support
Signed-off-by: Casey Bodley <cbodley@redhat.com>
Signed-off-by: Kefu Chai <kchai@redhat.com>
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/sstring.hh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/common/sstring.hh b/src/common/sstring.hh index 8d2c92cc4de..954c01d193f 100644 --- a/src/common/sstring.hh +++ b/src/common/sstring.hh @@ -38,6 +38,9 @@ #include <type_traits> #include <boost/utility/string_ref.hpp> +#include "include/buffer.h" +#include "include/denc.h" + template <typename char_type, typename Size, Size max_size> class basic_sstring; @@ -647,6 +650,60 @@ inline string_type to_sstring(T value) { return sstring::to_sstring<string_type>(value); } + +// encode/decode +template <typename Char, typename Size, Size Max> +struct denc_traits<basic_sstring<Char, Size, Max>> { +private: + using value_type = basic_sstring<Char, Size, Max>; +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<const char*>(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<const Char*>(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<const char*>(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 <typename T> inline |