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 //*****************************************************************
174 //*****************************************************************
176 {
177 };
178
179 //***************************************************************************
181 //***************************************************************************
182 template <typename T>
183 struct is_delegate : etl::bool_constant<etl::is_base_of<delegate_tag, T>::value>
184 {
185 };
186
187 //*************************************************************************
189 //*************************************************************************
190 template <typename T>
191 class delegate;
192
193 template <typename TReturn, typename TParam>
194 class delegate<TReturn(TParam)> : public private_delegate::call_if_impl<delegate<TReturn(TParam)>, TReturn, TParam>,
195 public delegate_tag
196 {
197 private:
198
200
201 public:
202
203 typedef TReturn (*function_type)(TParam);
204 typedef TReturn return_type;
205 typedef TParam argument_type;
206
208
209 //*************************************************************************
211 //*************************************************************************
213 {
214 }
215
216 //*************************************************************************
217 // Copy constructor.
218 //*************************************************************************
219 delegate(const delegate& other)
220 {
221 invocation = other.invocation;
222 }
223
224 //*************************************************************************
225 // Construct from a functor.
226 //*************************************************************************
227 template <typename TFunctor>
228 delegate(TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !is_delegate<TFunctor>::value, int>::type = 0)
229 {
230 assign((void*)(&instance), functor_stub<TFunctor>);
231 }
232
233 //*************************************************************************
234 // Construct from a const functor.
235 //*************************************************************************
236 template <typename TFunctor>
237 delegate(const TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !is_delegate<TFunctor>::value, int>::type = 0)
238 {
239 assign((void*)(&instance), const_functor_stub<TFunctor>);
240 }
241
242 //*************************************************************************
244 //*************************************************************************
245 template <TReturn(*Method)(TParam)>
247 {
248 return delegate(ETL_NULLPTR, function_stub<Method>);
249 }
250
251 //*************************************************************************
253 //*************************************************************************
254 template <typename TFunctor>
255 static
257 create(TFunctor& instance)
258 {
259 return delegate((void*)(&instance), functor_stub<TFunctor>);
260 }
261
262 //*************************************************************************
264 //*************************************************************************
265 template <typename TFunctor>
266 static
268 create(const TFunctor& instance)
269 {
270 return delegate((void*)(&instance), const_functor_stub<TFunctor>);
271 }
272
273 //*************************************************************************
275 //*************************************************************************
276 template <typename T, TReturn(T::*Method)(TParam)>
277 static delegate create(T& instance)
278 {
279 return delegate((void*)(&instance), method_stub<T, Method>);
280 }
281
282 //*************************************************************************
284 //*************************************************************************
285 template <typename T, TReturn(T::*Method)(TParam) const>
286 static delegate create(const T& instance)
287 {
288 return delegate((void*)(&instance), const_method_stub<T, Method>);
289 }
290
291 //*************************************************************************
293 //*************************************************************************
294 template <typename T, T& Instance, TReturn(T::*Method)(TParam)>
299
300 //*************************************************************************
303 //*************************************************************************
304 template <typename T, TReturn(T::* Method)(TParam), T& Instance>
309
310 //*************************************************************************
312 //*************************************************************************
313 template <typename T, T const& Instance, TReturn(T::*Method)(TParam) const>
318
319 //*************************************************************************
322 //*************************************************************************
323 template <typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
328
329#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
330 //*************************************************************************
333 //*************************************************************************
334 template <typename T, T& Instance>
339#endif
340
341 //*************************************************************************
343 //*************************************************************************
344 template <TReturn(*Method)(TParam)>
345 void set()
346 {
347 assign(ETL_NULLPTR, function_stub<Method>);
348 }
349
350 //*************************************************************************
352 //*************************************************************************
353 template <typename TFunctor>
355 set(TFunctor& instance)
356 {
357 assign((void*)(&instance), functor_stub<TFunctor>);
358 }
359
360 //*************************************************************************
362 //*************************************************************************
363 template <typename TFunctor>
365 set(const TFunctor& instance)
366 {
367 assign((void*)(&instance), const_functor_stub<TFunctor>);
368 }
369
370 //*************************************************************************
372 //*************************************************************************
373 template <typename T, TReturn(T::* Method)(TParam)>
374 void set(T& instance)
375 {
376 assign((void*)(&instance), method_stub<T, Method>);
377 }
378
379 //*************************************************************************
381 //*************************************************************************
382 template <typename T, TReturn(T::* Method)(TParam) const>
383 void set(T& instance)
384 {
385 assign((void*)(&instance), const_method_stub<T, Method>);
386 }
387
388 //*************************************************************************
390 //*************************************************************************
391 template <typename T, T& Instance, TReturn(T::* Method)(TParam)>
392 void set()
393 {
395 }
396
397 //*************************************************************************
400 //*************************************************************************
401 template <typename T, TReturn(T::* Method)(TParam), T& Instance>
402 void set()
403 {
405 }
406
407 //*************************************************************************
409 //*************************************************************************
410 template <typename T, T const& Instance, TReturn(T::* Method)(TParam) const>
411 void set()
412 {
414 }
415
416 //*************************************************************************
419 //*************************************************************************
420 template <typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
421 void set()
422 {
424 }
425
426 //*************************************************************************
428 //*************************************************************************
429 ETL_CONSTEXPR14 void clear()
430 {
431 invocation.clear();
432 }
433
434 //*************************************************************************
436 //*************************************************************************
438 {
439 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
440
441 return (*invocation.stub)(invocation.object, param);
442 }
443
444 //*************************************************************************
447 //*************************************************************************
448 template <typename TAlternative>
450 {
451 if (is_valid())
452 {
453 return (*invocation.stub)(invocation.object, param);
454 }
455 else
456 {
457 return alternative(param);
458 }
459 }
460
461 //*************************************************************************
464 //*************************************************************************
465 template <TReturn(*Method)(TParam)>
467 {
468 if (is_valid())
469 {
470 return (*invocation.stub)(invocation.object, param);
471 }
472 else
473 {
474 return (Method)(param);
475 }
476 }
477
478 //*************************************************************************
480 //*************************************************************************
482 {
483 invocation = rhs.invocation;
484 return *this;
485 }
486
487 //*************************************************************************
489 //*************************************************************************
490 template <typename TFunctor>
493 {
494 assign((void*)(&instance), functor_stub<TFunctor>);
495 return *this;
496 }
497
498 //*************************************************************************
500 //*************************************************************************
501 template <typename TFunctor>
503 operator =(const TFunctor& instance)
504 {
505 assign((void*)(&instance), const_functor_stub<TFunctor>);
506 return *this;
507 }
508
509 //*************************************************************************
511 //*************************************************************************
512 bool operator == (const delegate& rhs) const
513 {
514 return invocation == rhs.invocation;
515 }
516
517 //*************************************************************************
519 //*************************************************************************
520 bool operator != (const delegate& rhs) const
521 {
522 return invocation != rhs.invocation;
523 }
524
525 //*************************************************************************
527 //*************************************************************************
528 bool is_valid() const
529 {
530 return invocation.stub != ETL_NULLPTR;
531 }
532
533 //*************************************************************************
535 //*************************************************************************
536 operator bool() const
537 {
538 return is_valid();
539 }
540
541 private:
542
543 typedef TReturn(*stub_type)(void* object, TParam);
544
545 //*************************************************************************
547 //*************************************************************************
548 struct invocation_element
549 {
550 invocation_element()
551 : object(ETL_NULLPTR)
552 , stub(ETL_NULLPTR)
553 {
554 }
555
556 //***********************************************************************
557 invocation_element(void* object_, stub_type stub_)
558 : object(object_)
559 , stub(stub_)
560 {
561 }
562
563 //***********************************************************************
564 bool operator ==(const invocation_element& rhs) const
565 {
566 return (rhs.stub == stub) && (rhs.object == object);
567 }
568
569 //***********************************************************************
570 bool operator !=(const invocation_element& rhs) const
571 {
572 return (rhs.stub != stub) || (rhs.object != object);
573 }
574
575 //***********************************************************************
576 ETL_CONSTEXPR14 void clear()
577 {
578 object = ETL_NULLPTR;
579 stub = ETL_NULLPTR;
580 }
581
582 //***********************************************************************
583 void* object;
584 stub_type stub;
585 };
586
587 //*************************************************************************
589 //*************************************************************************
590 delegate(void* object, stub_type stub)
591 : invocation(object, stub)
592 {
593 }
594
595 //*************************************************************************
597 //*************************************************************************
598 delegate(stub_type stub)
599 : invocation(ETL_NULLPTR, stub)
600 {
601 }
602
603 //*************************************************************************
605 //*************************************************************************
606 void assign(void* object, stub_type stub)
607 {
608 invocation.object = object;
609 invocation.stub = stub;
610 }
611
612 //*************************************************************************
614 //*************************************************************************
615 template <typename T, TReturn(T::*Method)(TParam)>
616 static TReturn method_stub(void* object, TParam param)
617 {
618 T* p = static_cast<T*>(object);
619 return (p->*Method)(param);
620 }
621
622 //*************************************************************************
624 //*************************************************************************
625 template <typename T, TReturn(T::*Method)(TParam) const>
626 static TReturn const_method_stub(void* object, TParam param)
627 {
628 T* const p = static_cast<T*>(object);
629 return (p->*Method)(param);
630 }
631
632 //*************************************************************************
634 //*************************************************************************
635 template <typename T, TReturn(T::*Method)(TParam), T& Instance>
636 static TReturn method_instance_stub(void*, TParam param)
637 {
638 return (Instance.*Method)(param);
639 }
640
641 //*************************************************************************
643 //*************************************************************************
644 template <typename T, TReturn(T::*Method)(TParam) const, const T& Instance>
645 static TReturn const_method_instance_stub(void*, TParam param)
646 {
647 return (Instance.*Method)(param);
648 }
649
650#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
651 //*************************************************************************
653 //*************************************************************************
654 template <typename T, T& Instance>
655 static TReturn operator_instance_stub(void*, TParam param)
656 {
657 return Instance.operator()(param);
658 }
659#endif
660
661 //*************************************************************************
663 //*************************************************************************
664 template <TReturn(*Method)(TParam)>
665 static TReturn function_stub(void*, TParam param)
666 {
667 return (Method)(param);
668 }
669
670 //*************************************************************************
672 //*************************************************************************
673 template <typename TFunctor>
674 static TReturn functor_stub(void* object, TParam param)
675 {
676 TFunctor* p = static_cast<TFunctor*>(object);
677 return (p->operator())(param);
678 }
679
680 //*************************************************************************
682 //*************************************************************************
683 template <typename TFunctor>
684 static TReturn const_functor_stub(void* object, TParam param)
685 {
686 const TFunctor* p = static_cast<const TFunctor*>(object);
687 return (p->operator())(param);
688 }
689
690 //*************************************************************************
692 //*************************************************************************
693 invocation_element invocation;
694 };
695
696 //*************************************************************************
698 //*************************************************************************
699 template <typename TReturn>
700 class delegate<TReturn(void)> : public private_delegate::call_if_impl<delegate<TReturn(void)>, TReturn, void>
701 {
702 private:
703
704 typedef delegate<TReturn(void)> delegate_type;
705
706 public:
707
708 typedef TReturn (*function_type)(void);
709 typedef TReturn return_type;
710 typedef void argument_type;
711
712 using private_delegate::call_if_impl< delegate<TReturn(void)>, TReturn, void>::call_if;
713
714 //*************************************************************************
716 //*************************************************************************
718 {
719 }
720
721 //*************************************************************************
722 // Copy constructor.
723 //*************************************************************************
724 delegate(const delegate& other)
725 {
726 invocation = other.invocation;
727 }
728
729 //*************************************************************************
730 // Construct from functor.
731 //*************************************************************************
732 template <typename TFunctor>
733 delegate(TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !is_delegate<TFunctor>::value, int>::type = 0)
734 {
735 assign((void*)(&instance), functor_stub<TFunctor>);
736 }
737
738 //*************************************************************************
739 // Construct from const functor.
740 //*************************************************************************
741 template <typename TFunctor>
742 delegate(const TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !is_delegate<TFunctor>::value, int>::type = 0)
743 {
744 assign((void*)(&instance), const_functor_stub<TFunctor>);
745 }
746
747 //*************************************************************************
749 //*************************************************************************
750 template <TReturn(*Method)()>
752 {
753 return delegate(ETL_NULLPTR, function_stub<Method>);
754 }
755
756 //*************************************************************************
758 //*************************************************************************
759 template <typename TFunctor>
760 static
762 create(TFunctor& instance)
763 {
764 return delegate((void*)(&instance), functor_stub<TFunctor>);
765 }
766
767 //*************************************************************************
769 //*************************************************************************
770 template <typename TFunctor>
771 static
773 create(const TFunctor& instance)
774 {
775 return delegate((void*)(&instance), const_functor_stub<TFunctor>);
776 }
777
778 //*************************************************************************
780 //*************************************************************************
781 template <typename T, TReturn(T::* Method)()>
782 static delegate create(T& instance)
783 {
784 return delegate((void*)(&instance), method_stub<T, Method>);
785 }
786
787 //*************************************************************************
789 //*************************************************************************
790 template <typename T, TReturn(T::* Method)() const>
791 static delegate create(const T& instance)
792 {
793 return delegate((void*)(&instance), const_method_stub<T, Method>);
794 }
795
796 //*************************************************************************
798 //*************************************************************************
799 template <typename T, T& Instance, TReturn(T::* Method)()>
804
805 //*************************************************************************
808 //*************************************************************************
809 template <typename T, TReturn(T::* Method)(), T& Instance>
814
815 //*************************************************************************
817 //*************************************************************************
818 template <typename T, T const& Instance, TReturn(T::* Method)() const>
823
824 //*************************************************************************
827 //*************************************************************************
828 template <typename T, TReturn(T::* Method)() const, T const& Instance>
833
834#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
835 //*************************************************************************
838 //*************************************************************************
839 template <typename T, T& Instance>
844#endif
845
846 //*************************************************************************
848 //*************************************************************************
849 template <TReturn(*Method)()>
850 void set()
851 {
852 assign(ETL_NULLPTR, function_stub<Method>);
853 }
854
855 //*************************************************************************
857 //*************************************************************************
858 template <typename TFunctor>
860 set(TFunctor& instance)
861 {
862 assign((void*)(&instance), functor_stub<TFunctor>);
863 }
864
865 //*************************************************************************
867 //*************************************************************************
868 template <typename TFunctor>
870 set(const TFunctor& instance)
871 {
872 assign((void*)(&instance), const_functor_stub<TFunctor>);
873 }
874
875 //*************************************************************************
877 //*************************************************************************
878 template <typename T, TReturn(T::* Method)()>
879 void set(T& instance)
880 {
881 assign((void*)(&instance), method_stub<T, Method>);
882 }
883
884 //*************************************************************************
886 //*************************************************************************
887 template <typename T, TReturn(T::* Method)() const>
888 void set(T& instance)
889 {
890 assign((void*)(&instance), const_method_stub<T, Method>);
891 }
892
893 //*************************************************************************
895 //*************************************************************************
896 template <typename T, T& Instance, TReturn(T::* Method)()>
897 void set()
898 {
900 }
901
902 //*************************************************************************
905 //*************************************************************************
906 template <typename T, TReturn(T::* Method)(), T& Instance>
907 void set()
908 {
910 }
911
912 //*************************************************************************
914 //*************************************************************************
915 template <typename T, T const& Instance, TReturn(T::* Method)() const>
916 void set()
917 {
919 }
920
921 //*************************************************************************
924 //*************************************************************************
925 template <typename T, TReturn(T::* Method)() const, T const& Instance>
926 void set()
927 {
929 }
930
931 //*************************************************************************
933 //*************************************************************************
934 ETL_CONSTEXPR14 void clear()
935 {
936 invocation.clear();
937 }
938
939 //*************************************************************************
941 //*************************************************************************
943 {
944 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
945
946 return (*invocation.stub)(invocation.object);
947 }
948
949 //*************************************************************************
952 //*************************************************************************
953 template <typename TAlternative>
955 {
956 if (is_valid())
957 {
958 return (*invocation.stub)(invocation.object);
959 }
960 else
961 {
962 return alternative();
963 }
964 }
965
966 //*************************************************************************
969 //*************************************************************************
970 template <TReturn(*Method)()>
972 {
973 if (is_valid())
974 {
975 return (*invocation.stub)(invocation.object);
976 }
977 else
978 {
979 return (Method)();
980 }
981 }
982
983 //*************************************************************************
985 //*************************************************************************
987 {
988 invocation = rhs.invocation;
989 return *this;
990 }
991
992 //*************************************************************************
994 //*************************************************************************
995 template <typename TFunctor>
998 {
999 assign((void*)(&instance), functor_stub<TFunctor>);
1000 return *this;
1001 }
1002
1003 //*************************************************************************
1005 //*************************************************************************
1006 template <typename TFunctor>
1008 operator =(const TFunctor& instance)
1009 {
1010 assign((void*)(&instance), const_functor_stub<TFunctor>);
1011 return *this;
1012 }
1013
1014 //*************************************************************************
1016 //*************************************************************************
1017 bool operator == (const delegate& rhs) const
1018 {
1019 return invocation == rhs.invocation;
1020 }
1021
1022 //*************************************************************************
1024 //*************************************************************************
1025 bool operator != (const delegate& rhs) const
1026 {
1027 return invocation != rhs.invocation;
1028 }
1029
1030 //*************************************************************************
1032 //*************************************************************************
1033 bool is_valid() const
1034 {
1035 return invocation.stub != ETL_NULLPTR;
1036 }
1037
1038 //*************************************************************************
1040 //*************************************************************************
1041 operator bool() const
1042 {
1043 return is_valid();
1044 }
1045
1046 private:
1047
1048 typedef TReturn(*stub_type)(void* object);
1049
1050 //*************************************************************************
1052 //*************************************************************************
1053 struct invocation_element
1054 {
1055 invocation_element()
1056 : object(ETL_NULLPTR)
1057 , stub(ETL_NULLPTR)
1058 {
1059 }
1060
1061 //***********************************************************************
1062 invocation_element(void* object_, stub_type stub_)
1063 : object(object_)
1064 , stub(stub_)
1065 {
1066 }
1067
1068 //***********************************************************************
1069 bool operator ==(const invocation_element& rhs) const
1070 {
1071 return (rhs.stub == stub) && (rhs.object == object);
1072 }
1073
1074 //***********************************************************************
1075 bool operator !=(const invocation_element& rhs) const
1076 {
1077 return (rhs.stub != stub) || (rhs.object != object);
1078 }
1079
1080 //***********************************************************************
1081 ETL_CONSTEXPR14 void clear()
1082 {
1083 object = ETL_NULLPTR;
1084 stub = ETL_NULLPTR;
1085 }
1086
1087 //***********************************************************************
1088 void* object;
1089 stub_type stub;
1090 };
1091
1092 //*************************************************************************
1094 //*************************************************************************
1095 delegate(void* object, stub_type stub)
1096 : invocation(object, stub)
1097 {
1098 }
1099
1100 //*************************************************************************
1102 //*************************************************************************
1103 delegate(stub_type stub)
1104 : invocation(ETL_NULLPTR, stub)
1105 {
1106 }
1107
1108 //*************************************************************************
1110 //*************************************************************************
1111 void assign(void* object, stub_type stub)
1112 {
1113 invocation.object = object;
1114 invocation.stub = stub;
1115 }
1116
1117 //*************************************************************************
1119 //*************************************************************************
1120 template <typename T, TReturn(T::* Method)()>
1121 static TReturn method_stub(void* object)
1122 {
1123 T* p = static_cast<T*>(object);
1124 return (p->*Method)();
1125 }
1126
1127 //*************************************************************************
1129 //*************************************************************************
1130 template <typename T, TReturn(T::* Method)() const>
1131 static TReturn const_method_stub(void* object)
1132 {
1133 T* const p = static_cast<T*>(object);
1134 return (p->*Method)();
1135 }
1136
1137 //*************************************************************************
1139 //*************************************************************************
1140 template <typename T, TReturn(T::* Method)(), T& Instance>
1141 static TReturn method_instance_stub(void*)
1142 {
1143 return (Instance.*Method)();
1144 }
1145
1146 //*************************************************************************
1148 //*************************************************************************
1149 template <typename T, TReturn(T::* Method)() const, const T& Instance>
1150 static TReturn const_method_instance_stub(void*)
1151 {
1152 return (Instance.*Method)();
1153 }
1154
1155#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
1156 //*************************************************************************
1158 //*************************************************************************
1159 template <typename T, T& Instance>
1160 static TReturn operator_instance_stub(void*)
1161 {
1162 return Instance.operator()();
1163 }
1164#endif
1165
1166 //*************************************************************************
1168 //*************************************************************************
1169 template <TReturn(*Method)()>
1170 static TReturn function_stub(void*)
1171 {
1172 return (Method)();
1173 }
1174
1175 //*************************************************************************
1177 //*************************************************************************
1178 template <typename TFunctor>
1179 static TReturn functor_stub(void* object)
1180 {
1181 TFunctor* p = static_cast<TFunctor*>(object);
1182 return (p->operator())();
1183 }
1184
1185 //*************************************************************************
1187 //*************************************************************************
1188 template <typename TFunctor>
1189 static TReturn const_functor_stub(void* object)
1190 {
1191 const TFunctor* p = static_cast<const TFunctor*>(object);
1192 return (p->operator())();
1193 }
1194
1195 //*************************************************************************
1197 //*************************************************************************
1198 invocation_element invocation;
1199 };
1200}
1201
1202#endif
Definition delegate_cpp03.h:196
void set()
Set from function (Compile time).
Definition delegate_cpp03.h:345
etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, void >::type set(TFunctor &instance)
Set from Functor.
Definition delegate_cpp03.h:355
static etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, delegate >::type create(TFunctor &instance)
Create from a Functor.
Definition delegate_cpp03.h:257
static delegate create(T &instance)
Create from instance method (Run time).
Definition delegate_cpp03.h:277
static delegate create()
Create from instance method (Compile time).
Definition delegate_cpp03.h:295
TReturn call_or(TParam param) const
Definition delegate_cpp03.h:466
static delegate create()
Create from function (Compile time).
Definition delegate_cpp03.h:246
etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, void >::type set(const TFunctor &instance)
Set from const Functor.
Definition delegate_cpp03.h:365
delegate()
Default constructor.
Definition delegate_cpp03.h:212
void set(T &instance)
Set from instance method (Run time).
Definition delegate_cpp03.h:374
TReturn operator()(TParam param) const
Execute the delegate.
Definition delegate_cpp03.h:437
TReturn call_or(TAlternative alternative, TParam param) const
Definition delegate_cpp03.h:449
static delegate create(const T &instance)
Create from const instance method (Run time).
Definition delegate_cpp03.h:286
ETL_CONSTEXPR14 void clear()
Clear the delegate.
Definition delegate_cpp03.h:429
bool is_valid() const
Returns true if the delegate is valid.
Definition delegate_cpp03.h:528
static delegate create()
Definition delegate_cpp03.h:335
void set()
Set from instance method (Compile time).
Definition delegate_cpp03.h:392
static etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, delegate >::type create(const TFunctor &instance)
Create from a const Functor.
Definition delegate_cpp03.h:268
Specialisation for void parameter.
Definition delegate_cpp03.h:701
static etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, delegate >::type create(TFunctor &instance)
Create from Functor.
Definition delegate_cpp03.h:762
static delegate create(const T &instance)
Create from const instance method (Run time).
Definition delegate_cpp03.h:791
bool is_valid() const
Returns true if the delegate is valid.
Definition delegate_cpp03.h:1033
TReturn call_or(TAlternative alternative) const
Definition delegate_cpp03.h:954
delegate()
Default constructor.
Definition delegate_cpp03.h:717
static delegate create()
Create from function (Compile time).
Definition delegate_cpp03.h:751
void set()
Set from function (Compile time).
Definition delegate_cpp03.h:850
static delegate create(T &instance)
Create from instance method (Run time).
Definition delegate_cpp03.h:782
void set(T &instance)
Set from instance method (Run time).
Definition delegate_cpp03.h:879
static delegate create()
Definition delegate_cpp03.h:840
TReturn operator()() const
Execute the delegate.
Definition delegate_cpp03.h:942
etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, void >::type set(TFunctor &instance)
Set from Functor.
Definition delegate_cpp03.h:860
etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, void >::type set(const TFunctor &instance)
Set from const Functor.
Definition delegate_cpp03.h:870
static etl::enable_if< etl::is_class< TFunctor >::value &&!is_delegate< TFunctor >::value, delegate >::type create(const TFunctor &instance)
Create from const Functor.
Definition delegate_cpp03.h:773
void set()
Set from instance method (Compile time).
Definition delegate_cpp03.h:897
static delegate create()
Create from instance method (Compile time).
Definition delegate_cpp03.h:800
TReturn call_or() const
Definition delegate_cpp03.h:971
ETL_CONSTEXPR14 void clear()
Clear the delegate.
Definition delegate_cpp03.h:934
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:191
ETL_CONSTEXPR14 bool operator==(const etl::expected< TValue, TError > &lhs, const etl::expected< TValue2, TError2 > &rhs)
Equivalence operators.
Definition expected.h:968
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
enable_if
Definition type_traits_generator.h:1186
bitset_ext
Definition absolute.h:38
Definition type_traits_generator.h:844
Definition delegate_cpp03.h:176
is_class
Definition type_traits_generator.h:1256
is_delegate
Definition delegate_cpp03.h:184
pair holds two objects of arbitrary type
Definition utility.h:164
Definition delegate_cpp03.h:69