Embedded Template Library 1.0
Loading...
Searching...
No Matches
delegate_cpp03.h
1
2//
4//The MIT License(MIT)
5//
6//Embedded Template Library.
7//https://github.com/ETLCPP/etl
8//https://www.etlcpp.com
9//
10//Copyright(c) 2021 John Wellbelove
11//
12//Permission is hereby granted, free of charge, to any person obtaining a copy
13//of this software and associated documentation files(the "Software"), to deal
14//in the Software without restriction, including without limitation the rights
15//to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16//copies of the Software, and to permit persons to whom the Software is
17//furnished to do so, subject to the following conditions :
18//
19//The above copyright notice and this permission notice shall be included in all
20//copies or substantial portions of the Software.
21//
22//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28//SOFTWARE.
29//******************************************************************************/
30
32//
33//Copyright (C) 2017 by Sergey A Kryukov: derived work
34//http://www.SAKryukov.org
35//http://www.codeproject.com/Members/SAKryukov
36//
37//Based on original work by Sergey Ryazanov:
38//"The Impossibly Fast C++ Delegates", 18 Jul 2005
39//https://www.codeproject.com/articles/11015/the-impossibly-fast-c-delegates
40//
41//MIT license:
42//http://en.wikipedia.org/wiki/MIT_License
43//
44//Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibly-Fast-Cplusplus-Delegates-Fixed
45//
46//******************************************************************************/
47
48#ifndef ETL_DELEGATE_CPP03_INCLUDED
49#define ETL_DELEGATE_CPP03_INCLUDED
50
51#include "../platform.h"
52#include "../error_handler.h"
53#include "../exception.h"
54#include "../type_traits.h"
55#include "../utility.h"
56#include "../optional.h"
57
58#if defined(ETL_IN_DELEGATE_CPP03_UNIT_TEST)
59namespace etl_cpp03
60#else
61namespace etl
62#endif
63{
64 namespace private_delegate
65 {
66 //***********************************
67 template <typename TDelegate, typename TReturn, typename TParam>
69 {
71 {
72 TDelegate& d = static_cast<TDelegate&>(*this);
73
75
76 if (d.is_valid())
77 {
78 result = d(param);
79 }
80
81 return result;
82 }
83 };
84
85 //***********************************
86 template <typename TDelegate>
88 {
89 bool call_if()
90 {
91 TDelegate& d = static_cast<TDelegate&>(*this);
92
93 if (d.is_valid())
94 {
95 d();
96 return true;
97 }
98 else
99 {
100 return false;
101 }
102 }
103 };
104
105 //***********************************
106 template <typename TDelegate, typename TReturn>
108 {
109 etl::optional<TReturn> call_if()
110 {
111 TDelegate& d = static_cast<TDelegate&>(*this);
112
114
115 if (d.is_valid())
116 {
117 result = d();
118 }
119
120 return result;
121 }
122 };
123
124 //***********************************
125 template <typename TDelegate, typename TParam>
127 {
128 bool call_if(TParam param)
129 {
130 TDelegate& d = static_cast<TDelegate&>(*this);
131
132 if (d.is_valid())
133 {
134 d(param);
135 return true;
136 }
137 else
138 {
139 return false;
140 }
141 }
142 };
143 }
144
145 //***************************************************************************
147 //***************************************************************************
149 {
150 public:
151
152 delegate_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
154 {
155 }
156 };
157
158 //***************************************************************************
160 //***************************************************************************
162 {
163 public:
164
166 : delegate_exception(ETL_ERROR_TEXT("delegate:uninitialised", ETL_DELEGATE_FILE_ID"A"), file_name_, line_number_)
167 {
168 }
169 };
170
171 //*************************************************************************
173 //*************************************************************************
174 template <typename T>
175 class delegate;
176
177 template <typename TReturn, typename TParam>
178 class delegate<TReturn(TParam)> : public private_delegate::call_if_impl<delegate<TReturn(TParam)>, TReturn, TParam>
179 {
180 private:
181
183
184 public:
185
187
188 //*************************************************************************
190 //*************************************************************************
192 {
193 }
194
195 //*************************************************************************
196 // Copy constructor.
197 //*************************************************************************
198 delegate(const delegate& other)
199 {
200 invocation = other.invocation;
201 }
202
203 //*************************************************************************
204 // Construct from a functor.
205 //*************************************************************************
206 template <typename TFunctor>
207 delegate(TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
208 {
209 assign((void*)(&instance), functor_stub<TFunctor>);
210 }
211
212 //*************************************************************************
213 // Construct from a const functor.
214 //*************************************************************************
215 template <typename TFunctor>
216 delegate(const TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
217 {
218 assign((void*)(&instance), const_functor_stub<TFunctor>);
219 }
220
221 //*************************************************************************
223 //*************************************************************************
224 template <TReturn(*Method)(TParam)>
226 {
227 return delegate(ETL_NULLPTR, function_stub<Method>);
228 }
229
230 //*************************************************************************
232 //*************************************************************************
233 template <typename TFunctor>
234 static
236 create(TFunctor& instance)
237 {
238 return delegate((void*)(&instance), functor_stub<TFunctor>);
239 }
240
241 //*************************************************************************
243 //*************************************************************************
244 template <typename TFunctor>
245 static
247 create(const TFunctor& instance)
248 {
249 return delegate((void*)(&instance), const_functor_stub<TFunctor>);
250 }
251
252 //*************************************************************************
254 //*************************************************************************
255 template <typename T, TReturn(T::*Method)(TParam)>
256 static delegate create(T& instance)
257 {
258 return delegate((void*)(&instance), method_stub<T, Method>);
259 }
260
261 //*************************************************************************
263 //*************************************************************************
264 template <typename T, TReturn(T::*Method)(TParam) const>
265 static delegate create(const T& instance)
266 {
267 return delegate((void*)(&instance), const_method_stub<T, Method>);
268 }
269
270 //*************************************************************************
272 //*************************************************************************
273 template <typename T, T& Instance, TReturn(T::*Method)(TParam)>
278
279 //*************************************************************************
282 //*************************************************************************
283 template <typename T, TReturn(T::* Method)(TParam), T& Instance>
288
289 //*************************************************************************
291 //*************************************************************************
292 template <typename T, T const& Instance, TReturn(T::*Method)(TParam) const>
297
298 //*************************************************************************
301 //*************************************************************************
302 template <typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
307
308#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
309 //*************************************************************************
312 //*************************************************************************
313 template <typename T, T& Instance>
318#endif
319
320 //*************************************************************************
322 //*************************************************************************
323 template <TReturn(*Method)(TParam)>
324 void set()
325 {
326 assign(ETL_NULLPTR, function_stub<Method>);
327 }
328
329 //*************************************************************************
331 //*************************************************************************
332 template <typename TFunctor>
334 set(TFunctor& instance)
335 {
336 assign((void*)(&instance), functor_stub<TFunctor>);
337 }
338
339 //*************************************************************************
341 //*************************************************************************
342 template <typename TFunctor>
344 set(const TFunctor& instance)
345 {
346 assign((void*)(&instance), const_functor_stub<TFunctor>);
347 }
348
349 //*************************************************************************
351 //*************************************************************************
352 template <typename T, TReturn(T::* Method)(TParam)>
353 void set(T& instance)
354 {
355 assign((void*)(&instance), method_stub<T, Method>);
356 }
357
358 //*************************************************************************
360 //*************************************************************************
361 template <typename T, TReturn(T::* Method)(TParam) const>
362 void set(T& instance)
363 {
364 assign((void*)(&instance), const_method_stub<T, Method>);
365 }
366
367 //*************************************************************************
369 //*************************************************************************
370 template <typename T, T& Instance, TReturn(T::* Method)(TParam)>
371 void set()
372 {
374 }
375
376 //*************************************************************************
379 //*************************************************************************
380 template <typename T, TReturn(T::* Method)(TParam), T& Instance>
381 void set()
382 {
384 }
385
386 //*************************************************************************
388 //*************************************************************************
389 template <typename T, T const& Instance, TReturn(T::* Method)(TParam) const>
390 void set()
391 {
393 }
394
395 //*************************************************************************
398 //*************************************************************************
399 template <typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
400 void set()
401 {
403 }
404
405 //*************************************************************************
407 //*************************************************************************
408 ETL_CONSTEXPR14 void clear()
409 {
410 invocation.clear();
411 }
412
413 //*************************************************************************
415 //*************************************************************************
417 {
418 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
419
420 return (*invocation.stub)(invocation.object, param);
421 }
422
423 //*************************************************************************
426 //*************************************************************************
427 template <typename TAlternative>
429 {
430 if (is_valid())
431 {
432 return (*invocation.stub)(invocation.object, param);
433 }
434 else
435 {
436 return alternative(param);
437 }
438 }
439
440 //*************************************************************************
443 //*************************************************************************
444 template <TReturn(*Method)(TParam)>
446 {
447 if (is_valid())
448 {
449 return (*invocation.stub)(invocation.object, param);
450 }
451 else
452 {
453 return (Method)(param);
454 }
455 }
456
457 //*************************************************************************
459 //*************************************************************************
461 {
462 invocation = rhs.invocation;
463 return *this;
464 }
465
466 //*************************************************************************
468 //*************************************************************************
469 template <typename TFunctor>
472 {
473 assign((void*)(&instance), functor_stub<TFunctor>);
474 return *this;
475 }
476
477 //*************************************************************************
479 //*************************************************************************
480 template <typename TFunctor>
482 operator =(const TFunctor& instance)
483 {
484 assign((void*)(&instance), const_functor_stub<TFunctor>);
485 return *this;
486 }
487
488 //*************************************************************************
490 //*************************************************************************
491 bool operator == (const delegate& rhs) const
492 {
493 return invocation == rhs.invocation;
494 }
495
496 //*************************************************************************
498 //*************************************************************************
499 bool operator != (const delegate& rhs) const
500 {
501 return invocation != rhs.invocation;
502 }
503
504 //*************************************************************************
506 //*************************************************************************
507 bool is_valid() const
508 {
509 return invocation.stub != ETL_NULLPTR;
510 }
511
512 //*************************************************************************
514 //*************************************************************************
515 operator bool() const
516 {
517 return is_valid();
518 }
519
520 private:
521
522 typedef TReturn(*stub_type)(void* object, TParam);
523
524 //*************************************************************************
526 //*************************************************************************
527 struct invocation_element
528 {
529 invocation_element()
530 : object(ETL_NULLPTR)
531 , stub(ETL_NULLPTR)
532 {
533 }
534
535 //***********************************************************************
536 invocation_element(void* object_, stub_type stub_)
537 : object(object_)
538 , stub(stub_)
539 {
540 }
541
542 //***********************************************************************
543 bool operator ==(const invocation_element& rhs) const
544 {
545 return (rhs.stub == stub) && (rhs.object == object);
546 }
547
548 //***********************************************************************
549 bool operator !=(const invocation_element& rhs) const
550 {
551 return (rhs.stub != stub) || (rhs.object != object);
552 }
553
554 //***********************************************************************
555 ETL_CONSTEXPR14 void clear()
556 {
557 object = ETL_NULLPTR;
558 stub = ETL_NULLPTR;
559 }
560
561 //***********************************************************************
562 void* object;
563 stub_type stub;
564 };
565
566 //*************************************************************************
568 //*************************************************************************
569 delegate(void* object, stub_type stub)
570 : invocation(object, stub)
571 {
572 }
573
574 //*************************************************************************
576 //*************************************************************************
577 delegate(stub_type stub)
578 : invocation(ETL_NULLPTR, stub)
579 {
580 }
581
582 //*************************************************************************
584 //*************************************************************************
585 void assign(void* object, stub_type stub)
586 {
587 invocation.object = object;
588 invocation.stub = stub;
589 }
590
591 //*************************************************************************
593 //*************************************************************************
594 template <typename T, TReturn(T::*Method)(TParam)>
595 static TReturn method_stub(void* object, TParam param)
596 {
597 T* p = static_cast<T*>(object);
598 return (p->*Method)(param);
599 }
600
601 //*************************************************************************
603 //*************************************************************************
604 template <typename T, TReturn(T::*Method)(TParam) const>
605 static TReturn const_method_stub(void* object, TParam param)
606 {
607 T* const p = static_cast<T*>(object);
608 return (p->*Method)(param);
609 }
610
611 //*************************************************************************
613 //*************************************************************************
614 template <typename T, TReturn(T::*Method)(TParam), T& Instance>
615 static TReturn method_instance_stub(void*, TParam param)
616 {
617 return (Instance.*Method)(param);
618 }
619
620 //*************************************************************************
622 //*************************************************************************
623 template <typename T, TReturn(T::*Method)(TParam) const, const T& Instance>
624 static TReturn const_method_instance_stub(void*, TParam param)
625 {
626 return (Instance.*Method)(param);
627 }
628
629#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
630 //*************************************************************************
632 //*************************************************************************
633 template <typename T, T& Instance>
634 static TReturn operator_instance_stub(void*, TParam param)
635 {
636 return Instance.operator()(param);
637 }
638#endif
639
640 //*************************************************************************
642 //*************************************************************************
643 template <TReturn(*Method)(TParam)>
644 static TReturn function_stub(void*, TParam param)
645 {
646 return (Method)(param);
647 }
648
649 //*************************************************************************
651 //*************************************************************************
652 template <typename TFunctor>
653 static TReturn functor_stub(void* object, TParam param)
654 {
655 TFunctor* p = static_cast<TFunctor*>(object);
656 return (p->operator())(param);
657 }
658
659 //*************************************************************************
661 //*************************************************************************
662 template <typename TFunctor>
663 static TReturn const_functor_stub(void* object, TParam param)
664 {
665 const TFunctor* p = static_cast<const TFunctor*>(object);
666 return (p->operator())(param);
667 }
668
669 //*************************************************************************
671 //*************************************************************************
672 invocation_element invocation;
673 };
674
675 //*************************************************************************
677 //*************************************************************************
678 template <typename TReturn>
679 class delegate<TReturn(void)> : public private_delegate::call_if_impl<delegate<TReturn(void)>, TReturn, void>
680 {
681 private:
682
683 typedef delegate<TReturn(void)> delegate_type;
684
685 public:
686
687 using private_delegate::call_if_impl< delegate<TReturn(void)>, TReturn, void>::call_if;
688
689 //*************************************************************************
691 //*************************************************************************
693 {
694 }
695
696 //*************************************************************************
697 // Copy constructor.
698 //*************************************************************************
699 delegate(const delegate& other)
700 {
701 invocation = other.invocation;
702 }
703
704 //*************************************************************************
705 // Construct from functor.
706 //*************************************************************************
707 template <typename TFunctor>
708 delegate(TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
709 {
710 assign((void*)(&instance), functor_stub<TFunctor>);
711 }
712
713 //*************************************************************************
714 // Construct from const functor.
715 //*************************************************************************
716 template <typename TFunctor>
717 delegate(const TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
718 {
719 assign((void*)(&instance), const_functor_stub<TFunctor>);
720 }
721
722 //*************************************************************************
724 //*************************************************************************
725 template <TReturn(*Method)()>
727 {
728 return delegate(ETL_NULLPTR, function_stub<Method>);
729 }
730
731 //*************************************************************************
733 //*************************************************************************
734 template <typename TFunctor>
735 static
737 create(TFunctor& instance)
738 {
739 return delegate((void*)(&instance), functor_stub<TFunctor>);
740 }
741
742 //*************************************************************************
744 //*************************************************************************
745 template <typename TFunctor>
746 static
748 create(const TFunctor& instance)
749 {
750 return delegate((void*)(&instance), const_functor_stub<TFunctor>);
751 }
752
753 //*************************************************************************
755 //*************************************************************************
756 template <typename T, TReturn(T::* Method)()>
757 static delegate create(T& instance)
758 {
759 return delegate((void*)(&instance), method_stub<T, Method>);
760 }
761
762 //*************************************************************************
764 //*************************************************************************
765 template <typename T, TReturn(T::* Method)() const>
766 static delegate create(const T& instance)
767 {
768 return delegate((void*)(&instance), const_method_stub<T, Method>);
769 }
770
771 //*************************************************************************
773 //*************************************************************************
774 template <typename T, T& Instance, TReturn(T::* Method)()>
779
780 //*************************************************************************
783 //*************************************************************************
784 template <typename T, TReturn(T::* Method)(), T& Instance>
789
790 //*************************************************************************
792 //*************************************************************************
793 template <typename T, T const& Instance, TReturn(T::* Method)() const>
798
799 //*************************************************************************
802 //*************************************************************************
803 template <typename T, TReturn(T::* Method)() const, T const& Instance>
808
809#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
810 //*************************************************************************
813 //*************************************************************************
814 template <typename T, T& Instance>
819#endif
820
821 //*************************************************************************
823 //*************************************************************************
824 template <TReturn(*Method)()>
825 void set()
826 {
827 assign(ETL_NULLPTR, function_stub<Method>);
828 }
829
830 //*************************************************************************
832 //*************************************************************************
833 template <typename TFunctor>
835 set(TFunctor& instance)
836 {
837 assign((void*)(&instance), functor_stub<TFunctor>);
838 }
839
840 //*************************************************************************
842 //*************************************************************************
843 template <typename TFunctor>
845 set(const TFunctor& instance)
846 {
847 assign((void*)(&instance), const_functor_stub<TFunctor>);
848 }
849
850 //*************************************************************************
852 //*************************************************************************
853 template <typename T, TReturn(T::* Method)()>
854 void set(T& instance)
855 {
856 assign((void*)(&instance), method_stub<T, Method>);
857 }
858
859 //*************************************************************************
861 //*************************************************************************
862 template <typename T, TReturn(T::* Method)() const>
863 void set(T& instance)
864 {
865 assign((void*)(&instance), const_method_stub<T, Method>);
866 }
867
868 //*************************************************************************
870 //*************************************************************************
871 template <typename T, T& Instance, TReturn(T::* Method)()>
872 void set()
873 {
875 }
876
877 //*************************************************************************
880 //*************************************************************************
881 template <typename T, TReturn(T::* Method)(), T& Instance>
882 void set()
883 {
885 }
886
887 //*************************************************************************
889 //*************************************************************************
890 template <typename T, T const& Instance, TReturn(T::* Method)() const>
891 void set()
892 {
894 }
895
896 //*************************************************************************
899 //*************************************************************************
900 template <typename T, TReturn(T::* Method)() const, T const& Instance>
901 void set()
902 {
904 }
905
906 //*************************************************************************
908 //*************************************************************************
909 ETL_CONSTEXPR14 void clear()
910 {
911 invocation.clear();
912 }
913
914 //*************************************************************************
916 //*************************************************************************
918 {
919 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
920
921 return (*invocation.stub)(invocation.object);
922 }
923
924 //*************************************************************************
927 //*************************************************************************
928 template <typename TAlternative>
930 {
931 if (is_valid())
932 {
933 return (*invocation.stub)(invocation.object);
934 }
935 else
936 {
937 return alternative();
938 }
939 }
940
941 //*************************************************************************
944 //*************************************************************************
945 template <TReturn(*Method)()>
947 {
948 if (is_valid())
949 {
950 return (*invocation.stub)(invocation.object);
951 }
952 else
953 {
954 return (Method)();
955 }
956 }
957
958 //*************************************************************************
960 //*************************************************************************
962 {
963 invocation = rhs.invocation;
964 return *this;
965 }
966
967 //*************************************************************************
969 //*************************************************************************
970 template <typename TFunctor>
973 {
974 assign((void*)(&instance), functor_stub<TFunctor>);
975 return *this;
976 }
977
978 //*************************************************************************
980 //*************************************************************************
981 template <typename TFunctor>
983 operator =(const TFunctor& instance)
984 {
985 assign((void*)(&instance), const_functor_stub<TFunctor>);
986 return *this;
987 }
988
989 //*************************************************************************
991 //*************************************************************************
992 bool operator == (const delegate& rhs) const
993 {
994 return invocation == rhs.invocation;
995 }
996
997 //*************************************************************************
999 //*************************************************************************
1000 bool operator != (const delegate& rhs) const
1001 {
1002 return invocation != rhs.invocation;
1003 }
1004
1005 //*************************************************************************
1007 //*************************************************************************
1008 bool is_valid() const
1009 {
1010 return invocation.stub != ETL_NULLPTR;
1011 }
1012
1013 //*************************************************************************
1015 //*************************************************************************
1016 operator bool() const
1017 {
1018 return is_valid();
1019 }
1020
1021 private:
1022
1023 typedef TReturn(*stub_type)(void* object);
1024
1025 //*************************************************************************
1027 //*************************************************************************
1028 struct invocation_element
1029 {
1030 invocation_element()
1031 : object(ETL_NULLPTR)
1032 , stub(ETL_NULLPTR)
1033 {
1034 }
1035
1036 //***********************************************************************
1037 invocation_element(void* object_, stub_type stub_)
1038 : object(object_)
1039 , stub(stub_)
1040 {
1041 }
1042
1043 //***********************************************************************
1044 bool operator ==(const invocation_element& rhs) const
1045 {
1046 return (rhs.stub == stub) && (rhs.object == object);
1047 }
1048
1049 //***********************************************************************
1050 bool operator !=(const invocation_element& rhs) const
1051 {
1052 return (rhs.stub != stub) || (rhs.object != object);
1053 }
1054
1055 //***********************************************************************
1056 ETL_CONSTEXPR14 void clear()
1057 {
1058 object = ETL_NULLPTR;
1059 stub = ETL_NULLPTR;
1060 }
1061
1062 //***********************************************************************
1063 void* object;
1064 stub_type stub;
1065 };
1066
1067 //*************************************************************************
1069 //*************************************************************************
1070 delegate(void* object, stub_type stub)
1071 : invocation(object, stub)
1072 {
1073 }
1074
1075 //*************************************************************************
1077 //*************************************************************************
1078 delegate(stub_type stub)
1079 : invocation(ETL_NULLPTR, stub)
1080 {
1081 }
1082
1083 //*************************************************************************
1085 //*************************************************************************
1086 void assign(void* object, stub_type stub)
1087 {
1088 invocation.object = object;
1089 invocation.stub = stub;
1090 }
1091
1092 //*************************************************************************
1094 //*************************************************************************
1095 template <typename T, TReturn(T::* Method)()>
1096 static TReturn method_stub(void* object)
1097 {
1098 T* p = static_cast<T*>(object);
1099 return (p->*Method)();
1100 }
1101
1102 //*************************************************************************
1104 //*************************************************************************
1105 template <typename T, TReturn(T::* Method)() const>
1106 static TReturn const_method_stub(void* object)
1107 {
1108 T* const p = static_cast<T*>(object);
1109 return (p->*Method)();
1110 }
1111
1112 //*************************************************************************
1114 //*************************************************************************
1115 template <typename T, TReturn(T::* Method)(), T& Instance>
1116 static TReturn method_instance_stub(void*)
1117 {
1118 return (Instance.*Method)();
1119 }
1120
1121 //*************************************************************************
1123 //*************************************************************************
1124 template <typename T, TReturn(T::* Method)() const, const T& Instance>
1125 static TReturn const_method_instance_stub(void*)
1126 {
1127 return (Instance.*Method)();
1128 }
1129
1130#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
1131 //*************************************************************************
1133 //*************************************************************************
1134 template <typename T, T& Instance>
1135 static TReturn operator_instance_stub(void*)
1136 {
1137 return Instance.operator()();
1138 }
1139#endif
1140
1141 //*************************************************************************
1143 //*************************************************************************
1144 template <TReturn(*Method)()>
1145 static TReturn function_stub(void*)
1146 {
1147 return (Method)();
1148 }
1149
1150 //*************************************************************************
1152 //*************************************************************************
1153 template <typename TFunctor>
1154 static TReturn functor_stub(void* object)
1155 {
1156 TFunctor* p = static_cast<TFunctor*>(object);
1157 return (p->operator())();
1158 }
1159
1160 //*************************************************************************
1162 //*************************************************************************
1163 template <typename TFunctor>
1164 static TReturn const_functor_stub(void* object)
1165 {
1166 const TFunctor* p = static_cast<const TFunctor*>(object);
1167 return (p->operator())();
1168 }
1169
1170 //*************************************************************************
1172 //*************************************************************************
1173 invocation_element invocation;
1174 };
1175}
1176
1177#endif
Definition delegate_cpp03.h:179
void set()
Set from function (Compile time).
Definition delegate_cpp03.h:324
static etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, delegate >::type create(const TFunctor &instance)
Create from a const Functor.
Definition delegate_cpp03.h:247
static delegate create(T &instance)
Create from instance method (Run time).
Definition delegate_cpp03.h:256
static delegate create()
Create from instance method (Compile time).
Definition delegate_cpp03.h:274
TReturn call_or(TParam param) const
Definition delegate_cpp03.h:445
static delegate create()
Create from function (Compile time).
Definition delegate_cpp03.h:225
etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, void >::type set(const TFunctor &instance)
Set from const Functor.
Definition delegate_cpp03.h:344
etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, void >::type set(TFunctor &instance)
Set from Functor.
Definition delegate_cpp03.h:334
static etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, delegate >::type create(TFunctor &instance)
Create from a Functor.
Definition delegate_cpp03.h:236
delegate()
Default constructor.
Definition delegate_cpp03.h:191
void set(T &instance)
Set from instance method (Run time).
Definition delegate_cpp03.h:353
TReturn operator()(TParam param) const
Execute the delegate.
Definition delegate_cpp03.h:416
TReturn call_or(TAlternative alternative, TParam param) const
Definition delegate_cpp03.h:428
static delegate create(const T &instance)
Create from const instance method (Run time).
Definition delegate_cpp03.h:265
ETL_CONSTEXPR14 void clear()
Clear the delegate.
Definition delegate_cpp03.h:408
bool is_valid() const
Returns true if the delegate is valid.
Definition delegate_cpp03.h:507
static delegate create()
Definition delegate_cpp03.h:314
void set()
Set from instance method (Compile time).
Definition delegate_cpp03.h:371
Specialisation for void parameter.
Definition delegate_cpp03.h:680
etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, void >::type set(TFunctor &instance)
Set from Functor.
Definition delegate_cpp03.h:835
static delegate create(const T &instance)
Create from const instance method (Run time).
Definition delegate_cpp03.h:766
static etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, delegate >::type create(TFunctor &instance)
Create from Functor.
Definition delegate_cpp03.h:737
bool is_valid() const
Returns true if the delegate is valid.
Definition delegate_cpp03.h:1008
TReturn call_or(TAlternative alternative) const
Definition delegate_cpp03.h:929
delegate()
Default constructor.
Definition delegate_cpp03.h:692
static delegate create()
Create from function (Compile time).
Definition delegate_cpp03.h:726
static etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, delegate >::type create(const TFunctor &instance)
Create from const Functor.
Definition delegate_cpp03.h:748
void set()
Set from function (Compile time).
Definition delegate_cpp03.h:825
etl::enable_if< etl::is_class< TFunctor >::value &&!etl::is_same< delegate_type, TFunctor >::value, void >::type set(const TFunctor &instance)
Set from const Functor.
Definition delegate_cpp03.h:845
static delegate create(T &instance)
Create from instance method (Run time).
Definition delegate_cpp03.h:757
void set(T &instance)
Set from instance method (Run time).
Definition delegate_cpp03.h:854
static delegate create()
Definition delegate_cpp03.h:815
TReturn operator()() const
Execute the delegate.
Definition delegate_cpp03.h:917
void set()
Set from instance method (Compile time).
Definition delegate_cpp03.h:872
static delegate create()
Create from instance method (Compile time).
Definition delegate_cpp03.h:775
TReturn call_or() const
Definition delegate_cpp03.h:946
ETL_CONSTEXPR14 void clear()
Clear the delegate.
Definition delegate_cpp03.h:909
The base class for delegate exceptions.
Definition delegate_cpp03.h:149
The exception thrown when the delegate is uninitialised.
Definition delegate_cpp03.h:162
Declaration.
Definition delegate_cpp03.h:175
ETL_CONSTEXPR14 bool operator==(const etl::expected< TValue, TError > &lhs, const etl::expected< TValue2, TError2 > &rhs)
Equivalence operators.
Definition expected.h:966
#define ETL_ASSERT(b, e)
Definition error_handler.h:316
Definition exception.h:47
enable_if
Definition type_traits_generator.h:1191
is_same
Definition type_traits_generator.h:1041
bitset_ext
Definition absolute.h:38
is_class
Definition type_traits_generator.h:1261
pair holds two objects of arbitrary type
Definition utility.h:164
Definition delegate_cpp03.h:69