48#ifndef ETL_DELEGATE_CPP03_INCLUDED
49#define ETL_DELEGATE_CPP03_INCLUDED
58#if defined(ETL_IN_DELEGATE_CPP03_UNIT_TEST)
64 namespace private_delegate
67 template <
typename TDelegate,
typename TReturn,
typename TParam>
72 TDelegate& d =
static_cast<TDelegate&
>(*this);
86 template <
typename TDelegate>
91 TDelegate& d =
static_cast<TDelegate&
>(*this);
106 template <
typename TDelegate,
typename TReturn>
111 TDelegate& d =
static_cast<TDelegate&
>(*this);
125 template <
typename TDelegate,
typename TParam>
128 bool call_if(TParam param)
130 TDelegate& d =
static_cast<TDelegate&
>(*this);
152 delegate_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
153 :
exception(reason_, file_name_, line_number_)
161 class delegate_uninitialised :
public delegate_exception
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_)
182 template <
typename T>
190 template <
typename T>
193 template <
typename TReturn,
typename TParam>
199 typedef delegate<TReturn(TParam)> delegate_type;
203 typedef TReturn (*function_type)(TParam);
204 typedef TReturn return_type;
205 typedef TParam argument_type;
221 invocation = other.invocation;
227 template <
typename TFunctor>
230 assign((
void*)(&instance), functor_stub<TFunctor>);
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)
239 assign((
void*)(&instance), const_functor_stub<TFunctor>);
245 template <TReturn(*Method)(TParam)>
248 return delegate(ETL_NULLPTR, function_stub<Method>);
254 template <
typename TFunctor>
259 return delegate((
void*)(&instance), functor_stub<TFunctor>);
265 template <
typename TFunctor>
270 return delegate((
void*)(&instance), const_functor_stub<TFunctor>);
276 template <
typename T, TReturn(T::*Method)(TParam)>
279 return delegate((
void*)(&instance), method_stub<T, Method>);
285 template <
typename T, TReturn(T::*Method)(TParam) const>
288 return delegate((
void*)(&instance), const_method_stub<T, Method>);
294 template <
typename T, T& Instance, TReturn(T::*Method)(TParam)>
297 return delegate(method_instance_stub<T, Method, Instance>);
304 template <
typename T, TReturn(T::* Method)(TParam), T& Instance>
307 return delegate(method_instance_stub<T, Method, Instance>);
313 template <
typename T, T const& Instance, TReturn(T::*Method)(TParam) const>
316 return delegate(const_method_instance_stub<T, Method, Instance>);
323 template <
typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
326 return delegate(const_method_instance_stub<T, Method, Instance>);
329#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
334 template <
typename T, T& Instance>
337 return delegate(operator_instance_stub<T, Instance>);
344 template <TReturn(*Method)(TParam)>
347 assign(ETL_NULLPTR, function_stub<Method>);
353 template <
typename TFunctor>
357 assign((
void*)(&instance), functor_stub<TFunctor>);
363 template <
typename TFunctor>
365 set(
const TFunctor& instance)
367 assign((
void*)(&instance), const_functor_stub<TFunctor>);
373 template <
typename T, TReturn(T::* Method)(TParam)>
376 assign((
void*)(&instance), method_stub<T, Method>);
382 template <
typename T, TReturn(T::* Method)(TParam) const>
385 assign((
void*)(&instance), const_method_stub<T, Method>);
391 template <
typename T, T& Instance, TReturn(T::* Method)(TParam)>
394 assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
401 template <
typename T, TReturn(T::* Method)(TParam), T& Instance>
404 assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
410 template <
typename T, T const& Instance, TReturn(T::* Method)(TParam) const>
413 assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
420 template <
typename T, TReturn(T::* Method)(TParam) const, T const& Instance>
423 assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
441 return (*invocation.stub)(invocation.object, param);
448 template <
typename TAlternative>
449 TReturn
call_or(TAlternative alternative, TParam param)
const
453 return (*invocation.stub)(invocation.object, param);
457 return alternative(param);
465 template <TReturn(*Method)(TParam)>
470 return (*invocation.stub)(invocation.object, param);
474 return (Method)(param);
483 invocation = rhs.invocation;
490 template <
typename TFunctor>
492 operator =(TFunctor& instance)
494 assign((
void*)(&instance), functor_stub<TFunctor>);
501 template <
typename TFunctor>
503 operator =(
const TFunctor& instance)
505 assign((
void*)(&instance), const_functor_stub<TFunctor>);
514 return invocation == rhs.invocation;
522 return invocation != rhs.invocation;
530 return invocation.stub != ETL_NULLPTR;
536 operator bool()
const
543 typedef TReturn(*stub_type)(
void* object, TParam);
548 struct invocation_element
551 : object(ETL_NULLPTR)
557 invocation_element(
void* object_, stub_type stub_)
564 bool operator ==(
const invocation_element& rhs)
const
566 return (rhs.stub == stub) && (rhs.object == object);
570 bool operator !=(
const invocation_element& rhs)
const
572 return (rhs.stub != stub) || (rhs.object != object);
576 ETL_CONSTEXPR14
void clear()
578 object = ETL_NULLPTR;
590 delegate(
void*
object, stub_type stub)
591 : invocation(object, stub)
598 delegate(stub_type stub)
599 : invocation(ETL_NULLPTR, stub)
606 void assign(
void*
object, stub_type stub)
608 invocation.object = object;
609 invocation.stub = stub;
615 template <
typename T, TReturn(T::*Method)(TParam)>
616 static TReturn method_stub(
void*
object, TParam param)
618 T* p =
static_cast<T*
>(object);
619 return (p->*Method)(param);
625 template <
typename T, TReturn(T::*Method)(TParam) const>
626 static TReturn const_method_stub(
void*
object, TParam param)
628 T*
const p =
static_cast<T*
>(object);
629 return (p->*Method)(param);
635 template <
typename T, TReturn(T::*Method)(TParam), T& Instance>
636 static TReturn method_instance_stub(
void*, TParam param)
638 return (Instance.*Method)(param);
644 template <
typename T, TReturn(T::*Method)(TParam) const, const T& Instance>
645 static TReturn const_method_instance_stub(
void*, TParam param)
647 return (Instance.*Method)(param);
650#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
654 template <
typename T, T& Instance>
655 static TReturn operator_instance_stub(
void*, TParam param)
657 return Instance.operator()(param);
664 template <TReturn(*Method)(TParam)>
665 static TReturn function_stub(
void*, TParam param)
667 return (Method)(param);
673 template <
typename TFunctor>
674 static TReturn functor_stub(
void*
object, TParam param)
676 TFunctor* p =
static_cast<TFunctor*
>(object);
677 return (p->operator())(param);
683 template <
typename TFunctor>
684 static TReturn const_functor_stub(
void*
object, TParam param)
686 const TFunctor* p =
static_cast<const TFunctor*
>(object);
687 return (p->operator())(param);
693 invocation_element invocation;
699 template <
typename TReturn>
704 typedef delegate<TReturn(
void)> delegate_type;
708 typedef TReturn (*function_type)(void);
709 typedef TReturn return_type;
710 typedef void argument_type;
726 invocation = other.invocation;
732 template <
typename TFunctor>
735 assign((
void*)(&instance), functor_stub<TFunctor>);
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)
744 assign((
void*)(&instance), const_functor_stub<TFunctor>);
750 template <TReturn(*Method)()>
753 return delegate(ETL_NULLPTR, function_stub<Method>);
759 template <
typename TFunctor>
764 return delegate((
void*)(&instance), functor_stub<TFunctor>);
770 template <
typename TFunctor>
775 return delegate((
void*)(&instance), const_functor_stub<TFunctor>);
781 template <
typename T, TReturn(T::* Method)()>
784 return delegate((
void*)(&instance), method_stub<T, Method>);
790 template <
typename T, TReturn(T::* Method)() const>
793 return delegate((
void*)(&instance), const_method_stub<T, Method>);
799 template <
typename T, T& Instance, TReturn(T::* Method)()>
802 return delegate(method_instance_stub<T, Method, Instance>);
809 template <
typename T, TReturn(T::* Method)(), T& Instance>
812 return delegate(method_instance_stub<T, Method, Instance>);
818 template <
typename T, T const& Instance, TReturn(T::* Method)() const>
821 return delegate(const_method_instance_stub<T, Method, Instance>);
828 template <
typename T, TReturn(T::* Method)() const, T const& Instance>
831 return delegate(const_method_instance_stub<T, Method, Instance>);
834#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
839 template <
typename T, T& Instance>
842 return delegate(operator_instance_stub<T, Instance>);
849 template <TReturn(*Method)()>
852 assign(ETL_NULLPTR, function_stub<Method>);
858 template <
typename TFunctor>
862 assign((
void*)(&instance), functor_stub<TFunctor>);
868 template <
typename TFunctor>
870 set(
const TFunctor& instance)
872 assign((
void*)(&instance), const_functor_stub<TFunctor>);
878 template <
typename T, TReturn(T::* Method)()>
881 assign((
void*)(&instance), method_stub<T, Method>);
887 template <
typename T, TReturn(T::* Method)() const>
890 assign((
void*)(&instance), const_method_stub<T, Method>);
896 template <
typename T, T& Instance, TReturn(T::* Method)()>
899 assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
906 template <
typename T, TReturn(T::* Method)(), T& Instance>
909 assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
915 template <
typename T, T const& Instance, TReturn(T::* Method)() const>
918 assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
925 template <
typename T, TReturn(T::* Method)() const, T const& Instance>
928 assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
946 return (*invocation.stub)(invocation.object);
953 template <
typename TAlternative>
954 TReturn
call_or(TAlternative alternative)
const
958 return (*invocation.stub)(invocation.object);
962 return alternative();
970 template <TReturn(*Method)()>
975 return (*invocation.stub)(invocation.object);
988 invocation = rhs.invocation;
995 template <
typename TFunctor>
997 operator =(TFunctor& instance)
999 assign((
void*)(&instance), functor_stub<TFunctor>);
1006 template <
typename TFunctor>
1008 operator =(
const TFunctor& instance)
1010 assign((
void*)(&instance), const_functor_stub<TFunctor>);
1019 return invocation == rhs.invocation;
1027 return invocation != rhs.invocation;
1035 return invocation.stub != ETL_NULLPTR;
1041 operator bool()
const
1048 typedef TReturn(*stub_type)(
void* object);
1053 struct invocation_element
1055 invocation_element()
1056 : object(ETL_NULLPTR)
1062 invocation_element(
void* object_, stub_type stub_)
1069 bool operator ==(
const invocation_element& rhs)
const
1071 return (rhs.stub == stub) && (rhs.object == object);
1075 bool operator !=(
const invocation_element& rhs)
const
1077 return (rhs.stub != stub) || (rhs.object != object);
1081 ETL_CONSTEXPR14
void clear()
1083 object = ETL_NULLPTR;
1095 delegate(
void*
object, stub_type stub)
1096 : invocation(object, stub)
1103 delegate(stub_type stub)
1104 : invocation(ETL_NULLPTR, stub)
1111 void assign(
void*
object, stub_type stub)
1113 invocation.object = object;
1114 invocation.stub = stub;
1120 template <
typename T, TReturn(T::* Method)()>
1121 static TReturn method_stub(
void*
object)
1123 T* p =
static_cast<T*
>(object);
1124 return (p->*Method)();
1130 template <
typename T, TReturn(T::* Method)() const>
1131 static TReturn const_method_stub(
void*
object)
1133 T*
const p =
static_cast<T*
>(object);
1134 return (p->*Method)();
1140 template <
typename T, TReturn(T::* Method)(), T& Instance>
1141 static TReturn method_instance_stub(
void*)
1143 return (Instance.*Method)();
1149 template <
typename T, TReturn(T::* Method)() const, const T& Instance>
1150 static TReturn const_method_instance_stub(
void*)
1152 return (Instance.*Method)();
1155#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
1159 template <
typename T, T& Instance>
1160 static TReturn operator_instance_stub(
void*)
1162 return Instance.operator()();
1169 template <TReturn(*Method)()>
1170 static TReturn function_stub(
void*)
1178 template <
typename TFunctor>
1179 static TReturn functor_stub(
void*
object)
1181 TFunctor* p =
static_cast<TFunctor*
>(object);
1182 return (p->operator())();
1188 template <
typename TFunctor>
1189 static TReturn const_functor_stub(
void*
object)
1191 const TFunctor* p =
static_cast<const TFunctor*
>(object);
1192 return (p->operator())();
1198 invocation_element invocation;
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
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 exception thrown when the delegate is uninitialised.
Definition delegate_cpp03.h:162
Declaration.
Definition delegate_cpp03.h:191
Definition optional.h:1321
ETL_CONSTEXPR14 bool operator==(const etl::expected< TValue, TError > &lhs, const etl::expected< TValue2, TError2 > &rhs)
Equivalence operators.
Definition expected.h:962
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition exception.h:47
enable_if
Definition type_traits_generator.h:1254
bitset_ext
Definition absolute.h:39
Definition type_traits_generator.h:912
Definition delegate_cpp03.h:176
is_class
Definition type_traits_generator.h:1324
is_delegate
Definition delegate_cpp03.h:184
Definition delegate_cpp03.h:69