-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyte_array.hpp
More file actions
57 lines (49 loc) · 1.74 KB
/
byte_array.hpp
File metadata and controls
57 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/** @file
*
* @brief Utility functions to construct and compare a @c std::array of
* @c std::byte objects.
*
* A @c std::byte is defined as an enum and there are no implicit conversions from int
* to an enum class. Instead of writing:
*
* @code
* std::array<std::byte, 5> arr =
* { std::byte{0x36}, std::byte{0xd0}, std::byte{0x42}, std::byte{0xbe}, std::byte{0xef} };
* @endcode
* it is easier (specially for larger arrays) to write:
* @code
* auto arr = chops::make_byte_array(0x36, 0xd0, 0x42, 0xbe, 0xef);
* @endcode
*
* The @c make_byte_array function is taken from an example by Blitz Rakete on Stackoverflow
* (Blitz is user Rakete1111 on SO). The @c static_cast was added to eliminate
* "narrowing" warnings (since typically integer values are used to initialize the
* @c std::byte values, which would be a narrowing conversion).
*
* The @c compare_byte_arrays function simplifies test code, typically using the
* Catch2 unit test library.
*
* @authors Blitz Rakete, Cliff Green
*
* @copyright (c) 2017-2025 by Cliff Green
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#ifndef BYTE_ARRAY_HPP_INCLUDED
#define BYTE_ARRAY_HPP_INCLUDED
#include <array>
#include <cstddef> // std::byte
#include <utility> // std::forward
namespace chops {
template<typename... Ts>
constexpr std::array<std::byte, sizeof...(Ts)> make_byte_array(Ts&&... args) noexcept {
return { std::byte{static_cast<std::byte>(std::forward<Ts>(args))}... };
}
template <typename std::size_t N>
constexpr bool compare_byte_arrays (const std::array<std::byte, N>& lhs, const std::array<std::byte, N>& rhs) {
return lhs == rhs;
}
} // end namespace
#endif