Embedded Template Library 1.0
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 {
70 etl::optional<TReturn> call_if(TParam param)
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>
87 struct call_if_impl<TDelegate, void, void>
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>
107 struct call_if_impl<TDelegate, TReturn, void>
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>
126 struct call_if_impl<TDelegate, void, 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_)
153 : exception(reason_, file_name_, line_number_)
154 {
155 }
156 };
157
158 //***************************************************************************
160 //***************************************************************************
162 {
163 public:
164
165 delegate_uninitialised(string_type file_name_, numeric_type line_number_)
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
182 typedef delegate<TReturn(TParam)> delegate_type;
183
184 public:
185
186 using private_delegate::call_if_impl<delegate<TReturn(TParam)>, TReturn, TParam>::call_if;
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)>
275 {
276 return delegate(method_instance_stub<T, Instance, Method>);
277 }
278
279 //*************************************************************************
281 //*************************************************************************
282 template <typename T, T const& Instance, TReturn(T::*Method)(TParam) const>
284 {
285 return delegate(const_method_instance_stub<T, Instance, Method>);
286 }
287
288#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
289 //*************************************************************************
292 //*************************************************************************
293 template <typename T, T& Instance>
295 {
296 return delegate(operator_instance_stub<T, Instance>);
297 }
298#endif
299
300 //*************************************************************************
302 //*************************************************************************
303 template <TReturn(*Method)(TParam)>
304 void set()
305 {
306 assign(ETL_NULLPTR, function_stub<Method>);
307 }
308
309 //*************************************************************************
311 //*************************************************************************
312 template <typename TFunctor>
314 set(TFunctor& instance)
315 {
316 assign((void*)(&instance), functor_stub<TFunctor>);
317 }
318
319 //*************************************************************************
321 //*************************************************************************
322 template <typename TFunctor>
324 set(const TFunctor& instance)
325 {
326 assign((void*)(&instance), const_functor_stub<TFunctor>);
327 }
328
329 //*************************************************************************
331 //*************************************************************************
332 template <typename T, TReturn(T::* Method)(TParam)>
333 void set(T& instance)
334 {
335 assign((void*)(&instance), method_stub<T, Method>);
336 }
337
338 //*************************************************************************
340 //*************************************************************************
341 template <typename T, TReturn(T::* Method)(TParam) const>
342 void set(T& instance)
343 {
344 assign((void*)(&instance), const_method_stub<T, Method>);
345 }
346
347 //*************************************************************************
349 //*************************************************************************
350 template <typename T, T& Instance, TReturn(T::* Method)(TParam)>
351 void set()
352 {
353 assign(ETL_NULLPTR, method_instance_stub<T, Instance, Method>);
354 }
355
356 //*************************************************************************
358 //*************************************************************************
359 template <typename T, T const& Instance, TReturn(T::* Method)(TParam) const>
360 void set()
361 {
362 assign(ETL_NULLPTR, const_method_instance_stub<T, Instance, Method>);
363 }
364
365 //*************************************************************************
367 //*************************************************************************
368 ETL_CONSTEXPR14 void clear()
369 {
370 invocation.clear();
371 }
372
373 //*************************************************************************
375 //*************************************************************************
376 TReturn operator()(TParam param) const
377 {
378 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
379
380 return (*invocation.stub)(invocation.object, param);
381 }
382
383 //*************************************************************************
386 //*************************************************************************
387 template <typename TAlternative>
388 TReturn call_or(TAlternative alternative, TParam param) const
389 {
390 if (is_valid())
391 {
392 return (*invocation.stub)(invocation.object, param);
393 }
394 else
395 {
396 return alternative(param);
397 }
398 }
399
400 //*************************************************************************
403 //*************************************************************************
404 template <TReturn(*Method)(TParam)>
405 TReturn call_or(TParam param) const
406 {
407 if (is_valid())
408 {
409 return (*invocation.stub)(invocation.object, param);
410 }
411 else
412 {
413 return (Method)(param);
414 }
415 }
416
417 //*************************************************************************
419 //*************************************************************************
420 delegate& operator =(const delegate& rhs)
421 {
422 invocation = rhs.invocation;
423 return *this;
424 }
425
426 //*************************************************************************
428 //*************************************************************************
429 template <typename TFunctor>
431 operator =(TFunctor& instance)
432 {
433 assign((void*)(&instance), functor_stub<TFunctor>);
434 return *this;
435 }
436
437 //*************************************************************************
439 //*************************************************************************
440 template <typename TFunctor>
442 operator =(const TFunctor& instance)
443 {
444 assign((void*)(&instance), const_functor_stub<TFunctor>);
445 return *this;
446 }
447
448 //*************************************************************************
450 //*************************************************************************
451 bool operator == (const delegate& rhs) const
452 {
453 return invocation == rhs.invocation;
454 }
455
456 //*************************************************************************
458 //*************************************************************************
459 bool operator != (const delegate& rhs) const
460 {
461 return invocation != rhs.invocation;
462 }
463
464 //*************************************************************************
466 //*************************************************************************
467 bool is_valid() const
468 {
469 return invocation.stub != ETL_NULLPTR;
470 }
471
472 //*************************************************************************
474 //*************************************************************************
475 operator bool() const
476 {
477 return is_valid();
478 }
479
480 private:
481
482 typedef TReturn(*stub_type)(void* object, TParam);
483
484 //*************************************************************************
486 //*************************************************************************
487 struct invocation_element
488 {
489 invocation_element()
490 : object(ETL_NULLPTR)
491 , stub(ETL_NULLPTR)
492 {
493 }
494
495 //***********************************************************************
496 invocation_element(void* object_, stub_type stub_)
497 : object(object_)
498 , stub(stub_)
499 {
500 }
501
502 //***********************************************************************
503 bool operator ==(const invocation_element& rhs) const
504 {
505 return (rhs.stub == stub) && (rhs.object == object);
506 }
507
508 //***********************************************************************
509 bool operator !=(const invocation_element& rhs) const
510 {
511 return (rhs.stub != stub) || (rhs.object != object);
512 }
513
514 //***********************************************************************
515 ETL_CONSTEXPR14 void clear()
516 {
517 object = ETL_NULLPTR;
518 stub = ETL_NULLPTR;
519 }
520
521 //***********************************************************************
522 void* object;
523 stub_type stub;
524 };
525
526 //*************************************************************************
528 //*************************************************************************
529 delegate(void* object, stub_type stub)
530 : invocation(object, stub)
531 {
532 }
533
534 //*************************************************************************
536 //*************************************************************************
537 delegate(stub_type stub)
538 : invocation(ETL_NULLPTR, stub)
539 {
540 }
541
542 //*************************************************************************
544 //*************************************************************************
545 void assign(void* object, stub_type stub)
546 {
547 invocation.object = object;
548 invocation.stub = stub;
549 }
550
551 //*************************************************************************
553 //*************************************************************************
554 template <typename T, TReturn(T::*Method)(TParam)>
555 static TReturn method_stub(void* object, TParam param)
556 {
557 T* p = static_cast<T*>(object);
558 return (p->*Method)(param);
559 }
560
561 //*************************************************************************
563 //*************************************************************************
564 template <typename T, TReturn(T::*Method)(TParam) const>
565 static TReturn const_method_stub(void* object, TParam param)
566 {
567 T* const p = static_cast<T*>(object);
568 return (p->*Method)(param);
569 }
570
571 //*************************************************************************
573 //*************************************************************************
574 template <typename T, T& Instance, TReturn(T::*Method)(TParam)>
575 static TReturn method_instance_stub(void*, TParam param)
576 {
577 return (Instance.*Method)(param);
578 }
579
580 //*************************************************************************
582 //*************************************************************************
583 template <typename T, const T& Instance, TReturn(T::*Method)(TParam) const>
584 static TReturn const_method_instance_stub(void*, TParam param)
585 {
586 return (Instance.*Method)(param);
587 }
588
589#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
590 //*************************************************************************
592 //*************************************************************************
593 template <typename T, T& Instance>
594 static TReturn operator_instance_stub(void*, TParam param)
595 {
596 return Instance.operator()(param);
597 }
598#endif
599
600 //*************************************************************************
602 //*************************************************************************
603 template <TReturn(*Method)(TParam)>
604 static TReturn function_stub(void*, TParam param)
605 {
606 return (Method)(param);
607 }
608
609 //*************************************************************************
611 //*************************************************************************
612 template <typename TFunctor>
613 static TReturn functor_stub(void* object, TParam param)
614 {
615 TFunctor* p = static_cast<TFunctor*>(object);
616 return (p->operator())(param);
617 }
618
619 //*************************************************************************
621 //*************************************************************************
622 template <typename TFunctor>
623 static TReturn const_functor_stub(void* object, TParam param)
624 {
625 const TFunctor* p = static_cast<const TFunctor*>(object);
626 return (p->operator())(param);
627 }
628
629 //*************************************************************************
631 //*************************************************************************
632 invocation_element invocation;
633 };
634
635 //*************************************************************************
637 //*************************************************************************
638 template <typename TReturn>
639 class delegate<TReturn(void)> : public private_delegate::call_if_impl<delegate<TReturn(void)>, TReturn, void>
640 {
641 private:
642
643 typedef delegate<TReturn(void)> delegate_type;
644
645 public:
646
647 using private_delegate::call_if_impl< delegate<TReturn(void)>, TReturn, void>::call_if;
648
649 //*************************************************************************
651 //*************************************************************************
653 {
654 }
655
656 //*************************************************************************
657 // Copy constructor.
658 //*************************************************************************
659 delegate(const delegate& other)
660 {
661 invocation = other.invocation;
662 }
663
664 //*************************************************************************
665 // Construct from functor.
666 //*************************************************************************
667 template <typename TFunctor>
668 delegate(TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
669 {
670 assign((void*)(&instance), functor_stub<TFunctor>);
671 }
672
673 //*************************************************************************
674 // Construct from const functor.
675 //*************************************************************************
676 template <typename TFunctor>
677 delegate(const TFunctor& instance, typename etl::enable_if<etl::is_class<TFunctor>::value && !etl::is_same<delegate_type, TFunctor>::value, int>::type = 0)
678 {
679 assign((void*)(&instance), const_functor_stub<TFunctor>);
680 }
681
682 //*************************************************************************
684 //*************************************************************************
685 template <TReturn(*Method)()>
687 {
688 return delegate(ETL_NULLPTR, function_stub<Method>);
689 }
690
691 //*************************************************************************
693 //*************************************************************************
694 template <typename TFunctor>
695 static
697 create(TFunctor& instance)
698 {
699 return delegate((void*)(&instance), functor_stub<TFunctor>);
700 }
701
702 //*************************************************************************
704 //*************************************************************************
705 template <typename TFunctor>
706 static
708 create(const TFunctor& instance)
709 {
710 return delegate((void*)(&instance), const_functor_stub<TFunctor>);
711 }
712
713 //*************************************************************************
715 //*************************************************************************
716 template <typename T, TReturn(T::* Method)()>
717 static delegate create(T& instance)
718 {
719 return delegate((void*)(&instance), method_stub<T, Method>);
720 }
721
722 //*************************************************************************
724 //*************************************************************************
725 template <typename T, TReturn(T::* Method)() const>
726 static delegate create(const T& instance)
727 {
728 return delegate((void*)(&instance), const_method_stub<T, Method>);
729 }
730
731 //*************************************************************************
733 //*************************************************************************
734 template <typename T, T& Instance, TReturn(T::* Method)()>
736 {
737 return delegate(method_instance_stub<T, Instance, Method>);
738 }
739
740 //*************************************************************************
742 //*************************************************************************
743 template <typename T, T const& Instance, TReturn(T::* Method)() const>
745 {
746 return delegate(const_method_instance_stub<T, Instance, Method>);
747 }
748
749#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
750 //*************************************************************************
753 //*************************************************************************
754 template <typename T, T& Instance>
756 {
757 return delegate(operator_instance_stub<T, Instance>);
758 }
759#endif
760
761 //*************************************************************************
763 //*************************************************************************
764 template <TReturn(*Method)()>
765 void set()
766 {
767 assign(ETL_NULLPTR, function_stub<Method>);
768 }
769
770 //*************************************************************************
772 //*************************************************************************
773 template <typename TFunctor>
775 set(TFunctor& instance)
776 {
777 assign((void*)(&instance), functor_stub<TFunctor>);
778 }
779
780 //*************************************************************************
782 //*************************************************************************
783 template <typename TFunctor>
785 set(const TFunctor& instance)
786 {
787 assign((void*)(&instance), const_functor_stub<TFunctor>);
788 }
789
790 //*************************************************************************
792 //*************************************************************************
793 template <typename T, TReturn(T::* Method)()>
794 void set(T& instance)
795 {
796 assign((void*)(&instance), method_stub<T, Method>);
797 }
798
799 //*************************************************************************
801 //*************************************************************************
802 template <typename T, TReturn(T::* Method)() const>
803 void set(T& instance)
804 {
805 assign((void*)(&instance), const_method_stub<T, Method>);
806 }
807
808 //*************************************************************************
810 //*************************************************************************
811 template <typename T, T& Instance, TReturn(T::* Method)()>
812 void set()
813 {
814 assign(ETL_NULLPTR, method_instance_stub<T, Instance, Method>);
815 }
816
817 //*************************************************************************
819 //*************************************************************************
820 template <typename T, T const& Instance, TReturn(T::* Method)() const>
821 void set()
822 {
823 assign(ETL_NULLPTR, const_method_instance_stub<T, Instance, Method>);
824 }
825
826 //*************************************************************************
828 //*************************************************************************
829 ETL_CONSTEXPR14 void clear()
830 {
831 invocation.clear();
832 }
833
834 //*************************************************************************
836 //*************************************************************************
837 TReturn operator()() const
838 {
839 ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
840
841 return (*invocation.stub)(invocation.object);
842 }
843
844 //*************************************************************************
847 //*************************************************************************
848 template <typename TAlternative>
849 TReturn call_or(TAlternative alternative) const
850 {
851 if (is_valid())
852 {
853 return (*invocation.stub)(invocation.object);
854 }
855 else
856 {
857 return alternative();
858 }
859 }
860
861 //*************************************************************************
864 //*************************************************************************
865 template <TReturn(*Method)()>
866 TReturn call_or() const
867 {
868 if (is_valid())
869 {
870 return (*invocation.stub)(invocation.object);
871 }
872 else
873 {
874 return (Method)();
875 }
876 }
877
878 //*************************************************************************
880 //*************************************************************************
881 delegate& operator =(const delegate& rhs)
882 {
883 invocation = rhs.invocation;
884 return *this;
885 }
886
887 //*************************************************************************
889 //*************************************************************************
890 template <typename TFunctor>
892 operator =(TFunctor& instance)
893 {
894 assign((void*)(&instance), functor_stub<TFunctor>);
895 return *this;
896 }
897
898 //*************************************************************************
900 //*************************************************************************
901 template <typename TFunctor>
903 operator =(const TFunctor& instance)
904 {
905 assign((void*)(&instance), const_functor_stub<TFunctor>);
906 return *this;
907 }
908
909 //*************************************************************************
911 //*************************************************************************
912 bool operator == (const delegate& rhs) const
913 {
914 return invocation == rhs.invocation;
915 }
916
917 //*************************************************************************
919 //*************************************************************************
920 bool operator != (const delegate& rhs) const
921 {
922 return invocation != rhs.invocation;
923 }
924
925 //*************************************************************************
927 //*************************************************************************
928 bool is_valid() const
929 {
930 return invocation.stub != ETL_NULLPTR;
931 }
932
933 //*************************************************************************
935 //*************************************************************************
936 operator bool() const
937 {
938 return is_valid();
939 }
940
941 private:
942
943 typedef TReturn(*stub_type)(void* object);
944
945 //*************************************************************************
947 //*************************************************************************
948 struct invocation_element
949 {
950 invocation_element()
951 : object(ETL_NULLPTR)
952 , stub(ETL_NULLPTR)
953 {
954 }
955
956 //***********************************************************************
957 invocation_element(void* object_, stub_type stub_)
958 : object(object_)
959 , stub(stub_)
960 {
961 }
962
963 //***********************************************************************
964 bool operator ==(const invocation_element& rhs) const
965 {
966 return (rhs.stub == stub) && (rhs.object == object);
967 }
968
969 //***********************************************************************
970 bool operator !=(const invocation_element& rhs) const
971 {
972 return (rhs.stub != stub) || (rhs.object != object);
973 }
974
975 //***********************************************************************
976 ETL_CONSTEXPR14 void clear()
977 {
978 object = ETL_NULLPTR;
979 stub = ETL_NULLPTR;
980 }
981
982 //***********************************************************************
983 void* object;
984 stub_type stub;
985 };
986
987 //*************************************************************************
989 //*************************************************************************
990 delegate(void* object, stub_type stub)
991 : invocation(object, stub)
992 {
993 }
994
995 //*************************************************************************
997 //*************************************************************************
998 delegate(stub_type stub)
999 : invocation(ETL_NULLPTR, stub)
1000 {
1001 }
1002
1003 //*************************************************************************
1005 //*************************************************************************
1006 void assign(void* object, stub_type stub)
1007 {
1008 invocation.object = object;
1009 invocation.stub = stub;
1010 }
1011
1012 //*************************************************************************
1014 //*************************************************************************
1015 template <typename T, TReturn(T::* Method)()>
1016 static TReturn method_stub(void* object)
1017 {
1018 T* p = static_cast<T*>(object);
1019 return (p->*Method)();
1020 }
1021
1022 //*************************************************************************
1024 //*************************************************************************
1025 template <typename T, TReturn(T::* Method)() const>
1026 static TReturn const_method_stub(void* object)
1027 {
1028 T* const p = static_cast<T*>(object);
1029 return (p->*Method)();
1030 }
1031
1032 //*************************************************************************
1034 //*************************************************************************
1035 template <typename T, T& Instance, TReturn(T::* Method)()>
1036 static TReturn method_instance_stub(void*)
1037 {
1038 return (Instance.*Method)();
1039 }
1040
1041 //*************************************************************************
1043 //*************************************************************************
1044 template <typename T, const T& Instance, TReturn(T::* Method)() const>
1045 static TReturn const_method_instance_stub(void*)
1046 {
1047 return (Instance.*Method)();
1048 }
1049
1050#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
1051 //*************************************************************************
1053 //*************************************************************************
1054 template <typename T, T& Instance>
1055 static TReturn operator_instance_stub(void*)
1056 {
1057 return Instance.operator()();
1058 }
1059#endif
1060
1061 //*************************************************************************
1063 //*************************************************************************
1064 template <TReturn(*Method)()>
1065 static TReturn function_stub(void*)
1066 {
1067 return (Method)();
1068 }
1069
1070 //*************************************************************************
1072 //*************************************************************************
1073 template <typename TFunctor>
1074 static TReturn functor_stub(void* object)
1075 {
1076 TFunctor* p = static_cast<TFunctor*>(object);
1077 return (p->operator())();
1078 }
1079
1080 //*************************************************************************
1082 //*************************************************************************
1083 template <typename TFunctor>
1084 static TReturn const_functor_stub(void* object)
1085 {
1086 const TFunctor* p = static_cast<const TFunctor*>(object);
1087 return (p->operator())();
1088 }
1089
1090 //*************************************************************************
1092 //*************************************************************************
1093 invocation_element invocation;
1094 };
1095}
1096
1097#endif
Definition: delegate_cpp03.h:179
void set()
Set from function (Compile time).
Definition: delegate_cpp03.h:304
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:405
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:324
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:314
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:333
TReturn operator()(TParam param) const
Execute the delegate.
Definition: delegate_cpp03.h:376
TReturn call_or(TAlternative alternative, TParam param) const
Definition: delegate_cpp03.h:388
static delegate create(const T &instance)
Create from const instance method (Run time).
Definition: delegate_cpp03.h:265
bool is_valid() const
Returns true if the delegate is valid.
Definition: delegate_cpp03.h:467
static delegate create()
Definition: delegate_cpp03.h:294
void set()
Set from instance method (Compile time).
Definition: delegate_cpp03.h:351
Specialisation for void parameter.
Definition: delegate_cpp03.h:640
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:775
static delegate create(const T &instance)
Create from const instance method (Run 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(TFunctor &instance)
Create from Functor.
Definition: delegate_cpp03.h:697
bool is_valid() const
Returns true if the delegate is valid.
Definition: delegate_cpp03.h:928
TReturn call_or(TAlternative alternative) const
Definition: delegate_cpp03.h:849
delegate()
Default constructor.
Definition: delegate_cpp03.h:652
static delegate create()
Create from function (Compile time).
Definition: delegate_cpp03.h:686
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:708
void set()
Set from function (Compile time).
Definition: delegate_cpp03.h:765
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:785
static delegate create(T &instance)
Create from instance method (Run time).
Definition: delegate_cpp03.h:717
void set(T &instance)
Set from instance method (Run time).
Definition: delegate_cpp03.h:794
static delegate create()
Definition: delegate_cpp03.h:755
TReturn operator()() const
Execute the delegate.
Definition: delegate_cpp03.h:837
void set()
Set from instance method (Compile time).
Definition: delegate_cpp03.h:812
static delegate create()
Create from instance method (Compile time).
Definition: delegate_cpp03.h:735
TReturn call_or() const
Definition: delegate_cpp03.h:866
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
Definition: optional.h:108
#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
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
is_class
Definition: type_traits_generator.h:1261
Definition: delegate_cpp03.h:69