denc: enable decode basic_string<>
authorKefu Chai <kchai@redhat.com>
Mon, 26 Nov 2018 15:52:39 +0000 (23:52 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 27 Nov 2018 16:26:00 +0000 (00:26 +0800)
we have

buffer::list::iterator_impl<is_const>::copy(unsigned len,
std::string &dest)

but we don't have the templatized version of it. so, if we
plugin a different allocator to basic_string<>, the new string won't
decode with buffer::list::const_iterator. this decode variant is used if
the caller only has a (probably non-contiguous) buffer::list in hand.

in this change, copy(unsigned, char*) is used as an alternative.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/denc.h

index 2e5625989039980c08932a3070068a8baeabe3a3..d56bf93d86132cddc2310a2429bc7665b87ab7a9 100644 (file)
@@ -714,17 +714,36 @@ public:
     uint32_t len;
     denc(len, p);
     s.clear();
-    p.copy(len, s);
+    if constexpr (std::is_same_v<value_type, std::string>) {
+      p.copy(len, s);
+    } else {
+      s.append(len, 0);
+      p.copy(len, s.data());
+    }
   }
   template<class It>
-  static std::enable_if_t<is_const_iterator_v<It>>
-  decode_nohead(size_t len, value_type& s, It& p) {
+  static void decode_nohead(size_t len, value_type& s, It& p) {
     s.clear();
     if (len) {
       s.append(p.get_pos_add(len), len);
     }
   }
   template<class It>
+  static std::enable_if_t<is_const_iterator_v<It>>
+  decode_nohead(size_t len, value_type& s, It& p) {
+    if (len) {
+      if constexpr (std::is_same_v<value_type, std::string>) {
+        s.clear();
+        p.copy(len, s);
+      } else {
+        s.resize(len, 0);
+        p.copy(len, s.data());
+      }
+    } else {
+      s.clear();
+    }
+  }
+  template<class It>
   static std::enable_if_t<!is_const_iterator_v<It>>
   encode_nohead(const value_type& s, It& p) {
     auto len = s.length();