]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
denc: add denc_traits for enum class
authorCasey Bodley <cbodley@redhat.com>
Mon, 2 Mar 2020 20:20:16 +0000 (15:20 -0500)
committerCasey Bodley <cbodley@redhat.com>
Mon, 2 Mar 2020 21:09:23 +0000 (16:09 -0500)
enum traits cast to/from underlying type, and use the underlying type's
traits for encode/decode

Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/include/denc.h

index 6eb027cc0f5d103b92ad4bdac597ad2c9cac7149..b837f74e24aabbe0dd6458fa9633d979e9cf3fed 100644 (file)
@@ -402,6 +402,44 @@ struct denc_traits<T, std::enable_if_t<!std::is_void_v<_denc::ExtType_t<T>>>>
   }
 };
 
+
+// enum
+//
+template<typename T>
+struct denc_traits<T, std::enable_if_t<std::is_enum_v<T>>>
+{
+  static constexpr bool supported = true;
+  static constexpr bool featured = false;
+  static constexpr bool bounded = true;
+  static constexpr bool need_contiguous = false;
+
+  using enum_type = T;
+  using underlying_type = std::underlying_type_t<enum_type>;
+  using base_traits = denc_traits<underlying_type>;
+
+  static void bound_encode(const T &o, size_t& p, uint64_t f=0) {
+    base_traits::bound_encode(static_cast<underlying_type>(o), p, f);
+  }
+  template<class It>
+  static std::enable_if_t<!is_const_iterator_v<It>>
+  encode(const T &o, It& p, uint64_t f=0) {
+    base_traits::encode(static_cast<underlying_type>(o), p, f);
+  }
+  template<class It>
+  static std::enable_if_t<is_const_iterator_v<It>>
+  decode(T& o, It& p, uint64_t f=0) {
+    underlying_type v;
+    base_traits::decode(v, p, f);
+    o = static_cast<enum_type>(v);
+  }
+  static void decode(T& o, ceph::buffer::list::const_iterator &p) {
+    underlying_type v;
+    base_traits::decode(v, p);
+    o = static_cast<enum_type>(v);
+  }
+};
+
+
 // varint
 //
 // high bit of each byte indicates another byte follows.