Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
82// Concatenates arrays or integers as a byte array at compile time. Integer
83// values are copied little-endian. Spans are copied byte-for-byte.
84template <typename B = std::byte, typename... Args>
85PW_CONSTEVAL auto Concat(Args... args) {
86 std::array<B, (internal::SizeOfBytes(args) + ...)> bytes{};
87 internal::CopyBytes(bytes.data(), args...);
88 return bytes;
89}
90
91// Converts a string literal to an array of bytes, without the trailing '\0'.
92template <typename B = std::byte,
93 size_t kSize,
94 typename Indices = std::make_index_sequence<kSize - 1>>
95PW_CONSTEVAL auto String(const char (&str)[kSize]) {
96 return internal::String<B>(str, Indices{});
97}
98
99// String overload for the empty string "".
100template <typename B = std::byte>
101PW_CONSTEVAL auto String(const char (&)[1]) {
102 return std::array<B, 0>{};
103}
104
105// Creates an array of bytes from values passed as template parameters. The
106// values are guaranteed to be representable in the destination byte type.
107template <typename B, auto... values>
108PW_CONSTEVAL auto Array() {
109 static_assert((internal::CanBeRepresentedAsByteType<B>(values) && ...));
110 return std::array<B, sizeof...(values)>{static_cast<B>(values)...};
111}
112
113// Array() defaults to using std::byte.
114template <auto... values>
115PW_CONSTEVAL auto Array() {
116 return Array<std::byte, values...>();
117}
118
119// Creates an initialized array of bytes. Initializes the array to a value or
120// the return values from a function that accepts the index as a parameter.
121template <typename B, size_t kSize, typename T>
122constexpr auto Initialized(const T& value_or_function) {
123 std::array<B, kSize> array{};
124
125 for (size_t i = 0; i < kSize; ++i) {
126 if constexpr (std::is_integral_v<T>) {
127 array[i] = static_cast<B>(value_or_function);
128 } else {
129 array[i] = static_cast<B>(value_or_function(i));
130 }
131 }
132 return array;
133}
134
135// Initialized(value_or_function) defaults to using std::byte.
136template <size_t kSize, typename T>
137constexpr auto Initialized(const T& value_or_function) {
138 return Initialized<std::byte, kSize>(value_or_function);
139}
140
141// Creates an array of bytes from a series of function arguments. Unlike
142// Array(), MakeArray() cannot check if the values fit in the destination type.
143// MakeArray() should only be used when Array() is not suitable.
144template <typename B = std::byte, typename... Args>
145constexpr auto MakeArray(const Args&... args) {
146 return std::array<B, sizeof...(args)>{static_cast<B>(args)...};
147}
148
149} // namespace pw::bytes