tlx
Delegate< R(A...), Allocator > Class Template Reference

This is a faster replacement than std::function. More...

#include <delegate.hpp>

Classes

struct  IsConstMemberPair
 template for const class::function selector More...
 
struct  IsConstMemberPair< ConstMemberPair< C > >
 specialization for const class::function selector More...
 
struct  IsMemberPair
 template for class::function selector More...
 
struct  IsMemberPair< MemberPair< C > >
 specialization for class::function selector More...
 

Public Member Functions

 Delegate ()=default
 default constructor More...
 
 Delegate (const Delegate &)=default
 copy constructor More...
 
 Delegate (Delegate &&)=default
 move constructor More...
 
Delegateoperator= (const Delegate &)=default
 copy assignment operator More...
 
Delegateoperator= (Delegate &&)=default
 move assignment operator More...
 
Miscellaneous
void reset ()
 reset delegate to invalid. More...
 
void reset_caller () noexcept
 
void swap (Delegate &other) noexcept
 swap delegates More...
 
bool operator== (Delegate const &rhs) const noexcept
 compare delegate with another More...
 
bool operator!= (Delegate const &rhs) const noexcept
 compare delegate with another More...
 
bool operator< (Delegate const &rhs) const noexcept
 compare delegate with another More...
 
bool operator== (std::nullptr_t const) const noexcept
 compare delegate with another More...
 
bool operator!= (std::nullptr_t const) const noexcept
 compare delegate with another More...
 
 operator bool () const noexcept
 explicit conversion to bool -> valid or invalid. More...
 
operator() (A...args) const
 most important method: call. More...
 

Static Public Member Functions

Immediate Function Calls
template<R(*)(A...) Function>
static Delegate make () noexcept
 construction from an immediate function with no object or pointer. More...
 
Immediate Class::Method Calls with Objects
template<class C , R(C::*)(A...) Method>
static Delegate make (C *const object_ptr) noexcept
 construction for an immediate class::method with class object More...
 
template<class C , R(C::*)(A...) const Method>
static Delegate make (C const *const object_ptr) noexcept
 construction for an immediate class::method with class object More...
 
template<class C , R(C::*)(A...) Method>
static Delegate make (C &object) noexcept
 construction for an immediate class::method with class object by reference More...
 
template<class C , R(C::*)(A...) const Method>
static Delegate make (C const &object) noexcept
 construction for an immediate class::method with class object by reference More...
 

Private Types

using Caller = R(*)(void *, A &&...)
 type of the function caller pointer. More...
 
using Deleter = void(*)(void *)
 

Private Member Functions

 Delegate (const Caller &m, void *const obj) noexcept
 private constructor for plain More...
 

Static Private Member Functions

template<typename T >
static void store_deleter (void *const ptr)
 deleter for stored functor closures More...
 
Callers for simple function and immediate class::method calls.
template<R(*)(A...) Function>
static R function_caller (void *const, A &&...args)
 caller for an immediate function with no object or pointer. More...
 
static R function_ptr_caller (void *const object_ptr, A &&...args)
 caller for a plain function pointer. More...
 
template<class C , R(C::*)(A...) method_ptr>
static R method_caller (void *const object_ptr, A &&...args)
 function caller for immediate class::method function calls More...
 
template<class C , R(C::*)(A...) const method_ptr>
static R const_method_caller (void *const object_ptr, A &&...args)
 function caller for immediate const class::method functions calls. More...
 

Private Attributes

Caller caller_
 pointer to function caller which depends on the type in object_ptr_. More...
 
void * object_ptr_
 pointer to object held by the delegate: for plain function pointers it is the function pointer, for class::methods it is a pointer to the class instance, for functors it is a pointer to the shared_ptr store_ contents. More...
 
std::shared_ptr< void > store_
 shared_ptr used to contain a memory object containing the callable, like lambdas with closures, or our own wrappers. More...
 

Function Pointer Calls

 Delegate (R(*const function_ptr)(A...)) noexcept
 constructor from a plain function pointer with no object. More...
 
static Delegate make (R(*const function_ptr)(A...)) noexcept
 construction from a plain function pointer with no object. More...
 

Lambdas with Captures and Wrapped Class::Method Calls with Objects

template<typename T , typename = typename std::enable_if< !std::is_same<Delegate, typename std::decay<T>::type>::value >::type>
 Delegate (T &&f)
 constructor from any functor object T, which may be a lambda with capture or a MemberPair or ConstMemberPair wrapper. More...
 
template<class C >
 Delegate (C *const object_ptr, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object pointer. More...
 
template<class C >
 Delegate (C *const object_ptr, R(C::*const method_ptr)(A...) const)
 constructor for wrapping a const class::method with object pointer. More...
 
template<class C >
 Delegate (C &object, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object reference. More...
 
template<class C >
 Delegate (C const &object, R(C::*const method_ptr)(A...) const)
 constructor for wrapping a const class::method with object reference. More...
 
template<typename T >
static Delegate make (T &&f)
 constructor from any functor object T, which may be a lambda with capture or a MemberPair or ConstMemberPair wrapper. More...
 
template<class C >
static Delegate make (C *const object_ptr, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object pointer. More...
 
template<class C >
static Delegate make (C const *const object_ptr, R(C::*const method_ptr)(A...) const)
 constructor for wrapping a const class::method with object pointer. More...
 
template<class C >
static Delegate make (C &object, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object reference. More...
 
template<class C >
static Delegate make (C const &object, R(C::*const method_ptr)(A...) const)
 constructor for wrapping a const class::method with object reference. More...
 

Wrappers for indirect class::method calls.

template<class C >
using MemberPair = std::pair< C *const, R(C::*const)(A...)>
 wrappers for indirect class::method calls containing (object, method_ptr) More...
 
template<class C >
using ConstMemberPair = std::pair< C const *const, R(C::*const)(A...) const >
 wrappers for indirect const class::method calls containing (object, const method_ptr) More...
 
template<typename T >
static std::enable_if< !(IsMemberPair< T >::value||IsConstMemberPair< T >::value), R >::type functor_caller (void *const object_ptr, A &&...args)
 function caller for functor class. More...
 
template<typename T >
static std::enable_if< (IsMemberPair< T >::value||IsConstMemberPair< T >::value), R >::type functor_caller (void *const object_ptr, A &&...args)
 function caller for const functor class. More...
 

Detailed Description

template<typename R, typename... A, typename Allocator>
class tlx::Delegate< R(A...), Allocator >

This is a faster replacement than std::function.

Besides being faster and doing less allocations when used correctly, we use it in places where move-only lambda captures are necessary. std::function is required by the standard to be copy-constructible, and hence does not allow move-only lambda captures.

A Delegate contains a reference to any of the following callable objects:

  • an immediate function (called via one indirection)
  • a mutable function pointer (copied into the Delegate)
  • an immediate class::method call (called via one indirection)
  • a functor object (the whole object is copied into the Delegate)

All callable objects must have the signature ReturnType(Arguments ...). If a callable has this signature, it can be bound to the Delegate.

To implement all this the Delegate contains one pointer to a "caller stub" function, which depends on the contained object and can be an immediate function call, a pointer to the object associated with the callable, and a memory pointer (managed by shared_ptr) for holding larger callables that need to be copied.

A functor object can be a lambda function with its capture, an internally wrapped mutable class::method class stored as pair<object, method_ptr>, or any other old-school functor object.

Delegates can be constructed similar to std::function.

// in defining the Delegate we decide the ReturnType(Arguments ...) signature
using MyDelegate = Delegate<int(double)>;
// this is a plain function bound to the Delegate as a function pointer
int func(double a) { return a + 10; }
MyDelegate d1 = MyDelegate(func);
class AClass {
public:
int method(double d) { return d * d; }
};
AClass a;
// this is class::method bound to the Delegate via indirection, warning: this
// creates a needless allocation, because it is stored as pair<Class,Method>
MyDelegate d2 = MyDelegate(a, &AClass::method);
// same as above
MyDelegate d3 = MyDelegate::make(a, &AClass::method);
// class::method bound to the Delegate via instantiation of an immediate caller
// to the method AClass::method. this is preferred and does not require any
// memory allocation!
MyDelegate d4 = MyDelegate::make<AClass, &AClass::method>(a);
// a lambda with capture bound to the Delegate, this always performs a memory
// allocation to copy the capture closure.
double offset = 42.0;
MyDelegate d5 = [&](double a) { return a + offset; };

Definition at line 91 of file delegate.hpp.

Member Typedef Documentation

using Caller = R (*)(void*, A&& ...)
private

type of the function caller pointer.

Definition at line 309 of file delegate.hpp.

using ConstMemberPair = std::pair<C const* const, R(C::* const)(A...) const>
private

wrappers for indirect const class::method calls containing (object, const method_ptr)

Definition at line 386 of file delegate.hpp.

using Deleter = void (*)(void*)
private

Definition at line 311 of file delegate.hpp.

using MemberPair = std::pair<C* const, R(C::* const)(A...)>
private

wrappers for indirect class::method calls containing (object, method_ptr)

Definition at line 380 of file delegate.hpp.

Constructor & Destructor Documentation

Delegate ( )
default

default constructor

Delegate ( const Delegate< R(A...), Allocator > &  )
default

copy constructor

Delegate ( Delegate< R(A...), Allocator > &&  )
default

move constructor

Delegate ( R(*)(A...)  function_ptr)
inlineexplicitnoexcept

constructor from a plain function pointer with no object.

Definition at line 124 of file delegate.hpp.

Delegate ( T &&  f)
inline

constructor from any functor object T, which may be a lambda with capture or a MemberPair or ConstMemberPair wrapper.

Definition at line 182 of file delegate.hpp.

Delegate ( C *const  object_ptr,
R(C::*)(A...)  method_ptr 
)
inline

constructor for wrapping a class::method with object pointer.

Definition at line 211 of file delegate.hpp.

Delegate ( C *const  object_ptr,
R(C::*)(A...) const  method_ptr 
)
inline

constructor for wrapping a const class::method with object pointer.

Definition at line 216 of file delegate.hpp.

Delegate ( C &  object,
R(C::*)(A...)  method_ptr 
)
inline

constructor for wrapping a class::method with object reference.

Definition at line 221 of file delegate.hpp.

Delegate ( C const &  object,
R(C::*)(A...) const  method_ptr 
)
inline

constructor for wrapping a const class::method with object reference.

Definition at line 226 of file delegate.hpp.

Delegate ( const Caller m,
void *const  obj 
)
inlineprivatenoexcept

private constructor for plain

Definition at line 330 of file delegate.hpp.

Member Function Documentation

static R const_method_caller ( void *const  object_ptr,
A &&...  args 
)
inlinestaticprivate

function caller for immediate const class::method functions calls.

Definition at line 366 of file delegate.hpp.

static R function_caller ( void *  const,
A &&...  args 
)
inlinestaticprivate

caller for an immediate function with no object or pointer.

Definition at line 348 of file delegate.hpp.

static R function_ptr_caller ( void *const  object_ptr,
A &&...  args 
)
inlinestaticprivate

caller for a plain function pointer.

Definition at line 353 of file delegate.hpp.

static std::enable_if< !(IsMemberPair<T>::value || IsConstMemberPair<T>::value), R >::type functor_caller ( void *const  object_ptr,
A &&...  args 
)
inlinestaticprivate

function caller for functor class.

Definition at line 409 of file delegate.hpp.

static std::enable_if< (IsMemberPair<T>::value || IsConstMemberPair<T>::value), R >::type functor_caller ( void *const  object_ptr,
A &&...  args 
)
inlinestaticprivate

function caller for const functor class.

Definition at line 418 of file delegate.hpp.

static Delegate make ( )
inlinestaticnoexcept

construction from an immediate function with no object or pointer.

Definition at line 114 of file delegate.hpp.

static Delegate make ( R(*)(A...)  function_ptr)
inlinestaticnoexcept

construction from a plain function pointer with no object.

Definition at line 132 of file delegate.hpp.

static Delegate make ( C *const  object_ptr)
inlinestaticnoexcept

construction for an immediate class::method with class object

Definition at line 143 of file delegate.hpp.

static Delegate make ( C const *const  object_ptr)
inlinestaticnoexcept

construction for an immediate class::method with class object

Definition at line 149 of file delegate.hpp.

static Delegate make ( C &  object)
inlinestaticnoexcept

construction for an immediate class::method with class object by reference

Definition at line 157 of file delegate.hpp.

static Delegate make ( C const &  object)
inlinestaticnoexcept

construction for an immediate class::method with class object by reference

Definition at line 164 of file delegate.hpp.

static Delegate make ( T &&  f)
inlinestatic

constructor from any functor object T, which may be a lambda with capture or a MemberPair or ConstMemberPair wrapper.

Definition at line 205 of file delegate.hpp.

static Delegate make ( C *const  object_ptr,
R(C::*)(A...)  method_ptr 
)
inlinestatic

constructor for wrapping a class::method with object pointer.

Definition at line 231 of file delegate.hpp.

static Delegate make ( C const *const  object_ptr,
R(C::*)(A...) const  method_ptr 
)
inlinestatic

constructor for wrapping a const class::method with object pointer.

Definition at line 238 of file delegate.hpp.

static Delegate make ( C &  object,
R(C::*)(A...)  method_ptr 
)
inlinestatic

constructor for wrapping a class::method with object reference.

Definition at line 245 of file delegate.hpp.

static Delegate make ( C const &  object,
R(C::*)(A...) const  method_ptr 
)
inlinestatic

constructor for wrapping a const class::method with object reference.

Definition at line 251 of file delegate.hpp.

static R method_caller ( void *const  object_ptr,
A &&...  args 
)
inlinestaticprivate

function caller for immediate class::method function calls

Definition at line 359 of file delegate.hpp.

operator bool ( ) const
inlineexplicitnoexcept

explicit conversion to bool -> valid or invalid.

Definition at line 296 of file delegate.hpp.

bool operator!= ( Delegate< R(A...), Allocator > const &  rhs) const
inlinenoexcept

compare delegate with another

Definition at line 275 of file delegate.hpp.

bool operator!= ( std::nullptr_t  const) const
inlinenoexcept

compare delegate with another

Definition at line 291 of file delegate.hpp.

R operator() ( A...  args) const
inline

most important method: call.

The call is forwarded to the selected function caller.

Definition at line 300 of file delegate.hpp.

bool operator< ( Delegate< R(A...), Allocator > const &  rhs) const
inlinenoexcept

compare delegate with another

Definition at line 280 of file delegate.hpp.

Delegate& operator= ( const Delegate< R(A...), Allocator > &  )
default

copy assignment operator

Delegate& operator= ( Delegate< R(A...), Allocator > &&  )
default

move assignment operator

bool operator== ( Delegate< R(A...), Allocator > const &  rhs) const
inlinenoexcept

compare delegate with another

Definition at line 270 of file delegate.hpp.

bool operator== ( std::nullptr_t  const) const
inlinenoexcept

compare delegate with another

Definition at line 286 of file delegate.hpp.

void reset ( )
inline

reset delegate to invalid.

Definition at line 262 of file delegate.hpp.

void reset_caller ( )
inlinenoexcept

Definition at line 264 of file delegate.hpp.

static void store_deleter ( void *const  ptr)
inlinestaticprivate

deleter for stored functor closures

Definition at line 335 of file delegate.hpp.

void swap ( Delegate< R(A...), Allocator > &  other)
inlinenoexcept

swap delegates

Definition at line 267 of file delegate.hpp.

Member Data Documentation

Caller caller_
private

pointer to function caller which depends on the type in object_ptr_.

The caller_ contains a plain pointer to either function_caller, a function_ptr_caller, a method_caller, a const_method_caller, or a functor_caller.

Definition at line 317 of file delegate.hpp.

void* object_ptr_
private

pointer to object held by the delegate: for plain function pointers it is the function pointer, for class::methods it is a pointer to the class instance, for functors it is a pointer to the shared_ptr store_ contents.

Definition at line 323 of file delegate.hpp.

std::shared_ptr<void> store_
private

shared_ptr used to contain a memory object containing the callable, like lambdas with closures, or our own wrappers.

Definition at line 327 of file delegate.hpp.


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