48#ifndef ETL_DELEGATE_CPP11_INCLUDED
49#define ETL_DELEGATE_CPP11_INCLUDED
51#include "../platform.h"
52#include "../error_handler.h"
53#include "../exception.h"
54#include "../type_traits.h"
55#include "../function_traits.h"
56#include "../utility.h"
57#include "../optional.h"
64 class delegate_exception :
public exception
68 delegate_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
69 :
exception(reason_, file_name_, line_number_)
77 class delegate_uninitialised :
public delegate_exception
81 delegate_uninitialised(string_type file_name_, numeric_type line_number_)
82 : delegate_exception(ETL_ERROR_TEXT(
"delegate:uninitialised", ETL_DELEGATE_FILE_ID
"A"), file_name_, line_number_)
104 template <
typename T>
105 inline constexpr bool is_delegate_v = is_delegate<T>::value;
111 template <
typename T>
117 template <
typename TReturn,
typename... TParams>
146 template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !is_delegate<TLambda>::value,
void>>
147 ETL_CONSTEXPR14 delegate(
const TLambda& instance)
149 assign((
void*)(&instance), const_lambda_stub<TLambda>);
155 template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !
etl::is_same<
etl::delegate<TReturn(TParams...)>, TLambda>::value,
void>>
156 ETL_CONSTEXPR14 delegate(TLambda&& instance) =
delete;
161 template <TReturn(*Method)(TParams...)>
264#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
269 template <
typename T, T& Instance>
281 ETL_CONSTEXPR14
void set()
382 template <
typename TRet = TReturn>
402 template <
typename TRet = TReturn>
421 template <
typename TAlternative>
482 return invocation ==
rhs.invocation;
490 return invocation !=
rhs.invocation;
499 return invocation.stub != ETL_NULLPTR;
506 ETL_CONSTEXPR14
operator bool()
const
518 struct invocation_element
520 invocation_element() =
default;
523 ETL_CONSTEXPR14 invocation_element(
void*
object_, stub_type
stub_)
530 ETL_CONSTEXPR14
bool operator ==(
const invocation_element& rhs)
const
532 return (rhs.stub == stub) && (rhs.object == object);
536 ETL_CONSTEXPR14
bool operator !=(
const invocation_element& rhs)
const
538 return (rhs.stub != stub) || (rhs.object != object);
542 ETL_CONSTEXPR14
void clear()
544 object = ETL_NULLPTR;
549 void*
object = ETL_NULLPTR;
550 stub_type stub = ETL_NULLPTR;
556 ETL_CONSTEXPR14 delegate(
void*
object, stub_type stub)
557 : invocation(object, stub)
564 ETL_CONSTEXPR14 delegate(stub_type stub)
565 : invocation(ETL_NULLPTR, stub)
572 ETL_CONSTEXPR14
void assign(
void*
object, stub_type stub)
574 invocation.object = object;
575 invocation.stub = stub;
581 template <
typename T, TReturn(T::*Method)(TParams...)>
582 static ETL_CONSTEXPR14 TReturn method_stub(void* object, TParams... params)
584 T* p =
static_cast<T*
>(object);
591 template <
typename T, TReturn(T::*Method)(TParams...) const>
592 static ETL_CONSTEXPR14 TReturn const_method_stub(void* object, TParams... params)
594 T*
const p =
static_cast<T*
>(object);
601 template <
typename T, TReturn(T::*Method)(TParams...), T& Instance>
602 static ETL_CONSTEXPR14 TReturn method_instance_stub(void*, TParams... params)
610 template <
typename T, TReturn(T::*Method)(TParams...) const, const T& Instance>
611 static ETL_CONSTEXPR14 TReturn const_method_instance_stub(void*, TParams... params)
616#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
620 template <
typename T, T& Instance>
621 static ETL_CONSTEXPR14 TReturn operator_instance_stub(
void*, TParams... params)
630 template <TReturn(*Method)(TParams...)>
631 static ETL_CONSTEXPR14 TReturn function_stub(
void*, TParams... params)
639 template <
typename TLambda>
640 static ETL_CONSTEXPR14 TReturn lambda_stub(
void*
object, TParams... arg)
642 TLambda* p =
static_cast<TLambda*
>(object);
649 template <
typename TLambda>
650 static ETL_CONSTEXPR14 TReturn const_lambda_stub(
void*
object, TParams... arg)
652 const TLambda* p =
static_cast<const TLambda*
>(object);
659 invocation_element invocation;
666 template <auto Function>
668 constexpr auto make_delegate() ETL_NOEXCEPT
678 template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value,
void>>
680 constexpr auto make_delegate(TLambda& instance) ETL_NOEXCEPT
682 using function_type =
typename etl::function_traits<
decltype(&TLambda::operator())>::function_type;
690 template <
typename T, T& Instance>
692 constexpr auto make_delegate() ETL_NOEXCEPT
702 template <
typename T, auto Method, T& Instance,
typename = etl::enable_if_t<!etl::function_traits<decltype(Method)>::is_const>>
704 constexpr auto make_delegate() ETL_NOEXCEPT
714 template <
typename T, auto Method, const T& Instance,
typename = etl::enable_if_t<etl::function_traits<decltype(Method)>::is_const>>
716 constexpr auto make_delegate() ETL_NOEXCEPT
726 template <
typename T, auto Method>
728 constexpr auto make_delegate(T& instance) ETL_NOEXCEPT
738 template <
typename T, auto Method>
740 constexpr auto make_delegate(
const T& instance) ETL_NOEXCEPT
ETL_CONSTEXPR14 void set()
Set from function (Compile time).
Definition delegate_cpp11.h:281
static ETL_NODISCARD ETL_CONSTEXPR14 delegate create(const TLambda &instance)
Create from const Lambda or Functor.
Definition delegate_cpp11.h:183
static ETL_NODISCARD ETL_CONSTEXPR14 delegate create()
Definition delegate_cpp11.h:271
ETL_CONSTEXPR14 etl::enable_if_t< etl::is_same< TRet, void >::value, bool > call_if(TParams... args) const
Definition delegate_cpp11.h:385
ETL_CONSTEXPR14 void set(const TLambda &instance)
Set from const Lambda or Functor.
Definition delegate_cpp11.h:299
ETL_CONSTEXPR14 void set(TLambda &instance)
Set from Lambda or Functor.
Definition delegate_cpp11.h:290
ETL_CONSTEXPR14 delegate()
Default constructor.
Definition delegate_cpp11.h:125
TReturn operator()(TParams... args) const
Execute the delegate.
Definition delegate_cpp11.h:371
ETL_CONSTEXPR14 void clear()
Clear the delegate.
Definition delegate_cpp11.h:363
TReturn call_or(TAlternative alternative, TParams... args) const
Definition delegate_cpp11.h:422
TReturn call_or(TParams... args) const
Definition delegate_cpp11.h:439
static ETL_NODISCARD ETL_CONSTEXPR14 delegate create(TLambda &instance)
Create from Lambda or Functor.
Definition delegate_cpp11.h:173
ETL_CONSTEXPR14 etl::enable_if_t<!etl::is_same< TRet, void >::value, etl::optional< TReturn > > call_if(TParams... args) const
Definition delegate_cpp11.h:405
ETL_NODISCARD ETL_CONSTEXPR14 bool is_valid() const
Returns true if the delegate is valid.
Definition delegate_cpp11.h:497
static ETL_NODISCARD ETL_CONSTEXPR14 delegate create()
Create from function (Compile time).
Definition delegate_cpp11.h:163
The exception thrown when the delegate is uninitialised.
Definition delegate_cpp03.h:162
Declaration.
Definition delegate_cpp03.h:191
A templated set implementation that uses a fixed size buffer.
Definition set.h:2548
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
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
is_same
Definition type_traits_generator.h:1036
bitset_ext
Definition absolute.h:38
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition variant_pool_generator.h:348
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:654
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:642
Definition type_traits_generator.h:844
Definition delegate_cpp03.h:176
is_delegate
Definition delegate_cpp03.h:184
pair holds two objects of arbitrary type
Definition utility.h:164