From 98ebd62d7c35f6a8cb168ec4bc376f5d9d48065c Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Tue, 4 Jan 2022 02:28:20 +0100 Subject: [PATCH] Simplifications. --- doc/DoxygenLayout.xml | 2 +- include/aedis/resp3/adapt.hpp | 12 ++-- .../aedis/resp3/adapter/detail/adapters.hpp | 60 +++++++++---------- include/aedis/resp3/response_traits.hpp | 31 +++++----- 4 files changed, 50 insertions(+), 55 deletions(-) diff --git a/doc/DoxygenLayout.xml b/doc/DoxygenLayout.xml index 9fc5fe67..58fa06ab 100644 --- a/doc/DoxygenLayout.xml +++ b/doc/DoxygenLayout.xml @@ -4,7 +4,7 @@ - + diff --git a/include/aedis/resp3/adapt.hpp b/include/aedis/resp3/adapt.hpp index b7bbdd03..59b57e69 100644 --- a/include/aedis/resp3/adapt.hpp +++ b/include/aedis/resp3/adapt.hpp @@ -12,16 +12,15 @@ namespace aedis { namespace resp3 { -/** \ingroup functions - * @{ - */ - /** \brief Creates a void adapter + \ingroup functions The adapter returned by this function ignores any data and is useful to avoid wasting time with responses on which the user is insterested in. + Example usage: + @code co_await async_read(socket, buffer, adapt()); @endcode @@ -31,10 +30,13 @@ auto adapt() noexcept { return response_traits::adapt(); } /** \brief Adapts user data to the resp3 parser. + \ingroup functions For the types supported by this function see `response_traits`. For example + Example usage: + @code std::unordered_map cont; co_await async_read(socket, buffer, adapt(cont)); @@ -44,7 +46,5 @@ template auto adapt(T& t) noexcept { return response_traits::adapt(t); } -/*! @} */ - } // resp3 } // aedis diff --git a/include/aedis/resp3/adapter/detail/adapters.hpp b/include/aedis/resp3/adapter/detail/adapters.hpp index 9cd02e91..2799ec60 100644 --- a/include/aedis/resp3/adapter/detail/adapters.hpp +++ b/include/aedis/resp3/adapter/detail/adapters.hpp @@ -25,16 +25,7 @@ namespace aedis { namespace resp3 { namespace adapter { - -namespace detail -{ - -struct adapter_ignore { - void - operator()( - type, std::size_t, std::size_t, char const*, std::size_t, - std::error_code&) { } -}; +namespace detail { template typename std::enable_if::value, void>::type @@ -73,17 +64,21 @@ void set_on_resp3_error(type t, std::error_code& ec) } } -/** A general pupose redis response class - - A pre-order-view of the response tree. - */ +// Adapter that ignores responses. +struct ignore { + void + operator()( + type, std::size_t, std::size_t, char const*, std::size_t, + std::error_code&) { } +}; + template -class adapter_general { +class general { private: Container* result_; public: - adapter_general(Container& c = nullptr): result_(&c) {} + general(Container& c = nullptr): result_(&c) {} /** @brief Function called by the parser when new data has been processed. * @@ -104,17 +99,16 @@ public: void operator()( type t, - std::size_t n, + std::size_t aggregate_size, std::size_t depth, char const* data, std::size_t size, std::error_code&) { - result_->emplace_back(t, n, depth, std::string{data, size}); + result_->emplace_back(t, aggregate_size, depth, std::string{data, size}); } }; -// Adapter for simple data types. template class adapter_node { private: @@ -139,14 +133,14 @@ public: } }; -// Adapter for simple data types. +// Adapter for RESP3 simple data types. template -class adapter_simple { +class simple { private: T* result_; public: - adapter_simple(T& t) : result_(&t) {} + simple(T& t) : result_(&t) {} void operator()( @@ -176,12 +170,12 @@ public: }; template -class adapter_optional_simple { +class simple_optional { private: std::optional* result_; public: - adapter_optional_simple(std::optional& o) : result_(&o) {} + simple_optional(std::optional& o) : result_(&o) {} void operator()( @@ -216,16 +210,16 @@ public: } }; -/* A response type that parses the response directly in a vector. +/* A std::vector adapter. */ template -class adapter_vector { +class vector { private: int i_ = -1; Container* result_; public: - adapter_vector(Container& v) : result_{&v} {} + vector(Container& v) : result_{&v} {} void operator()(type t, @@ -261,12 +255,12 @@ public: }; template -class adapter_list { +class list { private: Container* result_; public: - adapter_list(Container& ref): result_(&ref) {} + list(Container& ref): result_(&ref) {} void operator()(type t, @@ -299,13 +293,13 @@ public: }; template -class adapter_set { +class set { private: Container* result_; Container::iterator hint_; public: - adapter_set(Container& c) + set(Container& c) : result_(&c) , hint_(std::end(c)) {} @@ -342,14 +336,14 @@ public: }; template -class adapter_map { +class map { private: Container* result_; Container::iterator current_; bool on_key_ = true; public: - adapter_map(Container& c) + map(Container& c) : result_(&c) , current_(std::end(c)) {} diff --git a/include/aedis/resp3/response_traits.hpp b/include/aedis/resp3/response_traits.hpp index 438eaa17..96526ae7 100644 --- a/include/aedis/resp3/response_traits.hpp +++ b/include/aedis/resp3/response_traits.hpp @@ -21,12 +21,13 @@ namespace aedis { namespace resp3 { -/** \brief Adapts C++ data structures to read operations. +/** \brief Adapts C++ built-in types to RESP3 read operations. * \ingroup classes * - * This class adapts C++ types to read operations. Users are advised - * to use `adapt()` function below for type deduction. The following - * types are supported. + * This class adapts C++ built-in types to RESP3 read operations. + * For type deduction see also `adapt()`. + * + * The following types are supported. * * 1. Integer data types e.g. `int`, `unsigned`, etc. * @@ -57,7 +58,7 @@ struct response_traits using response_type = T; /// The adapter type. - using adapter_type = adapter::detail::adapter_simple; + using adapter_type = adapter::detail::simple; /// Returns an adapter for the reponse r static auto adapt(response_type& r) noexcept { return adapter_type{r}; } @@ -67,7 +68,7 @@ template struct response_traits> { using response_type = std::optional; - using adapter_type = adapter::detail::adapter_optional_simple; + using adapter_type = adapter::detail::simple_optional; static auto adapt(response_type& i) noexcept { return adapter_type{i}; } }; @@ -75,7 +76,7 @@ template struct response_traits> { using response_type = std::vector; - using adapter_type = adapter::detail::adapter_vector; + using adapter_type = adapter::detail::vector; static auto adapt(response_type& v) noexcept { return adapter_type{v}; } }; @@ -91,7 +92,7 @@ template struct response_traits> { using response_type = std::vector; - using adapter_type = adapter::detail::adapter_general; + using adapter_type = adapter::detail::general; static auto adapt(response_type& v) noexcept { return adapter_type{v}; } }; @@ -99,7 +100,7 @@ template struct response_traits> { using response_type = std::list; - using adapter_type = adapter::detail::adapter_list; + using adapter_type = adapter::detail::list; static auto adapt(response_type& v) noexcept { return adapter_type{v}; } }; @@ -107,7 +108,7 @@ template struct response_traits> { using response_type = std::deque; - using adapter_type = adapter::detail::adapter_list; + using adapter_type = adapter::detail::list; static auto adapt(response_type& v) noexcept { return adapter_type{v}; } }; @@ -115,7 +116,7 @@ template struct response_traits> { using response_type = std::set; - using adapter_type = adapter::detail::adapter_set; + using adapter_type = adapter::detail::set; static auto adapt(response_type& s) noexcept { return adapter_type{s}; } }; @@ -123,7 +124,7 @@ template struct response_traits> { using response_type = std::unordered_set; - using adapter_type = adapter::detail::adapter_set; + using adapter_type = adapter::detail::set; static auto adapt(response_type& s) noexcept { return adapter_type{s}; } }; @@ -131,7 +132,7 @@ template struct response_traits> { using response_type = std::map; - using adapter_type = adapter::detail::adapter_map; + using adapter_type = adapter::detail::map; static auto adapt(response_type& s) noexcept { return adapter_type{s}; } }; @@ -139,7 +140,7 @@ template struct response_traits> { using response_type = std::unordered_map; - using adapter_type = adapter::detail::adapter_map; + using adapter_type = adapter::detail::map; static auto adapt(response_type& s) noexcept { return adapter_type{s}; } }; @@ -147,7 +148,7 @@ template <> struct response_traits { using response_type = void; - using adapter_type = adapter::detail::adapter_ignore; + using adapter_type = adapter::detail::ignore; static auto adapt() noexcept { return adapter_type{}; } };