From 9412de0c6719cbdc0864f647df1539bef016a613 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Tue, 9 Jan 2018 17:53:26 -0500 Subject: [PATCH] denc: Simplify tuple decode/encode Use index_sequence instead of rolling our own support infrastructure. Signed-off-by: Adam C. Emerson --- src/include/denc.h | 244 +++++++++++---------------------------------- 1 file changed, 57 insertions(+), 187 deletions(-) diff --git a/src/include/denc.h b/src/include/denc.h index 1b27f892327..9b291736899 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -1110,224 +1110,94 @@ public: } }; -namespace _denc { - template - struct indices {}; - - template - struct build_indices_helper; - template - struct build_indices_helper { - using type = indices; - }; - template - struct build_indices_helper { - using type = typename build_indices_helper::type; - }; - - template - struct build_indices { - using type = typename build_indices_helper::type; - }; - template<> - struct build_indices<0> { - using type = indices<>; - }; - template<> - struct build_indices<1> { - using type = indices<0>; - }; - - template - using build_indices_t = typename build_indices::type; - - template - struct tuple_traits; - template - struct tuple_traits { - static constexpr bool supported = (denc_traits::supported && - tuple_traits::supported); - static constexpr bool bounded = (denc_traits::bounded && - tuple_traits::bounded); - static constexpr bool featured = (denc_traits::featured || - tuple_traits::featured); - static constexpr bool need_contiguous = - (denc_traits::need_contiguous || - tuple_traits::need_contiguous); - }; - template<> - struct tuple_traits<> { - static constexpr bool supported = true; - static constexpr bool bounded = true; - static constexpr bool featured = false; - static constexpr bool need_contiguous = false; - }; -} - -template +template struct denc_traits< std::tuple, - std::enable_if_t<_denc::tuple_traits::supported>> { + std::enable_if_t<(denc_traits::supported && ...)>> { + private: static_assert(sizeof...(Ts) > 0, "Zero-length tuples are not supported."); using container = std::tuple; - template - static void bound_encode_helper_nfnb(const T& s, size_t& p, - _denc::indices) { - denc(std::get(s), p); - bound_encode_helper_nfnb(s, p, _denc::indices{}); - } - template - static void bound_encode_helper_nfnb(const T& s, size_t& p, - _denc::indices) { - denc(std::get(s), p); - } - - template - static void bound_encode_helper_nfb(const T& s, size_t& p, - _denc::indices) { - denc(std::get(s), p); - bound_encode_helper_nfb(s, p, _denc::indices{}); + template + static std::enable_if_t::featured> + bound_encode_helper(const U& s, size_t& p, + std::uint64_t f, + std::index_sequence) { + (denc(std::get(s), p, f), ..., void()); } - template - static void bound_encode_helper_nfb(const T& s, size_t& p, - _denc::indices) { - denc(std::get(s), p); + template + static std::enable_if_t::featured> + bound_encode_helper(const U& s, size_t& p, + std::index_sequence) { + (denc(std::get(s), p), ..., void()); } - template - static void bound_encode_helper_fnb(const T& s, size_t& p, uint64_t f, - _denc::indices) { - denc(std::get(s), p, f); - bound_encode_helper_fnb(s, p, f, _denc::indices{}); + template + static std::enable_if_t::featured> + encode_helper(const U& s, buffer::list::contiguous_appender& p, + std::uint64_t f, + std::index_sequence) { + (denc(std::get(s), p, f), ..., void()); } - template - static void bound_encode_helper_fnb(const T& s, size_t& p, uint64_t f, - _denc::indices) { - denc(std::get(s), p, f); + template + static std::enable_if_t::featured> + encode_helper(const U& s, buffer::list::contiguous_appender& p, + std::index_sequence) { + (denc(std::get(s), p), ..., void()); } - template - static void bound_encode_helper_fb(const T& s, size_t& p, uint64_t f, - _denc::indices) { - denc(std::get(s), p); - bound_encode_helper_fb(s, p, f, _denc::indices{}); - } - template - static void bound_encode_helper_fb(const T& s, size_t& p, uint64_t f, - _denc::indices) { - denc(std::get(s), p); + template + static void decode_helper(container& s, buffer::ptr::iterator& p, + std::index_sequence) { + (denc(std::get(s), p), ..., void()); } - template - static void encode_helper_nf(const T& s, buffer::list::contiguous_appender& p, - _denc::indices) { - denc(std::get(s), p); - encode_helper_nf(s, p, _denc::indices{}); - } - template - static void encode_helper_nf(const T& s, buffer::list::contiguous_appender& p, - _denc::indices) { - denc(std::get(s), p); - } - - template - static void encode_helper_f(const T& s, buffer::list::contiguous_appender& p, - uint64_t f, _denc::indices) { - denc(std::get(s), p, f); - encode_helper_nf(s, p, f, _denc::indices{}); - } - template - static void encode_helper_f(const T& s, buffer::list::contiguous_appender& p, - uint64_t f, _denc::indices) { - denc(std::get(s), p, f); - } - - template - static void decode_helper(T& s, buffer::ptr::iterator& p, - _denc::indices) { - denc(std::get(s), p); - decode_helper(s, p, _denc::indices{}); - } - template - static void decode_helper(T& s, buffer::ptr::iterator& p, - _denc::indices) { - denc(std::get(s), p); - } - template - static void decode_helper(T& s, buffer::list::iterator& p, - _denc::indices) { - denc(std::get(s), p); - decode_helper(s, p, _denc::indices{}); - } - template - static void decode_helper(T& s, buffer::list::iterator& p, - _denc::indices) { - denc(std::get(s), p); + template + static std::enable_if_t::need_contiguous> + decode_helper(U& s, buffer::list::iterator& p, std::index_sequence) { + (denc(std::get(s), p), ..., void()); } public: - using traits = _denc::tuple_traits; static constexpr bool supported = true; - static constexpr bool featured = traits::featured; - static constexpr bool bounded = traits::bounded; - static constexpr bool need_contiguous = traits::need_contiguous; - + static constexpr bool featured = (denc_traits::featured || ...); + static constexpr bool bounded = (denc_traits::bounded && ...); + static constexpr bool need_contiguous = + (denc_traits::need_contiguous || ...); - template - static std::enable_if_t - bound_encode(const container& s, size_t& p) { - bound_encode_helper_nfnb(s, p, _denc::build_indices_t{}); - } - template - static std::enable_if_t - bound_encode(const container& s, size_t& p) { - bound_encode_helper_nfb(s, p, _denc::build_indices_t{}); - } - template - static std::enable_if_t + template + static std::enable_if_t::featured> bound_encode(const container& s, size_t& p, uint64_t f) { - bound_encode_helper_fnb(s, p, _denc::build_indices_t{}); + bound_encode_helper(s, p, f, std::index_sequence_for{}); } - template - static std::enable_if_t - bound_encode(const container& s, size_t& p, uint64_t f) { - bound_encode_helper_fb(s, p, _denc::build_indices_t{}); + template + static std::enable_if_t::featured> + bound_encode(const container& s, size_t& p) { + bound_encode_helper(s, p, std::index_sequence_for{}); } - template - static std::enable_if_t - encode(const container& s, buffer::list::contiguous_appender& p) { - encode_helper_nf(s, p, _denc::build_indices_t{}); + template + static std::enable_if_t::featured> + encode(const container& s, buffer::list::contiguous_appender& p, uint64_t f) { + encode_helper(s, p, f, std::index_sequence_for{}); } - template - static std::enable_if_t - encode(const container& s, buffer::list::contiguous_appender& p, - uint64_t f) { - encode_helper_f(s, p, f, _denc::build_indices_t{}); + template + static std::enable_if_t::featured> + encode(const container& s, buffer::list::contiguous_appender& p) { + encode_helper(s, p, std::index_sequence_for{}); } static void decode(container& s, buffer::ptr::iterator& p, uint64_t f = 0) { - decode_helper(s, p, _denc::build_indices_t{}); + decode_helper(s, p, std::index_sequence_for{}); } - template - static std::enable_if_t + + template + static std::enable_if_t::need_contiguous> decode(container& s, buffer::list::iterator& p, uint64_t f = 0) { - decode_helper(s, p, _denc::build_indices_t{}); + decode_helper(s, p, std::index_sequence_for{}); } }; -- 2.39.5