From c92cc9fe51a718b3ab33a3a9a2f4cbcec2edad11 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Fri, 23 Dec 2016 12:43:06 -0500 Subject: [PATCH] denc: Support boost::container::flat_set Signed-off-by: Adam C. Emerson --- src/include/denc.h | 10 ++++++++++ src/include/encoding.h | 44 ++++++++++++++++++++++++++++++++++++++++++ src/test/test_denc.cc | 32 ++++++++++++++++++++++-------- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/include/denc.h b/src/include/denc.h index e5631737109..58b542a453a 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "include/int_types.h" #include "include/intarith.h" @@ -821,6 +822,15 @@ struct denc_traits< _denc::setlike_details>, T, Ts...> {}; +template +struct denc_traits< + boost::container::flat_set, + typename std::enable_if::supported != 0>::type> + : public _denc::container_base< + boost::container::flat_set, + _denc::setlike_details>, + T, Ts...> {}; + namespace _denc { template struct maplike_details : public container_details_base { diff --git a/src/include/encoding.h b/src/include/encoding.h index cbc762351d7..b8a358ef49a 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -504,6 +504,50 @@ inline typename std::enable_if::type } } +// boost::container::flat_set +template> +inline typename std::enable_if::type +encode(const boost::container::flat_set& s, bufferlist& bl) +{ + __u32 n = (__u32)(s.size()); + encode(n, bl); + for (const auto& e : s) + encode(e, bl); +} +template> +inline typename std::enable_if::type +decode(boost::container::flat_set& s, bufferlist::iterator& p) +{ + __u32 n; + decode(n, p); + s.clear(); + while (n--) { + T v; + decode(v, p); + s.insert(v); + } +} + +template> +inline typename std::enable_if::type +encode_nohead(const boost::container::flat_set& s, + bufferlist& bl) +{ + for (const auto& e : s) + encode(e, bl); +} +template> +inline typename std::enable_if::type +decode_nohead(int len, boost::container::flat_set& s, + bufferlist::iterator& p) +{ + for (int i=0; i inline void encode(const std::multiset& s, bufferlist& bl) diff --git a/src/test/test_denc.cc b/src/test/test_denc.cc index 3e79d83a940..12a5489f6c2 100644 --- a/src/test/test_denc.cc +++ b/src/test/test_denc.cc @@ -287,33 +287,49 @@ TEST(denc, list) } } -TEST(denc, set) -{ +template class C> +void test_setlike(const char* c) { { - cout << "set" << std::endl; - std::set s; + cout << c << "" << std::endl; + C s; s.insert("foo"); s.insert("bar"); s.insert("baz"); test_denc(s); } { - cout << "set" << std::endl; - std::set s; + cout << c << "" << std::endl; + C s; s.insert(1); s.insert(2); s.insert(3); test_denc(s); } { - cout << "set" << std::endl; - std::set s; + cout << c << "" << std::endl; + C s; s.insert(legacy_t(1)); s.insert(legacy_t(2)); test_encode_decode(s); } } +template +using default_set = std::set; + +TEST(denc, set) +{ + test_setlike("std::set"); +} + +template +using default_flat_set= boost::container::flat_set; + +TEST(denc, flat_set) +{ + test_setlike("std::set"); +} + struct foo_t { int32_t a = 0; uint64_t b = 123;