-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed_function.hpp
More file actions
2923 lines (2511 loc) · 116 KB
/
embed_function.hpp
File metadata and controls
2923 lines (2511 loc) · 116 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file embed_function.hpp
*
* @date 2026-2-7
*
* @version 2.0.12
*
* @copyright Copyright (c) 2026 Kim-J-Smith
* All rights reserved.
* (https://github.com/Kim-J-Smith/Embedded-Function)
*
* @attention This source is released under the MIT license
* (http://opensource.org/licenses/MIT)
*/
// Just like function pointers, it is quick and efficient.
/// @b EMBED_FN_CONFIG_USE_BIG_DEFAULT_BUFFER
/// If this macro is defined, bigger default buffer size will be used.
/// @b EMBED_FN_CONFIG_DISABLE_SMART_FORWARD
/// If this macro is defined, `smart_forward_t` will fall back to Perfect Forwarding.
/// @b EMBED_FN_CONFIG_UNDEF_MACROS
/// If this macro is defined, EMBED_* macros will be undefined at the end of this file.
/// @b EMBED_FN_HOOK_TRACE_EMPTY_CALL(message)
/// If this macro is defined, it will be called in function `throw_or_terminate()` in debug mode.
#ifndef EMBED_INCLUDED_EMBED_FUNCTION_HPP_
#define EMBED_INCLUDED_EMBED_FUNCTION_HPP_
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable: 4514 4668 4710 26495)
#endif
#ifndef EMBED_CXX_VERSION
# if defined(_MSC_VER) && ( _MSC_VER >= 1900 )
# define EMBED_CXX_VERSION _MSVC_LANG
# else
# define EMBED_CXX_VERSION __cplusplus
# endif
#endif
#ifndef EMBED_HAS_BUILTIN
# if defined(__has_builtin) && defined(__is_identifier)
# define EMBED_HAS_BUILTIN(x) (__has_builtin(x) || !__is_identifier(x))
# elif defined(__has_builtin)
# define EMBED_HAS_BUILTIN(x) __has_builtin(x)
# else
# define EMBED_HAS_BUILTIN(x) 0
# endif
#endif
#ifndef EMBED_HAS_ATTRIBUTE
# if defined(__has_attribute)
# define EMBED_HAS_ATTRIBUTE(x) __has_attribute(x)
# else
# define EMBED_HAS_ATTRIBUTE(x) 0
# endif
#endif
#ifndef EMBED_HAS_CXX_ATTRIBUTE
# if defined(__has_cpp_attribute)
# define EMBED_HAS_CXX_ATTRIBUTE(x) __has_cpp_attribute(x)
# else
# define EMBED_HAS_CXX_ATTRIBUTE(x) 0
# endif
#endif
#ifndef EMBED_CXX_ENABLE_EXCEPTION
# if defined(__cpp_exceptions)
# define EMBED_CXX_ENABLE_EXCEPTION (__cpp_exceptions != 0)
# elif defined(_MSC_VER) && defined(_HAS_EXCEPTIONS)
# define EMBED_CXX_ENABLE_EXCEPTION (_HAS_EXCEPTIONS != 0)
# elif (defined(__EXCEPTIONS) && __EXCEPTIONS == 1)
# define EMBED_CXX_ENABLE_EXCEPTION 1
# else
# define EMBED_CXX_ENABLE_EXCEPTION 0
# endif
#endif
#ifndef EMBED_ABI_VISIBILITY
# if defined(__GNUC__) || defined(__clang__)
# define EMBED_ABI_VISIBILITY(x) __attribute__((visibility(#x)))
# else
# define EMBED_ABI_VISIBILITY(x)
# endif
#endif
#ifndef EMBED_CXX14_CONSTEXPR
# if (EMBED_CXX_VERSION >= 201402L && __cpp_constexpr >= 201304L)
# define EMBED_CXX14_CONSTEXPR constexpr
# else
# define EMBED_CXX14_CONSTEXPR
# endif
#endif
#ifndef EMBED_INLINE
# if EMBED_HAS_ATTRIBUTE(always_inline)
# define EMBED_INLINE inline __attribute__((always_inline))
# elif defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC__)
# define EMBED_INLINE __forceinline
# else
# define EMBED_INLINE inline
# endif
#endif
#ifndef EMBED_RESTRICT
# if defined(__GNUC__) || defined(__clang__)
# define EMBED_RESTRICT __restrict__
# elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
# define EMBED_RESTRICT __restrict
# else
# define EMBED_RESTRICT
# endif
#endif
#ifndef EMBED_NODISCARD
# if (EMBED_CXX_VERSION >= 201703L && EMBED_HAS_CXX_ATTRIBUTE(nodiscard))
# define EMBED_NODISCARD [[nodiscard]]
# elif EMBED_HAS_ATTRIBUTE(warn_unused_result)
# define EMBED_NODISCARD __attribute__((warn_unused_result))
# else
# define EMBED_NODISCARD
# endif
#endif
#ifndef EMBED_FALLTHROUGH
# if (EMBED_CXX_VERSION >= 201703L && EMBED_HAS_CXX_ATTRIBUTE(fallthrough))
# define EMBED_FALLTHROUGH() [[fallthrough]]
# elif EMBED_HAS_CXX_ATTRIBUTE(gnu::fallthrough)
# define EMBED_FALLTHROUGH() [[gnu::fallthrough]]
# elif EMBED_HAS_CXX_ATTRIBUTE(clang::fallthrough)
# define EMBED_FALLTHROUGH() [[clang::fallthrough]]
# elif EMBED_HAS_ATTRIBUTE(fallthrough)
# define EMBED_FALLTHROUGH() __attribute__((fallthrough))
# else
# define EMBED_FALLTHROUGH() (static_cast<void>(0))
# endif
#endif
#if EMBED_CXX_VERSION >= 201103L
# include <cstddef> // std::size_t
# include <cstring> // std::memcpy, std::memset
# include <new> // IWYU pragma: keep (placement new, std::launder(C++17))
# include <utility> // std::move, std::forward, std::addressof, std::unreachable(C++23)
# include <functional> // std::bad_function_call
# include <exception> // std::terminate
# include <type_traits> // std::enable_if, ...
# include <initializer_list>
#else
# error The 'embed_function.hpp' requires the support of syntax features of C++11.\
You can use the '-std=c++11' compilation option, or simply switch to a newer compiler.
#endif
// const, volatile, {& | &&}, noexcept
#define EMBED_DETAIL_FN_EXPAND_IMPL(F, NOEXCEPT) \
F( , , , NOEXCEPT)\
F(const, , , NOEXCEPT)\
F( , volatile, , NOEXCEPT)\
F( , , & , NOEXCEPT)\
F( , , &&, NOEXCEPT)\
F(const, volatile, , NOEXCEPT)\
F(const, , & , NOEXCEPT)\
F(const, , &&, NOEXCEPT)\
F( , volatile, & , NOEXCEPT)\
F( , volatile, &&, NOEXCEPT)\
F(const, volatile, & , NOEXCEPT)\
F(const, volatile, &&, NOEXCEPT)
#if ( EMBED_CXX_VERSION >= 201703L || __cpp_noexcept_function_type >= 201510L )
// See https://en.cppreference.com/w/cpp/language/noexcept_spec .
// The noexcept-specification is a part of the function type and
// may appear as part of any function declarator. (Since C++17)
# define EMBED_DETAIL_FN_EXPAND(F) \
EMBED_DETAIL_FN_EXPAND_IMPL(F, ) EMBED_DETAIL_FN_EXPAND_IMPL(F, noexcept)
#else
# define EMBED_DETAIL_FN_EXPAND(F) \
EMBED_DETAIL_FN_EXPAND_IMPL(F, )
#endif
/// @brief Similar to `requires` in C++20.
/// Using SFINAE trait `enable_if_t` to require the template arguments.
#define EMBED_DETAIL_REQUIRES_IMPL(require_condition) \
::ebd::detail::enable_if_t<(require_condition), int> = 0
#define EMBED_DETAIL_REQUIRES(...) EMBED_DETAIL_REQUIRES_IMPL((__VA_ARGS__))
#if defined(_MSC_VER)
# define EMBED_DETAIL_FORCE_EBO __declspec(empty_bases)
#else
# define EMBED_DETAIL_FORCE_EBO
#endif
#if defined(_MSC_VER)
# define EMBED_DETAIL_VIRTUAL_INHERITANCE __virtual_inheritance
#else
# define EMBED_DETAIL_VIRTUAL_INHERITANCE
#endif
// Generate the default/delete move constructors and move assignment for specified class.
#define EMBED_DETAIL_MOVE_FUNCTION(class_name, default_or_delete) \
class_name(class_name&&) = default_or_delete;\
class_name& operator=(class_name&&) = default_or_delete;
// Generate the default/delete copy constructors and copy assignment for specified class.
#define EMBED_DETAIL_COPY_FUNCTION(class_name, default_or_delete) \
class_name(const class_name&) = default_or_delete;\
class_name& operator=(const class_name&) = default_or_delete;
// Generate default destructor and empty default constructor.
#define EMBED_DETAIL_DTOR_ECTOR_DEFAULT(class_name) \
~class_name() = default; \
class_name() = default;
// Generate all default functions (Ctor, Dtor, and assignment) for specified class.
#define EMBED_DETAIL_ALL_DEFAULT(class_name) \
EMBED_DETAIL_DTOR_ECTOR_DEFAULT(class_name) \
EMBED_DETAIL_COPY_FUNCTION(class_name, default) \
EMBED_DETAIL_MOVE_FUNCTION(class_name, default)
/// @brief Unify the two SFINAE writing methods of "enable_if" and "requires",
/// eliminating the need to maintain two sets of code.
/// @attention @b EMBED_DETAIL_TEMPLATE_BEGIN and @b EMBED_DETAIL_REQUIRES_END
/// MUST be used simultaneously and cannot be used separately.
#if !defined(__cpp_concepts) || ( __cpp_concepts < 201907L )
# define EMBED_DETAIL_TEMPLATE_BEGIN(...) template <__VA_ARGS__,
# define EMBED_DETAIL_REQUIRES_END(...) EMBED_DETAIL_REQUIRES(__VA_ARGS__)>
#else
# define EMBED_DETAIL_TEMPLATE_BEGIN(...) template <__VA_ARGS__>
# define EMBED_DETAIL_REQUIRES_END(...) requires(__VA_ARGS__)
#endif
#define EMBED_DETAIL_TEXT(text) EMBED_DETAIL_TEXT_IMPL(text)
#define EMBED_DETAIL_TEXT_IMPL(text) #text
#if __cpp_lib_launder >= 201606L
# define EMBED_DETAIL_LAUNDER(x) ( ::std::launder(x) )
#elif EMBED_HAS_BUILTIN(__builtin_launder)
namespace ebd { namespace detail {
template <typename T> EMBED_NODISCARD EMBED_INLINE constexpr
T* launder(T* ptr) noexcept { return __builtin_launder(ptr); }
}} // end namespace ebd::detail
# define EMBED_DETAIL_LAUNDER(x) ( ::ebd::detail::launder(x) )
#else
# define EMBED_DETAIL_LAUNDER(x) ( x )
#endif
#if EMBED_HAS_ATTRIBUTE(may_alias)
# define EMBED_DETAIL_ALIAS __attribute__((may_alias))
#else
# define EMBED_DETAIL_ALIAS
#endif
#ifndef EMBED_FN_HOOK_TRACE_EMPTY_CALL
# define EMBED_FN_HOOK_TRACE_EMPTY_CALL(message)
#endif
#if defined(__OPTIMIZE__) || defined(NDEBUG)
# define EMBED_DETAIL_FAIL_MESSAGE(message)
#else
# define EMBED_DETAIL_FAIL_MESSAGE(message) do { EMBED_FN_HOOK_TRACE_EMPTY_CALL(\
__FILE__ ":" EMBED_DETAIL_TEXT(__LINE__) " " message); } while(0)
#endif
#if __cpp_lib_unreachable >= 202202L
# define EMBED_DETAIL_UNREACHABLE() std::unreachable()
#elif EMBED_HAS_BUILTIN(__builtin_unreachable)
# define EMBED_DETAIL_UNREACHABLE() __builtin_unreachable()
#elif defined(__GNUC__) && (__GNUC__ >= 5)
# define EMBED_DETAIL_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
# define EMBED_DETAIL_UNREACHABLE() __assume(false)
#else
# define EMBED_DETAIL_UNREACHABLE()
#endif
namespace ebd EMBED_ABI_VISIBILITY(default) {
namespace detail {
/// @brief Here are some standard traits that are not supported in C++11.
inline namespace cxx_traits {
// See https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1558 .
template <typename... Args> struct make_void { using type = void; };
// See https://en.cppreference.com/w/cpp/header/type_traits.html .
template <typename... Args> using void_t = typename make_void<Args...>::type;
template <typename T>
using remove_cvref_t = typename std::remove_cv<
typename std::remove_reference<T>::type
>::type;
template <bool Cond, typename T = void>
using enable_if_t = typename std::enable_if<Cond, T>::type;
template <typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
template <typename T>
using remove_cv_t = typename std::remove_cv<T>::type;
template <bool Cond, typename IfTrue, typename IfFalse>
using conditional_t = typename std::conditional<Cond, IfTrue, IfFalse>::type;
template <typename T>
using decay_t = typename std::decay<T>::type;
template <typename T>
using remove_const_t = typename std::remove_const<T>::type;
template <typename T>
using remove_volatile_t = typename std::remove_volatile<T>::type;
template <bool Val>
using bool_constant = std::integral_constant<bool, Val>;
// (undocumented) Tags that used in `invoke_result`, `invoke`, `invoke_r`, etc.
class invoke_tag_normal {};
class invoke_tag_memfn_ref_like {};
class invoke_tag_memfn_pointer_like {};
class invoke_tag_memobj_ref_like {};
class invoke_tag_memobj_pointer_like {};
// (undocumented) Unwrap the `std::reference_wrapper` recursively.
template <typename T, typename U = remove_cvref_t<T>>
struct inv_unwrap {
using type = T;
using unwrap_once = T;
};
template <typename T, typename UnderType>
struct inv_unwrap<T, std::reference_wrapper<UnderType>> {
using type = typename inv_unwrap<UnderType&>::type;
using unwrap_once = UnderType&;
};
template <typename T>
using inv_unwrap_t = typename inv_unwrap<T>::type;
template <typename T>
using unwrap_once_t = typename inv_unwrap<T>::unwrap_once;
// (undocumented) Unwrap and forward std::reference_wrapper.
template <typename T>
EMBED_NODISCARD EMBED_INLINE constexpr enable_if_t<
std::is_same<T, unwrap_once_t<T>>::value, T&&
> unwrap_forward(remove_reference_t<T>&& obj) noexcept
{ return static_cast<T&&>(obj); }
template <typename T>
EMBED_NODISCARD EMBED_INLINE constexpr enable_if_t<
std::is_same<T, unwrap_once_t<T>>::value, T&&
> unwrap_forward(remove_reference_t<T>& obj) noexcept
{ return static_cast<T&&>(obj); }
template <typename T, typename Under = unwrap_once_t<T>,
EMBED_DETAIL_REQUIRES(!std::is_same<T, Under>::value)
> EMBED_NODISCARD EMBED_INLINE constexpr inv_unwrap_t<T>&&
unwrap_forward(remove_reference_t<T>&& obj) noexcept {
return unwrap_forward<Under>(obj.get());
}
template <typename T, typename Under = unwrap_once_t<T>,
EMBED_DETAIL_REQUIRES(!std::is_same<T, Under>::value)
> EMBED_NODISCARD EMBED_INLINE constexpr inv_unwrap_t<T>&&
unwrap_forward(remove_reference_t<T>& obj) noexcept {
return unwrap_forward<Under>(obj.get());
}
// (undocumented) Provide success type for invoke_result.
template <typename T, typename Tag>
struct success_type {
using type = T;
using tag = Tag;
};
// (undocumented) Trigger the SFINAE.
struct failure_type {};
// (undocumented) Get the invoke result of pointer to member
// object with the given reference-like argument.
template <typename MemObj, typename Arg>
struct invoke_result_of_memobj_ref_like_helper {
template<typename> static failure_type test(...) { return {}; }
template<typename T> static success_type<
/* type = */ decltype(std::declval<T>().*std::declval<MemObj>()),
/* tag = */ invoke_tag_memobj_ref_like
> test(int) { return {}; }
using type = decltype(test<Arg>(0));
};
// (undocumented) Get the invoke result of pointer to member
// object with the given pointer-like argument.
template <typename MemObj, typename Arg>
struct invoke_result_of_memobj_pointer_like_helper {
template<typename> static failure_type test(...) { return {}; }
template<typename T> static success_type<
/* type = */ decltype((*std::declval<T>()).*std::declval<MemObj>()),
/* tag = */ invoke_tag_memobj_pointer_like
> test(int) { return {}; }
using type = decltype(test<Arg>(0));
};
// (undocumented) Get the invoke result of pointer to member
// object with the given argument (reference-like or pointer-like).
template <typename T, typename U>
struct invoke_result_of_memobj; // Undefined
template <typename Class, typename RetT, typename Arg>
struct invoke_result_of_memobj<RetT Class::*, Arg> {
using MemberObj = RetT Class::*;
using ThisClass = remove_cvref_t<Arg>;
using type = typename conditional_t<
(std::is_same<Class, ThisClass>::value || std::is_base_of<Class, ThisClass>::value),
invoke_result_of_memobj_ref_like_helper<MemberObj, Arg>,
invoke_result_of_memobj_pointer_like_helper<MemberObj, Arg>
>::type;
};
// (undocumented) Get the invoke result of pointer to member
// function with the given arguments. And the first argument is
// reference-like object.
template <typename MemFunc, typename Arg, typename... ArgsType>
struct invoke_result_of_memfunc_ref_like_helper {
template<typename> static failure_type test(...) { return {}; }
template<typename T> static success_type<
/* type = */ decltype((std::declval<T>().*std::declval<MemFunc>())(
std::declval<ArgsType>()...
)),
/* tag = */ invoke_tag_memfn_ref_like
> test(int) { return {}; }
using type = decltype(test<Arg>(0));
};
// (undocumented) Get the invoke result of pointer to member
// function with the given arguments. And the first argument is
// pointer-like object.
template <typename MemFunc, typename Arg, typename... ArgsType>
struct invoke_result_of_memfunc_pointer_like_helper {
template<typename> static failure_type test(...) { return {}; }
template<typename T> static success_type<
/* type = */ decltype(((*std::declval<T>()).*std::declval<MemFunc>())(
std::declval<ArgsType>()...
)),
/* tag = */ invoke_tag_memfn_pointer_like
> test(int) { return {}; }
using type = decltype(test<Arg>(0));
};
// (undocumented) Get the invoke result of pointer to member
// function with the given arguments.
template <typename... T>
struct invoke_result_of_memfunc; // Undefined
template <typename Class, typename RetT, typename Arg, typename... ArgsType>
struct invoke_result_of_memfunc<RetT Class::*, Arg, ArgsType...> {
using MemberFunc = RetT Class::*;
using ThisClass = remove_cvref_t<Arg>;
using type = typename conditional_t<
(std::is_same<Class, ThisClass>::value || std::is_base_of<Class, ThisClass>::value),
invoke_result_of_memfunc_ref_like_helper<MemberFunc, Arg, ArgsType...>,
invoke_result_of_memfunc_pointer_like_helper<MemberFunc, Arg, ArgsType...>
>::type;
};
// (undocumented) Get invoke result of normal function with the given arguments.
template <typename Functor, typename... ArgsType>
struct invoke_result_of_normal {
template<typename> static failure_type test(...) { return {}; }
template<typename T> static success_type<
/* type = */ decltype(std::declval<T>()(
std::declval<ArgsType>()...)),
/* tag = */ invoke_tag_normal
> test(int) { return {}; }
using type = decltype(test<Functor>(0));
};
// (undocumented) Implement the `invoke_result`.
template <bool, bool, typename Func, typename... Args>
struct invoke_result_impl {
using type = failure_type;
};
template <typename PointerToMemObj, typename Arg>
struct invoke_result_impl<
/* is_memfunc_ptr = */ false,
/* is_memobj_ptr = */ true,
PointerToMemObj, Arg
> {
using type = typename invoke_result_of_memobj<
typename std::decay<PointerToMemObj>::type,
inv_unwrap_t<Arg>
>::type;
};
template <typename PointerToMemFunc, typename Arg, typename... ArgsType>
struct invoke_result_impl<
/* is_memfunc_ptr = */ true,
/* is_memobj_ptr = */ false,
PointerToMemFunc, Arg, ArgsType...
> {
using type = typename invoke_result_of_memfunc<
typename std::decay<PointerToMemFunc>::type,
inv_unwrap_t<Arg>, ArgsType...
>::type;
};
template <typename NormalFunc, typename... ArgsType>
struct invoke_result_impl<
/* is_memfunc_ptr = */ false,
/* is_memobj_ptr = */ false,
NormalFunc, ArgsType...
> {
using type = typename invoke_result_of_normal<
NormalFunc, ArgsType...
>::type;
};
// Get the invoke result and invoke tag.
// See https://en.cppreference.com/w/cpp/types/result_of.html .
template <typename Func, typename... ArgsT>
struct invoke_result : public invoke_result_impl<
std::is_member_function_pointer<
remove_reference_t<Func>
>::value,
std::is_member_object_pointer<
remove_reference_t<Func>
>::value,
Func, ArgsT...
>::type {};
// (undocumented) Check whether the INVOKE expression itself can throw.
template <typename Tag, typename...>
struct call_is_nothrow_impl {
static constexpr bool value = false;
};
template <typename NormalFunc, typename... Args>
struct call_is_nothrow_impl<invoke_tag_normal, NormalFunc, Args...> {
static constexpr bool value = noexcept(
std::declval<NormalFunc>()(std::declval<Args>()...));
};
template <typename MemObj, typename Arg>
struct call_is_nothrow_impl<invoke_tag_memobj_ref_like, MemObj, Arg> {
using U = inv_unwrap_t<Arg>;
static constexpr bool value = noexcept(
std::declval<U>().*std::declval<MemObj>());
};
template <typename MemObj, typename Arg>
struct call_is_nothrow_impl<invoke_tag_memobj_pointer_like, MemObj, Arg> {
static constexpr bool value = noexcept(
(*std::declval<Arg>()).*std::declval<MemObj>());
};
template <typename Memfunc, typename Arg, typename... Args>
struct call_is_nothrow_impl<
invoke_tag_memfn_ref_like, Memfunc, Arg, Args...> {
using U = inv_unwrap_t<Arg>;
static constexpr bool value = noexcept(
(std::declval<U>().*std::declval<Memfunc>()) (std::declval<Args>()...));
};
template <typename Memfunc, typename Arg, typename... Args>
struct call_is_nothrow_impl<
invoke_tag_memfn_pointer_like, Memfunc, Arg, Args...> {
static constexpr bool value = noexcept(
((*std::declval<Arg>()).*std::declval<Memfunc>()) (std::declval<Args>()...));
};
template <typename Func, typename... Args>
using call_is_nothrow = call_is_nothrow_impl<
typename invoke_result<Func, Args...>::tag, Func, Args...>;
// See https://en.cppreference.com/w/cpp/types/reference_converts_from_temporary.html .
template <typename To, typename From>
struct reference_converts_from_temporary
: public bool_constant<
#if __cpp_lib_reference_from_temporary >= 202202L
std::reference_converts_from_temporary_v<To, From>
#elif EMBED_HAS_BUILTIN(__reference_converts_from_temporary)
__reference_converts_from_temporary(To, From)
#else
false // After research, there is no better fall-back scheme.
#endif
> {};
// (undocumented) Implement the is_invocable, is_nothrow_invocable, etc.
template <typename Res, typename Ret,
bool RetIsVoid = std::is_void<Ret>::value, typename Enable = void>
struct is_invocable_impl : public std::false_type
{ using nothrow = std::false_type; };
template <typename Res, typename Ret>
struct is_invocable_impl<Res, Ret,
/* is_void<Ret>::value = */ true,
/* Enable = */ void_t<typename Res::type>>
: public std::true_type
{ using nothrow = std::true_type; };
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
# pragma GCC diagnostic ignored "-Wreturn-type"
#endif
template <typename Res, typename Ret>
struct is_invocable_impl<Res, Ret,
/* is_void<Ret>::value = */ false,
/* Enable = */ void_t<typename Res::type>
> {
using invoke_t = typename Res::type;
static invoke_t testGet() noexcept { return std::declval<invoke_t>(); }
template <typename T>
static void testConv(T) noexcept {}
template <typename, bool = true>
static std::false_type test(...) noexcept { return {}; }
template <typename Rt,
bool NoThrow = noexcept(testConv<Rt>(testGet())),
typename Enable = decltype(testConv<Rt>(testGet()))
>
static bool_constant<NoThrow>
test(int) noexcept { return {}; }
using type = decltype(test<Ret, true>(1));
using nothrow = decltype(test<Ret>(1));
};
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
// See https://en.cppreference.com/w/cpp/types/is_invocable.html .
template <typename Ret, typename Func, typename... Args>
struct is_invocable_r : public bool_constant<
is_invocable_impl<invoke_result<Func, Args...>, Ret>::type::value
> {};
template <typename Ret, typename Func, typename... Args>
struct is_nothrow_invocable_r : public bool_constant<
call_is_nothrow<Func, Args...>::value
&& is_invocable_impl<invoke_result<Func, Args...>, Ret>::nothrow::value
> {};
/// @fn invoke_impl
// (undocumented) Distribute the call of callable objects, including normal
// functions, pointer to member functions, and pointer to member objects
// (distinguish reference-like/pointer-like class object callers).
// Invokes the callable object directly with the given arguments.
// Used for free function, static member function, and functors (classes that overload operator()).
template <typename RetT, typename Func, typename... Args>
inline EMBED_CXX14_CONSTEXPR RetT
invoke_impl(invoke_tag_normal, Func&& fn, Args&&... args)
noexcept(is_nothrow_invocable_r<RetT, Func, Args...>::value)
{ return std::forward<Func>(fn)(std::forward<Args>(args)...); }
// Invokes the pointer to member object by the given "reference" of class object.
// Note: The `std::reference_wrapper` is also regarded as "reference".
template <typename RetT, typename MemObj, typename Arg>
inline EMBED_CXX14_CONSTEXPR RetT
invoke_impl(invoke_tag_memobj_ref_like, MemObj&& obj, Arg&& arg)
noexcept(is_nothrow_invocable_r<RetT, MemObj, Arg>::value)
{ return unwrap_forward<Arg>(arg).*std::forward<MemObj>(obj); }
// Invokes the pointer to member object by the given "pointer" of class object.
// Note: The `std::unique_ptr`, `std::shared_ptr` are also regarded as "pointer".
template <typename RetT, typename MemObj, typename Arg>
inline EMBED_CXX14_CONSTEXPR RetT
invoke_impl(invoke_tag_memobj_pointer_like, MemObj&& obj, Arg&& arg)
noexcept(is_nothrow_invocable_r<RetT, MemObj, Arg>::value)
{ return (*std::forward<Arg>(arg)).*std::forward<MemObj>(obj); }
// Invokes the pointer to member function by the given "reference" of class object.
// Note: The `std::reference_wrapper` is also regarded as "reference".
template <typename RetT, typename MemFunc, typename Arg, typename... ArgsType>
inline EMBED_CXX14_CONSTEXPR RetT
invoke_impl(invoke_tag_memfn_ref_like, MemFunc&& memfn, Arg&& arg, ArgsType&&... args)
noexcept(is_nothrow_invocable_r<RetT, MemFunc, Arg, ArgsType...>::value) {
return (unwrap_forward<Arg>(arg).*std::forward<MemFunc>(memfn))(
std::forward<ArgsType>(args)...
);
}
// Invokes the pointer to member function by the given "pointer" of class object.
// Note: The `std::unique_ptr`, `std::shared_ptr` are also regarded as "pointer".
template <typename RetT, typename MemFunc, typename Arg, typename... ArgsType>
inline EMBED_CXX14_CONSTEXPR RetT
invoke_impl(invoke_tag_memfn_pointer_like, MemFunc&& memfn, Arg&& arg, ArgsType&&... args)
noexcept(is_nothrow_invocable_r<RetT, MemFunc, Arg, ArgsType...>::value) {
return ((*std::forward<Arg>(arg)).*std::forward<MemFunc>(memfn))(
std::forward<ArgsType>(args)...
);
}
// See https://en.cppreference.com/w/cpp/utility/functional/invoke.html .
template <typename Result, typename Callee, typename... Args>
inline EMBED_CXX14_CONSTEXPR enable_if_t<
is_invocable_r<Result, Callee, Args...>::value
&& std::is_void<Result>::value>
invoke_r(Callee&& fn, Args&&... args)
noexcept(is_nothrow_invocable_r<Result, Callee, Args...>::value) {
using invoke_t = typename invoke_result<Callee, Args...>::type;
using tag_t = typename invoke_result<Callee, Args...>::tag;
// The `Result` is void, so there is no return.
invoke_impl<invoke_t>(tag_t{}, std::forward<Callee>(fn),
std::forward<Args>(args)...);
}
template <typename Result, typename Callee, typename... Args>
inline EMBED_CXX14_CONSTEXPR enable_if_t<
is_invocable_r<Result, Callee, Args...>::value
&& !std::is_void<Result>::value, Result>
invoke_r(Callee&& fn, Args&&... args)
noexcept(is_nothrow_invocable_r<Result, Callee, Args...>::value) {
using invoke_t = typename invoke_result<Callee, Args...>::type;
using tag_t = typename invoke_result<Callee, Args...>::tag;
// Assert no dangling.
static_assert(!reference_converts_from_temporary<Result, invoke_t>::value,
"Returning from invoke_r would bind a temporary object to the reference return type,"
" which would result in a dangling reference.");
return invoke_impl<invoke_t>(tag_t{}, std::forward<Callee>(fn),
std::forward<Args>(args)...);
}
} // end namespace cxx_traits
// Forward declaration.
template <std::size_t BufferSize, typename Config, typename Signature>
class EMBED_DETAIL_FORCE_EBO function;
/// @brief Here are some self-defined traits.
inline namespace fn_traits {
// The value is always false.
template <typename... Args>
struct always_false { static constexpr bool value = false; };
// Is trivial for the purposes of calls. (trivially destruct, copy and move)
// See https://itanium-cxx-abi.github.io/cxx-abi/abi.html#non-trivial-parameters .
template <typename T>
struct is_call_trivial : public bool_constant<
std::is_trivially_destructible<T>::value
&& std::is_trivially_copy_constructible<T>::value
&& std::is_trivially_move_constructible<T>::value
> {};
// std::is_trivial is deprecated in C++26. But we need it.
template <typename T>
struct is_traditional_trivial : public bool_constant<
std::is_trivially_default_constructible<T>::value
&& is_call_trivial<T>::value
> {};
// Check self.
template <typename A, typename B>
using is_self = std::is_same<remove_cvref_t<A>, remove_cvref_t<B>>;
// Configuration parameter package.
template <
bool IsCopyable,
bool IsView,
bool IsThrowing,
bool AssertObjectNoThrow
>
struct config_package {
// Whether the function wrapper is copyable.
static constexpr bool isCopyable = IsCopyable;
// Whether the function wrapper is actually a view.
static constexpr bool isView = IsView;
// Whether the function wrapper is throwing `std::bad_function_call`
// when it is called in an empty state.
static constexpr bool isThrowing = IsThrowing;
// Whether the function wrapper asserts that the callable object is not
// throwing exceptions when it is created, copied, moved, and called.
static constexpr bool assertNoThrow = AssertObjectNoThrow;
};
// Check whether the type is config_package or not.
template <typename T>
struct is_config_package : public std::false_type {};
template <bool... ConfigArgs>
struct is_config_package<config_package<ConfigArgs...>>
: public std::true_type {};
// Typename parameter package.
template <typename... Args>
struct args_package_impl {
static constexpr std::size_t argsNum = 0;
using type = args_package_impl<>;
using next_type = args_package_impl<>;
};
template <typename T, typename... Args>
struct args_package_impl<T, Args...> {
static constexpr std::size_t argsNum = sizeof...(Args) + 1;
using type = T;
using next_type = args_package_impl<Args...>;
};
// Implement the "get" trait of args_package.
template <std::size_t Index, typename Package>
struct get_args_helper {
using type = typename
get_args_helper<Index-1, typename Package::next_type>::type;
};
template <typename Package>
struct get_args_helper<0, Package> { using type = Package; };
// Typename parameter package. Easy to find index of element.
template <typename... Args>
struct args_package {
// The `get` is reserved for further use.
template <std::size_t Index>
using get = typename get_args_helper<
Index, args_package_impl<Args...>>::type::type;
static constexpr std::size_t size = args_package_impl<Args...>::argsNum;
};
// Unwrap the function signature.
template <typename T>
struct unwrap_signature {
static constexpr bool isSignature = false;
using ret = void;
using args = args_package<>;
using pure_sig = void();
static constexpr bool hasConst = false;
static constexpr bool hasVolatile = false;
static constexpr bool hasRRef = false;
static constexpr bool hasLRef = false;
static constexpr bool isNoexcept = false;
template <typename U>
using add_cv_like = U;
};
#define EMBED_DETAIL_UNWRAP_SIGNATURE_DEFINE(C, V, REF, NOEXCEPT) \
template <typename Ret, typename... Args> \
struct unwrap_signature<Ret(Args...) C V REF NOEXCEPT> { \
private: \
using is_ = std::false_type; \
using is_noexcept = std::true_type; \
public: \
static constexpr bool isSignature = true; \
using ret = Ret; \
using args = args_package<Args...>; \
using pure_sig = Ret(Args...); \
static constexpr bool hasConst = std::is_const<int C>::value; \
static constexpr bool hasVolatile = std::is_volatile<int V>::value; \
static constexpr bool hasRRef = std::is_rvalue_reference<int REF>::value; \
static constexpr bool hasLRef = std::is_lvalue_reference<int REF>::value; \
static constexpr bool isNoexcept = is_ ## NOEXCEPT::value; \
\
template <typename T> \
using add_cv_like = T C V; \
};
EMBED_DETAIL_FN_EXPAND(EMBED_DETAIL_UNWRAP_SIGNATURE_DEFINE)
#undef EMBED_DETAIL_UNWRAP_SIGNATURE_DEFINE
// Implement the "is_ebd_fn" trait.
template <typename T>
struct is_ebd_fn_impl : public std::false_type
{ using signature = void; };
template <std::size_t Buf, typename Cfg, typename Sig>
struct is_ebd_fn_impl<function<Buf, Cfg, Sig>>
: public bool_constant<
unwrap_signature<Sig>::isSignature
&& is_config_package<Cfg>::value
> { using signature = Sig; };
// Check whether the type is `ebd::detail::function` or not.
template <typename T>
using is_ebd_fn = is_ebd_fn_impl<remove_cvref_t<T>>;
// Throw std::bad_function_call or just call std::terminate().
template<bool IsThrowing>
[[noreturn]] inline enable_if_t<!IsThrowing>
throw_or_terminate() noexcept {
EMBED_DETAIL_FAIL_MESSAGE("[Embedded Function]: Empty function has been called!");
std::terminate();
}
template<bool IsThrowing>
[[noreturn]] inline enable_if_t<IsThrowing>
throw_or_terminate() noexcept(!EMBED_CXX_ENABLE_EXCEPTION) {
EMBED_DETAIL_FAIL_MESSAGE("[Embedded Function]: Empty function has been called!");
#if EMBED_CXX_ENABLE_EXCEPTION != 0
throw std::bad_function_call{};
#else
std::terminate();
#endif
}
// Check whether the callable object is function pointer or not.
template <typename T>
struct is_function_ptr : public std::false_type {};
template <typename Ret, typename... Args>
struct is_function_ptr<Ret(*)(Args...)>
: public std::true_type {};
#if ( EMBED_CXX_VERSION >= 201703L || __cpp_noexcept_function_type >= 201510L )
template <typename Ret, typename... Args>
struct is_function_ptr<Ret(*)(Args...) noexcept>
: public std::true_type {};
#endif
// Check to store origin type or not (store the pointer).
template <typename T, bool IsView,
typename DecT = decay_t<T>,
bool IsStoredOrigin = !IsView || is_function_ptr<DecT>::value
|| std::is_member_pointer<DecT>::value
>
struct is_stored_origin
: public bool_constant<IsStoredOrigin> {
static constexpr bool isTrivial = is_traditional_trivial<DecT>::value;
static_assert(!(IsView && IsStoredOrigin && !isTrivial),
"Internal error: Stored origin type in view mode must be trivially"
" copyable/destructible. Here Functor is stored originally,"
" but it is NOT trivial.");
};
// Get the really stored type.
template <typename T, bool IsView>
struct get_stored_type {
using type = conditional_t<is_stored_origin<T, IsView>::value,
decay_t<T>, typename std::add_pointer<decay_t<T>>::type>;
};
template <typename T, bool IsView = true>
using get_stored_type_t = typename get_stored_type<T, IsView>::type;
// Implement the `fn_can_convert`.
template <typename To, typename From>
struct fn_can_convert_impl : public std::false_type {};
template <std::size_t BufTo, typename CfgTo, typename SigTo,
std::size_t BufFrom, typename CfgFrom, typename SigFrom>
struct fn_can_convert_impl<
function<BufTo, CfgTo, SigTo>, function<BufFrom, CfgFrom, SigFrom>
> {
// Get the unwrap trait.
using unwrap_to = unwrap_signature<SigTo>;
using unwrap_from = unwrap_signature<SigFrom>;
// Get the return type and arguments package.
using sig_to_ret = typename unwrap_to::ret;
using sig_from_ret = typename unwrap_from::ret;
using sig_to_args = typename unwrap_to::args;
using sig_from_args = typename unwrap_from::args;
// Check the arguments of `From` and `To` are same.
static constexpr bool sig_ret_ok = std::is_same<sig_to_ret, sig_from_ret>::value;
static constexpr bool sig_args_ok = std::is_same<sig_to_args, sig_from_args>::value;
// Check the buffer size of `To` is bigger `From` or equals.
static constexpr bool buf_ok = BufTo >= BufFrom;
// Check the Configuration.
static constexpr bool cfg_ok =
CfgTo::isCopyable <= CfgFrom::isCopyable // Copyable to Move-only is OK.
&& CfgTo::isView == CfgFrom::isView
&& CfgTo::isThrowing == CfgFrom::isThrowing
&& CfgTo::assertNoThrow <= CfgFrom::assertNoThrow; // Assert to non-assert is OK.
/// TODO: Finalize the details of the conversion of the qualifiers
// Check the qualifiers.
static constexpr bool qualifier_ok =
!(unwrap_to::hasConst && !unwrap_from::hasConst)
&& (unwrap_to::hasVolatile == unwrap_from::hasVolatile)
&& (unwrap_to::hasRRef == unwrap_from::hasRRef)
&& (unwrap_to::hasLRef == unwrap_from::hasLRef)
&& (unwrap_to::isNoexcept == unwrap_from::isNoexcept);
static constexpr bool value =
buf_ok && cfg_ok && sig_ret_ok && sig_args_ok && qualifier_ok;
};