Thrill  0.1
Delegate< R(A...), Allocator > Class Template 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; }
};
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.

#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
 

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
 
template<class C , R(C::*)(A...) const Method>
static Delegate make (C const &object) noexcept
 

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_ = nullptr
 
void * object_ptr_ = nullptr
 
std::shared_ptr< void > store_
 

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)
 
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)
 
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...)>
 
template<class C >
using ConstMemberPair = std::pair< C const *const, R(C::*const)(A...) const >
 
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...
 

Member Typedef Documentation

◆ Caller

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

type of the function caller pointer.

Definition at line 308 of file delegate.hpp.

◆ ConstMemberPair

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 384 of file delegate.hpp.

◆ Deleter

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

Definition at line 310 of file delegate.hpp.

◆ MemberPair

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

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

Definition at line 378 of file delegate.hpp.

Constructor & Destructor Documentation

◆ Delegate() [1/10]

Delegate ( )
default

default constructor

◆ Delegate() [2/10]

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

copy constructor

◆ Delegate() [3/10]

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

move constructor

◆ Delegate() [4/10]

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

constructor from a plain function pointer with no object.

Definition at line 124 of file delegate.hpp.

◆ Delegate() [5/10]

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]

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

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

Definition at line 210 of file delegate.hpp.

◆ Delegate() [7/10]

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 215 of file delegate.hpp.

◆ Delegate() [8/10]

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

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

Definition at line 220 of file delegate.hpp.

◆ Delegate() [9/10]

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

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

Definition at line 225 of file delegate.hpp.

◆ Delegate() [10/10]

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

private constructor for plain

Definition at line 329 of file delegate.hpp.

Member Function Documentation

◆ const_method_caller()

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

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

Definition at line 364 of file delegate.hpp.

◆ function_caller()

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

caller for an immediate function with no object or pointer.

Definition at line 346 of file delegate.hpp.

◆ function_ptr_caller()

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

caller for a plain function pointer.

Definition at line 351 of file delegate.hpp.

◆ functor_caller() [1/2]

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 407 of file delegate.hpp.

References gen_data::value.

◆ functor_caller() [2/2]

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 416 of file delegate.hpp.

◆ make() [1/11]

static Delegate make ( )
inlinestaticnoexcept

construction from an immediate function with no object or pointer.

Definition at line 114 of file delegate.hpp.

◆ make() [2/11]

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.

◆ make() [3/11]

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.

◆ make() [4/11]

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.

◆ make() [5/11]

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.

◆ make() [6/11]

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.

References gen_data::value.

◆ make() [7/11]

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 204 of file delegate.hpp.

◆ make() [8/11]

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 230 of file delegate.hpp.

◆ make() [9/11]

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 237 of file delegate.hpp.

◆ make() [10/11]

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

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

Definition at line 244 of file delegate.hpp.

◆ make() [11/11]

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 250 of file delegate.hpp.

◆ method_caller()

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

function caller for immediate class::method function calls

Definition at line 357 of file delegate.hpp.

◆ operator bool()

operator bool ( ) const
inlineexplicitnoexcept

explicit conversion to bool -> valid or invalid.

Definition at line 295 of file delegate.hpp.

◆ operator!=() [1/2]

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

compare delegate with another

Definition at line 274 of file delegate.hpp.

References tlx::operator==().

◆ operator!=() [2/2]

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

compare delegate with another

Definition at line 290 of file delegate.hpp.

◆ operator()()

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

most important method: call. The call is forwarded to the selected function caller.

Definition at line 299 of file delegate.hpp.

◆ operator<()

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

compare delegate with another

Definition at line 279 of file delegate.hpp.

◆ operator=() [1/2]

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

copy assignment operator

◆ operator=() [2/2]

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

move assignment operator

◆ operator==() [1/2]

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

compare delegate with another

Definition at line 269 of file delegate.hpp.

◆ operator==() [2/2]

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

compare delegate with another

Definition at line 285 of file delegate.hpp.

◆ reset()

void reset ( )
inline

reset delegate to invalid.

Definition at line 261 of file delegate.hpp.

◆ reset_caller()

void reset_caller ( )
inlinenoexcept

Definition at line 263 of file delegate.hpp.

◆ store_deleter()

static void store_deleter ( void *const  ptr)
inlinestaticprivate

deleter for stored functor closures

Definition at line 334 of file delegate.hpp.

◆ swap()

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

swap delegates

Definition at line 266 of file delegate.hpp.

References tlx::swap().

Member Data Documentation

◆ caller_

Caller caller_ = nullptr
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 316 of file delegate.hpp.

◆ object_ptr_

void* object_ptr_ = nullptr
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 322 of file delegate.hpp.

◆ store_

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 326 of file delegate.hpp.


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