forked from jbcoe/polymorphic_value
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindirect.h
258 lines (214 loc) · 5.67 KB
/
indirect.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <memory>
#include <type_traits>
struct in_place_t
{
};
constexpr in_place_t in_place;
class bad_indirect_cast : public std::bad_cast
{
};
namespace detail
{
class control_block
{
public:
virtual ~control_block() = default;
virtual std::unique_ptr<control_block> copy() const = 0;
virtual std::unique_ptr<control_block> move() = 0;
virtual void* ptr() = 0;
};
template <typename T>
class direct_control_block final : public control_block
{
private:
T t;
public:
template <typename... Ts>
explicit direct_control_block(Ts&&... ts) :
t(std::forward<Ts>(ts)...)
{
}
std::unique_ptr<control_block> copy() const override
{
return std::make_unique<direct_control_block>(t);
}
std::unique_ptr<control_block> move() override
{
return std::make_unique<direct_control_block>(std::move(t));
}
void* ptr() override
{
return &t;
}
};
template <typename T>
using dcb_t = direct_control_block<std::remove_cv_t<std::remove_reference_t<T>>>;
} // namespace detail
template <typename T>
class indirect
{
template <typename>
friend class indirect;
private:
struct data
{
T* ptr;
std::unique_ptr<detail::control_block> cb;
data(std::unique_ptr<detail::control_block>&& cb_) :
ptr(static_cast<T*>(cb_->ptr())),
cb(std::move(cb_))
{
}
data& operator=(std::unique_ptr<detail::control_block>&& cb_)
{
ptr = static_cast<T*>(cb_->ptr());
cb = std::move(cb_);
return *this;
}
};
data m;
public:
template <typename T_ = T, std::enable_if_t<std::is_default_constructible<T_>::value, int> = 0>
indirect() :
m(std::make_unique<detail::dcb_t<T>>())
{
}
template <typename... Ts>
explicit indirect(in_place_t, Ts&&... ts) :
m(std::make_unique<detail::dcb_t<T>>(std::forward<Ts>(ts)...))
{
}
indirect(indirect const& other) :
m(other.m.cb->copy())
{
}
indirect(indirect&& other) :
m(other.m.cb->move())
{
}
indirect& operator=(indirect const& other)
{
m = other.m.cb->copy();
return *this;
}
indirect& operator=(indirect&& other)
{
m = other.m.cb->move();
return *this;
}
template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
indirect(indirect<U> const& other) :
m(other.m.cb->copy())
{
}
template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
indirect(indirect<U>&& other) :
m(other.m.cb->move())
{
}
template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
indirect& operator=(indirect<U> const& other)
{
m = other.m.cb->copy();
return *this;
}
template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
indirect& operator=(indirect<U>&& other)
{
m = other.m.cb->move();
return *this;
}
template <typename U, std::enable_if_t<std::is_base_of<T, std::remove_reference_t<U>>::value, int> = 0>
indirect(U&& other) :
m(std::make_unique<detail::dcb_t<U>>(std::forward<U>(other)))
{
}
template <typename U, std::enable_if_t<std::is_base_of<T, std::remove_reference_t<U>>::value, int> = 0>
indirect& operator=(U&& other)
{
m = std::make_unique<detail::dcb_t<U>>(std::forward<U>(other));
return *this;
}
void swap(indirect& other) noexcept
{
using std::swap;
swap(m.ptr, other.m.ptr);
swap(m.cb, other.m.cb);
}
T const* operator->() const noexcept
{
return m.ptr;
}
T* operator->() noexcept
{
return m.ptr;
}
T const& operator*() const noexcept
{
return *m.ptr;
}
T& operator*() noexcept
{
return *m.ptr;
}
private:
template <typename T, typename U>
friend indirect<T> static_indirect_cast(indirect<U> const& i);
template <typename T, typename U>
friend indirect<T> static_indirect_cast(indirect<U>&& i);
template <typename T, typename U>
friend indirect<T> dynamic_indirect_cast(indirect<U> const& i);
template <typename T, typename U>
friend indirect<T> dynamic_indirect_cast(indirect<U>&& i);
explicit indirect(std::unique_ptr<detail::control_block>&& cb) noexcept :
m(std::move(cb))
{
}
template <typename U>
void try_static_cast() const
{
static_cast<U*>(m.ptr);
}
template <typename U>
void try_dynamic_cast() const
{
if (!dynamic_cast<U*>(m.ptr))
{
throw bad_indirect_cast();
}
}
};
template <typename T, typename... Ts>
indirect<T> make_indirect(Ts&&... ts)
{
return indirect<T>(in_place, std::forward<Ts>(ts)...);
}
template <typename T, typename U>
indirect<T> static_indirect_cast(indirect<U> const& i)
{
i.try_static_cast<T>();
return indirect<T>(i.m.cb->copy());
}
template <typename T, typename U>
indirect<T> static_indirect_cast(indirect<U>&& i)
{
i.try_static_cast<T>();
return indirect<T>(i.m.cb->move());
}
template <typename T, typename U>
indirect<T> dynamic_indirect_cast(indirect<U> const& i)
{
i.try_dynamic_cast<T>();
return indirect<T>(i.m.cb->copy());
}
template <typename T, typename U>
indirect<T> dynamic_indirect_cast(indirect<U>&& i)
{
i.try_dynamic_cast<T>();
return indirect<T>(i.m.cb->move());
}
template <typename T>
void swap(indirect<T>& i1, indirect<T>& i2) noexcept
{
i1.swap(i2);
}