iceoryx_hoofs 2.0.3
Public Member Functions | List of all members
iox::cxx::function_ref< ReturnType(ArgTypes...)> Class Template Reference

cxx::function_ref is a non-owning reference to a callable. More...

#include <iceoryx_hoofs/cxx/function_ref.hpp>

Public Member Functions

 function_ref () noexcept
 Creates an empty function_ref in an invalid state. More...
 
 function_ref (const function_ref &) noexcept=default
 
function_refoperator= (const function_ref &) noexcept=default
 
template<typename CallableType , typename = std::enable_if_t<!is_function_pointer<CallableType>::value && !has_same_decayed_type<CallableType, function_ref>::value && is_invocable<CallableType, ArgTypes...>::value>>
 function_ref (CallableType &&callable) noexcept
 Creates a function_ref with a callable whose lifetime has to be longer than function_ref. More...
 
 function_ref (ReturnType(*function)(ArgTypes...)) noexcept
 Creates a function_ref from a function pointer. More...
 
 function_ref (function_ref &&rhs) noexcept
 
function_refoperator= (function_ref &&rhs) noexcept
 
ReturnType operator() (ArgTypes... args) const noexcept
 Calls the provided callable. More...
 
 operator bool () const noexcept
 Checks whether a valid target is contained. More...
 
void swap (function_ref &rhs) noexcept
 Swaps the contents of two function_ref's. More...
 

Detailed Description

template<class ReturnType, class... ArgTypes>
class iox::cxx::function_ref< ReturnType(ArgTypes...)>

cxx::function_ref is a non-owning reference to a callable.

   It has these features:
    * No heap usage
    * No exceptions
    * Stateful lambda support
    * C++11/14 support
Attention
Invoking an empty function_ref can lead to a program termination!
// Usage as function parameter
void fuu(cxx::function_ref<void()> callback)
{
callback();
}
// Call the lambda
fuu([]{ doSomething(); });
// Usage with l-values
// Pitfall: Ensure that lifetime of callable suits the point in time of calling callback()
auto callable = [&]{ doSomething(); };
cxx::function_ref<void()> callback(callable);
// Call the callback
callback();
Definition: function_ref.hpp:34

Constructor & Destructor Documentation

◆ function_ref() [1/3]

template<class ReturnType , class... ArgTypes>
iox::cxx::function_ref< ReturnType(ArgTypes...)>::function_ref ( )
noexcept

Creates an empty function_ref in an invalid state.

Note
Handle with care, program will terminate when calling an invalid function_ref

◆ function_ref() [2/3]

template<class ReturnType , class... ArgTypes>
template<typename CallableType , typename = std::enable_if_t<!is_function_pointer<CallableType>::value && !has_same_decayed_type<CallableType, function_ref>::value && is_invocable<CallableType, ArgTypes...>::value>>
iox::cxx::function_ref< ReturnType(ArgTypes...)>::function_ref ( CallableType &&  callable)
noexcept

Creates a function_ref with a callable whose lifetime has to be longer than function_ref.

Parameters
[in]callablethat is not a function_ref

◆ function_ref() [3/3]

template<class ReturnType , class... ArgTypes>
iox::cxx::function_ref< ReturnType(ArgTypes...)>::function_ref ( ReturnType(*)(ArgTypes...)  function)
noexcept

Creates a function_ref from a function pointer.

Parameters
[in]functionfunction pointer to function we want to reference
Note
This overload is needed, as the general implementation will not work properly for function pointers. This ctor is not needed anymore once we can use user-defined-deduction guides (C++17)

Member Function Documentation

◆ operator bool()

template<class ReturnType , class... ArgTypes>
iox::cxx::function_ref< ReturnType(ArgTypes...)>::operator bool ( ) const
explicitnoexcept

Checks whether a valid target is contained.

Returns
True if valid target is contained, otherwise false

◆ operator()()

template<class ReturnType , class... ArgTypes>
ReturnType iox::cxx::function_ref< ReturnType(ArgTypes...)>::operator() ( ArgTypes...  args) const
noexcept

Calls the provided callable.

Parameters
[in]Argumentsare forwarded to the underlying function pointer
Returns
Returns the data type of the underlying function pointer
Attention
Invoking an empty function_ref can lead to a program termination!

◆ swap()

template<class ReturnType , class... ArgTypes>
void iox::cxx::function_ref< ReturnType(ArgTypes...)>::swap ( function_ref< ReturnType(ArgTypes...)> &  rhs)
noexcept

Swaps the contents of two function_ref's.

Parameters
[in]Referenceto another function_ref

The documentation for this class was generated from the following file: