Files
fleet_sports_app_common/inc/app_utils.hpp
T
crosstyan 87e11418a8 refactor(utils): include <cstddef>/<type_traits> used by as_bytes
The as_bytes overloads use std::byte/std::size_t (<cstddef>) and
std::is_trivially_copyable_v (<type_traits>); after the earlier include trim
these compiled only via transitive <span> includes on the fleet's libstdc++.
Include them directly (include-what-you-use).
2026-07-16 00:49:40 +08:00

95 lines
2.8 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <span>
#include <type_traits>
#include <utility>
#include <version>
#ifdef __cpp_lib_format
#include <format>
#include <string>
#endif
namespace app::utils {
/// @brief reinterpret_cast a trivially copyable value to a span of bytes
template <typename T>
requires std::is_trivially_copyable_v<T>
inline std::span<const std::byte> as_bytes(const T &value) {
return {reinterpret_cast<const std::byte *>(&value), sizeof(value)};
}
/// @brief reinterpret_cast a trivially copyable value to a span of bytes (mutable)
template <typename T>
requires std::is_trivially_copyable_v<T>
inline std::span<std::byte> as_bytes(T &value) {
return {reinterpret_cast<std::byte *>(&value), sizeof(value)};
}
/// @brief convert a uint8_t span to a std::byte span
inline std::span<const std::byte> as_bytes(std::span<const uint8_t> span) {
return {reinterpret_cast<const std::byte *>(span.data()), span.size()};
}
/// @brief convert a uint8_t span to a std::byte span (mutable)
inline std::span<std::byte> as_bytes(std::span<uint8_t> span) {
return {reinterpret_cast<std::byte *>(span.data()), span.size()};
}
/// @brief construct a std::byte span from a raw pointer and size
inline std::span<std::byte> as_bytes(std::byte *data, std::size_t size) {
return {data, size};
}
/// @brief construct an immutable std::byte span from a raw pointer and size
inline std::span<const std::byte> as_bytes(const std::byte *data, std::size_t size) {
return {data, size};
}
/// @brief construct a std::byte span from a uint8_t raw pointer and size
inline std::span<std::byte> as_bytes(std::uint8_t *data, std::size_t size) {
return {reinterpret_cast<std::byte *>(data), size};
}
/// @brief construct an immutable std::byte span from a uint8_t raw pointer and size
inline std::span<const std::byte> as_bytes(const std::uint8_t *data, std::size_t size) {
return {reinterpret_cast<const std::byte *>(data), size};
}
/// @brief helper type for the visitor
template <class... Ts>
struct overloads : Ts... {
using Ts::operator()...;
};
/**
* @brief a simple RAII helper for `defer` like behavior
*/
template <typename F>
class deferrer {
F f_;
public:
explicit constexpr deferrer(F f) : f_(std::move(f)) {}
~deferrer() {
f_();
}
deferrer(const deferrer &) = delete;
deferrer &operator=(const deferrer &) = delete;
deferrer(deferrer &&) = delete;
deferrer &operator=(deferrer &&) = delete;
};
#ifdef __cpp_lib_format
inline std::string to_mac_string(std::span<const std::byte, 6> mac) {
return std::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
static_cast<uint8_t>(mac[0]),
static_cast<uint8_t>(mac[1]),
static_cast<uint8_t>(mac[2]),
static_cast<uint8_t>(mac[3]),
static_cast<uint8_t>(mac[4]),
static_cast<uint8_t>(mac[5]));
}
#endif
}