etl/docs/callbacks/function.md
John Wellbelove 4a88884b39
Issue/add hugo support for documentation (#1449)
* Add ranges

* Initial Hugo setup

* Work in progress

* Added selection for local or remote site

* Updated to 'light' theme

* Changed to using Hextra Hugo theme

* Changed to using Hextra Hugo theme

* Changed to Hextra Hugo theme

* Change to Hextra Hugo theme

* Updated Hugo setup.

* Updated Hugo setup.

# Conflicts:
#	docs/releases/_index.md

* Work in progress

* Added new fonts

Added new documentation

* Latest documentation updates

* Latest documentation updates

# Conflicts:
#	docs/containers/array.md
#	docs/containers/array_view.md
#	docs/containers/array_wrapper.md
#	docs/containers/bip_buffer_spsc_atomic.md
#	docs/containers/bitset.md
#	docs/containers/indirect_vector.md
#	docs/containers/vector.md
#	docs/getting-started/compilers.md

* Added bloom_filter markdown doc

* Added more documentation

Updated CSS for light and dark modes

* Fixed some menus

Added mode documentation files

* Updated CSS rules

Added badges to home page
Added uniqur_ptr + pool tutorial

* Fixed formatting on the home page markdown

Modified light amd dark code formatting

* Updated unique_ptr-with-pool

* Added container and shared message tutorials

* Updates to documentation

* Added const_multimap

* Updated source-formatting.md

* Added initial raw text files form Web site editor

* Innore coverage build directory

* Exported raw text documentation files from the web site editor

* Hugo updates

* Added Hugo intalation and markdown descriptions

* More addition to the documentation

* Added closure.md and updates to delegate.md

* Added format.md

* Added documentation for etl::delegate_observable, etl::function, Base64 codec

* Added io_port documentation

* Added basic_format_spec

* Added documentation for string_stream and string utilities.

* Added more documentation

Updated the documentation CSS

* Added documentation for clocks, day, duration

* Added more documentation for chrono classes

Updated callouts

* More chrono documentation

* Completed chrono documentation

* Maths functions documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Added multiple documentation files

* Added iterator.md

* Added debug_count.md and versions.md

* Added debug_count.md and versions.md

* Added more documentation

* More documentation

* Added some design pattern documentation

Modified some of the layout files
Modified the About documentation

* Converted more documentation pages

Modified the site CSS

* Added more documentation

Moced some documentation files to new directories

* Added more documentation

Tweaks to CSS

* Added callback_timer_deferred_locked documentation

* Added callback_timer_locked documentation

* More documentation updates

* More documentation updates

* More documentation updates

* New documentation files.

Harmonised file name format

* New documentation files.

* Multiple document updates

* Multiple document updates

* Final conversion of web pages

* Updates before PR

* Updates before PR

* Updates before PR

# Conflicts:
#	docs/blog/_index.md

* Final pre PR updates

* Updates to message framework documentation

* Renamed directory

* Fix spelling

* Added author and date to blog files

Moved documentation files merged from development

* Fixed 'Description' typo

* Fix typos

# Conflicts:
#	docs/IO/io_port.md
#	docs/containers/sets/const-multiset.md
#	docs/containers/sets/const-set.md
#	docs/maths/correlation.md
#	docs/maths/gamma.md

* Renamed two files to lower case

* Minor renaming

* Added author and date

* Updated callout on bresenham_line.md

Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset.
Updated the Python 'update_release.py' to copy 'version.txt'

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

# Conflicts:
#	docs/getting-started/view-the-docs-locally/hugo-commands.md

* Added a link to pseudo_moving_average.md

* Updated title pages for groups

* Fixed missing 404 for non-existent pages

* Fixed coordinate variable names in the 'Calculating the intersection' example

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2026-06-06 13:12:44 +01:00

9.3 KiB

title
function

{{< callout type="warning">}} Deprecated: Please use the more versatile etl::delegate or etl::inplace_function classes instead. {{< /callout >}}

{{< callout type="info">}} Header: function.h
Since: TBC
{{< /callout >}}

A set of wrapper templates to allow a member or static function to be called without the caller having to know the specific details of the callee apart from the parameter type. The templates allow the called function to be abstracted.

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

Template Description
etl::function Callbacks to free or member functions taking zero or one parameter.
Function pointer at runtime.
etl::function_fv Callback to a free function taking no parameters. (Functiion Void).
Function pointer at compile time.
etl::function_fp Callback to a free function taking one parameter. (Function Parameter).
Function pointer at compile time.
etl::function_mv Callback to a member function taking no parameters. (Member Void).
Function pointer at compile time.
etl::function_imv Callback to a member function taking no parameters. (Instance Member Void ).
Instance reference and function pointer at compile time.
etl::function_mp Callback to a member function taking one parameter. (Member Parameter).
Function pointer at compile time.
etl::function_imp Callback to a member function taking one parameter. (Instance Member Parameter).
Instance reference and function pointer at compile time.

Instances of any of these types may be passed as pointers or references to etl::ifunction.

Interface classes.

template <typename TParameter>
class ifunction

Description
Interface class for a function taking one parameter.


template <>
class ifunction<void>

Description
Interface class for a function taking no parameters.

All of the following classes are derived from the above interface classes.

Function address at run time.

The functions are called indirectly through an internal pointer.

template <typename TObject, 
          typename TParameter>
class function : public ifunction<TParameter>

Description
Class for a member function taking one parameter.


template <typename TObject>
class function<TObject, void> : public ifunction<void>

Description
Class for a member function taking no parameters.

template <typename TParameter>
class function<void, TParameter> : public ifunction<TParameter>

Description
Class for a free function taking one parameter.


template <>
class function<void, void> : public ifunction<void>

Description
Class for a free function taking no parameters.

Function address at compile time.

These will be more efficient than the previous definitions, as the one level of indirection is eliminated.

template <typename TObject, 
          void (TObject::*Function)()>
class function_mv : public ifunction<void>

Description
Class for a member function taking no parameters.


template <typename TObject, 
          TObject& Instance, 
          void (TObject::*Function)()>
class function_imv : public ifunction<void>

Description
Class for a member function taking no parameters.


template <typename TObject, 
          typename TParameter, 
          void (TObject::*Function)(TParameter)>
class function_mp : public ifunction<TParameter>

Description
Class for a member function taking one parameter.


template <typename TObject, 
          typename TParameter, 
          TObject& Instance, 
          void (TObject::*Function)(TParameter)>
class function_imp : public ifunction<TParameter>

Description
Class for a member function taking one parameter.


template <void (*Function)()>
class function_fv : public ifunction<void>

Description
Class for a free function taking no parameters.


template <typename TParameter, 
          void (*Function)()>
class function_fp : public ifunction<TParameter>

Description
Class for a free function taking one parameter.

Example

etl::function

These templates are designed to enable easy creation of callbacks to global, static and class member functions without the caller having to know which type actually it is.

One use that is applicable to embedded platforms is to use them connect interrupt vectors to class member handling functions. Particularly useful when the code is applicable to multiple targets devices.

The template is designed in such a way that the caller does not need to be aware of the type of the callee.

The caller declares an instance of etl::function or one of its variants. The caller defines a pointer or reference to an etl::ifunction. This pointer or reference will be initialised with the instance defined in the caller.

Example

The interrupt vector table contains entries for three timer timeouts and two uart character rx handlers. These are vectored to normal static functions.

void Timer1TimeoutInterrupt();
void Timer2TimeoutInterrupt();
void Timer3TimeoutInterrupt();
void Uart1RxInterrupt();
void Uart2RxInterrupt();

The timer timeout interrupts are handled by an instance of the class Timer and the free function FreeTimerInterruptHandler. The UART Rx interrupts are handled by instances of the class Uart.

Timer1 interrupts call the member function of an instance of Timer. Timer2 interrupts call the static member function of Timer. Timer3 interrupts call the free function FreeTimerInterruptHandler. UART1 interrupts call the member function of instance 1 of Uart. UART1 interrupts call the member function of instance 2 of Uart.

#include <iostream>
#include <iomanip>

#include "function.h"

//********************************
// Fake UART Rx register.
//********************************
char get_char()
{
  static char c = 'A';
  return c++;
}

//********************************
// Interrupt vectors & callbacks. 
//********************************
// Callback interfaces.
// Note that they do not require any knowledge about the callee apart from the parameter type.
etl::ifunction<void>* timer1_callback;   // A pointer to a callback taking no parameters.
etl::ifunction<void>* timer2_callback;   // A pointer to a callback taking no parameters.
etl::ifunction<void>* timer3_callback;   // A pointer to a callback taking no parameters.
etl::ifunction<char>* uart1_rx_callback; // A pointer to a callback taking a char parameter.
etl::ifunction<char>* uart2_rx_callback; // A pointer to a callback taking a char parameter.

extern "C"
{
// Function called from the timer1 interrupt vector.
void Timer1Interrupt()
{
  (*timer1_callback)();
}

// Function called from the timer2 interrupt vector.
void Timer2Interrupt()
{
  (*timer2_callback)();
}

// Function called from the timer3 interrupt vector.
void Timer3Interrupt()
{
  (*timer3_callback)();
}

// Function called from the UART1 rx interrupt vector.
void Uart1RxInterrupt()
{
  (*uart1_rx_callback)(get_char());
}

// Function called from the UART2 rx interrupt vector.
void Uart2RxInterrupt()
{
  (*uart2_rx_callback)(get_char());
}
}

//********************************
// Timer driver.
//********************************
class Timer
{
public:

  // Constructor.
  Timer()
  {
  }

  // Handler for interrupts from the timer.
  void MemberTimerInterruptHandler()
  {
    std::cout << "Timer interrupt (member)\n";
  }

  // Static handler for interrupts from the timer.
  static void StaticTimerInterruptHandler()
  {
    std::cout << "Timer interrupt (static)\n";
  }
};

//********************************
// Free function timer driver.
//********************************
void FreeTimerInterruptHandler()
{
  std::cout << "Timer interrupt (free)\n";
}

etl::function_fv<FreeTimerInterruptHandler> free_callback;

//********************************
// UART driver.
//********************************
class Uart
{
public:

  // Constructor.
  Uart(int port_id)
    : port_id(port_id)
  {
  }

  // Handler for rx interrupts from the UART.
  void RxInterruptHandler(char c)
  {
    std::cout << "UART" << port_id << " Rx char interrupt : Received '" << c << "'\n";
  }

  int port_id;
};

// Declare the driver instances.
Timer timer;
Uart  uart1(0);
Uart  uart2(1);

etl::function_imv<Timer, timer, &Timer::MemberTimerInterruptHandler> timer_member_callback;
etl::function_fv<&Timer::StaticTimerInterruptHandler>                timer_static_callback;
etl::function_imp<Uart, char, uart1, &Uart::RxInterruptHandler>      uart1_callback;
etl::function_imp<Uart, char, uart2, &Uart::RxInterruptHandler>      uart2_callback;

//********************************
// Test it out.
//********************************
int main()
{

  // Setup the callbacks.
  timer1_callback   = &timer_member_callback;
  timer2_callback   = &timer_static_callback;
  timer3_callback   = &free_callback;
  uart1_rx_callback = &uart1_callback;
  uart2_rx_callback = &uart2_callback;

  // Simulate the interrupts.
  Timer1Interrupt();
  Timer2Interrupt();
  Timer3Interrupt();
  Uart1RxInterrupt();
  Uart2RxInterrupt();

  return 0;
}