Thrill
0.1
|
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:
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.
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... | |
Delegate & | operator= (const Delegate &)=default |
copy assignment operator More... | |
Delegate & | operator= (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... | |
R | 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... | |
|
private |
type of the function caller pointer.
Definition at line 308 of file delegate.hpp.
|
private |
wrappers for indirect const class::method calls containing (object, const method_ptr)
Definition at line 384 of file delegate.hpp.
|
private |
Definition at line 310 of file delegate.hpp.
|
private |
wrappers for indirect class::method calls containing (object, method_ptr)
Definition at line 378 of file delegate.hpp.
|
default |
default constructor
|
inlineexplicitnoexcept |
constructor from a plain function pointer with no object.
Definition at line 124 of file delegate.hpp.
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.
|
inline |
constructor for wrapping a class::method with object pointer.
Definition at line 210 of file delegate.hpp.
|
inline |
constructor for wrapping a const class::method with object pointer.
Definition at line 215 of file delegate.hpp.
|
inline |
constructor for wrapping a class::method with object reference.
Definition at line 220 of file delegate.hpp.
|
inline |
constructor for wrapping a const class::method with object reference.
Definition at line 225 of file delegate.hpp.
private constructor for plain
Definition at line 329 of file delegate.hpp.
|
inlinestaticprivate |
function caller for immediate const class::method functions calls.
Definition at line 364 of file delegate.hpp.
|
inlinestaticprivate |
caller for an immediate function with no object or pointer.
Definition at line 346 of file delegate.hpp.
|
inlinestaticprivate |
caller for a plain function pointer.
Definition at line 351 of file delegate.hpp.
|
inlinestaticprivate |
function caller for functor class.
Definition at line 407 of file delegate.hpp.
References gen_data::value.
|
inlinestaticprivate |
function caller for const functor class.
Definition at line 416 of file delegate.hpp.
|
inlinestaticnoexcept |
construction from an immediate function with no object or pointer.
Definition at line 114 of file delegate.hpp.
|
inlinestaticnoexcept |
construction from a plain function pointer with no object.
Definition at line 132 of file delegate.hpp.
|
inlinestaticnoexcept |
construction for an immediate class::method with class object
Definition at line 143 of file delegate.hpp.
|
inlinestaticnoexcept |
construction for an immediate class::method with class object
Definition at line 149 of file delegate.hpp.
|
inlinestaticnoexcept |
construction for an immediate class::method with class object by reference
Definition at line 157 of file delegate.hpp.
|
inlinestaticnoexcept |
construction for an immediate class::method with class object by reference
Definition at line 164 of file delegate.hpp.
References gen_data::value.
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.
|
inlinestatic |
constructor for wrapping a class::method with object pointer.
Definition at line 230 of file delegate.hpp.
|
inlinestatic |
constructor for wrapping a const class::method with object pointer.
Definition at line 237 of file delegate.hpp.
|
inlinestatic |
constructor for wrapping a class::method with object reference.
Definition at line 244 of file delegate.hpp.
|
inlinestatic |
constructor for wrapping a const class::method with object reference.
Definition at line 250 of file delegate.hpp.
|
inlinestaticprivate |
function caller for immediate class::method function calls
Definition at line 357 of file delegate.hpp.
|
inlineexplicitnoexcept |
explicit conversion to bool -> valid or invalid.
Definition at line 295 of file delegate.hpp.
|
inlinenoexcept |
compare delegate with another
Definition at line 274 of file delegate.hpp.
References tlx::operator==().
|
inlinenoexcept |
compare delegate with another
Definition at line 290 of file delegate.hpp.
|
inline |
most important method: call. The call is forwarded to the selected function caller.
Definition at line 299 of file delegate.hpp.
|
inlinenoexcept |
compare delegate with another
Definition at line 279 of file delegate.hpp.
|
inlinenoexcept |
compare delegate with another
Definition at line 269 of file delegate.hpp.
|
inlinenoexcept |
compare delegate with another
Definition at line 285 of file delegate.hpp.
|
inline |
reset delegate to invalid.
Definition at line 261 of file delegate.hpp.
|
inlinenoexcept |
Definition at line 263 of file delegate.hpp.
|
inlinestaticprivate |
deleter for stored functor closures
Definition at line 334 of file delegate.hpp.
|
inlinenoexcept |
|
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.
|
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.
|
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.