From: Kefu Chai Date: Fri, 3 Nov 2017 08:10:07 +0000 (+0800) Subject: denc: support enum with underlying type X-Git-Tag: v13.0.1~289^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F18701%2Fhead;p=ceph.git denc: support enum with underlying type Signed-off-by: Kefu Chai --- diff --git a/src/include/denc.h b/src/include/denc.h index 823da640860b..9dc1b7b5f4c6 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 af3346e1a091..21e31d4fab9f 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[] = {