async_mqtt 5.0.0
Loading...
Searching...
No Matches
hex_dump.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_UTIL_HEX_DUMP_HPP)
8#define ASYNC_MQTT_UTIL_HEX_DUMP_HPP
9
10#include <ostream>
11#include <iomanip>
12
13#include <async_mqtt/packet/packet_iterator.hpp>
14
15namespace async_mqtt {
16
17template <typename Packet>
18struct hex_dump_t {
19 hex_dump_t(Packet const& p):p{p} {}
20
21 Packet const& p;
22};
23
24template <typename Packet>
25inline std::ostream& operator<< (std::ostream& o, hex_dump_t<Packet> const& v) {
26 auto cbs = v.p.const_buffer_sequence();
27 auto [it, end] = make_packet_range(cbs);
28 std::ios::fmtflags f(o.flags());
29 o << std::hex;
30 for (; it != end; ++it) {
31 o << "0x" << std::setw(2) << std::setfill('0') << (static_cast<int>(*it) & 0xff) << ' ';
32 }
33 o.flags(f);
34 return o;
35}
36
37
44template <typename Packet>
45hex_dump_t<Packet> hex_dump(Packet const& p) {
46 return hex_dump_t<Packet>{p};
47}
48
49} // namespace async_mqtt
50
51#endif // ASYNC_MQTT_UTIL_HEX_DUMP_HPP)