Loading...
Searching...
No Matches
result_traits.hpp
1/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#ifndef BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP
8#define BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP
9
10#include <boost/redis/error.hpp>
11#include <boost/redis/resp3/type.hpp>
12#include <boost/redis/ignore.hpp>
13#include <boost/redis/adapter/detail/adapters.hpp>
14#include <boost/redis/adapter/result.hpp>
15#include <boost/redis/adapter/ignore.hpp>
16#include <boost/mp11.hpp>
17
18#include <vector>
19#include <tuple>
20#include <string_view>
21#include <variant>
22
23namespace boost::redis::adapter::detail
24{
25
26/* Traits class for response objects.
27 *
28 * Provides traits for all supported response types i.e. all STL
29 * containers and C++ buil-in types.
30 */
31template <class Result>
32struct result_traits {
33 using adapter_type = adapter::detail::wrapper<typename std::decay<Result>::type>;
34 static auto adapt(Result& r) noexcept { return adapter_type{&r}; }
35};
36
37template <>
38struct result_traits<result<ignore_t>> {
39 using response_type = result<ignore_t>;
40 using adapter_type = ignore;
41 static auto adapt(response_type) noexcept { return adapter_type{}; }
42};
43
44template <>
45struct result_traits<ignore_t> {
46 using response_type = ignore_t;
47 using adapter_type = ignore;
48 static auto adapt(response_type) noexcept { return adapter_type{}; }
49};
50
51template <class T>
52struct result_traits<result<resp3::basic_node<T>>> {
53 using response_type = result<resp3::basic_node<T>>;
54 using adapter_type = adapter::detail::general_simple<response_type>;
55 static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
56};
57
58template <class String, class Allocator>
59struct result_traits<result<std::vector<resp3::basic_node<String>, Allocator>>> {
60 using response_type = result<std::vector<resp3::basic_node<String>, Allocator>>;
61 using adapter_type = adapter::detail::general_aggregate<response_type>;
62 static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
63};
64
65template <class T>
66using adapter_t = typename result_traits<std::decay_t<T>>::adapter_type;
67
68template<class T>
69auto internal_adapt(T& t) noexcept
70 { return result_traits<std::decay_t<T>>::adapt(t); }
71
72template <std::size_t N>
73struct assigner {
74 template <class T1, class T2>
75 static void assign(T1& dest, T2& from)
76 {
77 dest[N].template emplace<N>(internal_adapt(std::get<N>(from)));
78 assigner<N - 1>::assign(dest, from);
79 }
80};
81
82template <>
83struct assigner<0> {
84 template <class T1, class T2>
85 static void assign(T1& dest, T2& from)
86 {
87 dest[0].template emplace<0>(internal_adapt(std::get<0>(from)));
88 }
89};
90
91template <class Tuple>
92class static_aggregate_adapter;
93
94template <class Tuple>
95class static_aggregate_adapter<result<Tuple>> {
96private:
97 using adapters_array_type =
98 std::array<
99 mp11::mp_rename<
100 mp11::mp_transform<
101 adapter_t, Tuple>,
102 std::variant>,
103 std::tuple_size<Tuple>::value>;
104
105 std::size_t i_ = 0;
106 std::size_t aggregate_size_ = 0;
107 adapters_array_type adapters_;
108 result<Tuple>* res_ = nullptr;
109
110public:
111 explicit static_aggregate_adapter(result<Tuple>* r = nullptr)
112 {
113 if (r) {
114 res_ = r;
115 detail::assigner<std::tuple_size<Tuple>::value - 1>::assign(adapters_, r->value());
116 }
117 }
118
119 template <class String>
120 void count(resp3::basic_node<String> const& nd)
121 {
122 if (nd.depth == 1) {
123 if (is_aggregate(nd.data_type))
124 aggregate_size_ = element_multiplicity(nd.data_type) * nd.aggregate_size;
125 else
126 ++i_;
127
128 return;
129 }
130
131 if (--aggregate_size_ == 0)
132 ++i_;
133 }
134
135 template <class String>
136 void operator()(resp3::basic_node<String> const& nd, system::error_code& ec)
137 {
138 using std::visit;
139
140 if (nd.depth == 0) {
141 auto const real_aggr_size = nd.aggregate_size * element_multiplicity(nd.data_type);
142 if (real_aggr_size != std::tuple_size<Tuple>::value)
144
145 return;
146 }
147
148 visit([&](auto& arg){arg(nd, ec);}, adapters_[i_]);
149 count(nd);
150 }
151};
152
153template <class... Ts>
154struct result_traits<result<std::tuple<Ts...>>>
155{
156 using response_type = result<std::tuple<Ts...>>;
157 using adapter_type = static_aggregate_adapter<response_type>;
158 static auto adapt(response_type& r) noexcept { return adapter_type{&r}; }
159};
160
161} // boost::redis::adapter::detail
162
163#endif // BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP
system::result< Value, error > result
Stores response to individual Redis commands.
Definition: result.hpp:56
ignore_t ignore
Global ignore object.
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition: ignore.hpp:31
@ incompatible_size
Aggregate container has incompatible size.