async_mqtt 5.0.0
Loading...
Searching...
No Matches
endian_convert.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_ENDIAN_CONVERT_HPP)
8#define ASYNC_MQTT_UTIL_ENDIAN_CONVERT_HPP
9
10#include <algorithm>
11
12#include <boost/endian/conversion.hpp>
13
14#include <async_mqtt/util/static_vector.hpp>
15
16namespace async_mqtt {
17
18template <typename T>
19T endian_load(char const* buf) {
20 static_vector<unsigned char, sizeof(T)> usbuf(sizeof(T));
21 std::copy(buf, buf + sizeof(T), usbuf.data());
22 return boost::endian::endian_load<T, sizeof(T), boost::endian::order::big>(usbuf.data());
23}
24
25template <typename T>
26void endian_store(T val, char* buf) {
27 static_vector<unsigned char, sizeof(T)> usbuf(sizeof(T));
28 boost::endian::endian_store<T, sizeof(T), boost::endian::order::big>(usbuf.data(), val);
29 std::copy(usbuf.begin(), usbuf.end(), buf);
30}
31
32template <typename T>
33static_vector<char, sizeof(T)> endian_static_vector(T val) {
34 static_vector<unsigned char, sizeof(T)> usbuf(sizeof(T));
35 boost::endian::endian_store<T, sizeof(T), boost::endian::order::big>(usbuf.data(), val);
36 static_vector<char, sizeof(T)> cbuf(sizeof(T));
37 std::copy(usbuf.begin(), usbuf.end(), cbuf.begin());
38 return cbuf;
39}
40
41} // namespace async_mqtt
42
43#endif // ASYNC_MQTT_UTIL_ENDIAN_CONVERT_HPP