// Copyright 2020 The Pigweed Authors // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy of // the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. // Utilities for building std::byte arrays from strings or integer values. #pragma once #include #include namespace pw { template constexpr void CopyBytes(std::byte* array, T value, Args... args) { if constexpr (std::is_integral_v) { if constexpr (sizeof(T) == 1u) { *array++ = static_cast(value); } else { for (size_t i = 0; i < sizeof(T); ++i) { *array++ = static_cast(value & 0xFF); value >>= 8; } } } else { static_assert(sizeof(value[0]) == sizeof(std::byte)); for (auto b : value) { *array++ = static_cast(b); } } if constexpr (sizeof...(args) > 0u) { CopyBytes(array, args...); } } template constexpr size_t SizeOfBytes(const T& arg) { if constexpr (std::is_integral_v) { return sizeof(arg); } else { return arg.size(); } } // Converts a series of integers or byte arrays to a std::byte array at compile // time. template constexpr auto AsBytes(Args... args) { std::array bytes{}; auto iterator = bytes.begin(); CopyBytes(iterator, args...); return bytes; } namespace internal { template constexpr auto ByteStr(const T& array, std::index_sequence) { return std::array{static_cast(array[kIndex])...}; } } // namespace internal // Converts a string literal to a byte array, without the trailing '\0'. template > constexpr auto ByteStr(const char (&str)[kSize]) { return internal::ByteStr(str, Indices{}); } } // namespace pw