async_mqtt 5.0.0
Loading...
Searching...
No Matches
packet_iterator.hpp
1// Copyright Takatoshi Kondo 2022
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_PACKET_PACKET_ITERATOR_HPP)
8#define ASYNC_MQTT_PACKET_PACKET_ITERATOR_HPP
9
10#include <vector>
11#include <tuple>
12#include <string>
13
14#include <boost/asio/buffer.hpp>
15#include <boost/asio/buffers_iterator.hpp>
16
17#include <async_mqtt/buffer.hpp>
18
19namespace async_mqtt {
20
21namespace as = boost::asio;
22
23template <template <typename...> typename Container, typename Buffer>
24using packet_iterator = as::buffers_iterator<Container<Buffer>>;
25
26template <template <typename...> typename Container, typename Buffer>
27std::pair<packet_iterator<Container, Buffer>, packet_iterator<Container, Buffer>>
28make_packet_range(Container<Buffer> const& cbs) {
29 return {
30 packet_iterator<Container, Buffer>::begin(cbs),
31 packet_iterator<Container, Buffer>::end(cbs)
32 };
33}
34
35template <template <typename...> typename Container, typename Buffer>
36std::string
37to_string(Container<Buffer> const& cbs) {
38 auto [b, e] = make_packet_range(cbs);
39 return std::string(b, e);
40}
41
42template <template <typename...> typename Container, typename Buffer>
43buffer
44to_buffer(Container<Buffer> const& cbs) {
45 switch (cbs.size()) {
46 case 0: return buffer{};
47 case 1: return cbs.front();
48 default: {
49 auto [b, e] = make_packet_range(cbs);
50 return allocate_buffer(b, e);
51 }
52 }
53}
54
55} // namespace async_mqtt
56
57#endif // ASYNC_MQTT_PACKET_PACKET_ITERATOR_HPP