A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd
MIT licensed
Join the ETL community

delegate


A set of templates to allow a member function, static function (member or free), functor or lambda to be called without
the caller having to know the specific details of the callee apart from the return and parameter type. The templates
allow the called function to be abstracted. Uses variadic templates to enable variable number of function parameters.

The delegate functions may be defined at compile time and/or runtime, depending on the function type.
Most delegates can only be constructed via a create function, except lambdas and functors, which can be created by
the delegate constructor.

This may be used to implement a platform abstraction layer that allows an embedded application to interface with
multiple hardware platforms.

Notes:
This class is not compatible with C++03 or earlier (sorry).

You cannot use lambdas that capture variables  that will have gone out of scope when the delegate is called. This would
result in  dangling references.

etl::delegate<TReturn(TParams…)>

For the examples below, assume the following definitions.

class Test
{
public:
 
  int member_function(int);
  static int member_static(int);
  int operator()(int);
  int operator()(int) const;
};

Test test;

int global(int);

auto lambda = [](int i){ return i; };

____________________________________________________________________________________________________

Run time construction


Lambdas

etl::delegate<int(int)> d(lamda);
etl::delegate<int(int)> d = lamda;
etl::delegate<int(int)> d = etl::delegate<int(int)>::create(lamda);

Functors

etl::delegate<int(int)> d(test);
etl::delegate<int(int)> d = test;
etl::delegate<int(int)> d = etl::delegate<int(int)>::create(test);

Member functions

etl::delegate<int(int)> d = etl::delegate<int(int)>::create<Test, &Test::member_function>(test);

____________________________________________________________________________________________________

Compile time construction


Global functions

etl::delegate<int(int)> d = etl::delegate<int(int)>::create<global>();


Member functions (if the instance is known at compile time)

etl::delegate<int(int)> d = etl::delegate<int(int)>::create<Test, test, &Test::member_function>();

Functors (if the instance is known at compile time)

etl::delegate<int(int)> d = etl::delegate<int(int)>::create<Test, test>();
etl::delegate<int(int)> d = etl::delegate<int(int)>::create<const Test, test>();
Note: These are disabled for GCC as they generate an 'Internal Compiler Error' in GCC v8.1

Static member functions

etl::delegate<int(int)> d = etl::delegate<int(int)>::create<Test::member_static>();

____________________________________________________________________________________________________

Calling the delegate


The delegate may be called as a function with the defined parameter signature.

etl::delegate<int(int)> d;

int r = d(3);

____________________________________________________________________________________________________

Operations


delegate()

Constructs an uninitialised delegate.
____________________________________________________________________________________________________

delegate(const delegate& other)

Copy constructs a delegate.
____________________________________________________________________________________________________


TReturn operator()(TParams... args) const


Executes the delegate.

____________________________________________________________________________________________________

bool is_valid() const
operator bool() const

Returns true if the delegate has been initialised, otherwise false.
____________________________________________________________________________________________________

operator =()

Assigns from another delegate or lambda.

____________________________________________________________________________________________________

operator ==()
operator !=()

Compares delegates.

delegate.h