namespace _denc {
template<typename T, typename... Us>
inline constexpr bool is_any_of = (... || std::is_same_v<T, Us>);
-} // namespace _denc
+
+template<typename T, typename=void> struct underlying_type {
+ using type = T;
+};
+template<typename T>
+struct underlying_type<T, std::enable_if_t<std::is_enum_v<T>>> {
+ using type = std::underlying_type_t<T>;
+};
+template<typename T>
+using underlying_type_t = typename underlying_type<T>::type;
+}
template<class It>
struct is_const_iterator
return *reinterpret_cast<T*>(i.get_pos_add(sizeof(T)));
}
-// network-order integer encoding
template<typename T>
struct denc_traits<
T,
std::enable_if_t<
- _denc::is_any_of<T,
+ _denc::is_any_of<_denc::underlying_type_t<T>,
ceph_le64, ceph_le32, ceph_le16, uint8_t
#ifndef _CHAR_IS_SIGNED
, int8_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.
test_denc(c);
}
-TEST(denc, enum_class)
-{
- enum class enum_class_8 : int8_t { value };
- test_denc(enum_class_8::value);
- enum class enum_class_u8 : uint8_t { value };
- test_denc(enum_class_u8::value);
- enum class enum_class_16 : int16_t { value };
- test_denc(enum_class_16::value);
- enum class enum_class_u16 : uint16_t { value };
- test_denc(enum_class_u16::value);
- enum class enum_class_32 : int32_t { value };
- test_denc(enum_class_32::value);
- enum class enum_class_u32 : uint32_t { value };
- test_denc(enum_class_u32::value);
- enum class enum_class_64 : int64_t { value };
- test_denc(enum_class_64::value);
- enum class enum_class_u64 : uint64_t { value };
- test_denc(enum_class_u64::value);
- enum class enum_class { value };
- test_denc(enum_class::value);
-}
-
struct legacy_t {
int32_t a = 1;
void encode(bufferlist& bl) const {