NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
data.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <array>
9#include <type_traits>
10#include <utility>
11
13#include "NNL/common/io.hpp"
14namespace nnl {
19namespace utl::data {
20
27
36constexpr u32 FourCC(const char (&str)[5]) noexcept {
37 u32 result{0};
38 for (std::size_t i = 0; i < 4; ++i) {
39 result |= static_cast<u32>(static_cast<u8>(str[i])) << (i * CHAR_BIT);
40 }
41 return result;
42}
43
50template <typename T>
51T SwapEndian(T src) noexcept {
52 static_assert(std::is_arithmetic_v<T>);
53 T swapped;
54
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];
57
58 return swapped;
59}
60
67template <typename E>
68constexpr typename std::underlying_type<E>::type as_int(E e) noexcept {
69 return static_cast<typename std::underlying_type<E>::type>(e);
70}
71
85template <class T, class U>
86constexpr T narrow_cast(U&& u) noexcept {
87 return static_cast<T>(std::forward<U>(u));
88}
89
101template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
102constexpr T narrow(U u) {
103 constexpr const bool is_different_signedness = (std::is_signed<T>::value != std::is_signed<U>::value);
104
105 const T t = utl::data::narrow_cast<T>(u);
106
107#if defined(__clang__) || defined(__GNUC__)
108#pragma GCC diagnostic push
109#pragma GCC diagnostic ignored "-Wfloat-equal"
110#endif
111 if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{})))) {
113 }
114#if defined(__clang__) || defined(__GNUC__)
115#pragma GCC diagnostic pop
116#endif
117
118 return t;
119}
120
127u32 CRC32(BufferView data, u32 polynomial = 0xEDB88320) noexcept;
128
135u32 XXH32(BufferView data, u32 seed = 0xC0108888);
136
146 public:
152 void Update(BufferView data) noexcept;
158 [[nodiscard]] std::array<u8, 16> Final() noexcept;
159
160 private:
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] = {};
168
169 const unsigned char* Body(const unsigned char* data, std::size_t size);
170};
171
184std::array<u8, 16> MD5(BufferView data) noexcept;
194template <typename To, typename From>
195auto ReinterpretContainer(const std::vector<From>& container) {
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");
198
199 std::size_t new_size = (container.size() * sizeof(From)) / sizeof(To);
200
201 if (new_size * sizeof(To) != container.size() * sizeof(From)) {
202 NNL_THROW(RuntimeError(NNL_SRCTAG("size mismatch")));
203 }
204
205 std::vector<To> new_container(new_size);
206
207 std::memcpy(new_container.data(), container.data(), new_container.size() * sizeof(To));
208
209 return new_container;
210}
211
218template <typename To, typename From>
219auto CastContainer(const std::vector<From>& container) {
220 std::vector<To> new_container(container.size());
221
222 for (std::size_t i = 0; i < container.size(); i++) {
223 new_container[i] = static_cast<To>(container[i]);
224 }
225
226 return new_container;
227}
228
230} // namespace utl::data
231} // namespace nnl
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
Reader implementation for read-only memory buffers.
Definition io.hpp:598
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