Mbed OS Reference
Loading...
Searching...
No Matches
FunctionPointerWithContext< ContextType > Class Template Reference

Function like object adapter over freestanding and member functions. More...

#include <FunctionPointerWithContext.h>

Inheritance diagram for FunctionPointerWithContext< ContextType >:
SafeBool< FunctionPointerWithContext< ContextType > > base

Public Member Functions

 FunctionPointerWithContext (void(*function)(ContextType context)=NULL)
 Create a FunctionPointerWithContext from a pointer to a freestanding function. More...
 
template<typename T >
 FunctionPointerWithContext (T *object, void(T::*member)(ContextType context))
 Create a FunctionPointerWithContext from a pointer to a member function and the instance which is used to call it. More...
 
 FunctionPointerWithContext (const FunctionPointerWithContext &that)
 Copy construction. More...
 
FunctionPointerWithContextoperator= (const FunctionPointerWithContext &that)
 Copy assignment. More...
 
void attach (void(*function)(ContextType context)=NULL)
 Adapt a freestanding function. More...
 
template<typename T >
void attach (T *object, void(T::*member)(ContextType context))
 Adapt a pointer to member function and the instance to use to call it. More...
 
void call (ContextType context) const
 Call the adapted function and functions chained to the instance. More...
 
void call (ContextType context)
 Call the adapted function and functions chained to the instance. More...
 
void operator() (ContextType context) const
 Call the adapted function and functions chained to the instance. More...
 
bool toBool () const
 Indicate if a callable object is being adapted. More...
 
void chainAsNext (pFunctionPointerWithContext_t next)
 Set a FunctionPointer instance as the next element in the chain of callable objects. More...
 
pFunctionPointerWithContext_t getNext () const
 Access the next element in the call chain. More...
 
pvoidfcontext_t get_function () const
 Access the next element in the call chain. More...
 
 operator BoolType_t () const
 Bool operator implementation, derived class must provide a bool toBool() const function. More...
 

Protected Types

typedef void(base::* BoolType_t) () const
 The bool type is a pointer to method that can be used in boolean context. More...
 

Protected Member Functions

void invalidTag () const
 Nonimplemented call, use to disallow conversion between unrelated types. More...
 
void trueTag () const
 Special member function that indicates a true value. More...
 

Detailed Description

template<typename ContextType>
class FunctionPointerWithContext< ContextType >

Function like object adapter over freestanding and member functions.

Freestanding and member functions are two distinct types in C++. One is not convertible into the other, and the call syntax between the two is different even if conceptually they are similar: Both primitives can be copied, called and produce a result.

To solve incompatibilities, this class adapts freestanding and member functions to a common interface. The interface chosen is similar to the freestanding function pointers interface:

  • Copyable.
  • Nullable.
  • Callable.

This class also offers a mechanism to chain other instances to it. When an instance is called, all the instances being part of the chain are called.

Attention
freestanding or member function adapted must accept a single argument, and this argument is a pointer to ContextType. Adapted primitives do not return anything.
Template Parameters
ContextTypeType of the argument pointee.

Definition at line 58 of file common/FunctionPointerWithContext.h.

Member Typedef Documentation

◆ BoolType_t

typedef void(base::* BoolType_t) () const
protectedinherited

The bool type is a pointer to method that can be used in boolean context.

Definition at line 50 of file common/SafeBool.h.

Constructor & Destructor Documentation

◆ FunctionPointerWithContext() [1/3]

FunctionPointerWithContext ( void(*)(ContextType context)  function = NULL)

Create a FunctionPointerWithContext from a pointer to a freestanding function.

Parameters
[in]functionThe freestanding function to attach.

Definition at line 70 of file common/FunctionPointerWithContext.h.

◆ FunctionPointerWithContext() [2/3]

FunctionPointerWithContext ( T *  object,
void(T::*)(ContextType context)  member 
)

Create a FunctionPointerWithContext from a pointer to a member function and the instance which is used to call it.

Parameters
[in]objectPointer to the instance which is used to invoke member.
[in]memberPointer to the member function to adapt.

Definition at line 85 of file common/FunctionPointerWithContext.h.

◆ FunctionPointerWithContext() [3/3]

FunctionPointerWithContext ( const FunctionPointerWithContext< ContextType > &  that)

Copy construction.

Parameters
[in]thatThe FunctionPointerWithContext instance used to create this.

Definition at line 97 of file common/FunctionPointerWithContext.h.

Member Function Documentation

◆ operator=()

FunctionPointerWithContext & operator= ( const FunctionPointerWithContext< ContextType > &  that)

Copy assignment.

Parameters
[in]thatThe FunctionPointerWithContext instance copied into this.

Definition at line 107 of file common/FunctionPointerWithContext.h.

◆ attach() [1/2]

void attach ( void(*)(ContextType context)  function = NULL)

Adapt a freestanding function.

Previous content adapted is discarded while function replaces it.

Note
This function is equivalent to a call to the copy assignment operator.
Parameters
[in]functionThe freestanding function to attach.

Definition at line 129 of file common/FunctionPointerWithContext.h.

◆ attach() [2/2]

void attach ( T *  object,
void(T::*)(ContextType context)  member 
)

Adapt a pointer to member function and the instance to use to call it.

Previous content adapted is discarded while the adaptation of the pair object and member replaces it.

Note
This function is equivalent to a call to the copy assignment operator.
Parameters
[in]objectPointer to the instance is used to invoke member.
[in]memberPointer to the member function to adapt.

Definition at line 148 of file common/FunctionPointerWithContext.h.

◆ call() [1/2]

void call ( ContextType  context) const

Call the adapted function and functions chained to the instance.

Parameters
[in]contextparameter to pass to chain of adapted functions.

Definition at line 164 of file common/FunctionPointerWithContext.h.

◆ call() [2/2]

void call ( ContextType  context)

Call the adapted function and functions chained to the instance.

Parameters
[in]contextparameter to pass to chain of adapted functions.

Definition at line 174 of file common/FunctionPointerWithContext.h.

◆ operator()()

void operator() ( ContextType  context) const

Call the adapted function and functions chained to the instance.

Parameters
[in]contextparameter to pass to chain of adapted functions.

Definition at line 184 of file common/FunctionPointerWithContext.h.

◆ toBool()

bool toBool ( ) const

Indicate if a callable object is being adapted.

Note
implementation of safe bool operator.
Returns
true if the content of the instance can be invoked and false otherwise.

Definition at line 199 of file common/FunctionPointerWithContext.h.

◆ chainAsNext()

void chainAsNext ( pFunctionPointerWithContext_t  next)

Set a FunctionPointer instance as the next element in the chain of callable objects.

Note
Invoking call() on the head FunctionPointer invokes all chained callbacks.
Refer to CallChainOfFunctionPointerWithContext as an alternative.
Parameters
nextThe instance to set as the next element in the chain of callable objects.

Definition at line 216 of file common/FunctionPointerWithContext.h.

◆ getNext()

pFunctionPointerWithContext_t getNext ( ) const

Access the next element in the call chain.

If there is no next element in the chain, this function returns NULL.

Returns
A pointer to the next FunctionPointerWithContext instance in the chain.

Definition at line 229 of file common/FunctionPointerWithContext.h.

◆ get_function()

pvoidfcontext_t get_function ( ) const

Access the next element in the call chain.

If there is no next element in the chain, this function returns NULL.

Returns
A pointer to the next FunctionPointerWithContext instance in the chain.

Definition at line 242 of file common/FunctionPointerWithContext.h.

◆ operator BoolType_t()

operator BoolType_t ( ) const
inherited

Bool operator implementation, derived class must provide a bool toBool() const function.

Definition at line 119 of file common/SafeBool.h.

◆ invalidTag()

void invalidTag ( ) const
protectedinherited

Nonimplemented call, use to disallow conversion between unrelated types.

◆ trueTag()

void trueTag ( ) const
protectedinherited

Special member function that indicates a true value.

Definition at line 60 of file common/SafeBool.h.

Field Documentation

◆ _function

pvoidfcontext_t _function

Static function pointer - NULL if none attached.

Definition at line 302 of file common/FunctionPointerWithContext.h.

◆ _memberFunctionAndPointer

MemberFunctionAndPtr _memberFunctionAndPointer
mutable

object this pointer and pointer to member - _memberFunctionAndPointer._object will be NULL if none attached

Definition at line 307 of file common/FunctionPointerWithContext.h.