Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions example/cpp20_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

#include <boost/redis/connection.hpp>
#include <boost/redis/resp3/node.hpp>
#include <boost/redis/resp3/type.hpp>

#include <boost/asio/as_tuple.hpp>
#include <boost/asio/awaitable.hpp>
Expand All @@ -14,10 +16,12 @@
#include <boost/asio/signal_set.hpp>

#include <iostream>
#include <span>

#if defined(BOOST_ASIO_HAS_CO_AWAIT)

namespace asio = boost::asio;
namespace resp3 = boost::redis::resp3;
using namespace std::chrono_literals;
using boost::redis::request;
using boost::redis::generic_flat_response;
Expand Down Expand Up @@ -72,10 +76,14 @@ auto receiver(std::shared_ptr<connection> conn) -> asio::awaitable<void>

// The response must be consumed without suspending the
// coroutine i.e. without the use of async operations.
for (auto const& elem : resp.value())
std::cout << elem.value << "\n";

std::cout << std::endl;
for (std::span<const resp3::node_view> message : resp.value().messages()) {
if (
message.size() == 4u && message[0].data_type == resp3::type::push &&
message[1].value == "message") {
std::cout << "Channel: " << message[2].value << ", message: " << message[3].value
<< "\n";
}
}

resp.value().clear();
}
Expand Down
8 changes: 8 additions & 0 deletions include/boost/redis/resp3/flat_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef BOOST_REDIS_RESP3_FLAT_TREE_HPP
#define BOOST_REDIS_RESP3_FLAT_TREE_HPP

#include <boost/redis/resp3/messages_view.hpp>
#include <boost/redis/resp3/node.hpp>
#include <boost/redis/resp3/tree.hpp>

Expand Down Expand Up @@ -354,6 +355,13 @@ class flat_tree {
*/
std::size_t get_total_msgs() const noexcept { return total_msgs_; }

messages_view messages() const noexcept
{
return messages_view{
{begin(), end()}
};
}

private:
template <class> friend class adapter::detail::general_aggregate;

Expand Down
27 changes: 27 additions & 0 deletions include/boost/redis/resp3/impl/messages_view.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) 2026 Marcelo Zimbres Silva (mzimbres@gmail.com),
// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/redis/resp3/messages_view.hpp>
#include <boost/redis/resp3/node.hpp>

#include <algorithm>
#include <cstddef>

namespace boost::redis::resp3 {

std::size_t messages_view::compute_message_size(span<const node_view> messages)
{
if (messages.empty())
return 0u;
auto it = std::find_if(messages.begin() + 1u, messages.end(), [](const resp3::node_view& n) {
return n.depth == 0u;
});
return it - messages.begin();
}

} // namespace boost::redis::resp3
91 changes: 91 additions & 0 deletions include/boost/redis/resp3/messages_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Copyright (c) 2026 Marcelo Zimbres Silva (mzimbres@gmail.com),
// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_REDIS_RESP3_MESSAGES_VIEW_HPP
#define BOOST_REDIS_RESP3_MESSAGES_VIEW_HPP

#include <boost/redis/resp3/node.hpp>

#include <boost/core/span.hpp>

#include <cstddef>
#include <iterator>

namespace boost::redis::resp3 {

class messages_view {
span<const node_view> nodes_;

static std::size_t compute_message_size(span<const node_view> nodes);

public:
// TODO: explicit?
messages_view(span<const node_view> nodes) noexcept
: nodes_{nodes}
{ }

class iterator {
const node_view* data_;
const node_view* end_; // required to iterate
std::size_t size_; // of the found range

friend class messages_view;

iterator(const node_view* data, const node_view* end, std::size_t size) noexcept
: data_{data}
, end_{end}
, size_{size}
{ }

void increment()
{
data_ += size_;
size_ = compute_message_size({data_, end_});
}

public:
using value_type = span<const node_view>;
using reference = span<const node_view>;
using pointer = span<const node_view>;
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;

iterator() = default;

reference operator*() const noexcept { return {data_, size_}; }

pointer operator->() const noexcept { return {data_, size_}; }

iterator& operator++() noexcept
{
increment();
return *this;
}

iterator operator++(int) noexcept
{
iterator res{*this};
increment();
return res;
}

bool operator==(const iterator& rhs) const noexcept { return data_ == rhs.data_; }
bool operator!=(const iterator& rhs) const noexcept { return !(*this == rhs); }
};

iterator begin() const noexcept
{
return {nodes_.begin(), nodes_.end(), compute_message_size(nodes_)};
}
iterator end() const noexcept { return {nodes_.end(), nodes_.end(), 0u}; }
bool empty() const noexcept { return nodes_.empty(); }
};

} // namespace boost::redis::resp3

#endif // BOOST_REDIS_RESP3_FLAT_TREE_HPP
1 change: 1 addition & 0 deletions include/boost/redis/src.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <boost/redis/impl/sentinel_resolve_fsm.ipp>
#include <boost/redis/impl/subscription_tracker.ipp>
#include <boost/redis/impl/writer_fsm.ipp>
#include <boost/redis/resp3/impl/messages_view.ipp>
#include <boost/redis/resp3/impl/parser.ipp>
#include <boost/redis/resp3/impl/serialization.ipp>
#include <boost/redis/resp3/impl/type.ipp>