-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlazy.hpp
134 lines (110 loc) · 2.82 KB
/
lazy.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#include "common.hpp"
#include <cassert>
namespace util {
template <typename T>
class alignas(T) [[gnu::packed]] lazy final {
std::aligned_storage_t<sizeof(T), std::alignment_of_v<T>> storage_ = {};
bool initialized_ = false;
void destroy() {
if _likely(initialized_) [[likely]] {
std::destroy_at(&value());
initialized_ = false;
}
}
public:
lazy() = default;
template <typename U = T> requires std::is_copy_constructible_v<U>
lazy(const lazy<T> &value) {
if _unlikely(!value.initialized_) [[unlikely]] {
return;
}
new (&storage_) T(value.value());
}
template <typename U = T> requires std::is_move_constructible_v<U>
lazy(lazy<T> &&value) {
if _unlikely(!value.initialized_) [[unlikely]] {
return;
}
new (&storage_) T(std::move(value.value()));
}
template <typename U = T> requires std::is_copy_constructible_v<U>
lazy(const T &value) {
new (&storage_) T(value.value());
}
template <typename U = T> requires std::is_move_constructible_v<U>
lazy(T &&value) {
new (&storage_) T(std::move(value.value()));
}
~lazy() {
destroy();
}
template <typename U = T> requires std::is_copy_assignable_v<U>
lazy<T> & operator = (const lazy<T> &value) {
if _likely(value.initialized_) [[likely]] {
value() = value.value();
}
else {
destroy();
}
return *this;
}
template <typename U = T> requires std::is_move_assignable_v<U>
lazy<T> & operator = (lazy<T> &&value) {
if _likely(value.initialized_) [[likely]] {
value() = std::move(value.value());
}
else {
destroy();
}
return *this;
}
template <typename U = T> requires std::is_copy_assignable_v<U>
lazy<T> & operator = (const T &value) {
value() = value;
return *this;
}
template <typename U = T> requires std::is_move_assignable_v<U>
lazy<T> & operator = (T &&value) {
value() = std::move(value);
return *this;
}
operator bool() const _const {
return _likely(initialized_);
}
bool initialized() const _const {
return _likely(initialized_);
}
template <typename... Args>
T& initialize(Args&& ...args) {
assert(!initialized_);
new (&storage_) T(std::forward<Args>(args)...);
initialized_ = true;
return value();
}
T& value() _const {
assert(initialized_);
return *std::launder(reinterpret_cast<T*>(&storage_));
}
const T& value() const _const {
assert(initialized_);
return *std::launder(reinterpret_cast<const T*>(&storage_));
}
T* operator -> () _const {
assert(initialized_);
return &value();
}
const T* operator -> () const _const {
assert(initialized_);
return &value();
}
T& operator * () _const {
assert(initialized_);
return value();
}
const T& operator * () const _const {
assert(initialized_);
return value();
}
};
}