Loading...
Searching...
No Matches
result_traits.hpp
1/* Copyright (c) 2018-2024 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/adapter/detail/adapters.hpp>
11#include <boost/redis/adapter/ignore.hpp>
12#include <boost/redis/adapter/result.hpp>
13#include <boost/redis/error.hpp>
14#include <boost/redis/ignore.hpp>
15#include <boost/redis/resp3/type.hpp>
16
17#include <boost/mp11.hpp>
18
19#include <string_view>
20#include <tuple>
21#include <variant>
22#include <vector>
23
24namespace boost::redis::adapter::detail {
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 = 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{
71 return result_traits<std::decay_t<T>>::adapt(t);
72}
73
74template <std::size_t N>
75struct assigner {
76 template <class T1, class T2>
77 static void assign(T1& dest, T2& from)
78 {
79 dest[N].template emplace<N>(internal_adapt(std::get<N>(from)));
80 assigner<N - 1>::assign(dest, from);
81 }
82};
83
84template <>
85struct assigner<0> {
86 template <class T1, class T2>
87 static void assign(T1& dest, T2& from)
88 {
89 dest[0].template emplace<0>(internal_adapt(std::get<0>(from)));
90 }
91};
92
93template <class Tuple>
94class static_aggregate_adapter;
95
96template <class Tuple>
97class static_aggregate_adapter<result<Tuple>> {
98private:
99 using adapters_array_type = std::array<
100 mp11::mp_rename<mp11::mp_transform<adapter_t, Tuple>, std::variant>,
101 std::tuple_size<Tuple>::value>;
102
103 // Tuple element we are currently on.
104 std::size_t i_ = 0;
105
106 // Nested aggregate element counter.
107 std::size_t aggregate_size_ = 0;
108
109 adapters_array_type adapters_;
110 result<Tuple>* res_ = nullptr;
111
112public:
113 explicit static_aggregate_adapter(result<Tuple>* r = nullptr)
114 {
115 if (r) {
116 res_ = r;
117 detail::assigner<std::tuple_size<Tuple>::value - 1>::assign(adapters_, r->value());
118 }
119 }
120
121 template <class String>
122 void count(resp3::basic_node<String> const& elem)
123 {
124 if (elem.depth == 1 && is_aggregate(elem.data_type)) {
125 aggregate_size_ = element_multiplicity(elem.data_type) * elem.aggregate_size;
126 }
127
128 if (aggregate_size_ == 0) {
129 i_ += 1;
130 } else {
131 aggregate_size_ -= 1;
132 }
133 }
134
135 template <class String>
136 void operator()(resp3::basic_node<String> const& elem, system::error_code& ec)
137 {
138 using std::visit;
139
140 if (elem.depth == 0) {
141 auto const multiplicity = element_multiplicity(elem.data_type);
142 auto const real_aggr_size = elem.aggregate_size * multiplicity;
143 if (real_aggr_size != std::tuple_size<Tuple>::value)
145
146 return;
147 }
148
149 visit(
150 [&](auto& arg) {
151 arg(elem, ec);
152 },
153 adapters_[i_]);
154 count(elem);
155 }
156};
157
158template <class... Ts>
159struct result_traits<result<std::tuple<Ts...>>> {
160 using response_type = result<std::tuple<Ts...>>;
161 using adapter_type = static_aggregate_adapter<response_type>;
162 static auto adapt(response_type& r) noexcept { return adapter_type{&r}; }
163};
164
165} // namespace boost::redis::adapter::detail
166
167#endif // BOOST_REDIS_ADAPTER_RESPONSE_TRAITS_HPP
ignore_t ignore
Global ignore object.
system::result< Value, error > result
Stores response to individual Redis commands.
Definition result.hpp:54
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:30
@ incompatible_size
Aggregate container has incompatible size.