36constexpr u32 FourCC(
const char (&str)[5])
noexcept {
38 for (std::size_t i = 0; i < 4; ++i) {
39 result |=
static_cast<u32>(
static_cast<u8>(str[i])) << (i * CHAR_BIT);
52 static_assert(std::is_arithmetic_v<T>);
55 for (std::size_t i = 0; i <
sizeof(T); i++)
56 reinterpret_cast<unsigned char*
>(&swapped)[i] =
reinterpret_cast<unsigned char*
>(&src)[
sizeof(T) - i - 1];
68constexpr typename std::underlying_type<E>::type
as_int(E e)
noexcept {
69 return static_cast<typename std::underlying_type<E>::type
>(e);
85template <
class T,
class U>
87 return static_cast<T
>(std::forward<U>(u));
101template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* =
nullptr>
103 constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value);
107#if defined(__clang__) || defined(__GNUC__)
108#pragma GCC diagnostic push
109#pragma GCC diagnostic ignored "-Wfloat-equal"
111 if (
static_cast<U
>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{})))) {
114#if defined(__clang__) || defined(__GNUC__)
115#pragma GCC diagnostic pop
158 [[nodiscard]] std::array<u8, 16>
Final() noexcept;
161 std::
size_t lo_ = 0, hi_ = 0;
162 std::
size_t a_ = 0x67452301;
163 std::
size_t b_ = 0xefcdab89;
164 std::
size_t c_ = 0x98badcfe;
165 std::
size_t d_ = 0x10325476;
166 unsigned char buffer_[64] = {};
167 std::size_t block_[16] = {};
169 const unsigned char* Body(
const unsigned char*
data, std::size_t size);
194template <
typename To,
typename From>
196 static_assert(std::is_trivially_copyable_v<From>,
"From type must be trivially copyable");
197 static_assert(std::is_trivially_copyable_v<To>,
"To type must be trivially copyable");
199 std::size_t new_size = (container.size() *
sizeof(From)) /
sizeof(To);
201 if (new_size *
sizeof(To) != container.size() *
sizeof(From)) {
205 std::vector<To> new_container(new_size);
207 std::memcpy(new_container.data(), container.data(), new_container.size() *
sizeof(To));
209 return new_container;
218template <
typename To,
typename From>
220 std::vector<To> new_container(container.size());
222 for (std::size_t i = 0; i < container.size(); i++) {
223 new_container[i] =
static_cast<To
>(container[i]);
226 return new_container;
Contains macros and definitions for fixed-width types.
std::array< u8, 16 > Final() noexcept
Finalizes the hashing process and retrieves the result.
void Update(BufferView data) noexcept
Updates the hash state by processing a new chunk of data.
A utility class for calculating MD5 message digests.
Definition data.hpp:145
T SwapEndian(T src) noexcept
Swaps endianness of a value.
Definition data.hpp:51
auto CastContainer(const std::vector< From > &container)
Casts container elements to a different type.
Definition data.hpp:219
constexpr T narrow_cast(U &&u) noexcept
Narrowing cast without bounds checking.
Definition data.hpp:86
auto ReinterpretContainer(const std::vector< From > &container)
Reinterprets container data as a different type (via a bitwise copy)
Definition data.hpp:195
std::array< u8, 16 > MD5(BufferView data) noexcept
Calculates an MD5 checksum.
u32 CRC32(BufferView data, u32 polynomial=0xEDB88320) noexcept
Calculates a CRC32 checksum.
constexpr u32 FourCC(const char(&str)[5]) noexcept
Generates a numeric four character code from a given string.
Definition data.hpp:36
u32 XXH32(BufferView data, u32 seed=0xC0108888)
Calculates an XXH32 checksum.
constexpr T narrow(U u)
Safe narrowing cast with runtime bounds checking.
Definition data.hpp:102
constexpr std::underlying_type< E >::type as_int(E e) noexcept
Converts an enum value to its underlying integer type.
Definition data.hpp:68
Exception thrown for generic runtime errors.
Definition exception.hpp:82
Exception thrown for narrowing conversion errors.
Definition exception.hpp:121
#define NNL_THROW(object)
Throws an exception or terminates the program if exceptions are disabled.
Definition exception.hpp:46
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
std::uint8_t u8
8-bit unsigned integer
Definition fixed_type.hpp:64
Provides classes for reading and writing binary data to and from various sources.
Provides functions and classes for handling binary data.
Definition data.hpp:19
Definition exception.hpp:56