From 7aaa02dc32393187f34f276839ec3b701e85239e Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Mon, 2 Mar 2020 15:20:16 -0500 Subject: [PATCH] denc: add denc_traits for enum class enum traits cast to/from underlying type, and use the underlying type's traits for encode/decode Signed-off-by: Casey Bodley --- src/include/denc.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/include/denc.h b/src/include/denc.h index 6eb027cc0f5..b837f74e24a 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -402,6 +402,44 @@ struct denc_traits>>> } }; + +// enum +// +template +struct denc_traits>> +{ + 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; + using base_traits = denc_traits; + + static void bound_encode(const T &o, size_t& p, uint64_t f=0) { + base_traits::bound_encode(static_cast(o), p, f); + } + template + static std::enable_if_t> + encode(const T &o, It& p, uint64_t f=0) { + base_traits::encode(static_cast(o), p, f); + } + template + static std::enable_if_t> + decode(T& o, It& p, uint64_t f=0) { + underlying_type v; + base_traits::decode(v, p, f); + o = static_cast(v); + } + static void decode(T& o, ceph::buffer::list::const_iterator &p) { + underlying_type v; + base_traits::decode(v, p); + o = static_cast(v); + } +}; + + // varint // // high bit of each byte indicates another byte follows. -- 2.39.5