15#include <unordered_map>
22hi_warning_ignore_msvc(26474);
24namespace hi::inline
v1 {
26[[nodiscard]]
bool equal_ptr(
auto *p1,
auto *p2)
noexcept
28 return static_cast<void *
>(p1) ==
static_cast<void *
>(p2);
31template<
typename T,
typename U>
32void memswap(T& dst, U& src)
34 static_assert(
sizeof(T) ==
sizeof(U),
"memswap requires both objects of equal size");
35 std::byte tmp[
sizeof(T)];
36 memcpy(tmp, &src,
sizeof(T));
37 memcpy(&src, &dst,
sizeof(U));
38 memcpy(&dst, tmp,
sizeof(T));
51template<
typename InputIt,
typename T>
55 return new (dst) T(*src);
62template<
typename InputIt,
typename T>
70 while (src != src_last) {
113 if (src_first < dst_first) {
114 auto dst_last = dst_first + (src_last - src_first);
118 while (src != src_first) {
122 }
else if (src_first > dst_first) {
123 auto src = src_first;
124 auto dst = dst_first;
125 while (src != src_last) {
147 while (src != src_last) {
154template<
typename It,
typename... Args>
157 for (
auto it = first; it != last; ++it) {
173constexpr T *ceil(T *ptr,
std::size_t alignment)
noexcept
175 hilet aligned_byte_offset = ceil(
reinterpret_cast<uintptr_t
>(ptr),
static_cast<uintptr_t
>(alignment));
176 return reinterpret_cast<T *
>(aligned_byte_offset);
182 hilet aligned_byte_offset =
floor(
reinterpret_cast<uintptr_t
>(ptr),
static_cast<uintptr_t
>(alignment));
183 return reinterpret_cast<T *
>(aligned_byte_offset);
195 return static_cast<char *
>(ptr) + distance;
207 return static_cast<char const *
>(ptr) + distance;
214 while (i != v.end()) {
223template<
typename K,
typename T>
227 while (i != v.end()) {
228 if (i->second.expired()) {
236template<
typename K,
typename T>
240 while (i != v.end()) {
241 cleanupWeakPointers(i->second);
242 if (i->second.size() == 0) {
250template<
typename Value,
typename Map,
typename Key,
typename... Args>
255 hilet i = map.find(key);
256 if (i == map.end()) {
257 value = std::make_shared<Value>(std::forward<Args>(args)...);
258 map.insert_or_assign(key, value);
268[[nodiscard]] hi_force_inline
constexpr T
load(uint8_t
const *src)
noexcept
272 if (not std::is_constant_evaluated()) {
278 if constexpr (std::endian::native == std::endian::little) {
279 for (
auto i =
sizeof(T); i != 0; --i) {
284 for (
auto i = 0; i !=
sizeof(T); ++i) {
293[[nodiscard]] hi_force_inline
constexpr void store(T src, uint8_t
const *dst)
noexcept
295 using unsigned_type = std::make_unsigned_t<T>;
297 hilet src_ =
static_cast<unsigned_type
>(src);
299 if (not std::is_constant_evaluated()) {
300#if HI_COMPILER == HI_CC_MSVC
301 *
reinterpret_cast<__unaligned unsigned_type
const *
>(dst) = src_;
309 if constexpr (std::endian::native == std::endian::little) {
310 for (
auto i = 0; i !=
sizeof(T); ++i) {
311 dst[i] =
static_cast<uint8_t
>(src_);
315 for (
auto i =
sizeof(T); i != 0; --i) {
316 dst[i] =
static_cast<uint8_t
>(src_);
323[[nodiscard]] hi_force_inline
constexpr void store_or(T src, uint8_t *dst)
noexcept
325 using unsigned_type = std::make_unsigned_t<T>;
327 auto src_ =
static_cast<unsigned_type
>(src);
329 if (not std::is_constant_evaluated()) {
330#if HI_COMPILER == HI_CC_MSVC
331 *
reinterpret_cast<__unaligned unsigned_type *
>(dst) |= src_;
341 if constexpr (std::endian::native == std::endian::little) {
342 for (
auto i = 0; i !=
sizeof(T); ++i) {
343 dst[i] |=
static_cast<uint8_t
>(src_);
347 for (
auto i =
sizeof(T); i != 0; --i) {
348 dst[i] |=
static_cast<uint8_t
>(src_);
Utilities to assert and bound check.
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_axiom_not_null(expression)
Assert if an expression is not nullptr.
Definition assert.hpp:140
Miscellaneous math functions.
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
hi_force_inline T load(void const *src) noexcept
Load an integer from unaligned memory in native byte-order.
Definition endian.hpp:201
T * placement_copy(InputIt src, T *dst)
Copy an object to another memory locations.
Definition memory.hpp:52
void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:192
void construct(It first, It last, Args const &...args)
Construct a set of objects.
Definition memory.hpp:155
constexpr bool is_aligned(T *p)
Check if a pointer is properly aligned for the object it is pointing at.
Definition memory.hpp:165
void placement_move_within_array(T *src_first, T *src_last, T *dst_first)
Move an objects between two memory locations.
Definition memory.hpp:109
T * placement_move(T *src, T *dst)
Move an object between two memory locations.
Definition memory.hpp:89