-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwarding_storage.hpp
193 lines (169 loc) · 5.23 KB
/
forwarding_storage.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef OPEN_STORAGE_HPP
#define OPEN_STORAGE_HPP
#include <concepts>
#include <memory>
#include <type_traits>
#include "basic_storage.hpp"
template <int N, typename Base>
struct forwarding_storage : basic_storage<N, Base>
{
template <std::derived_from<Base> Derived, typename... Args>
forwarding_storage(std::type_identity<Derived> w, Args &&... args)
{
bool constexpr new_one_fits = sizeof(Derived) <= MinimumN;
is_forwarded_ = !new_one_fits;
if constexpr (new_one_fits)
{
super::template unsafe_construct<Derived>(std::forward<Args>(args)...);
}
else
{
construct_forwarded<Derived>(std::forward<Args>(args)...);
}
}
~forwarding_storage()
{
destroy();
}
forwarding_storage(forwarding_storage && other) noexcept
{
if (other.is_forwarded_)
{
super::template unsafe_construct<unique_base_ptr>(std::move(other.unsafe_unique_ptr_reference()));
}
else
{
super::move_construct_in_place_base(other.pointer());
}
}
forwarding_storage(forwarding_storage const & other)
{
if (other.is_forwarded_)
{
Base * copy = copy_construct_on_heap(other);
super::template unsafe_construct<unique_base_ptr>(copy);
}
else
{
super::copy_construct_in_place_base(other.pointer());
}
}
forwarding_storage & operator=(forwarding_storage const & other)
{
if (is_forwarded_ && other.is_forwarded_)
{
unsafe_unique_ptr_reference().reset(copy_construct_on_heap(other));
}
else
{
destroy();
super::copy_construct_in_place_base(other.pointer());
}
}
forwarding_storage & operator=(forwarding_storage && other) noexcept
{
if (is_forwarded_ && other.is_forwarded_)
{
unsafe_unique_ptr_reference().reset(move_construct_on_heap(other));
}
else
{
destroy();
super::move_construct_in_place_base(other.pointer());
}
}
template <std::derived_from<Base> Derived, typename... Args>
Derived * emplace(Args &&... args)
{
bool constexpr new_one_fits = sizeof(Derived) <= MinimumN;
if (is_forwarded_)
{
if constexpr (new_one_fits)
{
is_forwarded_ = false;
super::template unsafe_destroy<unique_base_ptr>();
return super::template unsafe_construct<Derived>(std::forward<Args>(args)...);
}
else
{
auto & uptr = unsafe_unique_ptr_reference();
uptr = std::make_unique<Derived>(std::forward<Args>(args)...);
return reinterpret_cast<Derived *>(uptr.get());
}
}
else
{
super::unsafe_destroy_base();
if constexpr (new_one_fits)
{
return super::template unsafe_construct<Derived>(std::forward<Args>(args)...);
}
else
{
is_forwarded_ = true;
return construct_forwarded<Derived>(std::forward<Args>(args)...);
}
}
}
Base * pointer()
{
if (is_forwarded_)
{
return unsafe_unique_ptr_reference().get();
}
else
{
return super::unsafe_base_pointer();
}
}
Base const * pointer() const
{
if (is_forwarded_)
{
return unsafe_unique_ptr_reference().get();
}
else
{
return super::unsafe_base_pointer();
}
}
private:
typedef std::unique_ptr<Base> unique_base_ptr;
void destroy()
{
if (is_forwarded_)
{
super::template unsafe_destroy<unique_base_ptr>();
}
else
{
super::unsafe_destroy_base();
}
}
Base * copy_construct_on_heap(forwarding_storage const & other)
{
std::unique_ptr<std::byte[N]> heap_storage = std::make_unique<std::byte[N]>();
super::copy_construct_in_place_base(other.unsafe_unique_ptr_reference().get(), heap_storage.get());
return reinterpret_cast<Base *>(heap_storage.release());
}
Base * move_construct_on_heap(forwarding_storage && other)
{
std::unique_ptr<std::byte[N]> heap_storage = std::make_unique<std::byte[N]>();
super::move_construct_in_place_base(other.unsafe_unique_ptr_reference().get(), heap_storage.get());
return reinterpret_cast<Base *>(heap_storage.release());
}
unique_base_ptr & unsafe_unique_ptr_reference()
{
return *super::template unsafe_pointer<unique_base_ptr>();
}
static int constexpr MinimumN = N < sizeof(unique_base_ptr) ? sizeof(unique_base_ptr) : N;
typedef basic_storage<N, Base> super;
template <typename Derived, typename... Args>
Derived * construct_forwarded(Args &&... args)
{
auto result = super::template unsafe_construct<unique_base_ptr>(std::make_unique<Derived>(std::forward<Args>(args)...));
return reinterpret_cast<Derived *>(result->get());
}
bool is_forwarded_;
};
#endif