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

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.
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
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
template<class C, R(C::*)(A...) Method>
static Delegate make (C &object) noexcept
 construction for an immediate class::method with class object by reference
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

Private Types

using Caller
 type of the function caller pointer.
using Deleter

Private Member Functions

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

Static Private Member Functions

template<typename T>
static void store_deleter (void *const ptr)
 deleter for stored functor closures
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.
static R function_ptr_caller (void *const object_ptr, A &&... args)
 caller for a plain function pointer.
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
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.

Private Attributes

Caller caller_
 pointer to function caller which depends on the type in object_ptr_.
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.
std::shared_ptr< void > store_
 shared_ptr used to contain a memory object containing the callable, like lambdas with closures, or our own wrappers.

Wrappers for indirect class::method calls.

template<class C>
using MemberPair
 wrappers for indirect class::method calls containing (object, method_ptr)
template<class C>
using ConstMemberPair
 wrappers for indirect const class::method calls containing (object, const method_ptr)
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.
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.

Function Pointer Calls

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

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.
template<class C>
 Delegate (C *const object_ptr, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object pointer.
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.
template<class C>
 Delegate (C &object, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object reference.
template<class C>
 Delegate (C const &object, R(C::*const method_ptr)(A...) const)
 constructor for wrapping a const class::method with object reference.
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.
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.
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.
template<class C>
static Delegate make (C &object, R(C::*const method_ptr)(A...))
 constructor for wrapping a class::method with object reference.
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.

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; }
};
// this is class::method bound to the Delegate via indirection, warning: this
// creates a needless allocation, because it is stored as pair<Class,Method>
// same as above
// 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!
// 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; };
Delegate()=default
default constructor

Definition at line 91 of file delegate.hpp.

Member Typedef Documentation

◆ Caller

template<typename R, typename... A, typename Allocator>
using Caller
private

type of the function caller pointer.

Definition at line 309 of file delegate.hpp.

◆ ConstMemberPair

template<typename R, typename... A, typename Allocator>
template<class C>
using ConstMemberPair
private

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

Definition at line 385 of file delegate.hpp.

◆ Deleter

template<typename R, typename... A, typename Allocator>
using Deleter
private

Definition at line 311 of file delegate.hpp.

◆ MemberPair

template<typename R, typename... A, typename Allocator>
template<class C>
using MemberPair
private

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

Definition at line 379 of file delegate.hpp.

Constructor & Destructor Documentation

◆ Delegate() [1/10]

template<typename R, typename... A, typename Allocator>
Delegate ( )
default

default constructor

◆ Delegate() [2/10]

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

copy constructor

◆ Delegate() [3/10]

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

move constructor

◆ Delegate() [4/10]

template<typename R, typename... A, typename Allocator>
Delegate ( R(* function_ptr )(A...))
inlineexplicitnoexcept

constructor from a plain function pointer with no object.

Definition at line 124 of file delegate.hpp.

◆ Delegate() [5/10]

template<typename R, typename... A, typename Allocator>
template<typename T, typename = typename std::enable_if< !std::is_same<Delegate, typename std::decay<T>::type>::value >::type>
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() [6/10]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate ( C *const object_ptr,
R(C::* method_ptr )(A...) )
inline

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

Definition at line 211 of file delegate.hpp.

◆ Delegate() [7/10]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate ( C *const object_ptr,
R(C::* method_ptr )(A...) const )
inline

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

Definition at line 216 of file delegate.hpp.

◆ Delegate() [8/10]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate ( C & object,
R(C::* method_ptr )(A...) )
inline

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

Definition at line 221 of file delegate.hpp.

◆ Delegate() [9/10]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate ( C const & object,
R(C::* method_ptr )(A...) const )
inline

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

Definition at line 226 of file delegate.hpp.

◆ Delegate() [10/10]

template<typename R, typename... A, typename Allocator>
Delegate ( const Caller & m,
void *const obj )
inlineprivatenoexcept

private constructor for plain

Definition at line 330 of file delegate.hpp.

Member Function Documentation

◆ const_method_caller()

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) const method_ptr>
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.

◆ function_caller()

template<typename R, typename... A, typename Allocator>
template<R(*)(A...) Function>
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.

◆ function_ptr_caller()

template<typename R, typename... A, typename Allocator>
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.

◆ functor_caller() [1/2]

template<typename R, typename... A, typename Allocator>
template<typename T>
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.

◆ functor_caller() [2/2]

template<typename R, typename... A, typename Allocator>
template<typename T>
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.

◆ make() [1/11]

template<typename R, typename... A, typename Allocator>
template<R(*)(A...) Function>
Delegate make ( )
inlinestaticnoexcept

construction from an immediate function with no object or pointer.

Definition at line 114 of file delegate.hpp.

◆ make() [2/11]

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) Method>
Delegate make ( C & object)
inlinestaticnoexcept

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

Definition at line 157 of file delegate.hpp.

◆ make() [3/11]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate make ( C & object,
R(C::* method_ptr )(A...) )
inlinestatic

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

Definition at line 245 of file delegate.hpp.

◆ make() [4/11]

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) Method>
Delegate make ( C *const object_ptr)
inlinestaticnoexcept

construction for an immediate class::method with class object

Definition at line 143 of file delegate.hpp.

◆ make() [5/11]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate make ( C *const object_ptr,
R(C::* method_ptr )(A...) )
inlinestatic

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

Definition at line 231 of file delegate.hpp.

◆ make() [6/11]

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) const Method>
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.

◆ make() [7/11]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate make ( C const & object,
R(C::* method_ptr )(A...) const )
inlinestatic

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

Definition at line 251 of file delegate.hpp.

◆ make() [8/11]

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) const Method>
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.

◆ make() [9/11]

template<typename R, typename... A, typename Allocator>
template<class C>
Delegate make ( C const *const object_ptr,
R(C::* method_ptr )(A...) const )
inlinestatic

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

Definition at line 238 of file delegate.hpp.

◆ make() [10/11]

template<typename R, typename... A, typename Allocator>
Delegate make ( R(* function_ptr )(A...))
inlinestaticnoexcept

construction from a plain function pointer with no object.

Definition at line 132 of file delegate.hpp.

◆ make() [11/11]

template<typename R, typename... A, typename Allocator>
template<typename T>
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.

◆ method_caller()

template<typename R, typename... A, typename Allocator>
template<class C, R(C::*)(A...) method_ptr>
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()

template<typename R, typename... A, typename Allocator>
operator bool ( ) const
inlineexplicitnoexcept

explicit conversion to bool -> valid or invalid.

Definition at line 296 of file delegate.hpp.

◆ operator!=() [1/2]

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

compare delegate with another

Definition at line 275 of file delegate.hpp.

◆ operator!=() [2/2]

template<typename R, typename... A, typename Allocator>
bool operator!= ( std::nullptr_t const ) const
inlinenoexcept

compare delegate with another

Definition at line 291 of file delegate.hpp.

◆ operator()()

template<typename R, typename... A, typename Allocator>
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.

◆ operator<()

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

compare delegate with another

Definition at line 280 of file delegate.hpp.

◆ operator=() [1/2]

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

copy assignment operator

◆ operator=() [2/2]

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

move assignment operator

◆ operator==() [1/2]

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

compare delegate with another

Definition at line 270 of file delegate.hpp.

◆ operator==() [2/2]

template<typename R, typename... A, typename Allocator>
bool operator== ( std::nullptr_t const ) const
inlinenoexcept

compare delegate with another

Definition at line 286 of file delegate.hpp.

◆ reset()

template<typename R, typename... A, typename Allocator>
void reset ( )
inline

reset delegate to invalid.

Definition at line 262 of file delegate.hpp.

◆ reset_caller()

template<typename R, typename... A, typename Allocator>
void reset_caller ( )
inlinenoexcept

Definition at line 264 of file delegate.hpp.

◆ store_deleter()

template<typename R, typename... A, typename Allocator>
template<typename T>
void store_deleter ( void *const ptr)
inlinestaticprivate

deleter for stored functor closures

Definition at line 335 of file delegate.hpp.

◆ swap()

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

swap delegates

Definition at line 267 of file delegate.hpp.

Member Data Documentation

◆ caller_

template<typename R, typename... A, typename Allocator>
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.

◆ object_ptr_

template<typename R, typename... A, typename Allocator>
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.

◆ store_

template<typename R, typename... A, typename Allocator>
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: