MySQL 26.10.0
Source Code Documentation
ut::Function_reference< Return_type(Args...)> Class Template Reference

A non-owning reference to a callable - a free function, like sin, or the operator() of an object, together with pointer to that object, such a Less functor or a lambda. More...

#include <ut0function_reference.h>

Classes

union  context_t
 

Public Types

using signature_t = Return_type(Args...)
 
using ptr_t = Return_type(*)(const context_t &, Args...)
 
using rawfn_ptr_t = Return_type(*)(Args...)
 

Public Member Functions

template<typename Callable >
 Function_reference (Callable *callable)
 Creates a reference to the callable->operator(). More...
 
 Function_reference (rawfn_ptr_t fn_ptr)
 Creates a reference to a free function fn_ptr. More...
 
Return_type operator() (Args... args) const
 

Private Attributes

context_t m_context
 A crucial context for making a call to the referenced function. More...
 
ptr_t m_call_with_context
 How to call the referenced function given the m_context. More...
 

Detailed Description

template<typename Return_type, typename... Args>
class ut::Function_reference< Return_type(Args...)>

A non-owning reference to a callable - a free function, like sin, or the operator() of an object, together with pointer to that object, such a Less functor or a lambda.

It is the users responsibility to ensure that the lifetime of the callable referenced covers the lifetime of this Function_reference. This is the price to be paid for the benefit that this object has fixed size (just two pointers) and does not require dynamic allocation, which makes it useful for callbacks and type erasure, in places where std::function would be to expensive due to its need to copy the (variable size) object to dynamically allocated space.

Member Typedef Documentation

◆ ptr_t

template<typename Return_type , typename... Args>
using ut::Function_reference< Return_type(Args...)>::ptr_t = Return_type (*)(const context_t &, Args...)

◆ rawfn_ptr_t

template<typename Return_type , typename... Args>
using ut::Function_reference< Return_type(Args...)>::rawfn_ptr_t = Return_type (*)(Args...)

◆ signature_t

template<typename Return_type , typename... Args>
using ut::Function_reference< Return_type(Args...)>::signature_t = Return_type(Args...)

Constructor & Destructor Documentation

◆ Function_reference() [1/2]

template<typename Return_type , typename... Args>
template<typename Callable >
ut::Function_reference< Return_type(Args...)>::Function_reference ( Callable *  callable)
inline

Creates a reference to the callable->operator().

Parameters
[in]callableAn object with operator() which you want to reference. Note that the constness of callable impacts constness of the operator() which will be picked if there are two. For example a lambda. This object must have life-time covering the life-time of Function_reference.

◆ Function_reference() [2/2]

template<typename Return_type , typename... Args>
ut::Function_reference< Return_type(Args...)>::Function_reference ( rawfn_ptr_t  fn_ptr)
inline

Creates a reference to a free function fn_ptr.

Parameters
[in]fn_ptrA pointer to a free function which you want to reference

Member Function Documentation

◆ operator()()

template<typename Return_type , typename... Args>
Return_type ut::Function_reference< Return_type(Args...)>::operator() ( Args...  args) const
inline

Member Data Documentation

◆ m_call_with_context

template<typename Return_type , typename... Args>
ptr_t ut::Function_reference< Return_type(Args...)>::m_call_with_context
private

How to call the referenced function given the m_context.

In case of an object's method, that would be a state-less non-capturing lambda which just calls the m_context->operator() with provided args. In case of a free function this will simply call m_context with provided args. It is a non-capturing lambda, i.e. lambda which does not require "this" pointer to be called because it has no state - for such a lambda one can take its address and store it (in m_call_with_context) and subsequently use it to call it as a regular function, passing m_context as the first argument. This is a crucial trick: instead of storing the pointer to operator() as a pointer-to-member of Callable, which would require the Function_reference type to depend on Callable, we hide any mention of Callable inside the lambda.

◆ m_context

template<typename Return_type , typename... Args>
context_t ut::Function_reference< Return_type(Args...)>::m_context
private

A crucial context for making a call to the referenced function.

In case of an object's method, that would be the pointer to the object. In case of a free function, this is just the free function itself.


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