8#include "../macros.hpp"
10#include "exception.hpp"
11#include "not_null.hpp"
18#include <unordered_map>
23hi_export_module(hikogui.utility.memory);
28hi_warning_ignore_msvc(26474);
31hi_warning_ignore_msvc(26472);
41template<
template<
typename...>
typename T,
typename...
Args>
45 return std::make_unique<deduced_type>(std::forward<Args>(
args)...);
54template<
template<
typename...>
typename T,
typename...
Args>
58 return std::make_shared<deduced_type>(std::forward<Args>(
args)...);
67template<
template<
typename...>
typename T,
typename...
Args>
81template<
template<
typename...>
typename T,
typename...
Args>
89[[
nodiscard]]
bool equal_ptr(
auto *p1,
auto *p2)
noexcept
91 return static_cast<void *
>(p1) ==
static_cast<void *
>(p2);
94template<
typename T,
typename U>
95void memswap(T&
dst, U&
src)
97 static_assert(
sizeof(T) ==
sizeof(U),
"memswap requires both objects of equal size");
98 std::byte
tmp[
sizeof(T)];
114template<
typename InputIt,
typename T>
117 hi_axiom_not_null(
dst);
125template<
typename InputIt,
typename T>
154 hi_axiom_not_null(
src);
155 hi_axiom_not_null(
dst);
158 std::destroy_at(
src);
217template<
typename It,
typename...
Args>
220 for (
auto it = first;
it != last; ++
it) {
234constexpr T *ceil(T *ptr,
std::size_t alignment)
noexcept
255 hi_axiom_not_null(ptr);
256 return static_cast<char *
>(ptr) + distance;
267 hi_axiom_not_null(ptr);
268 return static_cast<char const *
>(ptr) + distance;
275 while (i != v.end()) {
284template<
typename K,
typename T>
288 while (i != v.end()) {
289 if (i->second.expired()) {
297template<
typename K,
typename T>
301 while (i != v.end()) {
302 cleanupWeakPointers(i->second);
303 if (i->second.size() == 0) {
311template<
typename Value,
typename Map,
typename Key,
typename...
Args>
316 auto const i = map.find(key);
317 if (i == map.end()) {
318 value = std::make_shared<Value>(std::forward<Args>(
args)...);
319 map.insert_or_assign(key, value);
328template<numeric T,
byte_like B>
333 if (
not std::is_constant_evaluated()) {
339 if constexpr (std::endian::native == std::endian::little) {
340 for (
auto i =
sizeof(T); i != 0; --i) {
341 if constexpr (
sizeof(r) > 1) {
344 r |=
static_cast<uint8_t
>(
src[i - 1]);
347 for (
auto i = 0; i !=
sizeof(T); ++i) {
348 if constexpr (
sizeof(r) > 1) {
351 r |=
static_cast<uint8_t
>(
src[i]);
358[[
nodiscard]] hi_inline T unaligned_load(
void const *
src)
noexcept
363template<numeric T,
byte_like B>
364constexpr void unaligned_store(T
src, B *
dst)
noexcept
370 if (
not std::is_constant_evaluated()) {
375 if constexpr (std::endian::native == std::endian::little) {
376 for (
auto i = 0; i !=
sizeof(T); ++i) {
381 for (
auto i =
sizeof(T); i != 0; --i) {
382 dst[i - 1] =
static_cast<B
>(
src_);
389void unaligned_store(T
src,
void *
dst)
noexcept
391 return unaligned_store(
src,
reinterpret_cast<std::byte *
>(
dst));
394template<std::
integral T>
395hi_force_inline
constexpr void store_or(T
src, uint8_t *
dst)
noexcept
397 hi_axiom_not_null(
dst);
403 if (
not std::is_constant_evaluated()) {
410 if constexpr (std::endian::native == std::endian::little) {
411 for (
auto i = 0; i !=
sizeof(T); ++i) {
416 for (
auto i =
sizeof(T); i != 0; --i) {
Miscellaneous math functions.
Functions for casting values between types savely.
Utilities for throwing exceptions and terminating the application.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
auto make_shared_ctad_not_null(Args &&...args)
make_shared with CTAD (Class Template Argument Deduction)
Definition memory.hpp:82
T * placement_copy(InputIt src, T *dst)
Copy an object to another memory locations.
Definition memory.hpp:115
auto make_shared_ctad(Args &&...args)
make_shared with CTAD (Class Template Argument Deduction)
Definition memory.hpp:55
void construct(It first, It last, Args const &...args)
Construct a set of objects.
Definition memory.hpp:218
hi_inline void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:253
auto make_unique_ctad(Args &&...args)
make_unique with CTAD (Class Template Argument Deduction)
Definition memory.hpp:42
constexpr bool is_aligned(T *p)
Check if a pointer is properly aligned for the object it is pointing at.
Definition memory.hpp:228
auto make_unique_ctad_not_null(Args &&...args)
make_unique with CTAD (Class Template Argument Deduction)
Definition memory.hpp:68
void placement_move_within_array(T *src_first, T *src_last, T *dst_first)
Move an objects between two memory locations.
Definition memory.hpp:172
constexpr T unaligned_load(B const *src) noexcept
Make an unaligned load of an unsigned integer.
Definition memory.hpp:329
T * placement_move(T *src, T *dst)
Move an object between two memory locations.
Definition memory.hpp:152
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378