libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
from_memory.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <cstdint>
12#include <new>
13
14namespace from {
15template <typename T>
17 static_assert(sizeof(T) > 0, "T must be a complete type");
18 static constexpr auto align_val = alignof(T) - 1;
19
20public:
21 using value_type = T;
22 using pointer_type = T*;
23
24 static constexpr auto size = sizeof(T);
25 static constexpr auto alignment = alignof(T);
26
27 pointer_type get() const noexcept {
28 if (ptr && ptr->is_init) {
29 std::byte* mem = ptr->memory;
30 uintptr_t align_up = -reinterpret_cast<intptr_t>(mem) & align_val;
31 return std::launder(reinterpret_cast<pointer_type>(mem + align_up));
32 }
33 return nullptr;
34 }
35
36 value_type& operator*() const noexcept {
37 return *get();
38 }
39
40 pointer_type operator->() const noexcept {
41 return get();
42 }
43
44 explicit operator bool() const noexcept {
45 return static_cast<bool>(get());
46 }
47
48private:
49 struct {
50 std::byte memory[size + align_val];
51 bool is_init;
52 }* ptr;
53};
54} // namespace from
Definition from_memory.hpp:16