From 9b2d1df4d5949c6b746fdd53e786de0bf6c15236 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 20 Aug 2022 11:20:26 +0800 Subject: [PATCH] include/denc: define is_const_iterator as a concept just for better readability Signed-off-by: Kefu Chai --- src/crimson/os/seastore/seastore_types.h | 18 +++-- src/include/denc.h | 87 ++++++++++-------------- 2 files changed, 45 insertions(+), 60 deletions(-) diff --git a/src/crimson/os/seastore/seastore_types.h b/src/crimson/os/seastore/seastore_types.h index 07b13269c6870..9b52e1d861b6c 100644 --- a/src/crimson/os/seastore/seastore_types.h +++ b/src/crimson/os/seastore/seastore_types.h @@ -2005,16 +2005,15 @@ struct denc_traits { p += sizeof(crimson::os::seastore::device_type_t); } template - static std::enable_if_t> - encode( + requires (!is_const_iterator) + static void encode( const crimson::os::seastore::device_type_t &o, It& p, uint64_t f=0) { get_pos_add(p) = o; } - template - static std::enable_if_t> - decode( + template + static void decode( crimson::os::seastore::device_type_t& o, It& p, uint64_t f=0) { @@ -2042,16 +2041,15 @@ struct denc_traits { p += sizeof(crimson::os::seastore::segment_type_t); } template - static std::enable_if_t> - encode( + requires (!is_const_iterator) + static void encode( const crimson::os::seastore::segment_type_t &o, It& p, uint64_t f=0) { get_pos_add(p) = o; } - template - static std::enable_if_t> - decode( + template + static void decode( crimson::os::seastore::segment_type_t& o, It& p, uint64_t f=0) { diff --git a/src/include/denc.h b/src/include/denc.h index 6dc661d3b2f19..96b7e7c099fc7 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -283,29 +283,17 @@ using underlying_type_t = typename underlying_type::type; } template -struct is_const_iterator - : std::conditional_t>, - std::true_type, - std::false_type> -{}; -template<> -struct is_const_iterator : std::false_type {}; -template<> -struct is_const_iterator : std::false_type { - // appender is used for *changing* the buffer -}; -template -inline constexpr bool is_const_iterator_v = is_const_iterator::value; +concept is_const_iterator = +std::is_const_v>; -template -std::enable_if_t, const T&> -get_pos_add(It& i) { +template +const T& get_pos_add(It& i) { return *reinterpret_cast(i.get_pos_add(sizeof(T))); } template -std::enable_if_t, T&> -get_pos_add(It& i) { +requires (!is_const_iterator) +T& get_pos_add(It& i) { return *reinterpret_cast(i.get_pos_add(sizeof(T))); } @@ -327,13 +315,12 @@ struct denc_traits< p += sizeof(T); } template - static std::enable_if_t> - encode(const T &o, It& p, uint64_t f=0) { + requires (!is_const_iterator) + static void encode(const T &o, It& p, uint64_t f=0) { get_pos_add(p) = o; } - template - static std::enable_if_t> - decode(T& o, It& p, uint64_t f=0) { + template + static void decode(T& o, It& p, uint64_t f=0) { o = get_pos_add(p); } static void decode(T& o, ceph::buffer::list::const_iterator &p) { @@ -397,13 +384,12 @@ struct denc_traits>>> p += sizeof(etype); } template - static std::enable_if_t> - encode(const T &o, It& p, uint64_t f=0) { + requires (!is_const_iterator) + static void encode(const T &o, It& p, uint64_t f=0) { get_pos_add(p) = o; } - template - static std::enable_if_t> - decode(T& o, It &p, uint64_t f=0) { + template + static void decode(T& o, It &p, uint64_t f=0) { o = get_pos_add(p); } static void decode(T& o, ceph::buffer::list::const_iterator &p) { @@ -455,8 +441,8 @@ inline void denc_signed_varint(int64_t v, size_t& p) { p += sizeof(v) + 2; } template -inline std::enable_if_t> -denc_signed_varint(int64_t v, It& p) { +requires (!is_const_iterator) +void denc_signed_varint(int64_t v, It& p) { if (v < 0) { v = (-v << 1) | 1; } else { @@ -465,9 +451,8 @@ denc_signed_varint(int64_t v, It& p) { denc_varint(v, p); } -template -inline std::enable_if_t> -denc_signed_varint(T& v, It& p) +template +inline void denc_signed_varint(T& v, It& p) { int64_t i = 0; denc_varint(i, p); @@ -518,8 +503,8 @@ inline void denc_signed_varint_lowz(int64_t v, size_t& p) { p += sizeof(v) + 2; } template -inline std::enable_if_t> -denc_signed_varint_lowz(int64_t v, It& p) { +requires (!is_const_iterator) +inline void denc_signed_varint_lowz(int64_t v, It& p) { bool negative = false; if (v < 0) { v = -v; @@ -535,9 +520,8 @@ denc_signed_varint_lowz(int64_t v, It& p) { denc_varint(v, p); } -template -inline std::enable_if_t> -denc_signed_varint_lowz(T& v, It& p) +template +inline void denc_signed_varint_lowz(T& v, It& p) { int64_t i = 0; denc_varint(i, p); @@ -569,8 +553,8 @@ inline void denc_lba(uint64_t v, size_t& p) { } template -inline std::enable_if_t> -denc_lba(uint64_t v, It& p) { +requires (!is_const_iterator) +inline void denc_lba(uint64_t v, It& p) { int low_zero_nibbles = v ? (int)(ctz(v) / 4) : 0; int pos; uint32_t word; @@ -606,9 +590,8 @@ denc_lba(uint64_t v, It& p) { *(__u8*)p.get_pos_add(1) = byte; } -template -inline std::enable_if_t> -denc_lba(uint64_t& v, It& p) { +template +inline void denc_lba(uint64_t& v, It& p) { uint32_t word = *(ceph_le32*)p.get_pos_add(sizeof(uint32_t)); int shift = 0; switch (word & 7) { @@ -658,7 +641,8 @@ inline std::enable_if_t denc( } template> -inline std::enable_if_t> +requires traits::supported && (!is_const_iterator) +inline void denc(const T& o, It& p, uint64_t features=0) @@ -670,8 +654,9 @@ denc(const T& o, } } -template> -inline std::enable_if_t> +template> +requires traits::supported +inline void denc(T& o, It& p, uint64_t features=0) @@ -779,7 +764,8 @@ public: } } template - static std::enable_if_t> + requires (!is_const_iterator) + static void encode_nohead(const value_type& s, It& p) { auto len = s.length(); maybe_inline_memcpy(p.get_pos_add(len), s.data(), len, 16); @@ -799,13 +785,14 @@ struct denc_traits { p += sizeof(uint32_t) + v.length(); } template - static std::enable_if_t> + requires (!is_const_iterator) + static void encode(const ceph::buffer::ptr& v, It& p, uint64_t f=0) { denc((uint32_t)v.length(), p); p.append(v); } - template - static std::enable_if_t> + template + static void decode(ceph::buffer::ptr& v, It& p, uint64_t f=0) { uint32_t len; denc(len, p); -- 2.39.5