From dec0f05288dc4fce0f5ae2de7cf4dd8f9281fe1f Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Wed, 14 Sep 2016 13:33:25 -0400 Subject: [PATCH] include/denc: _nohead variants for containers Signed-off-by: Sage Weil --- src/include/denc.h | 119 +++++++++++++++++++++++++++++++++++++++++ src/include/encoding.h | 41 +++++++++----- 2 files changed, 146 insertions(+), 14 deletions(-) diff --git a/src/include/denc.h b/src/include/denc.h index 2de284c8a4e44..59f7686907b52 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -796,6 +796,33 @@ struct denc_traits< denc(s[i], p); } } + + // nohead + template + static typename std::enable_if::type + encode_nohead(const std::vector& s, buffer::list::contiguous_appender& p) { + for (const T& e : s) { + denc(e, p); + } + } + template + static typename std::enable_if::type + encode_nohead(const std::vector& s, buffer::list::contiguous_appender& p, + uint64_t f) { + for (const T& e : s) { + denc(e, p, f); + } + } + static void decode_nohead(size_t num, std::vector& s, + buffer::ptr::iterator& p) { + s.resize(num); + for (unsigned i=0; i + static typename std::enable_if::type + encode_nohead(const std::set& s, buffer::list::contiguous_appender& p) { + for (const T& e : s) { + denc(e, p); + } + } + template + static typename std::enable_if::type + encode_nohead(const std::set& s, buffer::list::contiguous_appender& p, + uint64_t f) { + for (const T& e : s) { + denc(e, p, f); + } + } + static void decode_nohead(size_t num, std::set& s, + buffer::ptr::iterator& p) { + s.clear(); + while (num--) { + T temp; + denc(temp, p); + s.insert(temp); + } + } + }; // @@ -973,6 +1029,36 @@ struct denc_traits< denc(v[key], p); } } + + // nohead variants + template + static typename std::enable_if::type + encode_nohead(const std::map& v, bufferlist::contiguous_appender& p) { + for (const auto& i : v) { + denc(i.first, p); + denc(i.second, p); + } + } + template + static typename std::enable_if::type + encode_nohead(const std::map& v, bufferlist::contiguous_appender& p, + uint64_t f) { + for (const auto& i : v) { + denc(i.first, p, f); + denc(i.second, p, f); + } + } + static void decode_nohead(size_t num, std::map& v, + buffer::ptr::iterator& p) { + v.clear(); + A key; + while (num--) { + denc(key, p); + denc(v[key], p); + } + } }; @@ -1088,6 +1174,39 @@ inline typename std::enable_if> +inline typename std::enable_if::type encode_nohead( + const T& o, + bufferlist& bl) +{ + size_t len = 0; + traits::bound_encode(o, len); + auto a = bl.get_contiguous_appender(len); + traits::encode_nohead(o, a); +} + +template> +inline typename std::enable_if::type decode_nohead( + size_t num, + T& o, + bufferlist::iterator& p) +{ + if (!num) + return; + if (p.end()) + throw buffer::end_of_buffer(); + bufferptr tmp; + bufferlist::iterator t = p; + t.copy_shallow(p.get_bl().length() - p.get_off(), tmp); + auto cp = tmp.begin(); + traits::decode_nohead(num, o, cp); + p.advance((ssize_t)cp.get_offset()); +} + + // ---------------------------------------------------------------- // DENC diff --git a/src/include/encoding.h b/src/include/encoding.h index 89fa521996202..2611330961dbd 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -506,14 +506,16 @@ inline typename std::enable_if::type } } -template -inline void encode_nohead(const std::set& s, bufferlist& bl) +template> +inline typename std::enable_if::type + encode_nohead(const std::set& s, bufferlist& bl) { for (typename std::set::const_iterator p = s.begin(); p != s.end(); ++p) encode(*p, bl); } -template -inline void decode_nohead(int len, std::set& s, bufferlist::iterator& p) +template> +inline typename std::enable_if::type + decode_nohead(int len, std::set& s, bufferlist::iterator& p) { for (int i=0; i::type decode(v[i], p); } -template -inline void encode_nohead(const std::vector& v, bufferlist& bl) +template> +inline typename std::enable_if::type + encode_nohead(const std::vector& v, bufferlist& bl) { for (typename std::vector::const_iterator p = v.begin(); p != v.end(); ++p) encode(*p, bl); } -template -inline void decode_nohead(int len, std::vector& v, bufferlist::iterator& p) +template> +inline typename std::enable_if::type + decode_nohead(int len, std::vector& v, bufferlist::iterator& p) { v.resize(len); for (__u32 i=0; i& m, bufferlist::iterator& p) decode(m[k], p); } } -template -inline void encode_nohead(const std::map& m, bufferlist& bl) +template, typename u_traits=denc_traits> +inline typename std::enable_if::type + encode_nohead(const std::map& m, bufferlist& bl) { for (typename std::map::const_iterator p = m.begin(); p != m.end(); ++p) { encode(p->first, bl); encode(p->second, bl); } } -template -inline void encode_nohead(const std::map& m, bufferlist& bl, uint64_t features) +template, typename u_traits=denc_traits> +inline typename std::enable_if::type + encode_nohead(const std::map& m, bufferlist& bl, uint64_t features) { for (typename std::map::const_iterator p = m.begin(); p != m.end(); ++p) { encode(p->first, bl, features); encode(p->second, bl, features); } } -template -inline void decode_nohead(int n, std::map& m, bufferlist::iterator& p) +template, typename u_traits=denc_traits> +inline typename std::enable_if::type + decode_nohead(int n, std::map& m, bufferlist::iterator& p) { m.clear(); while (n--) { -- 2.39.5