mirror of
https://fuchsia.googlesource.com/third_party/pigweed.googlesource.com/pigweed/pigweed
synced 2024-09-21 14:16:26 +00:00
cdd6dfcfc6
- Update comments for FlashMemory class. - Split InMemoryFakeFlash to move the fixed-size buffer into the derived FakeFlashBuffer class. - Support initializing FakeFlashBuffer to data provided at construction. - Allow direct access to the underlying fake flash buffer for testing. - Create utilities for working with byte arrays in byte_utils.h. Change-Id: I90d33621cb91da079d7213fe7d33823494120e48
73 lines
2.1 KiB
C++
73 lines
2.1 KiB
C++
// 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 <array>
|
|
#include <cstddef>
|
|
|
|
namespace pw {
|
|
|
|
template <typename T, typename... Args>
|
|
constexpr void CopyBytes(std::byte* array, T value, Args... args) {
|
|
if constexpr (std::is_integral_v<T>) {
|
|
if constexpr (sizeof(T) == 1u) {
|
|
*array++ = static_cast<std::byte>(value);
|
|
} else {
|
|
for (size_t i = 0; i < sizeof(T); ++i) {
|
|
*array++ = static_cast<std::byte>(value & 0xFF);
|
|
value >>= 8;
|
|
}
|
|
}
|
|
} else {
|
|
static_assert(sizeof(value[0]) == sizeof(std::byte));
|
|
for (auto b : value) {
|
|
*array++ = static_cast<std::byte>(b);
|
|
}
|
|
}
|
|
|
|
if constexpr (sizeof...(args) > 0u) {
|
|
CopyBytes(array, args...);
|
|
}
|
|
}
|
|
|
|
// Converts a series of integers to a std::byte array at compile time.
|
|
template <typename... Args>
|
|
constexpr auto AsBytes(Args... args) {
|
|
std::array<std::byte, (sizeof(args) + ...)> bytes{};
|
|
|
|
auto iterator = bytes.begin();
|
|
CopyBytes(iterator, args...);
|
|
|
|
return bytes;
|
|
}
|
|
|
|
namespace internal {
|
|
|
|
template <typename T, size_t... kIndex>
|
|
constexpr auto ByteStr(const T& array, std::index_sequence<kIndex...>) {
|
|
return std::array{static_cast<std::byte>(array[kIndex])...};
|
|
}
|
|
|
|
} // namespace internal
|
|
|
|
// Converts a string literal to a byte array, without the trailing '\0'.
|
|
template <size_t kSize, typename Indices = std::make_index_sequence<kSize - 1>>
|
|
constexpr auto ByteStr(const char (&str)[kSize]) {
|
|
return internal::ByteStr(str, Indices{});
|
|
}
|
|
|
|
} // namespace pw
|