A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

etl::function

The etl::function, etl::function_fv, etl::function_fp, etl::function_mv and etl::function_mp templates are
designed to enable easy creation of callbacks to global, static and class member functions.

etl::function    Callbacks to free or member functions taking zero or one parameter. Function pointer at runtime.
etl::function_fv Callback to free function taking no parameters. Function pointer at compile time.
etl::function_fp Callback to free functions taking one parameter. Function pointer at compile time.
etl::function_mv Callback to member function taking no parameters. Function pointer at compile time.
etl::function_fp Callback to member function taking one parameter. Function pointer at compile time.

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.

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

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

Example:-

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

void TimerTimeoutInterrupt();
void Uart1RxInterrupt();
void Uart2RxInterrupt();

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

#include <iostream>
#include <iomanip>

#include "function.h"

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

//********************************
// Interrupt vectors & callbacks.
//********************************
namespace Vectors
{
  // Callback interfaces.
  // Note that they do not require any knowledge about the callee apart from the parameter type.
  etl::ifunction<void>* timer_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.

  // Function called from the timer interrupt vector.
  void TimerInterrupt()
  {
    (*timer_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()
    : callback(*this)
  {
  }

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

  etl::function_mv<Timer, &Timer::TimerInterruptHandler> callback;
};

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

  // Constructor.
  Uart(int port_id)
    : callback(*this),
      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";
  }

  etl::function_mp<Uart, char, &Uart::RxInterruptHandler> callback;
  int port_id;
};

//********************************
// Test it out.
//********************************
int main()
{
  // Declare the driver instances.
  Timer timer;
  Uart  uart1(0);
  Uart  uart2(1);

  // Setup the callbacks.
  Vectors::timer_callback    = &timer.callback;
  Vectors::uart1_rx_callback = &uart1.callback;
  Vectors::uart2_rx_callback = &uart2.callback;

  // Simulate the interrupts.
  Vectors::TimerInterrupt();
  Vectors::Uart1RxInterrupt();
  Vectors::Uart2RxInterrupt();

  return 0;
}