Skip to content

Commit 026eb58

Browse files
committed
fixup
1 parent eca728d commit 026eb58

File tree

6 files changed

+74
-37
lines changed

6 files changed

+74
-37
lines changed

example/cpp20_chat_room.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using boost::asio::dynamic_buffer;
3232
using boost::asio::redirect_error;
3333
using boost::redis::config;
3434
using boost::redis::connection;
35-
using boost::redis::generic_response;
35+
using boost::redis::resp3::flat_tree;
3636
using boost::redis::request;
3737
using boost::system::error_code;
3838
using namespace std::chrono_literals;
@@ -45,7 +45,7 @@ auto receiver(std::shared_ptr<connection> conn) -> awaitable<void>
4545
request req;
4646
req.push("SUBSCRIBE", "channel");
4747

48-
generic_response resp;
48+
flat_tree resp;
4949
conn->set_receive_response(resp);
5050

5151
while (conn->will_reconnect()) {
@@ -57,9 +57,13 @@ auto receiver(std::shared_ptr<connection> conn) -> awaitable<void>
5757
co_await conn->async_receive2(redirect_error(ec));
5858
if (ec)
5959
break; // Connection lost, break so we can reconnect to channels.
60-
std::cout << resp.value().at(1).value << " " << resp.value().at(2).value << " "
61-
<< resp.value().at(3).value << std::endl;
62-
resp.value().clear();
60+
61+
for (auto const& elem: resp.get_view())
62+
std::cout << elem.value << "\n";
63+
64+
std::cout << std::endl;
65+
66+
resp.clear();
6367
}
6468
}
6569
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,34 @@ class general_aggregate {
177177
}
178178
};
179179

180+
template <>
181+
class general_aggregate<resp3::tree> {
182+
private:
183+
resp3::tree* tree_ = nullptr;
184+
185+
public:
186+
explicit general_aggregate(resp3::tree* c = nullptr)
187+
: tree_(c)
188+
{ }
189+
190+
void on_init() { }
191+
void on_done() { }
192+
193+
template <class String>
194+
void on_node(resp3::basic_node<String> const& nd, system::error_code&)
195+
{
196+
BOOST_ASSERT_MSG(!!tree_, "Unexpected null pointer");
197+
198+
resp3::node tmp;
199+
tmp.data_type = nd.data_type;
200+
tmp.aggregate_size = nd.aggregate_size;
201+
tmp.depth = nd.depth;
202+
tmp.value = std::string{std::cbegin(nd.value), std::cend(nd.value)};
203+
204+
tree_->push_back(std::move(tmp));
205+
}
206+
};
207+
180208
template <>
181209
class general_aggregate<resp3::flat_tree> {
182210
private:
@@ -197,7 +225,6 @@ class general_aggregate<resp3::flat_tree> {
197225
void on_node(resp3::basic_node<String> const& nd, system::error_code&)
198226
{
199227
BOOST_ASSERT_MSG(!!tree_, "Unexpected null pointer");
200-
201228
tree_->push(nd);
202229
}
203230
};

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ struct response_traits<result<ignore_t>> {
9292
};
9393

9494
template <class String, class Allocator>
95-
struct response_traits<result<std::vector<resp3::basic_node<String>, Allocator>>> {
96-
using response_type = result<std::vector<resp3::basic_node<String>, Allocator>>;
95+
struct response_traits<result<resp3::basic_tree<String, Allocator>>> {
96+
using response_type = result<resp3::basic_tree<String, Allocator>>;
9797
using adapter_type = general_aggregate<response_type>;
9898

9999
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
100100
};
101101

102-
template <class... Ts>
103-
struct response_traits<response<Ts...>> {
104-
using response_type = response<Ts...>;
105-
using adapter_type = static_adapter<response_type>;
102+
template <class String>
103+
struct response_traits<resp3::basic_tree<String>> {
104+
using response_type = resp3::basic_tree<String>;
105+
using adapter_type = general_aggregate<response_type>;
106106

107-
static auto adapt(response_type& r) noexcept { return adapter_type{r}; }
107+
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
108108
};
109109

110110
template <>
@@ -114,6 +114,15 @@ struct response_traits<resp3::flat_tree> {
114114

115115
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
116116
};
117+
118+
template <class... Ts>
119+
struct response_traits<response<Ts...>> {
120+
using response_type = response<Ts...>;
121+
using adapter_type = static_adapter<response_type>;
122+
123+
static auto adapt(response_type& r) noexcept { return adapter_type{r}; }
124+
};
125+
117126
} // namespace boost::redis::adapter::detail
118127

119128
#endif // BOOST_REDIS_ADAPTER_DETAIL_RESPONSE_TRAITS_HPP

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

Lines changed: 9 additions & 1 deletion
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/resp3/tree.hpp>
1617
#include <boost/redis/response.hpp>
1718

1819
#include <boost/mp11.hpp>
@@ -57,12 +58,19 @@ struct result_traits<result<resp3::basic_node<T>>> {
5758
};
5859

5960
template <class String, class Allocator>
60-
struct result_traits<result<std::vector<resp3::basic_node<String>, Allocator>>> {
61+
struct result_traits<result<resp3::basic_tree<String, Allocator>>> {
6162
using response_type = result<std::vector<resp3::basic_node<String>, Allocator>>;
6263
using adapter_type = adapter::detail::general_aggregate<response_type>;
6364
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
6465
};
6566

67+
template <class String>
68+
struct result_traits<resp3::basic_tree<String>> {
69+
using response_type = resp3::basic_tree<String>;
70+
using adapter_type = adapter::detail::general_aggregate<response_type>;
71+
static auto adapt(response_type& v) noexcept { return adapter_type{&v}; }
72+
};
73+
6674
template <>
6775
struct result_traits<resp3::flat_tree> {
6876
using response_type = resp3::flat_tree;

include/boost/redis/resp3/tree.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
namespace boost::redis::resp3 {
1616

1717
/// A RESP3 tree that owns its data.
18-
template <class String>
19-
using basic_tree = std::vector<basic_node<String>>;
18+
template <class String, class Allocator = std::allocator<basic_node<String>>>
19+
using basic_tree = std::vector<basic_node<String>, Allocator>;
2020

2121
/// A RESP3 tree that owns its data.
2222
using tree = basic_tree<std::string>;

test/test_low_level_sync_sans_io.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
using boost::redis::request;
2323
using boost::redis::adapter::adapt2;
2424
using boost::redis::adapter::result;
25-
using boost::redis::generic_response;
25+
using boost::redis::resp3::tree;
2626
using boost::redis::resp3::flat_tree;
2727
using boost::redis::ignore_t;
2828
using boost::redis::resp3::detail::deserialize;
@@ -350,43 +350,35 @@ node from_node_view(node_view const& v)
350350
return ret;
351351
}
352352

353-
generic_response from_flat(flat_tree const& resp)
353+
tree from_flat(flat_tree const& resp)
354354
{
355-
generic_response ret;
355+
tree ret;
356356
for (auto const& e: resp.get_view())
357-
ret.value().push_back(from_node_view(e));
357+
ret.push_back(from_node_view(e));
358358

359359
return ret;
360360
}
361361

362-
// Parses the same data into a generic_response and a
362+
// Parses the same data into a tree and a
363363
// flat_tree, they should be equal to each other.
364-
BOOST_AUTO_TEST_CASE(generic_flat_resps_views_are_set)
364+
BOOST_AUTO_TEST_CASE(flat_tree_views_are_set)
365365
{
366-
using boost::redis::resp3::node_view;
367-
using boost::redis::resp3::type;
368-
369-
generic_response resp1;
366+
tree resp1;
370367
flat_tree fresp;
371368

372369
deserialize(resp3_set, adapt2(resp1));
373370
deserialize(resp3_set, adapt2(fresp));
374371

375-
BOOST_TEST(resp1.has_value());
376-
377372
BOOST_CHECK_EQUAL(fresp.get_reallocs(), 1u);
378373
BOOST_CHECK_EQUAL(fresp.get_total_msgs(), 1u);
379374

380375
auto const resp2 = from_flat(fresp);
381-
BOOST_CHECK_EQUAL(resp1.value(), resp2.value());
376+
BOOST_CHECK_EQUAL(resp1, resp2);
382377
}
383378

384379
// The response should be reusable.
385-
BOOST_AUTO_TEST_CASE(generic_flat_resp_reuse)
380+
BOOST_AUTO_TEST_CASE(flat_tree_reuse)
386381
{
387-
using boost::redis::resp3::node_view;
388-
using boost::redis::resp3::type;
389-
390382
flat_tree tmp;
391383

392384
// First use
@@ -408,11 +400,8 @@ BOOST_AUTO_TEST_CASE(generic_flat_resp_reuse)
408400
BOOST_CHECK_EQUAL(resp1, tmp.get_view());
409401
}
410402

411-
BOOST_AUTO_TEST_CASE(generic_flat_resp_copy_assign)
403+
BOOST_AUTO_TEST_CASE(flat_tree_copy_assign)
412404
{
413-
using boost::redis::resp3::node_view;
414-
using boost::redis::resp3::type;
415-
416405
flat_tree resp;
417406
deserialize(resp3_set, adapt2(resp));
418407

0 commit comments

Comments
 (0)