C/C++ API Reference
Loading...
Searching...
No Matches
array.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15// Utilities for building std::byte arrays from strings or integer values at
16// compile time.
17#pragma once
18
19#include <array>
20#include <cstddef>
21#include <iterator>
22
23#include "pw_polyfill/language_feature_macros.h"
24
25namespace pw::bytes {
26namespace internal {
27
28template <typename T>
29constexpr bool UseBytesDirectly = std::is_integral_v<T> || std::is_enum_v<T>;
30
31// Internal implementation functions. CopyBytes copies bytes from an array of
32// byte-sized elements or the underlying bytes of an integer (as little-endian).
33// std::memcpy cannot be used since it is not constexpr.
34template <typename B, typename T, typename... Args>
35PW_CONSTEVAL void CopyBytes(B* array, T value, Args... args) {
36 static_assert(sizeof(B) == sizeof(std::byte));
37
38 if constexpr (UseBytesDirectly<T>) {
39 if constexpr (sizeof(T) == 1u) {
40 *array++ = static_cast<B>(value);
41 } else {
42 for (size_t i = 0; i < sizeof(T); ++i) {
43 *array++ = static_cast<B>(value & 0xFF);
44 value >>= 8;
45 }
46 }
47 } else {
48 static_assert(sizeof(value[0]) == sizeof(B));
49 for (auto b : value) {
50 *array++ = static_cast<B>(b);
51 }
52 }
53
54 if constexpr (sizeof...(args) > 0u) {
55 CopyBytes(array, args...);
56 }
57}
58
59// Evaluates to the size in bytes of an integer or byte array.
60template <typename T>
61PW_CONSTEVAL size_t SizeOfBytes(const T& arg) {
62 if constexpr (UseBytesDirectly<T>) {
63 return sizeof(arg);
64 } else {
65 static_assert(sizeof(arg[0]) == sizeof(std::byte));
66 return std::size(arg);
67 }
68}
69
70template <typename B, typename T, size_t... kIndex>
71PW_CONSTEVAL auto String(const T& array, std::index_sequence<kIndex...>) {
72 return std::array{static_cast<B>(array[kIndex])...};
73}
74
75template <typename T, typename U>
76PW_CONSTEVAL bool CanBeRepresentedAsByteType(const U& value) {
77 return static_cast<U>(static_cast<T>(value)) == value;
78}
79
80} // namespace internal
81
83
86template <typename B = std::byte, typename... Args>
87PW_CONSTEVAL auto Concat(Args... args) {
88 std::array<B, (internal::SizeOfBytes(args) + ...)> bytes{};
89 internal::CopyBytes(bytes.data(), args...);
90 return bytes;
91}
92
94template <typename B = std::byte,
95 size_t kSize,
96 typename Indices = std::make_index_sequence<kSize - 1>>
97PW_CONSTEVAL auto String(const char (&str)[kSize]) {
98 return internal::String<B>(str, Indices{});
99}
100
102template <typename B = std::byte>
103PW_CONSTEVAL auto String(const char (&)[1]) {
104 return std::array<B, 0>{};
105}
106
109template <typename B, auto... values>
111 static_assert((internal::CanBeRepresentedAsByteType<B>(values) && ...));
112 return std::array<B, sizeof...(values)>{static_cast<B>(values)...};
113}
114
116template <auto... values>
118 return Array<std::byte, values...>();
119}
120
123template <typename B, size_t kSize, typename T>
124constexpr auto Initialized(const T& value_or_function) {
125 std::array<B, kSize> array{};
126
127 for (size_t i = 0; i < kSize; ++i) {
128 if constexpr (std::is_integral_v<T>) {
129 array[i] = static_cast<B>(value_or_function);
130 } else {
131 array[i] = static_cast<B>(value_or_function(i));
132 }
133 }
134 return array;
135}
136
138template <size_t kSize, typename T>
139constexpr auto Initialized(const T& value_or_function) {
140 return Initialized<std::byte, kSize>(value_or_function);
141}
142
146template <typename B = std::byte, typename... Args>
147constexpr auto MakeArray(const Args&... args) {
148 return std::array<B, sizeof...(args)>{static_cast<B>(args)...};
149}
150
152
153} // namespace pw::bytes
constexpr auto MakeArray(const Args &... args)
Definition: array.h:147
PW_CONSTEVAL auto String(const char(&str)[kSize])
Converts a string literal to an array of bytes, without the trailing '\0'.
Definition: array.h:97
constexpr auto Initialized(const T &value_or_function)
Definition: array.h:124
PW_CONSTEVAL auto Array()
Definition: array.h:110
PW_CONSTEVAL auto Concat(Args... args)
Definition: array.h:87
#define PW_CONSTEVAL
Definition: language_feature_macros.h:40