-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunction.hpp
More file actions
563 lines (491 loc) · 18 KB
/
Copy pathfunction.hpp
File metadata and controls
563 lines (491 loc) · 18 KB
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
// #define SOLID_THROW_ON_BIG_FUNCTION
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <solid/utility/any.hpp>
#include <type_traits>
#include <typeindex>
#include <utility>
#include "solid/system/exception.hpp"
#include "solid/system/log.hpp"
#include "solid/utility/anyimpl.hpp"
#include "solid/utility/common.hpp"
#include "solid/utility/typetraits.hpp"
namespace solid {
template <size_t Size, size_t Align = alignof(std::uintptr_t)>
requires(std::popcount(Align) == 1)
constexpr size_t function_size_to_small_size()
{
constexpr size_t max = std::max(sizeof(std::uintptr_t), Align);
static_assert(Size >= max, "size must be greater than sizeof(std::uintptr_t)");
return Size - max;
}
constexpr size_t function_default_size = function_size_to_small_size<32>();
constexpr size_t function_default_align = alignof(std::uintptr_t);
template <class,
size_t SmallSize = function_default_size,
size_t SmallAlign = function_default_align,
StoreOption Option = StoreOption::AcceptBig>
requires(SmallSize > 0 and SmallSize >= SmallAlign
and (SmallSize % SmallAlign == 0) and std::popcount(SmallAlign) == 1)
class Function; // undefined
template <class T>
struct is_function;
template <class R, class... ArgTypes, size_t SmallSize, size_t SmallAlign, StoreOption Option>
struct is_function<Function<R(ArgTypes...), SmallSize, SmallAlign, Option>> : std::true_type {
};
template <class T>
struct is_function : std::false_type {
};
template <class T>
inline constexpr bool is_function_v = is_function<T>::value;
namespace fnc_impl {
using any_impl::representation;
using any_impl::RepresentationE;
using any_impl::reversed_representation_and_flags_mask;
template <class R, class... ArgTypes>
struct BaseRTTI {
using CopyFncT = uintptr_t(const void*, void*, size_t, size_t, void*&);
using MoveFncT = uintptr_t(void*, void*, size_t, size_t, void*&);
using InvokeFncT = R(void*, ArgTypes&&...);
InvokeFncT& invoke_fnc_;
CopyFncT& copy_fnc_;
MoveFncT& move_fnc_;
const bool is_copyable_;
const bool is_movable_;
static BaseRTTI const& get(uintptr_t const _rtti) noexcept
{
return *reinterpret_cast<const BaseRTTI*>(_rtti & reversed_representation_and_flags_mask);
}
};
template <class R, class... ArgTypes>
struct BigRTTI : BaseRTTI<R, ArgTypes...> {
using DestroyFncT = void(void*) noexcept;
DestroyFncT& destroy_fnc_;
template <class T>
static void destroy(void* const _what) noexcept
{
::delete static_cast<T*>(_what);
}
static BigRTTI const& get(uintptr_t const _rtti) noexcept
{
return *reinterpret_cast<const BigRTTI*>(_rtti & reversed_representation_and_flags_mask);
}
};
template <class R, class... ArgTypes>
struct SmallRTTI : BaseRTTI<R, ArgTypes...> {
using DestroyFncT = void(void*) noexcept;
DestroyFncT* pdestroy_fnc_;
template <class T>
static void destroy(void* const _what) noexcept
{
std::destroy_at(std::launder(static_cast<T*>(_what)));
}
static SmallRTTI const& get(uintptr_t const _rtti) noexcept
{
return *reinterpret_cast<const SmallRTTI*>(_rtti & reversed_representation_and_flags_mask);
}
};
template <class T, class R, class... ArgTypes>
R do_invoke(void* _pvalue, ArgTypes&&... _args)
{
T* pfun = reinterpret_cast<T*>(_pvalue);
return std::invoke(*pfun, static_cast<ArgTypes&&>(_args)... /* std::forward<ArgTypes>(_args)... */);
}
template <class T, class R, class... ArgTypes>
uintptr_t do_copy(
const void*,
void*, size_t, size_t,
void*&);
template <class T, class R, class... ArgTypes>
uintptr_t do_move(
void*,
void*, size_t, size_t,
void*&);
template <class T, class R, class... ArgTypes>
uintptr_t do_move_big(
void*,
void*, size_t, size_t,
void*&);
template <class T, class R, class... ArgTypes>
inline constexpr BigRTTI<R, ArgTypes...> big_rtti = {
{do_invoke<T, R, ArgTypes&&...>,
do_copy<T, R, ArgTypes...>,
do_move_big<T, R, ArgTypes...>,
std::is_copy_constructible_v<T>,
std::is_move_constructible_v<T>},
BigRTTI<R, ArgTypes...>::template destroy<T>,
};
template <class T, class R, class... ArgTypes>
inline constexpr SmallRTTI<R, ArgTypes...> small_rtti = {
{do_invoke<T, R, ArgTypes&&...>,
do_copy<T, R, ArgTypes...>,
do_move<T, R, ArgTypes...>,
std::is_copy_constructible_v<T>,
std::is_move_constructible_v<T>},
std::is_trivially_copyable_v<T> ? nullptr : &SmallRTTI<R, ArgTypes...>::template destroy<T>,
};
template <class T, class R, class... ArgTypes>
uintptr_t do_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const size_t _small_align,
void*& _rpto_big)
{
if constexpr (std::is_copy_constructible_v<T>) {
if (sizeof(T) <= _small_cap and alignof(T) <= _small_align) {
T& rdst = *static_cast<T*>(_pto_small);
const T& rsrc = *static_cast<const T*>(_pfrom);
::new (std::addressof(rdst)) T(rsrc);
return representation(&small_rtti<T, R, ArgTypes...>, RepresentationE::Small);
} else {
#if defined(SOLID_THROW_ON_BIG_FUNCTION)
solid_throw("Big Function");
#endif
_rpto_big = ::new T(*static_cast<const T*>(_pfrom));
return representation(&big_rtti<T, R, ArgTypes...>, RepresentationE::Big);
}
} else {
solid_throw("Function: contained value not copyable");
return 0;
}
}
template <class T, class R, class... ArgTypes>
uintptr_t do_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const size_t _small_align,
void*& _rpto_big)
{
if constexpr (std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap and alignof(T) <= _small_align) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (std::addressof(rdst)) T{std::move(rsrc)};
return representation(&small_rtti<T, R, ArgTypes...>, RepresentationE::Small);
} else {
#if defined(SOLID_THROW_ON_BIG_FUNCTION)
solid_throw("Big Function");
#endif
_rpto_big = ::new T{std::move(*static_cast<T*>(_pfrom))};
return representation(&big_rtti<T, R, ArgTypes...>, RepresentationE::Big);
}
} else {
solid_throw("Function: contained value not movable");
return 0u;
}
}
template <class T, class R, class... ArgTypes>
uintptr_t do_move_big(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const size_t _small_align,
void*& _rpto_big)
{
if constexpr (std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap and alignof(T) <= _small_align) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (std::addressof(rdst)) T{std::move(rsrc)};
return representation(&small_rtti<T, R, ArgTypes...>, RepresentationE::Small);
} else {
_rpto_big = static_cast<T*>(_pfrom);
return representation(&big_rtti<T, R, ArgTypes...>, RepresentationE::Big);
}
} else {
_rpto_big = static_cast<T*>(_pfrom);
return representation(&big_rtti<T, R, ArgTypes...>, RepresentationE::Big);
}
}
} // namespace fnc_impl
template <class R, class... ArgTypes,
size_t SmallSize, size_t SmallAlign,
StoreOption Option>
requires(SmallSize > 0 and SmallSize >= SmallAlign
and (SmallSize % SmallAlign == 0) and std::popcount(SmallAlign) == 1)
class Function<R(ArgTypes...), SmallSize, SmallAlign, Option> {
using BaseRTTI_T = fnc_impl::BaseRTTI<R, ArgTypes...>;
struct Small {
using RTTI_T = fnc_impl::SmallRTTI<R, ArgTypes...>;
alignas(SmallAlign) mutable unsigned char data_[SmallSize];
};
struct Big {
using RTTI_T = fnc_impl::BigRTTI<R, ArgTypes...>;
mutable void* ptr_;
};
struct Storage {
uintptr_t rtti_ = 0;
union {
Small small_;
Big big_;
};
};
Storage storage_{};
template <class F, size_t S, size_t A, StoreOption O>
requires(S > 0 and S >= A
and (S % A == 0) and std::popcount(A) == 1)
friend class Function;
public:
using ThisT = Function<R(ArgTypes...), SmallSize, SmallAlign, Option>;
static constexpr size_t smallCapacity()
{
return SmallSize;
}
static constexpr size_t smallAlign()
{
return SmallAlign;
}
template <class T>
static constexpr bool is_small_type()
{
return alignof(T) <= smallAlign() && sizeof(T) <= smallCapacity();
}
Function() noexcept = default;
Function(std::nullptr_t) noexcept {}
Function(const ThisT& _other)
{
doCopyFrom(_other);
}
template <size_t Sz, size_t Al, StoreOption Op>
Function(const Function<R(ArgTypes...), Sz, Al, Op>& _other)
{
doCopyFrom(_other);
}
Function(ThisT&& _other) noexcept
{
doMoveFrom(_other);
}
template <size_t Sz, size_t Al, StoreOption Op>
Function(Function<R(ArgTypes...), Sz, Al, Op>&& _other) noexcept
{
doMoveFrom(_other);
}
template <class T, StoreOption Opt = Option>
Function(T&& _fun, std::integral_constant<StoreOption, Opt> = store_option_dispatch<Opt>())
requires(not is_function_v<std::decay_t<T>> and not is_specialization_v<std::decay_t<T>, std::in_place_type_t>)
{
using FncT = std::decay_t<T>;
static_assert(Opt == StoreOption::AcceptBig or is_small_type<FncT>(), "Function not small. Construct by using AcceptBigT{} or assign using .emplace()");
if constexpr (is_small_type<FncT>()) {
storage_.rtti_ = fnc_impl::representation(&fnc_impl::small_rtti<FncT, R, ArgTypes...>, fnc_impl::RepresentationE::Small);
auto& rval = reinterpret_cast<FncT&>(storage_.small_.data_);
std::construct_at(std::addressof(rval), std::forward<T>(_fun));
} else {
#if defined(SOLID_THROW_ON_BIG_FUNCTION)
solid_throw("Big Function");
#endif
FncT* const ptr = ::new FncT(std::forward<T>(_fun));
storage_.big_.ptr_ = ptr;
storage_.rtti_ = fnc_impl::representation(&fnc_impl::big_rtti<FncT, R, ArgTypes...>, fnc_impl::RepresentationE::Big);
}
}
~Function() noexcept
{
auto const rtti = storage_.rtti_;
switch (fnc_impl::representation(rtti)) {
[[likely]] case fnc_impl::RepresentationE::Small:
if (auto* pf = Small::RTTI_T::get(rtti).pdestroy_fnc_) {
(*pf)(storage_.small_.data_);
}
break;
case fnc_impl::RepresentationE::Big:
Big::RTTI_T::get(rtti).destroy_fnc_(storage_.big_.ptr_);
break;
case fnc_impl::RepresentationE::None:
[[fallthrough]];
default:
break;
}
}
ThisT& operator=(const ThisT& _other)
{
*this = ThisT{_other};
return *this;
}
ThisT& operator=(ThisT&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <size_t Sz, size_t Al, StoreOption Op>
ThisT& operator=(const Function<R(ArgTypes...), Sz, Al, Op>& _other)
{
*this = ThisT{_other};
return *this;
}
template <size_t Sz, size_t Al, StoreOption Op>
ThisT& operator=(Function<R(ArgTypes...), Sz, Al, Op>&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <class T>
ThisT& operator=(T&& _rvalue)
{
*this = ThisT{std::forward<T>(_rvalue)};
return *this;
}
template <class T>
ThisT& emplace(T&& _rvalue)
{
*this = ThisT{std::forward<T>(_rvalue), AcceptBigT{}};
return *this;
}
void reset() noexcept
{
auto const rtti = storage_.rtti_;
switch (fnc_impl::representation(rtti)) {
[[likely]] case fnc_impl::RepresentationE::Small:
if (auto* pfnc = Small::RTTI_T::get(rtti).pdestroy_fnc_) {
(*pfnc)(storage_.small_.data_);
}
break;
case fnc_impl::RepresentationE::Big:
Big::RTTI_T::get(rtti).destroy_fnc_(storage_.big_.ptr_);
break;
case fnc_impl::RepresentationE::None:
[[fallthrough]];
default:
break;
}
storage_.rtti_ = 0;
}
R operator()(ArgTypes... _args) const
{
auto const rtti = storage_.rtti_;
if (auto const repr = fnc_impl::representation(rtti); repr == fnc_impl::RepresentationE::Small) [[likely]] {
return Small::RTTI_T::get(rtti).invoke_fnc_(storage_.small_.data_, static_cast<ArgTypes&&>(_args)...);
} else if (repr == fnc_impl::RepresentationE::Big) {
return Big::RTTI_T::get(rtti).invoke_fnc_(storage_.big_.ptr_, static_cast<ArgTypes&&>(_args)...);
} else {
throw std::bad_function_call();
}
}
template <size_t Sz, size_t Al, StoreOption Op>
void swap(Function<R(ArgTypes...), Sz, Al, Op>& _other) noexcept
{
_other = std::exchange(*this, std::move(_other));
}
bool has_value() const noexcept
{
return storage_.rtti_ != 0;
}
bool empty() const noexcept
{
return !has_value();
}
explicit operator bool() const noexcept
{
return has_value();
}
bool is_movable() const
{
auto const rtti = storage_.rtti_;
if (rtti) [[likely]] {
return BaseRTTI_T::get(rtti).is_movable_;
}
return true;
}
bool is_copyable() const
{
auto const rtti = storage_.rtti_;
if (rtti) [[likely]] {
return BaseRTTI_T::get(rtti).is_copyable_;
}
return true;
}
[[nodiscard]] bool is_small() const
{
return fnc_impl::representation(storage_.rtti_) == fnc_impl::RepresentationE::Small;
}
[[nodiscard]] bool is_big() const
{
return fnc_impl::representation(storage_.rtti_) == fnc_impl::RepresentationE::Big;
}
private:
template <size_t Sz, size_t Al, StoreOption Op>
void doMoveFrom(Function<R(ArgTypes...), Sz, Al, Op>& _other)
{
storage_.rtti_ = 0u;
switch (fnc_impl::representation(_other.storage_.rtti_)) {
case fnc_impl::RepresentationE::Small: {
storage_.rtti_ = Small::RTTI_T::get(_other.storage_.rtti_).move_fnc_(_other.storage_.small_.data_, storage_.small_.data_, smallCapacity(), smallAlign(), storage_.big_.ptr_);
_other.reset();
} break;
case fnc_impl::RepresentationE::Big: {
storage_.rtti_ = Big::RTTI_T::get(_other.storage_.rtti_).move_fnc_(_other.storage_.big_.ptr_, storage_.small_.data_, smallCapacity(), smallAlign(), storage_.big_.ptr_);
if (is_big()) {
_other.storage_.rtti_ = 0u;
}
_other.reset();
} break;
default:
break;
}
}
template <size_t Sz, size_t Al, StoreOption Op>
void doCopyFrom(const Function<R(ArgTypes...), Sz, Al, Op>& _other)
{
storage_.rtti_ = 0u;
switch (fnc_impl::representation(_other.storage_.rtti_)) {
case fnc_impl::RepresentationE::Small: {
storage_.rtti_ = Small::RTTI_T::get(_other.storage_.rtti_).copy_fnc_(_other.storage_.small_.data_, storage_.small_.data_, smallCapacity(), smallAlign(), storage_.big_.ptr_);
} break;
case fnc_impl::RepresentationE::Big: {
storage_.rtti_ = Big::RTTI_T::get(_other.storage_.rtti_).copy_fnc_(_other.storage_.big_.ptr_, storage_.small_.data_, smallCapacity(), smallAlign(), storage_.big_.ptr_);
} break;
default:
break;
}
}
template <typename Fnc>
void doEmplace(Fnc _fun)
{
using FncT = std::decay_t<Fnc>;
if constexpr (is_small_type<FncT>()) {
storage_.rtti_ = representation(&fnc_impl::small_rtti<FncT, R, ArgTypes...>, fnc_impl::RepresentationE::Small);
auto& rval = reinterpret_cast<FncT&>(storage_.small_.data_);
std::construct_at(std::addressof(rval), std::move(_fun));
// new (&rval) FncT(std::move(_fun));
} else {
#if defined(SOLID_THROW_ON_BIG_FUNCTION)
solid_throw("Big Function");
#endif
FncT* const ptr = ::new FncT(std::move(_fun));
storage_.big_.ptr_ = ptr;
storage_.rtti_ = representation(&fnc_impl::big_rtti<FncT, R, ArgTypes...>, fnc_impl::RepresentationE::Big);
}
}
};
//-----------------------------------------------------------------------------
template <class T>
using Function64T = Function<T, function_size_to_small_size<64>()>;
template <class T>
using Function96T = Function<T, function_size_to_small_size<96>()>;
template <class T>
using Function128T = Function<T, function_size_to_small_size<128>()>;
template <class T>
using Function256T = Function<T, function_size_to_small_size<256>()>;
template <class T>
using SmallFunctionT = Function<T, function_default_size, function_default_align, StoreOption::RejectBig>;
template <class T>
using SmallFunction64T = Function<T, function_size_to_small_size<64>(), function_default_align, StoreOption::RejectBig>;
template <class T>
using SmallFunction96T = Function<T, function_size_to_small_size<96>(), function_default_align, StoreOption::RejectBig>;
template <class T>
using SmallFunction128T = Function<T, function_size_to_small_size<128>(), function_default_align, StoreOption::RejectBig>;
template <class T>
using SmallFunction256T = Function<T, function_size_to_small_size<256>(), function_default_align, StoreOption::RejectBig>;
//-----------------------------------------------------------------------------
} // namespace solid