9 #if __cplusplus >= 201402L
11 template <
typename T, std::size_t N, std::size_t... Ns>
12 std::array<T, N> make_array_impl(std::initializer_list<T> t,
13 std::index_sequence<Ns...>) {
14 return std::array<T, N>{*(t.begin() + Ns)...};
17 template <
typename T, std::
size_t N>
18 std::array<T, N> make_array(std::initializer_list<T> t) {
20 throw std::out_of_range(
"make_array out of range");
22 return make_array_impl<T, N>(t, std::make_index_sequence<N>());
27 template <
typename ByteType,
size_t ByteLength = 32>
class fix_bytes {
29 using byte_t = ByteType;
30 const static size_t fixed_length;
32 static_assert(
sizeof(ByteType) == 1,
33 "cannot use ByteType that with larger size than 1");
36 #if __cplusplus >= 201402L
37 fix_bytes(std::initializer_list<byte_t> l)
38 : m_value(internal::make_array<byte_t, ByteLength>(l)) {}
43 template <
typename T>
fix_bytes(
const T *buf,
size_t buf_len) {
44 static_assert(
sizeof(T) ==
sizeof(ByteType),
"invalid buf pointer");
45 if (buf_len >= ByteLength) {
46 memcpy(m_value.data(), buf, ByteLength);
48 throw std::runtime_error(
"unmatched byte length");
64 m_value = std::move(v.m_value);
69 return m_value == v.m_value;
73 return m_value != v.m_value;
76 return memcmp(m_value, v.m_value, ByteLength) < 0;
82 std::copy(ret.m_value.begin(), ret.m_value.begin() + ByteLength,
84 std::copy(ret.m_value.begin() + ByteLength, ret.m_value.end(),
88 const byte_t &operator[](
size_t index)
const {
return m_value[index]; }
89 byte_t &operator[](
size_t index) {
return m_value[index]; }
91 inline size_t size()
const {
return ByteLength; }
93 inline const byte_t *value()
const {
return m_value.data(); }
95 inline byte_t *value() {
return m_value.data(); }
97 bool empty()
const {
return m_value.empty(); }
100 std::array<byte_t, ByteLength> m_value;