48 #ifndef ETL_DELEGATE_INCLUDED
49 #define ETL_DELEGATE_INCLUDED
56 #if ETL_CPP11_SUPPORTED == 0
57 #error NOT SUPPORTED FOR C++03 OR BELOW
72 delegate_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
73 :
exception(reason_, file_name_, line_number_)
86 :
delegate_exception(ETL_ERROR_TEXT(
"delegate:uninitialised", ETL_FILE
"A"), file_name_, line_number_)
93 template <
typename TReturn,
typename... TParams>
111 template <typename TLambda, typename = typename etl::enable_if<etl::is_class<TLambda>::value,
void>::type>
114 assign((
void*)(&instance), lambda_stub<TLambda>);
120 template <TReturn(*Method)(TParams...)>
123 return delegate(ETL_NULLPTR, function_stub<Method>);
129 template <typename TLambda, typename = typename etl::enable_if<etl::is_class<TLambda>::value,
void>::type>
132 return delegate((
void*)(&instance), lambda_stub<TLambda>);
138 template <
typename T, TReturn(T::*Method)(TParams...)>
141 return delegate((
void*)(&instance), method_stub<T, Method>);
148 template <
typename T, TReturn(T::*Method)(TParams...)>
154 template <
typename T, TReturn(T::*Method)(TParams...)
const>
157 return delegate((
void*)(&instance), const_method_stub<T, Method>);
163 template <
typename T, TReturn(T::*Method)(TParams...)
const>
169 template <
typename T, T& Instance, TReturn(T::*Method)(TParams...)>
172 return delegate(method_instance_stub<T, Instance, Method>);
178 template <
typename T, T
const& Instance, TReturn(T::*Method)(TParams...)
const>
181 return delegate(const_method_instance_stub<T, Instance, Method>);
184 #if !defined(ETL_COMPILER_GCC)
189 template <
typename T, T& Instance>
192 return delegate(operator_instance_stub<T, Instance>);
203 return (*invocation.stub)(invocation.object, args...);
214 template <typename TLambda, typename = typename etl::enable_if<etl::is_class<TLambda>::value,
void>::type>
217 assign((
void*)(&instance), lambda_stub<TLambda>);
226 return invocation == rhs.invocation;
234 return invocation != rhs.invocation;
242 return invocation.stub != ETL_NULLPTR;
248 operator bool()
const
255 using stub_type = TReturn(*)(
void* object, TParams...);
260 struct invocation_element
262 invocation_element() =
default;
265 constexpr invocation_element(
void* object_, stub_type stub_)
272 bool operator ==(
const invocation_element& rhs)
const
274 return (rhs.stub == stub) && (rhs.object == object);
278 bool operator !=(
const invocation_element& rhs)
const
280 return (rhs.stub != stub) || (rhs.object != object);
284 void*
object = ETL_NULLPTR;
285 stub_type stub = ETL_NULLPTR;
291 constexpr delegate(
void*
object, stub_type stub)
292 : invocation(object, stub)
299 constexpr delegate(stub_type stub)
300 : invocation(ETL_NULLPTR, stub)
307 void assign(
void*
object, stub_type stub)
309 invocation.object = object;
310 invocation.stub = stub;
316 template <
typename T, TReturn(T::*Method)(TParams...)>
317 static TReturn method_stub(
void*
object, TParams... params)
319 T* p =
static_cast<T*
>(object);
320 return (p->*Method)(params...);
326 template <
typename T, TReturn(T::*Method)(TParams...)
const>
327 static TReturn const_method_stub(
void*
object, TParams... params)
329 T*
const p =
static_cast<T*
>(object);
330 return (p->*Method)(params...);
336 template <
typename T, T& Instance, TReturn(T::*Method)(TParams...)>
337 static TReturn method_instance_stub(
void*, TParams... params)
339 return (Instance.*Method)(params...);
345 template <
typename T,
const T& Instance, TReturn(T::*Method)(TParams...)
const>
346 static TReturn const_method_instance_stub(
void*, TParams... params)
348 return (Instance.*Method)(params...);
351 #if !defined(ETL_COMPILER_GCC)
355 template <
typename T, T& Instance>
356 static TReturn operator_instance_stub(
void*, TParams... params)
358 return Instance.operator()(params...);
365 template <TReturn(*Method)(TParams...)>
366 static TReturn function_stub(
void*, TParams... params)
368 return (Method)(params...);
374 template <
typename TLambda>
375 static TReturn lambda_stub(
void*
object, TParams... arg)
377 TLambda* p =
static_cast<TLambda*
>(object);
378 return (p->operator())(arg...);
384 invocation_element invocation;
constexpr static delegate create(T &instance)
Create from instance method (Run time).
Definition: delegate.h:139
constexpr static delegate create(T &&instance)=delete
Disable create from rvalue instance method (Run time).
static delegate create(T &&instance)=delete
constexpr static delegate create(const T &instance)
Create from const instance method (Run time).
Definition: delegate.h:155
constexpr static delegate create()
Create from function (Compile time).
Definition: delegate.h:121
TReturn operator()(TParams... args) const
Execute the delegate.
Definition: delegate.h:199
delegate()=default
Default constructor.
bool is_valid() const
Returns true if the delegate is valid.
Definition: delegate.h:240
constexpr static delegate create()
Definition: delegate.h:190
constexpr static delegate create()
Create from instance method (Compile time).
Definition: delegate.h:170
constexpr static delegate create(const TLambda &instance)
Create from Lambda or Functor.
Definition: delegate.h:130
The base class for delegate exceptions.
Definition: delegate.h:69
The exception thrown when the delegate is uninitialised.
Definition: delegate.h:82
Definition: delegate.h:91
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: absolute.h:37
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:582