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

Delegate Service

This template class allows easier integration of 'C' style events (such as interrupt vectors) and C++ handlers.
It can allow abstraction between low level events such as interrupts and their application dependent handlers.
The handlers may be any combination of global, static, member functions, lambdas and functors.
It utilises the templated function wrapper. See here.

The delegate callbacks are identified by an id. The values of the ids must range from zero or a specified offset, up to the
maximum range of specified delegates. Calling an unused delegate id will either do nothing or, if the user has specified a
handler, call this with the id of the delegate.

There are functions that use both runtime and compile time checks of the delegate id.
Compile time is preferable.
____________________________________________________________________________________________________

Member functions


template <const size_t RANGE, const size_t OFFSET = 0U>
etl::delegate_service

RANGE  The id range of the delegates (last - first + 1).
OFFSET The starting id for the range.
____________________________________________________________________________________________________
delegate_service()

Sets all of the delegates to route to the unhandled delegate.
Sets the unhandled delegate to default (do nothing).
____________________________________________________________________________________________________
template <const size_t ID>
void register_delegate(etl::delegate<void(size_t)>& callback)

Registers delegate with the id specified in the template parameter.
A compile time error will occur if the id is out of range.
____________________________________________________________________________________________________
void register_delegate(size_t id, etl::delegate<void(size_t)>& callback)

Registers delegate with the id specified in the template parameter.
The registration will be ignored if the id is out of range.
____________________________________________________________________________________________________
void register_unhandled_delegate(etl::delegate<void(size_t)>& callback)

Registers the delegate to be used for unhandled ids.
____________________________________________________________________________________________________
template <const size_t ID>
void call()

Calls the delegate associated with the id.
Calls the unhandled delegate if the id has not been registered.
A compile time error will occur if the id is out of range.
____________________________________________________________________________________________________
void call(const size_t id)

Calls the delegate associated with the id.
Calls the unhandled delegate if the id has not been registered or if is out of range.

____________________________________________________________________________________________________

Example


Tutorial

See the example project in etl\examples\FunctionInterruptSimulation-Delegates
delegate_service.h