-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunction.hpp
563 lines (489 loc) · 18.4 KB
/
function.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// solid/utility/function.hpp
//
// Copyright (c) 2018,2020 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// 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.
//
#pragma once
#include <algorithm>
#include <cstddef>
#include <functional>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include "solid/system/exception.hpp"
#include "solid/system/log.hpp"
#include "solid/utility/common.hpp"
#include "solid/utility/typetraits.hpp"
namespace solid {
inline constexpr size_t function_default_data_size = 3 * sizeof(void*);
inline constexpr size_t function_size_from_sizeof(const size_t _sizeof)
{
return _sizeof - sizeof(void*);
}
template <class T>
inline constexpr const T& function_max(const T& a, const T& b)
{
return (a < b) ? b : a;
}
template <class, size_t DataSize = function_default_data_size>
class Function; // undefined
template <class T>
struct is_function;
template <class R, class... ArgTypes, size_t DataSize>
struct is_function<Function<R(ArgTypes...), DataSize>> : std::true_type {
};
template <class T>
struct is_function : std::false_type {
};
namespace fnc_impl {
enum struct RepresentationE : uintptr_t {
None = 0,
Small,
Big,
};
constexpr uintptr_t representation_mask = 3;
constexpr uintptr_t representation_and_flags_mask = representation_mask /* + (3 << 2)*/;
template <class R, class... ArgTypes>
struct SmallRTTI;
template <class R, class... ArgTypes>
struct BigRTTI {
using BigRTTIT = BigRTTI<R, ArgTypes...>;
using SmallRTTIT = SmallRTTI<R, ArgTypes...>;
using DestroyFncT = void(void*);
using CopyFncT = RepresentationE(const void*, void*, const size_t, const SmallRTTIT*&, void*&, const BigRTTIT*&);
using MoveFncT = RepresentationE(void*, void*, const size_t, const SmallRTTIT*&, void*&, const BigRTTIT*&);
using InvokeFncT = R(const void*, ArgTypes&&...);
template <class T>
static void destroy(void* const _what) noexcept
{
::delete static_cast<T*>(_what);
}
InvokeFncT* pinvoke_fnc_;
DestroyFncT* pdestroy_fnc_;
CopyFncT* pcopy_fnc_;
MoveFncT* pmove_fnc_;
const bool is_copyable_;
const bool is_movable_;
};
template <class R, class... ArgTypes>
struct SmallRTTI {
using BigRTTIT = BigRTTI<R, ArgTypes...>;
using SmallRTTIT = SmallRTTI<R, ArgTypes...>;
using DestroyFncT = void(void*) noexcept;
using CopyFncT = RepresentationE(const void*, void*, const size_t, const SmallRTTIT*&, void*&, const BigRTTIT*&);
using MoveFncT = RepresentationE(void*, void*, const size_t, const SmallRTTIT*&, void*&, const BigRTTIT*&);
using InvokeFncT = R(const void*, ArgTypes&&...);
template <class T>
static void destroy(void* const _what) noexcept
{
static_cast<T*>(_what)->~T();
}
InvokeFncT* pinvoke_fnc_;
DestroyFncT* pdestroy_fnc_;
CopyFncT* pcopy_fnc_;
MoveFncT* pmove_fnc_;
const bool is_copyable_;
const bool is_movable_;
};
template <class T, class R, class... ArgTypes>
R do_invoke(const void* _pvalue, ArgTypes&&... _args)
{
return std::invoke(*const_cast<T*>(static_cast<const T*>(_pvalue)), std::forward<ArgTypes>(_args)...);
}
template <class T, class R, class... ArgTypes>
RepresentationE do_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti);
template <class T, class R, class... ArgTypes>
RepresentationE do_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti);
template <class T, class R, class... ArgTypes>
RepresentationE do_move_big(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti);
template <class T, class R, class... ArgTypes>
inline constexpr BigRTTI<R, ArgTypes...> big_rtti = {
&do_invoke<T, R, ArgTypes&&...>,
&BigRTTI<R, ArgTypes...>::template destroy<T>, &do_copy<T, R, ArgTypes...>, &do_move_big<T, R, ArgTypes...>,
std::is_copy_constructible_v<T>, std::is_move_constructible_v<T>};
template <class T, class R, class... ArgTypes>
inline constexpr SmallRTTI<R, ArgTypes...> small_rtti = {
&do_invoke<T, R, ArgTypes&&...>,
&SmallRTTI<R, ArgTypes...>::template destroy<T>, &do_copy<T, R, ArgTypes...>, &do_move<T, R, ArgTypes...>,
std::is_copy_constructible_v<T>, std::is_move_constructible_v<T>};
template <class T, class R, class... ArgTypes>
RepresentationE do_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_copy_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
const T& rsrc = *static_cast<const T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T(rsrc);
_rpsmall_rtti = &small_rtti<T, R, ArgTypes...>;
return RepresentationE::Small;
} else {
_rpto_big = ::new T(*static_cast<const T*>(_pfrom));
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
}
} else if constexpr (std::is_trivially_constructible_v<T> || std::is_copy_constructible_v<T>) {
_rpto_big = ::new T(*static_cast<const T*>(_pfrom));
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
} else {
solid_throw("Function: contained value not copyable");
return RepresentationE::None;
}
}
template <class T, class R, class... ArgTypes>
RepresentationE do_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T{std::move(rsrc)};
_rpsmall_rtti = &small_rtti<T, R, ArgTypes...>;
return RepresentationE::Small;
} else {
_rpto_big = ::new T{std::move(*static_cast<T*>(_pfrom))};
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
}
} else if constexpr (std::is_move_constructible_v<T>) {
_rpto_big = ::new T{std::move(*static_cast<T*>(_pfrom))};
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
} else {
solid_throw("Function: contained value not movable");
return RepresentationE::None;
}
}
template <class T, class R, class... ArgTypes>
RepresentationE do_move_big(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI<R, ArgTypes...>*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI<R, ArgTypes...>*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T{std::move(rsrc)};
_rpsmall_rtti = &small_rtti<T, R, ArgTypes...>;
return RepresentationE::Small;
} else {
_rpto_big = static_cast<T*>(_pfrom); //::new T{ std::move(*static_cast<T*>(_pfrom)) };
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
}
} else {
_rpto_big = static_cast<T*>(_pfrom); //::new T{ std::move(*static_cast<T*>(_pfrom)) };
_rpbig_rtti = &big_rtti<T, R, ArgTypes...>;
return RepresentationE::Big;
}
}
constexpr size_t compute_small_capacity(const size_t _req_capacity)
{
const size_t end_capacity = sizeof(uintptr_t) + sizeof(void*);
const size_t req_capacity = function_max(_req_capacity, function_max(end_capacity, sizeof(max_align_t)) - end_capacity);
const size_t tot_capacity = padded_size(req_capacity + sizeof(uintptr_t) + sizeof(void*), alignof(max_align_t));
return tot_capacity - end_capacity;
}
} // namespace fnc_impl
template <class R, class... ArgTypes, size_t DataSize>
class Function<R(ArgTypes...), DataSize> {
static constexpr size_t small_capacity = fnc_impl::compute_small_capacity(function_max(sizeof(void*) * 3, DataSize));
static constexpr size_t big_padding = small_capacity - sizeof(void*);
struct Small {
unsigned char data_[small_capacity];
const fnc_impl::SmallRTTI<R, ArgTypes...>* prtti_;
};
struct Big {
unsigned char padding_[big_padding];
void* ptr_;
const fnc_impl::BigRTTI<R, ArgTypes...>* prtti_;
};
struct Storage {
union {
Small small_;
Big big_;
};
uintptr_t type_data_;
};
// static_assert(sizeof(_Storage_t) == _Any_trivial_space_size + sizeof(void*));
// static_assert(is_standard_layout_v<_Storage_t>);
union {
Storage storage_{};
max_align_t dummy_;
};
template <class F, size_t S>
friend class Function;
private:
fnc_impl::RepresentationE representation() const noexcept
{
return static_cast<fnc_impl::RepresentationE>(storage_.type_data_ & fnc_impl::representation_and_flags_mask);
}
void representation(const fnc_impl::RepresentationE _repr) noexcept
{
storage_.type_data_ &= (~fnc_impl::representation_and_flags_mask);
storage_.type_data_ |= static_cast<uintptr_t>(_repr);
}
const std::type_info* typeInfo() const noexcept
{
return reinterpret_cast<const std::type_info*>(storage_.type_data_ & ~fnc_impl::representation_and_flags_mask);
}
public:
using ThisT = Function<R(ArgTypes...), DataSize>;
template <class T>
static constexpr bool is_small_type()
{
return alignof(T) <= alignof(max_align_t) && sizeof(T) <= small_capacity;
}
static constexpr size_t smallCapacity()
{
return small_capacity;
}
Function() noexcept = default;
Function(std::nullptr_t) noexcept {}
Function(const ThisT& _other)
{
doCopyFrom(_other);
}
template <size_t Sz>
Function(const Function<R(ArgTypes...), Sz>& _other)
{
doCopyFrom(_other);
}
Function(ThisT&& _other) noexcept
{
doMoveFrom(_other);
}
template <size_t Sz>
Function(Function<R(ArgTypes...), Sz>&& _other) noexcept
{
doMoveFrom(_other);
}
template <class T, std::enable_if_t<std::conjunction_v<std::negation<is_function<std::decay_t<T>>>, std::negation<is_specialization<std::decay_t<T>, std::in_place_type_t>> /*,
std::is_copy_constructible<std::decay_t<T>>*/
>,
int>
= 0>
Function(const T& _value)
{
doEmplace<std::decay_t<T>>(std::move(_value));
}
template <class T, std::enable_if_t<std::conjunction_v<std::negation<is_function<std::decay_t<T>>>, std::negation<is_specialization<std::decay_t<T>, std::in_place_type_t>> /*,
std::is_copy_constructible<std::decay_t<T>>*/
>,
int>
= 0>
Function(T&& _rvalue)
{
doEmplace<std::decay_t<T>>(std::forward<T>(_rvalue));
}
~Function() noexcept
{
reset();
}
ThisT& operator=(const ThisT& _other)
{
*this = ThisT{_other};
return *this;
}
ThisT& operator=(ThisT&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <size_t Sz>
ThisT& operator=(const Function<R(ArgTypes...), Sz>& _other)
{
*this = ThisT{_other};
return *this;
}
template <size_t Sz>
ThisT& operator=(Function<R(ArgTypes...), Sz>&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <class T, std::enable_if_t<std::conjunction_v<std::negation<is_function<std::decay_t<T>>>, std::is_copy_constructible<std::decay_t<T>>>, int> = 0>
ThisT& operator=(T&& _rvalue)
{
*this = ThisT{std::forward<T>(_rvalue)};
return *this;
}
void reset() noexcept
{
switch (representation()) {
case fnc_impl::RepresentationE::Small:
storage_.small_.prtti_->pdestroy_fnc_(&storage_.small_.data_);
break;
case fnc_impl::RepresentationE::Big:
storage_.big_.prtti_->pdestroy_fnc_(storage_.big_.ptr_);
break;
case fnc_impl::RepresentationE::None:
default:
break;
}
storage_.type_data_ = 0;
}
R operator()(ArgTypes... _args) const
{
if (has_value()) {
if (is_small()) {
return storage_.small_.prtti_->pinvoke_fnc_(&storage_.small_.data_, std::forward<ArgTypes>(_args)...);
} else {
return storage_.big_.prtti_->pinvoke_fnc_(storage_.big_.ptr_, std::forward<ArgTypes>(_args)...);
}
} else {
throw std::bad_function_call();
}
}
template <size_t Sz>
void swap(Function<R(ArgTypes...), Sz>& _other) noexcept
{
_other = std::exchange(*this, std::move(_other));
}
bool has_value() const noexcept
{
return storage_.type_data_ != 0;
}
bool empty() const noexcept
{
return !has_value();
}
explicit operator bool() const noexcept
{
return has_value();
}
const std::type_info& type() const noexcept
{
const std::type_info* const pinfo = typeInfo();
return pinfo ? *pinfo : typeid(void);
}
bool is_movable() const
{
if (is_small()) {
return storage_.small_.prtti_->is_movable_;
} else if (is_big()) {
return storage_.big_.prtti_->is_copyable_;
}
return true;
}
bool is_copyable() const
{
if (is_small()) {
return storage_.small_.prtti_->is_copyable_;
} else if (is_big()) {
return storage_.big_.prtti_->is_copyable_;
}
return true;
}
bool is_small() const
{
return representation() == fnc_impl::RepresentationE::Small;
}
bool is_big() const
{
return representation() == fnc_impl::RepresentationE::Big;
}
private:
template <size_t Sz>
void doMoveFrom(Function<R(ArgTypes...), Sz>& _other)
{
storage_.type_data_ = _other.storage_.type_data_;
representation(fnc_impl::RepresentationE::None);
switch (_other.representation()) {
case fnc_impl::RepresentationE::Small: {
const auto repr = _other.storage_.small_.prtti_->pmove_fnc_(
_other.storage_.small_.data_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
} break;
case fnc_impl::RepresentationE::Big: {
const auto repr = _other.storage_.big_.prtti_->pmove_fnc_(
_other.storage_.big_.ptr_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
if (repr == fnc_impl::RepresentationE::Big) {
_other.storage_.type_data_ = 0;
}
} break;
default:
break;
}
}
template <size_t Sz>
void doCopyFrom(const Function<R(ArgTypes...), Sz>& _other)
{
storage_.type_data_ = _other.storage_.type_data_;
representation(fnc_impl::RepresentationE::None);
switch (_other.representation()) {
case fnc_impl::RepresentationE::Small: {
const auto repr = _other.storage_.small_.prtti_->pcopy_fnc_(
_other.storage_.small_.data_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
} break;
case fnc_impl::RepresentationE::Big: {
const auto repr = _other.storage_.big_.prtti_->pcopy_fnc_(
_other.storage_.big_.ptr_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
} break;
default:
break;
}
}
template <class T, class... Args>
T& doEmplace(Args&&... _args)
{
if constexpr (is_small_type<T>()) {
auto& rval = reinterpret_cast<T&>(storage_.small_.data_);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rval)))) T{std::forward<Args>(_args)...};
storage_.small_.prtti_ = &fnc_impl::small_rtti<T, R, ArgTypes...>;
storage_.type_data_ = reinterpret_cast<uintptr_t>(&typeid(T));
representation(fnc_impl::RepresentationE::Small);
return rval;
} else {
T* const ptr = ::new T(std::forward<Args>(_args)...);
storage_.big_.ptr_ = ptr;
storage_.big_.prtti_ = &fnc_impl::big_rtti<T, R, ArgTypes...>;
storage_.type_data_ = reinterpret_cast<uintptr_t>(&typeid(T));
representation(fnc_impl::RepresentationE::Big);
return *ptr;
}
}
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
} // namespace solid
#ifdef SOLID_USE_STD_FUNCTION
#define solid_function_t(...) std::function<__VA_ARGS__>
#else
#define solid_function_t(...) solid::Function<__VA_ARGS__>
#endif
#define solid_function_empty(f) (!f)
#define solid_function_clear(f) (f = nullptr)