Skip to content

Commit 7e7e3dd

Browse files
committed
Draft version without consume_one implementation
1 parent b822247 commit 7e7e3dd

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

include/boost/redis/adapter/detail/adapters.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <boost/redis/resp3/node.hpp>
1313
#include <boost/redis/resp3/serialization.hpp>
1414
#include <boost/redis/resp3/type.hpp>
15+
#include <boost/redis/response.hpp>
1516

1617
#include <boost/assert.hpp>
1718

@@ -170,6 +171,33 @@ class general_aggregate {
170171
}
171172
};
172173

174+
template <>
175+
class general_aggregate<result<flat_response_impl>> {
176+
private:
177+
result<flat_response_impl>* result_;
178+
179+
public:
180+
explicit general_aggregate(result<flat_response_impl>* c = nullptr)
181+
: result_(c)
182+
{ }
183+
template <class String>
184+
void operator()(resp3::basic_node<String> const& nd, system::error_code&)
185+
{
186+
BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
187+
switch (nd.data_type) {
188+
case resp3::type::blob_error:
189+
case resp3::type::simple_error:
190+
*result_ = error{
191+
nd.data_type,
192+
std::string{std::cbegin(nd.value), std::cend(nd.value)}
193+
};
194+
break;
195+
default:
196+
result_->value().push_back(nd);
197+
}
198+
}
199+
};
200+
173201
template <class Node>
174202
class general_simple {
175203
private:

include/boost/redis/adapter/detail/response_traits.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ struct response_traits<response<Ts...>> {
133133
static auto adapt(response_type& r) noexcept { return adapter_type{r}; }
134134
};
135135

136+
template <>
137+
struct response_traits<generic_flat_response> {
138+
using response_type = generic_flat_response;
139+
using adapter_type = vector_adapter<response_type>;
140+
141+
static auto adapt(response_type& v) noexcept { return adapter_type{v}; }
142+
};
143+
136144
template <class Adapter>
137145
class wrapper {
138146
public:

include/boost/redis/adapter/detail/result_traits.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <boost/redis/error.hpp>
1414
#include <boost/redis/ignore.hpp>
1515
#include <boost/redis/resp3/type.hpp>
16+
#include <boost/redis/response.hpp>
1617

1718
#include <boost/mp11.hpp>
1819

@@ -62,6 +63,13 @@ struct result_traits<result<std::vector<resp3::basic_node<String>, Allocator>>>
6263
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
6364
};
6465

66+
template <>
67+
struct result_traits<generic_flat_response> {
68+
using response_type = generic_flat_response;
69+
using adapter_type = adapter::detail::general_aggregate<response_type>;
70+
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
71+
};
72+
6573
template <class T>
6674
using adapter_t = typename result_traits<std::decay_t<T>>::adapter_type;
6775

include/boost/redis/resp3/node.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ using node = basic_node<std::string>;
6666
*/
6767
using node_view = basic_node<std::string_view>;
6868

69+
/**
70+
* TODO: documentation
71+
*/
72+
struct offset_node : node_view {
73+
std::size_t offset{};
74+
std::size_t size{};
75+
};
76+
6977
} // namespace boost::redis::resp3
7078

7179
#endif // BOOST_REDIS_RESP3_NODE_HPP

include/boost/redis/response.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,83 @@ using response = std::tuple<adapter::result<Ts>...>;
3434
*/
3535
using generic_response = adapter::result<std::vector<resp3::node>>;
3636

37+
struct flat_response_impl {
38+
private:
39+
class iterator {
40+
public:
41+
using value_type = resp3::node_view;
42+
using difference_type = std::ptrdiff_t;
43+
using pointer = void;
44+
using reference = value_type;
45+
using iterator_category = std::forward_iterator_tag;
46+
47+
explicit iterator(flat_response_impl* owner, std::size_t i) noexcept
48+
: owner_(owner)
49+
, index_(i)
50+
{ }
51+
52+
value_type operator*() const { return owner_->operator[](index_); }
53+
54+
iterator& operator++()
55+
{
56+
++index_;
57+
return *this;
58+
}
59+
60+
bool operator==(const iterator& other) const { return index_ == other.index_; }
61+
bool operator!=(const iterator& other) const { return !(*this == other); }
62+
63+
private:
64+
flat_response_impl* owner_;
65+
std::size_t index_;
66+
};
67+
68+
public:
69+
resp3::node_view at(std::size_t index) { return make_node_view(view_.at(index)); }
70+
71+
std::size_t size() { return view_.size(); }
72+
73+
resp3::node_view operator[](std::size_t index) { return make_node_view(view_[index]); }
74+
75+
iterator begin() { return iterator{this, 0}; }
76+
77+
iterator end() { return iterator{this, view_.size()}; }
78+
79+
template <typename String>
80+
void push_back(const resp3::basic_node<String>& nd)
81+
{
82+
resp3::offset_node new_node;
83+
new_node.data_type = nd.data_type;
84+
new_node.aggregate_size = nd.aggregate_size;
85+
new_node.depth = nd.depth;
86+
new_node.offset = data_.size();
87+
new_node.size = nd.value.size();
88+
89+
data_ += std::string{std::cbegin(nd.value), std::cend(nd.value)};
90+
91+
view_.push_back(std::move(new_node));
92+
}
93+
94+
private:
95+
resp3::node_view make_node_view(const resp3::offset_node& n)
96+
{
97+
return resp3::node_view{
98+
.data_type = n.data_type,
99+
.aggregate_size = n.aggregate_size,
100+
.depth = n.depth,
101+
.value = std::string_view{data_.data() + n.offset, n.size}
102+
};
103+
}
104+
105+
std::string data_;
106+
std::vector<resp3::offset_node> view_;
107+
};
108+
109+
/**
110+
* TODO: documentation
111+
*/
112+
using generic_flat_response = adapter::result<flat_response_impl>;
113+
37114
/** @brief Consume on response from a generic response
38115
*
39116
* This function rotates the elements so that the start of the next

0 commit comments

Comments
 (0)