-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_movable.h
348 lines (320 loc) · 9.36 KB
/
any_movable.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef any_movable_h_INCLUDED
#define any_movable_h_INCLUDED
#include <utility> //std::move/forward
#include <new> // placement new
#include <typeinfo> // typeid, std::type_info
#include <any> // bad_any_cast
#include <type_traits>
#include <iostream>
class any_movable
{
union Storage
{
unsigned char data[6 * sizeof(void *)]; // approx same size as std::function's buffer
long double to_ensure_alignment_with_almost_anything;
};
struct Base
{
// The interface that any_movable will use to handle the held item
// A Derived<T> template will derive from this and implement the interface for each T
virtual ~Base() {}
virtual Base * move_to(void * dst) { return new (dst) Base(); }
virtual std::type_info const & getTypeInfo() const { return typeid(void); }
virtual void * getItem() { return nullptr; }
virtual void const * getItem() const { return nullptr; }
virtual bool can_assign() const { return true; } // you can assign nothing to nothing!
virtual void move_assign(void * data) { }
virtual void youveGotToThrowItThrowIt() const { throw getItem(); } //nullptr!
virtual bool isClass() const { return false; } // is the type you are holding a class or non-class
};
// Implement Base for each T
template<typename T>
struct Derived : Base
{
T t;
template <typename ...Args>
Derived(Args &&... args) : t(std::forward<Args>(args)...)
{
}
virtual ~Derived() {}
virtual Base * move_to(void * dst) override
{
return new (dst) Derived(std::move(t));
}
virtual std::type_info const & getTypeInfo() const override
{
return typeid(T);
}
virtual void * getItem() override
{
return &t;
}
virtual void const * getItem() const override
{
return &t;
}
virtual void move_assign(void * data) override
{
if constexpr (std::is_move_assignable_v<T>)
t = std::move(*reinterpret_cast<T *>(data));
}
virtual bool can_assign() const override
{
return std::is_move_assignable_v<T>;
}
virtual void youveGotToThrowItThrowIt() const
{
throw const_cast<T *>(reinterpret_cast<T const *>(getItem())); // so evil!
}
virtual bool isClass() const
{
return std::is_class_v<T>;
}
};
Storage storage;
Base * ptr = nullptr;
template<typename T>
T * try_as_base() const
{
// What follows one of the more evil things I've done....
// We would like to ask ptr, which holds some type X, whether X is derived from T
// But we can't ask that because ptr is not a template (so we can't pass <T>).
// ie It is not possible to have a virtual template function (for sound reasons).
// HOWEVER, what we can, magically, terribly, do is
// call a non-template function on ptr, asking it to throw a X * exception
// which we will attempt to catch here as a T *.
// If the catch is successful, then X derives from T.
try
{
if (ptr)
ptr->youveGotToThrowItThrowIt();
}
catch (T * p)
{
// yes it does derive from T!
return p;
}
catch (...)
{
}
return nullptr;
}
template<typename T, typename ...Args>
std::decay_t<T> & takeAndMake(Args &&... args)
{
reset();
using UT = std::decay_t<T>; // remove ref, etc
if (sizeof(Derived<UT>) <= sizeof(Storage))
ptr = new (storage.data) Derived<UT>(std::forward<Args>(args)...); // TODO: use C++20 std::construct_at for constexpr
else
ptr = new Derived<UT>(std::forward<Args>(args)...);
return access<UT>();
}
void take(any_movable && other)
{
reset();
if ((void *)other.ptr == other.storage.data) // so not null either
{
// we must transfer from one storage to another
// but we don't know how (we don't know what Derived is)
// so ask ptr to do it via a virtual function
ptr = other.ptr->move_to(storage.data);
other.ptr->~Base();
}
else
{
// take ownership (and might be null)
ptr = other.ptr;
}
other.ptr = nullptr;
}
public:
void reset()
{
if ((void *)ptr == (void *)storage.data) // local? (also not null)
ptr->~Base(); // destroy (possibly Derived), but don't deallocate
else
delete ptr; // null is fine here
ptr = nullptr;
}
any_movable() {}
~any_movable()
{
reset();
}
any_movable(any_movable &) = delete;
any_movable(any_movable const &) = delete;
any_movable & operator=(any_movable &) = delete;
any_movable & operator=(any_movable const &) = delete;
template<typename T>
any_movable(T && t)
{
takeAndMake<T>(std::forward<T>(t));
}
template<typename T>
any_movable & operator=(T && t)
{
if (has_type<T>() && ptr->can_assign())
ptr->move_assign(&t); // we already hold a T, so assign it the new value
else
takeAndMake<T>(std::forward<T>(t)); // rebuild from the ground up
return *this;
}
any_movable(any_movable && other)
{
take(std::move(other));
}
any_movable & operator=(any_movable && other)
{
if (ptr && ptr->can_assign() && type() == other.type()) { // we've already got one
ptr->move_assign(other.ptr->getItem());
// we could just leave the moved-from T,
// but I suspect most people expect it to be reset
other.reset();
}
else {
take(std::move(other));
}
return *this;
}
template <typename T, typename ...Args>
std::decay_t<T> & emplace(Args &&... args)
{
return takeAndMake<T>(std::forward<Args>(args)...);
}
std::type_info const & type() const
{
return ptr ? ptr->getTypeInfo() : typeid(void);
}
bool has_value() const
{
return ptr != nullptr;
}
template <typename T>
bool has_type() const
{
return ptr && ptr->getTypeInfo() == typeid(T);
}
template<typename T>
bool has_dynamic_type() const
{
// try_as_base is slow, last resort
return has_type<T>() || (try_as_base<T>() != nullptr);
}
template <typename T>
T const * access_ptr() const
{
if (has_type<T>()) {
return reinterpret_cast<T const *>(ptr->getItem());
}
return nullptr;
}
template <typename T>
T * access_ptr()
{
if (has_type<T>()) {
return reinterpret_cast<T *>(ptr->getItem());
}
return nullptr;
}
template <typename T>
T const & access() const
{
if (T const * p = access_ptr<T>())
return *p;
throw std::bad_any_cast();
}
template<typename T>
T & access()
{
if (T * p = access_ptr<T>())
return *p;
throw std::bad_any_cast();
}
template <typename T>
T const * access_ptr_dynamic() const
{
if (T * p = access_ptr<T>())
return p;
return try_as_base<T>();
}
template <typename T>
T * access_ptr_dynamic()
{
if (T * p = access_ptr<T>())
return p;
return try_as_base<T>();
}
template <typename T>
T const & access_dynamic() const
{
if (T const * p = access_ptr_dynamic<T>())
return *p;
throw std::bad_any_cast();
}
template<typename T>
T & access_dynamic()
{
if (T * p = access_ptr_dynamic<T>())
return *p;
throw std::bad_any_cast();
}
};
template <typename T>
[[nodiscard]] T const * any_dynamic_cast(any_movable const * a)
{
return a->access_ptr_dynamic<T>();
}
template <typename T>
[[nodiscard]] T * any_dynamic_cast(any_movable * a)
{
return a->access_ptr_dynamic<T>();
}
template <typename T>
[[nodiscard]] T const & any_dynamic_cast(any_movable const & a)
{
return a.access_dynamic<T>();
}
template <typename T>
[[nodiscard]] T & any_dynamic_cast(any_movable & a)
{
return a.access_dynamic<T>();
}
template <typename T>
[[nodiscard]] T && any_dynamic_cast(any_movable && a)
{
return std::move(a.access_dynamic<T>());
}
// it is (somewhat) OK to open std namespace when you are overloading on your own type
namespace std
{
template <typename T>
[[nodiscard]] T const * any_cast(any_movable const * a)
{
return a ? a->access_ptr<T>() : nullptr;
}
template <typename T>
[[nodiscard]] T * any_cast(any_movable * a)
{
return a ? a->access_ptr<T>() : nullptr;
}
//
// for references, std::any returns a copy of T, but I think T & makes more sense
//
template <typename T>
[[nodiscard]] T const & any_cast(any_movable const & a)
{
return a.access<T>();
}
template <typename T>
[[nodiscard]] T & any_cast(any_movable & a)
{
return a.access<T>();
}
template <typename T>
[[nodiscard]] T && any_cast(any_movable && a)
{
return std::move(a.access<T>());
}
}
#endif // _h