-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstdex.hpp
550 lines (461 loc) · 15.1 KB
/
stdex.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*//////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014 Jamboree
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////*/
#ifndef STDEX_FUNCTION_HPP_INCLUDED
#define STDEX_FUNCTION_HPP_INCLUDED
#include <functional>
#include <type_traits>
#include <typeinfo>
#include <cstdint>
// std::is_trivially_move_constructible is not well supported, so I resort to
// Boost here :/
#include <boost/type_traits/has_trivial_move_constructor.hpp>
namespace stdex
{
template<class F, F* f>
struct function_wrapper;
template<class R, class... Ts, R(*f)(Ts...)>
struct function_wrapper<R(Ts...), f>
{
template<class... As>
R operator()(As&&... as) const
{
return f(std::forward<As>(as)...);
}
};
template<class T, class F, F(T::*f)>
struct method_wrapper;
template<class T, class R, class... Ts, R(T::*f)(Ts...)>
struct method_wrapper<T, R(Ts...), f>
{
explicit method_wrapper(T* that)
: that(that)
{}
template<class... As>
R operator()(As&&... as) const
{
return (that->*f)(std::forward<As>(as)...);
}
private:
T* that;
};
}
namespace stdex { namespace detail
{
template<class R, class... Ts>
R bad_call(std::uintptr_t /*data*/, void*, Ts... /*args*/)
{
throw std::bad_function_call();
}
enum class ctrl_code
{
del, copy, move, get
};
inline bool null_ctrl(std::uintptr_t* /*data*/, std::uintptr_t* /*dst*/, ctrl_code /*code*/)
{
return false;
}
template<class T>
struct is_emplaceable
: std::integral_constant<bool, sizeof(T) <= sizeof(std::uintptr_t)
&& alignof(std::uintptr_t) % alignof(T) == 0
&& std::is_nothrow_move_constructible<T>::value
&& std::is_nothrow_destructible<T>::value>
{};
template<class F, class Alloc = std::allocator<void>, class = void>
struct function_manager
{
struct wrapper
: Alloc::template rebind<wrapper>::other, F
{
typedef typename Alloc::template rebind<wrapper>::other alloc_base;
using F::operator();
wrapper(alloc_base&& alloc, F&& f)
: alloc_base(std::move(alloc)), F(std::move(f))
{}
};
static void create(std::uintptr_t* data, F& f, Alloc const& alloc = Alloc())
{
typename wrapper::alloc_base a(alloc);
*data = reinterpret_cast<std::uintptr_t>(
new(a.allocate(1)) wrapper(std::move(a), std::move(f)));
}
template<class R, class... Ts>
static R fwd(std::uintptr_t data, void*, Ts... args)
{
return (*reinterpret_cast<wrapper*>(data))(std::forward<Ts>(args)...);
}
static bool ctrl(std::uintptr_t* src, std::uintptr_t* dst, ctrl_code code)
{
wrapper* data = reinterpret_cast<wrapper*>(*src);
switch (code)
{
case ctrl_code::copy:
{
typename wrapper::alloc_base a(*data);
*dst = reinterpret_cast<std::uintptr_t>(new(a.allocate(1))
wrapper(std::move(a), F(static_cast<F&&>(*data))));
break;
}
case ctrl_code::move:
*dst = *src;
break;
case ctrl_code::del:
{
typename wrapper::alloc_base a(std::move(*data));
data->~wrapper();
a.deallocate(data, 1);
break;
}
case ctrl_code::get:
*dst = reinterpret_cast<std::uintptr_t>(&typeid(F));
*++dst = reinterpret_cast<std::uintptr_t>(data);
}
return true;
}
};
template<class F>
struct fwd_emplaceable
{
template<class R, class... Ts>
static R fwd(std::uintptr_t, void* data, Ts... args)
{
return (*static_cast<F*>(data))(std::forward<Ts>(args)...);
}
};
template<class F>
struct fwd_emplaceable<F*>
{
template<class R, class... Ts>
static R fwd(std::uintptr_t fp, void*, Ts... args)
{
return reinterpret_cast<F*>(fp)(std::forward<Ts>(args)...);
}
};
template<class F, F* f>
struct fwd_emplaceable<function_wrapper<F, f> >
{
template<class R, class... Ts>
static R fwd(std::uintptr_t, void*, Ts... args)
{
return f(std::forward<Ts>(args)...);
}
};
template<class T, class F, F(T::*f)>
struct fwd_emplaceable<method_wrapper<T, F, f> >
{
template<class R, class... Ts>
static R fwd(std::uintptr_t data, void*, Ts... args)
{
return (reinterpret_cast<T*>(data)->*f)(std::forward<Ts>(args)...);
}
};
template<class F, class Alloc>
struct function_manager<F, Alloc,
typename std::enable_if<is_emplaceable<F>::value>::type>
: fwd_emplaceable<F>
{
static void create(std::uintptr_t* data, F& f, Alloc const& = Alloc())
{
new(data) F(std::move(f));
}
static bool ctrl(std::uintptr_t* src, std::uintptr_t* dst, ctrl_code code)
{
F* data = static_cast<F*>(static_cast<void*>(src));
switch (code)
{
case ctrl_code::copy:
new(dst) F(*data);
break;
case ctrl_code::move:
new(dst) F(std::move(*data));
if (boost::has_trivial_move_constructor<F>::value)
return false;
// no break
case ctrl_code::del:
data->~F();
break;
case ctrl_code::get:
*dst = reinterpret_cast<std::uintptr_t>(&typeid(F));
*++dst = reinterpret_cast<std::uintptr_t>(data);
}
return true;
}
};
template<class F>
struct trampoline
{
trampoline() {}
trampoline(F* f)
: f(f)
{}
F* f;
};
}}
namespace stdex
{
template<class... Sig>
class function;
template<>
class function<>
{
protected:
struct internal_tag {};
template<class T>
struct is_subset_of
: std::integral_constant<bool, true>
{};
explicit function(internal_tag) {}
template<class F, class Alloc>
function(internal_tag, F& f, Alloc const& alloc)
: _ctrl(detail::function_manager<F, Alloc>::ctrl)
{
detail::function_manager<F, Alloc>::create(&_data, f, alloc);
}
template<class... Sig>
function(internal_tag, function<Sig...>& other, bool& moved)
: _ctrl(other._ctrl)
{
moved = _ctrl(&other._data, &_data, detail::ctrl_code::move);
}
public:
function() noexcept
: _ctrl(detail::null_ctrl)
{}
function(std::nullptr_t) noexcept
: _ctrl(detail::null_ctrl)
{}
template<class F, class Alloc = std::allocator<void> >
function(F f, Alloc const& alloc = Alloc())
: _ctrl(detail::function_manager<F, Alloc>::ctrl)
{
detail::function_manager<F, Alloc>::create(&_data, f, alloc);
}
template<class R2, class... T2s>
function(R2(*p)(T2s...)) noexcept
{
if (p)
init_raw(p);
else
init_null();
}
// copy
function(function const& other)
: _ctrl(other._ctrl)
{
_ctrl(&other._data, &_data, detail::ctrl_code::copy);
}
// copy from superset
template<class... Sig>
function(function<Sig...> const& other)
: _ctrl(other._ctrl)
{
_ctrl(&other._data, &_data, detail::ctrl_code::copy);
}
// move / move from superset
template<class... Sig>
function(function<Sig...>&& other) noexcept
: _ctrl(other._ctrl)
{
if (_ctrl(&other._data, &_data, detail::ctrl_code::move))
other.init_null();
}
function& operator=(function other) noexcept
{
swap(other);
return *this;
}
void swap(function& other) noexcept
{
std::uintptr_t tmp;
_ctrl(&_data, &tmp, detail::ctrl_code::move);
other._ctrl(&other._data, &_data, detail::ctrl_code::move);
_ctrl(&tmp, &other._data, detail::ctrl_code::move);
std::swap(_ctrl, other._ctrl);
}
~function()
{
_ctrl(&_data, nullptr, detail::ctrl_code::del);
}
explicit operator bool() const noexcept
{
return _ctrl != detail::null_ctrl;
}
std::type_info const& target_type() const
{
std::uintptr_t ret[2] =
{reinterpret_cast<std::uintptr_t>(&typeid(void))};
_ctrl(&_data, ret, detail::ctrl_code::get);
return *reinterpret_cast<std::type_info const*>(ret[0]);
}
template<class T>
T* target() noexcept
{
std::uintptr_t ret[2] =
{reinterpret_cast<std::uintptr_t>(&typeid(void))};
_ctrl(&_data, ret, detail::ctrl_code::get);
if (*reinterpret_cast<std::type_info const*>(ret[0]) == typeid(T))
return reinterpret_cast<T*>(ret[1]);
else
return nullptr;
}
template<class T>
T const* target() const noexcept
{
return const_cast<function*>(this)->template target<T>();
}
// dummy
template<class T>
void operator()();
protected:
template<class F>
void init_raw(F f)
{
_ctrl = detail::function_manager<F>::ctrl;
detail::function_manager<F>::create(&_data, f);
}
void init_null()
{
_ctrl = detail::null_ctrl;
}
void steal(function& other)
{
swap(other);
}
bool (*_ctrl)(std::uintptr_t*, std::uintptr_t*, detail::ctrl_code);
mutable std::uintptr_t _data; // may store small object inplace
};
template<class R, class... Ts, class... Rest>
class function<R(Ts...), Rest...>
: function<Rest...> // avoid slicing
, public detail::trampoline<R(std::uintptr_t, void*, Ts...)>
{
typedef function<Rest...> base_type;
typedef detail::trampoline<R(std::uintptr_t, void*, Ts...)> caller;
protected:
typedef typename base_type::internal_tag internal_tag;
template<class T>
struct is_subset_of
: std::integral_constant<bool, std::is_base_of<caller, T>::value
&& base_type::template is_subset_of<T>::value>
{};
explicit function(internal_tag tag)
: base_type(tag)
{}
template<class F, class Alloc>
function(internal_tag tag, F& f, Alloc const& alloc)
: base_type(tag, f, alloc)
, caller(detail::function_manager<F, Alloc>::template fwd<R, Ts...>)
{}
template<class... Sig>
function(internal_tag tag, function<Sig...>& other, bool& moved)
: base_type(tag, other, moved), caller(other)
{}
public:
function() noexcept
: caller(detail::bad_call<R, Ts...>)
{}
function(std::nullptr_t) noexcept
: caller(detail::bad_call<R, Ts...>)
{}
template<class F, class Alloc = std::allocator<void> >
function(F f, Alloc const& alloc = Alloc())
: base_type(internal_tag(), f, alloc)
, caller(detail::function_manager<F, Alloc>::template fwd<R, Ts...>)
{}
template<class R2, class... T2s>
function(R2(*p)(T2s...)) noexcept
: base_type(internal_tag())
{
if (p)
init_raw(p);
else
init_null();
}
// copy
function(function const& other)
: base_type(static_cast<base_type const&>(other)), caller(other)
{}
// copy from superset
template<class... Sig>
function(function<Sig...> const& other, typename std::enable_if<
is_subset_of<function<Sig...> >::value>::type* = 0)
: base_type(other), caller(other)
{}
// move / move from superset
template<class... Sig>
function(function<Sig...>&& other, typename std::enable_if<is_subset_of<
function<Sig...> >::value, bool>::type moved = false) noexcept
: base_type(internal_tag(), other, moved), caller(other)
{
if (moved)
other.init_null();
}
function& operator=(function other) noexcept
{
steal(other);
return *this;
}
void swap(function& other) noexcept
{
base_type::swap(other);
std::swap(caller::f, other.caller::f);
}
R operator()(Ts... args) const
{
return
caller::f(this->_data, &this->_data, std::forward<Ts>(args)...);
}
using base_type::operator();
using base_type::operator bool;
using base_type::target_type;
using base_type::target;
protected:
template<class... Sig>
friend class function;
template<class F>
void init_raw(F f)
{
caller::f = detail::function_manager<F>::template fwd<R, Ts...>;
base_type::init_raw(f);
}
void init_null()
{
caller::f = detail::bad_call<R, Ts...>;
base_type::init_null();
}
void steal(function& other) noexcept
{
base_type::steal(other);
caller::f = other.caller::f;
}
};
template<class... Sig>
inline void swap(function<Sig...>& a, function<Sig...>& b)
{
a.swap(b);
}
template<class... Sig>
inline bool operator==(function<Sig...> const& f, std::nullptr_t)
{
return !f;
}
template<class... Sig>
inline bool operator==(std::nullptr_t, function<Sig...> const& f)
{
return !f;
}
template<class... Sig>
inline bool operator!=(function<Sig...> const& f, std::nullptr_t)
{
return bool(f);
}
template<class... Sig>
inline bool operator!=(std::nullptr_t, function<Sig...> const& f)
{
return bool(f);
}
}
#endif