From 3fa4e8109e10c3b60b73d333ca4932775ab85e63 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 3 Nov 2017 16:10:07 +0800 Subject: [PATCH] denc: support enum with underlying type Signed-off-by: Kefu Chai --- src/include/denc.h | 10 +++++++++- src/test/encoding.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/include/denc.h b/src/include/denc.h index 823da640860b7..9dc1b7b5f4c6a 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -231,6 +231,13 @@ namespace _denc { std::true_type, is_any_of>::type {}; + template struct underlying_type { + using type = T; + }; + template struct underlying_type< + T, typename std::enable_if::value>::type> { + using type = typename std::underlying_type::type; + }; } @@ -238,7 +245,8 @@ template struct denc_traits< T, typename std::enable_if< - _denc::is_any_of::type, + ceph_le64, ceph_le32, ceph_le16, uint8_t #ifndef _CHAR_IS_SIGNED , int8_t #endif diff --git a/src/test/encoding.cc b/src/test/encoding.cc index af3346e1a0916..21e31d4fab9f4 100644 --- a/src/test/encoding.cc +++ b/src/test/encoding.cc @@ -264,6 +264,23 @@ void encode>(const ceph_le64&, ASSERT_TRUE(false); } +namespace { + // search `underlying_type` in denc.h for supported underlying types + enum class Colour : int8_t { R,G,B }; + ostream& operator<<(ostream& os, Colour c) { + switch (c) { + case Colour::R: + return os << "Colour::R"; + case Colour::G: + return os << "Colour::G"; + case Colour::B: + return os << "Colour::B"; + default: + return os << "Colour::???"; + } + } +} + TEST(EncodingRoundTrip, Integers) { // int types { @@ -288,6 +305,16 @@ TEST(EncodingRoundTrip, Integers) { i = 42; test_encode_and_decode(i); } + // enum + { + test_encode_and_decode(Colour::R); + // this should not build, as the size of unsigned is not the same on + // different archs, that's why denc_traits<> intentionally leaves + // `int` and `unsigned int` out of supported types. + // + // enum E { R, G, B }; + // test_encode_and_decode(R); + } } const char* expected_what[] = { -- 2.39.5