Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/redis/adapter/any_adapter.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
0002  *
0003  * Distributed under the Boost Software License, Version 1.0. (See
0004  * accompanying file LICENSE.txt)
0005  */
0006 
0007 #ifndef BOOST_REDIS_ANY_ADAPTER_HPP
0008 #define BOOST_REDIS_ANY_ADAPTER_HPP
0009 
0010 
0011 #include <boost/redis/resp3/node.hpp>
0012 #include <boost/redis/adapter/adapt.hpp>
0013 #include <boost/system/error_code.hpp>
0014 #include <cstddef>
0015 #include <functional>
0016 #include <string_view>
0017 #include <type_traits>
0018 
0019 namespace boost::redis {
0020 
0021 namespace detail { 
0022 
0023 // Forward decl
0024 template <class Executor>
0025 class basic_connection;
0026 
0027 }
0028 
0029 /** @brief A type-erased reference to a response.
0030  *  @ingroup high-level-api
0031  *
0032  *  A type-erased response adapter. It can be executed using @ref connection::async_exec.
0033  *  Using this type instead of raw response references enables separate compilation.
0034  *
0035  *  Given a response object `resp` that can be passed to `async_exec`, the following two
0036  *  statements have the same effect:
0037  *  ```
0038  *      co_await conn.async_exec(req, resp);
0039  *      co_await conn.async_exec(req, any_response(resp));
0040  *  ```
0041  */
0042 class any_adapter
0043 {
0044     using fn_type = std::function<void(std::size_t, resp3::basic_node<std::string_view> const&, system::error_code&)>;
0045 
0046     struct impl_t {
0047         fn_type adapt_fn;
0048         std::size_t supported_response_size;
0049     } impl_;
0050 
0051     template <class T>
0052     static auto create_impl(T& resp) -> impl_t
0053     {
0054         using namespace boost::redis::adapter;
0055         auto adapter = boost_redis_adapt(resp);
0056         std::size_t size = adapter.get_supported_response_size();
0057         return { std::move(adapter), size };
0058     }
0059 
0060     template <class Executor>
0061     friend class basic_connection;
0062 
0063 public:
0064     /**
0065      * @brief Constructor.
0066      * 
0067      * Creates a type-erased response adapter from `resp` by calling
0068      * `boost_redis_adapt`. `T` must be a valid Redis response type.
0069      * Any type passed to @ref connection::async_exec qualifies.
0070      *
0071      * This object stores a reference to `resp`, which must be kept alive
0072      * while `*this` is being used.
0073      */
0074     template <class T, class = std::enable_if_t<!std::is_same_v<T, any_adapter>>>
0075     explicit any_adapter(T& resp) : impl_(create_impl(resp)) {}
0076 };
0077 
0078 }
0079 
0080 #endif