48#ifndef ETL_DELEGATE_CPP03_INCLUDED
49#define ETL_DELEGATE_CPP03_INCLUDED
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"
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_)
166 :
delegate_exception(ETL_ERROR_TEXT(
"delegate:uninitialised", ETL_DELEGATE_FILE_ID
"A"), file_name_, line_number_)
174 template <
typename T>
177 template <
typename TReturn,
typename TParam>
200 invocation = other.invocation;
206 template <
typename TFunctor>
209 assign((
void*)(&instance), functor_stub<TFunctor>);
215 template <
typename TFunctor>
218 assign((
void*)(&instance), const_functor_stub<TFunctor>);
224 template <TReturn(*Method)(TParam)>
227 return delegate(ETL_NULLPTR, function_stub<Method>);
233 template <
typename TFunctor>
238 return delegate((
void*)(&instance), functor_stub<TFunctor>);
244 template <
typename TFunctor>
249 return delegate((
void*)(&instance), const_functor_stub<TFunctor>);
255 template <
typename T, TReturn(T::*Method)(TParam)>
258 return delegate((
void*)(&instance), method_stub<T, Method>);
264 template <
typename T, TReturn(T::*Method)(TParam) const>
267 return delegate((
void*)(&instance), const_method_stub<T, Method>);
273 template <
typename T, T& Instance, TReturn(T::*Method)(TParam)>
276 return delegate(method_instance_stub<T, Instance, Method>);
282 template <
typename T, T const& Instance, TReturn(T::*Method)(TParam) const>
285 return delegate(const_method_instance_stub<T, Instance, Method>);
288#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
293 template <
typename T, T& Instance>
296 return delegate(operator_instance_stub<T, Instance>);
303 template <TReturn(*Method)(TParam)>
306 assign(ETL_NULLPTR, function_stub<Method>);
312 template <
typename TFunctor>
316 assign((
void*)(&instance), functor_stub<TFunctor>);
322 template <
typename TFunctor>
324 set(
const TFunctor& instance)
326 assign((
void*)(&instance), const_functor_stub<TFunctor>);
332 template <
typename T, TReturn(T::* Method)(TParam)>
335 assign((
void*)(&instance), method_stub<T, Method>);
341 template <
typename T, TReturn(T::* Method)(TParam) const>
344 assign((
void*)(&instance), const_method_stub<T, Method>);
350 template <
typename T, T& Instance, TReturn(T::* Method)(TParam)>
353 assign(ETL_NULLPTR, method_instance_stub<T, Instance, Method>);
359 template <
typename T, T const& Instance, TReturn(T::* Method)(TParam) const>
362 assign(ETL_NULLPTR, const_method_instance_stub<T, Instance, Method>);
368 ETL_CONSTEXPR14
void clear()
380 return (*invocation.stub)(invocation.object, param);
387 template <
typename TAlternative>
388 TReturn
call_or(TAlternative alternative, TParam param)
const
392 return (*invocation.stub)(invocation.object, param);
396 return alternative(param);
404 template <TReturn(*Method)(TParam)>
409 return (*invocation.stub)(invocation.object, param);
413 return (Method)(param);
422 invocation = rhs.invocation;
429 template <
typename TFunctor>
431 operator =(TFunctor& instance)
433 assign((
void*)(&instance), functor_stub<TFunctor>);
440 template <
typename TFunctor>
442 operator =(
const TFunctor& instance)
444 assign((
void*)(&instance), const_functor_stub<TFunctor>);
453 return invocation == rhs.invocation;
461 return invocation != rhs.invocation;
469 return invocation.stub != ETL_NULLPTR;
475 operator bool()
const
482 typedef TReturn(*stub_type)(
void* object, TParam);
487 struct invocation_element
490 : object(ETL_NULLPTR)
496 invocation_element(
void* object_, stub_type stub_)
503 bool operator ==(
const invocation_element& rhs)
const
505 return (rhs.stub == stub) && (rhs.object == object);
509 bool operator !=(
const invocation_element& rhs)
const
511 return (rhs.stub != stub) || (rhs.object != object);
515 ETL_CONSTEXPR14
void clear()
517 object = ETL_NULLPTR;
529 delegate(
void*
object, stub_type stub)
530 : invocation(object, stub)
537 delegate(stub_type stub)
538 : invocation(ETL_NULLPTR, stub)
545 void assign(
void*
object, stub_type stub)
547 invocation.object = object;
548 invocation.stub = stub;
554 template <
typename T, TReturn(T::*Method)(TParam)>
555 static TReturn method_stub(
void*
object, TParam param)
557 T* p =
static_cast<T*
>(object);
558 return (p->*Method)(param);
564 template <
typename T, TReturn(T::*Method)(TParam) const>
565 static TReturn const_method_stub(
void*
object, TParam param)
567 T*
const p =
static_cast<T*
>(object);
568 return (p->*Method)(param);
574 template <
typename T, T& Instance, TReturn(T::*Method)(TParam)>
575 static TReturn method_instance_stub(
void*, TParam param)
577 return (Instance.*Method)(param);
583 template <
typename T, const T& Instance, TReturn(T::*Method)(TParam) const>
584 static TReturn const_method_instance_stub(
void*, TParam param)
586 return (Instance.*Method)(param);
589#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
593 template <
typename T, T& Instance>
594 static TReturn operator_instance_stub(
void*, TParam param)
596 return Instance.operator()(param);
603 template <TReturn(*Method)(TParam)>
604 static TReturn function_stub(
void*, TParam param)
606 return (Method)(param);
612 template <
typename TFunctor>
613 static TReturn functor_stub(
void*
object, TParam param)
615 TFunctor* p =
static_cast<TFunctor*
>(object);
616 return (p->operator())(param);
622 template <
typename TFunctor>
623 static TReturn const_functor_stub(
void*
object, TParam param)
625 const TFunctor* p =
static_cast<const TFunctor*
>(object);
626 return (p->operator())(param);
632 invocation_element invocation;
638 template <
typename TReturn>
661 invocation = other.invocation;
667 template <
typename TFunctor>
670 assign((
void*)(&instance), functor_stub<TFunctor>);
676 template <
typename TFunctor>
679 assign((
void*)(&instance), const_functor_stub<TFunctor>);
685 template <TReturn(*Method)()>
688 return delegate(ETL_NULLPTR, function_stub<Method>);
694 template <
typename TFunctor>
699 return delegate((
void*)(&instance), functor_stub<TFunctor>);
705 template <
typename TFunctor>
710 return delegate((
void*)(&instance), const_functor_stub<TFunctor>);
716 template <
typename T, TReturn(T::* Method)()>
719 return delegate((
void*)(&instance), method_stub<T, Method>);
725 template <
typename T, TReturn(T::* Method)() const>
728 return delegate((
void*)(&instance), const_method_stub<T, Method>);
734 template <
typename T, T& Instance, TReturn(T::* Method)()>
737 return delegate(method_instance_stub<T, Instance, Method>);
743 template <
typename T, T const& Instance, TReturn(T::* Method)() const>
746 return delegate(const_method_instance_stub<T, Instance, Method>);
749#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
754 template <
typename T, T& Instance>
757 return delegate(operator_instance_stub<T, Instance>);
764 template <TReturn(*Method)()>
767 assign(ETL_NULLPTR, function_stub<Method>);
773 template <
typename TFunctor>
777 assign((
void*)(&instance), functor_stub<TFunctor>);
783 template <
typename TFunctor>
785 set(
const TFunctor& instance)
787 assign((
void*)(&instance), const_functor_stub<TFunctor>);
793 template <
typename T, TReturn(T::* Method)()>
796 assign((
void*)(&instance), method_stub<T, Method>);
802 template <
typename T, TReturn(T::* Method)() const>
805 assign((
void*)(&instance), const_method_stub<T, Method>);
811 template <
typename T, T& Instance, TReturn(T::* Method)()>
814 assign(ETL_NULLPTR, method_instance_stub<T, Instance, Method>);
820 template <
typename T, T const& Instance, TReturn(T::* Method)() const>
823 assign(ETL_NULLPTR, const_method_instance_stub<T, Instance, Method>);
829 ETL_CONSTEXPR14
void clear()
841 return (*invocation.stub)(invocation.object);
848 template <
typename TAlternative>
849 TReturn
call_or(TAlternative alternative)
const
853 return (*invocation.stub)(invocation.object);
857 return alternative();
865 template <TReturn(*Method)()>
870 return (*invocation.stub)(invocation.object);
883 invocation = rhs.invocation;
890 template <
typename TFunctor>
892 operator =(TFunctor& instance)
894 assign((
void*)(&instance), functor_stub<TFunctor>);
901 template <
typename TFunctor>
903 operator =(
const TFunctor& instance)
905 assign((
void*)(&instance), const_functor_stub<TFunctor>);
914 return invocation == rhs.invocation;
922 return invocation != rhs.invocation;
930 return invocation.stub != ETL_NULLPTR;
936 operator bool()
const
943 typedef TReturn(*stub_type)(
void* object);
948 struct invocation_element
951 : object(ETL_NULLPTR)
957 invocation_element(
void* object_, stub_type stub_)
964 bool operator ==(
const invocation_element& rhs)
const
966 return (rhs.stub == stub) && (rhs.object == object);
970 bool operator !=(
const invocation_element& rhs)
const
972 return (rhs.stub != stub) || (rhs.object != object);
976 ETL_CONSTEXPR14
void clear()
978 object = ETL_NULLPTR;
990 delegate(
void*
object, stub_type stub)
991 : invocation(object, stub)
998 delegate(stub_type stub)
999 : invocation(ETL_NULLPTR, stub)
1006 void assign(
void*
object, stub_type stub)
1008 invocation.object = object;
1009 invocation.stub = stub;
1015 template <
typename T, TReturn(T::* Method)()>
1016 static TReturn method_stub(
void*
object)
1018 T* p =
static_cast<T*
>(object);
1019 return (p->*Method)();
1025 template <
typename T, TReturn(T::* Method)() const>
1026 static TReturn const_method_stub(
void*
object)
1028 T*
const p =
static_cast<T*
>(object);
1029 return (p->*Method)();
1035 template <
typename T, T& Instance, TReturn(T::* Method)()>
1036 static TReturn method_instance_stub(
void*)
1038 return (Instance.*Method)();
1044 template <
typename T, const T& Instance, TReturn(T::* Method)() const>
1045 static TReturn const_method_instance_stub(
void*)
1047 return (Instance.*Method)();
1050#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
1054 template <
typename T, T& Instance>
1055 static TReturn operator_instance_stub(
void*)
1057 return Instance.operator()();
1064 template <TReturn(*Method)()>
1065 static TReturn function_stub(
void*)
1073 template <
typename TFunctor>
1074 static TReturn functor_stub(
void*
object)
1076 TFunctor* p =
static_cast<TFunctor*
>(object);
1077 return (p->operator())();
1083 template <
typename TFunctor>
1084 static TReturn const_functor_stub(
void*
object)
1086 const TFunctor* p =
static_cast<const TFunctor*
>(object);
1087 return (p->operator())();
1093 invocation_element invocation;
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