4#if __has_include(<boost/version.hpp>)
5#include <boost/version.hpp>
8#include <boost/endian/conversion.hpp>
9#include <boost/serialization/base_object.hpp>
10#include <boost/serialization/serialization.hpp>
11#include <boost/serialization/vector.hpp>
14#pragma message "Boost wasn't found. Serialization for ByteArray is unavailable."
28 struct ByteView :
public std::span<const std::byte>
30 using std::span<
const std::byte>::span;
31 using std::span<
const std::byte>::operator=;
32 using std::span<
const std::byte>::operator[];
44 [[nodiscard]]
const T *
as()
const
46 return reinterpret_cast<const T *
>(data());
60 return ByteView{ data() + from, size() - from };
76 return ByteView{ data() + from, length };
90 using std::vector<std::byte>::vector;
91 using std::vector<std::byte>::operator=;
92 using std::vector<std::byte>::operator[];
103 template <
typename T>
104 [[nodiscard]]
constexpr T *
as()
106 return reinterpret_cast<T *
>(data());
118 template <
typename T>
119 [[nodiscard]]
constexpr const T *
as()
const
121 return reinterpret_cast<const T *
>(data());
145 template <
typename First,
typename Second,
typename... Args>
146 void append(First &&first, Second &&second, Args &&...args)
148 append(std::forward<First>(first));
149 append(std::forward<Second>(second));
163 template <
typename First,
typename Second>
164 void append(First &&first, Second &&second)
166 append(std::forward<First>(first));
167 append(std::forward<Second>(second));
180 template <
typename T>
181 requires(
sizeof(T) ==
sizeof(std::byte))
void append(
const std::vector<T> &other)
183 reserve(size() + other.size());
184 if constexpr (std::is_trivially_constructible_v<std::byte, T>)
186 insert(end(), other.begin(), other.end());
190 std::transform(other.begin(), other.end(), std::back_inserter(*
this),
191 [](
const T &t) { return static_cast<std::byte>(t); });
205 template <
typename T>
206 requires(
sizeof(T) ==
sizeof(std::byte))
void append(
const std::span<T> &other)
208 reserve(size() + other.size());
209 if constexpr (std::is_trivially_constructible_v<std::byte, T>)
211 insert(end(), other.begin(), other.end());
215 std::transform(other.begin(), other.end(), std::back_inserter(*
this),
216 [](
const T &t) { return static_cast<std::byte>(t); });
230 template <
typename T>
231 requires(
sizeof(T) ==
sizeof(std::byte))
void append(
const std::basic_string<T> &other)
233 reserve(size() + other.size());
234 if constexpr (std::is_trivially_constructible_v<std::byte, T>)
236 insert(end(), other.begin(), other.end());
240 std::transform(other.begin(), other.end(), std::back_inserter(*
this),
241 [](
const T &t) { return static_cast<std::byte>(t); });
254 template <
typename T>
255 requires(
sizeof(T) ==
sizeof(std::byte))
void append(
const std::basic_string_view<T> &other)
257 reserve(size() + other.size());
258 if constexpr (std::is_trivially_constructible_v<std::byte, T>)
260 insert(end(), other.begin(), other.end());
264 std::transform(other.begin(), other.end(), std::back_inserter(*
this),
265 [](
const T &t) { return static_cast<std::byte>(t); });
278 template <
typename... Args>
282 result.
append(std::forward<Args>(args)...);
302 template <std::
integral Integer>
306 rv.resize(
sizeof(Integer));
307 *rv.
as<Integer>() = integer;
322 return ByteView{ data() + from, size() - from };
335 [[nodiscard]]
constexpr ByteView view(
size_t from,
size_t length)
const
337 return ByteView{ data() + from, length };
341 friend class boost::serialization::access;
353 template <
class Archive>
354 void serialize(Archive &ar, [[maybe_unused]]
const unsigned int version)
356 ar &boost::serialization::base_object<std::vector<std::byte>>(*this);
373 template <
typename Integer>
374 [[nodiscard]]
inline Integer bytes_to_integer(
const ByteView byte_view)
377 "The byte array is too small to be converted to the specified integer type");
378 return boost::endian::little_to_native(*
reinterpret_cast<const Integer *
>(byte_view.data()));
391 template <
typename Integer>
392 [[nodiscard]]
inline ByteArray integer_to_bytes(
const Integer value)
394 ByteArray byte_array(
sizeof(Integer));
395 *byte_array.as<Integer>() = boost::endian::native_to_little(value);
410 return bytes_to_integer<uint16_t>(byte_view);
434 return bytes_to_integer<uint32_t>(byte_view);
458 return bytes_to_integer<uint64_t>(byte_view);
482 return bytes_to_integer<int16_t>(byte_view);
506 return bytes_to_integer<int32_t>(byte_view);
530 return bytes_to_integer<int64_t>(byte_view);
Precompiled header (PCH) file for common headers used across the library.