]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
sstring: add denc() support
authorCasey Bodley <cbodley@redhat.com>
Tue, 16 May 2017 19:46:02 +0000 (15:46 -0400)
committerCasey Bodley <cbodley@redhat.com>
Thu, 18 May 2017 18:46:59 +0000 (14:46 -0400)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/sstring.hh

index 8d2c92cc4de9fbb6798d8fdbeaad8a1c59817e9f..954c01d193fcddd1d9ab2039242920873fbba747 100644 (file)
@@ -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