Delegate< RESULT(ARGS...)> Class Template Reference

#include <delegate.h>

Inheritance diagram for Delegate< RESULT(ARGS...)>:

Detailed Description

template<typename RESULT, typename... ARGS>
class maxon::Delegate< RESULT(ARGS...)>

Template Parameters
RESULTReturn value type of the callable.

Classes

struct  ObjectPointerWrapper
 
struct  PTMFWrapper
 

Public Types

using StubPtrType = RESULT(*)(void *, ARGS...)
 
using ResultValueType = RESULT
 
- Public Types inherited from DelegateBase
using Stub = void(*)()
 

Public Member Functions

 Delegate ()=default
 
 Delegate (const Delegate &)=delete
 
 Delegate (Delegate &&src)=default
 
MAXON_IMPLICIT Delegate (const std::nullptr_t)
 
 Delegate (RESULT(*functionPtr)(ARGS...))
 
template<typename OBJECT >
 Delegate (OBJECT *objectPtr, RESULT(OBJECT::*methodPtr)(ARGS...))
 
template<typename OBJECT >
 Delegate (const OBJECT *objectPtr, RESULT(OBJECT::*methodPtr)(ARGS...) const)
 
template<typename OBJECT >
 Delegate (OBJECT &object, RESULT(OBJECT::*methodPtr)(ARGS...))
 
template<typename OBJECT >
 Delegate (const OBJECT &object, RESULT(OBJECT::*methodPtr)(ARGS...) const)
 
template<typename REF , typename OBJECT >
 Delegate (Bool marker, REF &&reference, RESULT(*const &methodPtr)(OBJECT *, ARGS...))
 
template<typename FN , typename = typename std::enable_if<!std :: is_convertible<Delegate, typename std::decay<FN>::type>::value && (GetCollectionKind<FN>::value == COLLECTION_KIND::NONE)>::type>
MAXON_IMPLICIT Delegate (FN &&fn)
 
Delegateoperator= (const Delegate &)=delete
 
ResultMem CopyFrom (const Delegate &src)
 
ResultMem CopyFrom (const std::nullptr_t)
 
ResultMem CopyFrom (RESULT(*functionPtr)(ARGS...))
 
template<typename FN , typename = typename std::enable_if<!STD_IS_REPLACEMENT(convertible, Delegate, typename std::decay<FN>::type)>::type>
ResultMem CopyFrom (FN &&fn)
 
template<typename OBJ >
ResultMem CopyFrom (OBJ &&object, StubPtrType stub)
 
Delegateoperator= (Delegate &&)=default
 
Delegateoperator= (const std::nullptr_t)
 
Delegateoperator= (RESULT(*const functionPtr)(ARGS...))
 
template<typename FN , typename = typename std::enable_if<!STD_IS_REPLACEMENT(convertible, Delegate, typename std::decay<FN>::type)>::type>
Delegateoperator= (FN &&fn)
 
MAXON_ATTRIBUTE_FORCE_INLINE void Swap (Delegate &other)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== (const Delegate &rhs) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator!= (const Delegate &rhs) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< (const Delegate &rhs) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== (const std::nullptr_t) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator!= (const std::nullptr_t) const
 
MAXON_ATTRIBUTE_FORCE_INLINE operator Bool () const
 
MAXON_ATTRIBUTE_FORCE_INLINE RESULT operator() (ARGS... args) const
 
- Public Member Functions inherited from DelegateBase
 DelegateBase ()
 
 ~DelegateBase ()
 
template<typename STUBPTR >
 DelegateBase (void *objectPtr, STUBPTR stubPtr, Callback callback)
 
template<typename FN , typename STUBPTR >
 DelegateBase (FN &&fn, STUBPTR stub, Callback callback)
 
 DelegateBase (const DelegateBase &)=delete
 
 DelegateBase (DelegateBase &&src)
 
 MAXON_OPERATOR_MOVE_ASSIGNMENT (DelegateBase)
 
void Reset ()
 
ResultMem CopyFrom (const DelegateBase &src)
 
template<typename FN , typename STUBPTR >
ResultMem CopyFrom (FN &&f, STUBPTR stub, Callback callback)
 
template<typename OBJ , typename STUBPTR >
ResultMem CopyFrom (OBJ &&object, STUBPTR stub)
 
Bool IsEmpty () const
 
Bool IsPopulated () const
 
Bool IsStaticFunctionPointer () const
 
template<typename FUNCTIONPTR >
FUNCTIONPTR GetStaticFunctionPointer () const
 
template<typename STUBPTR >
STUBPTR GetStubPointer () const
 

Static Public Member Functions

static const DelegateNullValueRef ()
 
template<RESULT(*)(ARGS...) FUNCTION>
static ResultOk< DelegateCreate ()
 
static ResultOk< DelegateCreate (RESULT(*functionPtr)(ARGS...))
 
template<typename FN >
static Result< DelegateCreate (FN &&fn)
 
template<RESULT(*)(ARGS...) FUNCTION>
static Delegate CreateByReference ()
 
static Delegate CreateByReference (RESULT(*functionPtr)(ARGS...))
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) METHOD_PTR>
static Delegate CreateByReference (OBJECT *objectPtr)
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) const METHOD_PTR>
static Delegate CreateByReference (const OBJECT *objectPtr)
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) METHOD_PTR>
static Delegate CreateByReference (OBJECT &object)
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) const METHOD_PTR>
static Delegate CreateByReference (const OBJECT &object)
 
template<typename OBJECT >
static Delegate CreateByReference (OBJECT *objectPtr, RESULT(OBJECT::*methodPtr)(ARGS...))
 
template<typename OBJECT >
static Delegate CreateByReference (const OBJECT *objectPtr, RESULT(OBJECT::*methodPtr)(ARGS...) const)
 
template<typename OBJECT >
static Delegate CreateByReference (OBJECT &object, RESULT(OBJECT::*methodPtr)(ARGS...))
 
template<typename OBJECT >
static Delegate CreateByReference (const OBJECT &object, RESULT(OBJECT::*methodPtr)(ARGS...) const)
 
template<typename FN >
static Delegate CreateByReference (FN &&fn)
 

Private Member Functions

 Delegate (void *o, StubPtrType m, Callback c)
 
template<typename FN >
DelegateConstructFrom (FN &&fn)
 

Static Private Member Functions

static StubPtrType CheckStub (StubPtrType ptr)
 
template<RESULT(*)(ARGS...) FUNCTION>
static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctionStub (void *objectPtr, ARGS... args)
 
template<typename T >
static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctionIndirectStub (void *objectPtr, ARGS... args)
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) METHOD_PTR>
static MAXON_ATTRIBUTE_FORCE_INLINE RESULT MethodStub (void *objectPtr, ARGS... args)
 
template<typename OBJECT , RESULT(OBJECT::*)(ARGS...) const METHOD_PTR>
static MAXON_ATTRIBUTE_FORCE_INLINE RESULT ConstMethodStub (void *objectPtr, ARGS... args)
 
template<typename T >
static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctorStub (void *objectPtr, ARGS... args)
 

Additional Inherited Members

- Protected Types inherited from DelegateBase
enum class  CALLBACKMODE {
  ALLOC ,
  DESTRUCT_AND_RELEASE ,
  COPY ,
  MOVE_AND_DESTRUCT
}
 
using Callback = Int(*)(DelegateBase *dst, const DelegateBase *src, CALLBACKMODE mode)
 
- Protected Member Functions inherited from DelegateBase
enum maxon::DelegateBase::CALLBACKMODE MAXON_ENUM_ORDERED_LIST_CLASS (CALLBACKMODE)
 
- Static Protected Member Functions inherited from DelegateBase
template<typename REF >
static Callback GetReferenceUtilityStub (void *)
 
template<typename REF >
static Callback GetReferenceUtilityStub (PointerHandler *)
 
static Int PTMFUtilityStub (DelegateBase *dst, const DelegateBase *src, CALLBACKMODE mode)
 
template<typename T >
static Int UtilityStub (DelegateBase *dst, const DelegateBase *src, CALLBACKMODE mode)
 
template<typename REFERENCE >
static Int ReferenceUtilityStub (DelegateBase *dst, const DelegateBase *src, CALLBACKMODE mode)
 
- Protected Attributes inherited from DelegateBase
void * _storage [5]
 
void * _objectPtr
 
Stub _stubPtr
 
Callback _callback
 

Member Typedef Documentation

◆ StubPtrType

using StubPtrType = RESULT (*)(void*, ARGS...)

◆ ResultValueType

using ResultValueType = RESULT

Constructor & Destructor Documentation

◆ Delegate() [1/12]

Delegate ( void *  o,
StubPtrType  m,
Callback  c 
)
private

◆ Delegate() [2/12]

Delegate ( )
default

◆ Delegate() [3/12]

Delegate ( const Delegate< RESULT(ARGS...)> &  )
delete

◆ Delegate() [4/12]

Delegate ( Delegate< RESULT(ARGS...)> &&  src)
default

◆ Delegate() [5/12]

MAXON_IMPLICIT Delegate ( const std::nullptr_t  )

◆ Delegate() [6/12]

Delegate ( RESULT(*)(ARGS...)  functionPtr)
explicit

Constructs a delegate for a function pointer. This is less efficient than Delegate::Create<METHODE_NAME>() because it requires an extra indirect function pointer jump (the method can't be inlined).

◆ Delegate() [7/12]

Delegate ( OBJECT *  objectPtr,
RESULT(OBJECT::*)(ARGS...)  methodPtr 
)

Constructs a delegate for a virtual member function of an object.

Note
The caller retains ownership of the object referenced by `objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectPtrSelf pointer of the object.
[in]methodPtrPointer to the member function.

◆ Delegate() [8/12]

Delegate ( const OBJECT *  objectPtr,
RESULT(OBJECT::*)(ARGS...) const  methodPtr 
)

Constructs a delegate for a const virtual member function of an object.

Note
The caller retains ownership of the object referenced by `objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectPtrSelf pointer of the object.
[in]methodPtrPointer to the const member function.

◆ Delegate() [9/12]

Delegate ( OBJECT &  object,
RESULT(OBJECT::*)(ARGS...)  methodPtr 
)

Constructs a delegate for a virtual member function of an object.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectReference of the object.
[in]methodPtrPointer to the member function.

◆ Delegate() [10/12]

Delegate ( const OBJECT &  object,
RESULT(OBJECT::*)(ARGS...) const  methodPtr 
)

Constructs a delegate for a const virtual member function of a const object.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectReference of the const object.
[in]methodPtrPointer to the const member function.

◆ Delegate() [11/12]

Delegate ( Bool  marker,
REF &&  reference,
RESULT(*&)(OBJECT *, ARGS...)  methodPtr 
)

◆ Delegate() [12/12]

MAXON_IMPLICIT Delegate ( FN &&  fn)

Constructs a delegate when T is not a Delegate, but a function pointer, ptmf or a PTMFWrapper<OBJECT*, RESULT (OBJECT::*)(ARGS...)>.

Member Function Documentation

◆ CheckStub()

static StubPtrType CheckStub ( StubPtrType  ptr)
staticprivate

◆ NullValueRef()

static const Delegate& NullValueRef ( )
static

◆ operator=() [1/5]

Delegate& operator= ( const Delegate< RESULT(ARGS...)> &  )
delete

A delegate cannot be copied without error checks because this might involve memory allocations.

Note
Use CopyFrom().

◆ CopyFrom() [1/5]

ResultMem CopyFrom ( const Delegate< RESULT(ARGS...)> &  src)

Copies a delegate.

Parameters
[in]srcSource delegate.
Returns
OK on success.

◆ CopyFrom() [2/5]

ResultMem CopyFrom ( const std::nullptr_t  )

Resets the delegate.

Returns
Always true.

◆ CopyFrom() [3/5]

ResultMem CopyFrom ( RESULT(*)(ARGS...)  functionPtr)

Copies the function pointer.

Parameters
[in]functionPtrStatic function pointer
Returns
Always true.

◆ CopyFrom() [4/5]

ResultMem CopyFrom ( FN &&  fn)

Copies or moves fn into the Delegate and allocates memory if necessary.

Template Parameters
FNThe callable type.
Parameters
[in]fnCallable.
Returns
OK on success.

◆ CopyFrom() [5/5]

ResultMem CopyFrom ( OBJ &&  object,
StubPtrType  stub 
)

Copies or moves object into the Delegate and sets #stub as the handler for delegate calls. #stub will receive a pointer to the object held by the Delegate in its first parameter.

Template Parameters
OBJThe object type.
Parameters
[in]objectThe object to move or copy.
[in]stubThe call handler.
Returns
OK on success.

◆ operator=() [2/5]

Delegate& operator= ( Delegate< RESULT(ARGS...)> &&  )
default

◆ operator=() [3/5]

Delegate& operator= ( const std::nullptr_t  )

◆ operator=() [4/5]

Delegate& operator= ( RESULT(*)(ARGS...)  functionPtr)

◆ operator=() [5/5]

Delegate& operator= ( FN &&  fn)

Assign when T is not a Delegate, but a function pointer, ptmf or a PTMFWrapper<OBJECT*, RESULT (OBJECT::*)(ARGS...)>.

◆ Create() [1/3]

static ResultOk<Delegate> Create ( )
static

Returns a delegate for a static member function (direct call). This is the most efficient delegate because the stub can inline the function (or at least make a direct jump).

Returns
Always succeeds and returns a Delegate.

◆ Create() [2/3]

static ResultOk<Delegate> Create ( RESULT(*)(ARGS...)  functionPtr)
static

Returns delegate for a function pointer. Less efficient than using Delegate::Create<T>() because this requires an extra indirect function pointer jump.

Returns
Always succeeds and returns a Delegate.

◆ Create() [3/3]

static Result<Delegate> Create ( FN &&  fn)
static

Returns a delegate for an object with a callable, usually a lambda. The object is copied (or moved if possible). If the allocation fails an error is returned.

Template Parameters
FNThe callable object type.
Parameters
[in]fnCallable.
Returns
Returns a Delegate on success.

◆ CreateByReference() [1/11]

static Delegate CreateByReference ( )
static

Returns a delegate for a static member function (direct call). This is the most efficient delegate because the stub can inline the function (or at least make a direct jump).

Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [2/11]

static Delegate CreateByReference ( RESULT(*)(ARGS...)  functionPtr)
static

Returns delegate for a function pointer. Less efficient than using Delegate::Create<T>() because this requires an extra indirect function pointer jump.

Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [3/11]

static Delegate CreateByReference ( OBJECT *  objectPtr)
static

Returns a delegate for a virtual member function.

Note
The caller retains ownership of the object referenced by #objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
METHOD_PTRThe virtual member function type (which can be resolved to a specific function pointer).
Parameters
[in]objectPtrSelf pointer of the object.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [4/11]

static Delegate CreateByReference ( const OBJECT *  objectPtr)
static

Returns a delegate for a const virtual member function.

Note
The caller retains ownership of the object referenced by #objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
METHOD_PTRThe const virtual member function type (which can be resolved to a specific function pointer).
Parameters
[in]objectPtrSelf pointer of the object.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [5/11]

static Delegate CreateByReference ( OBJECT &  object)
static

Returns a delegate for a virtual member function.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
METHOD_PTRThe virtual member function type (which can be resolved to a specific function pointer).
Parameters
[in]objectReference of the object.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [6/11]

static Delegate CreateByReference ( const OBJECT &  object)
static

Returns a delegate for a const virtual member function.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
METHOD_PTRThe virtual member function type (which can be resolved to a specific function pointer).
Parameters
[in]objectReference of the object.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [7/11]

static Delegate CreateByReference ( OBJECT *  objectPtr,
RESULT(OBJECT::*)(ARGS...)  methodPtr 
)
static

Returns a delegate for a virtual member function.

Note
The caller retains ownership of the object referenced by #objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectPtrSelf pointer of the object.
[in]methodPtrPointer to the member function.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [8/11]

static Delegate CreateByReference ( const OBJECT *  objectPtr,
RESULT(OBJECT::*)(ARGS...) const  methodPtr 
)
static

Returns a delegate for a const virtual member function.

Note
The caller retains ownership of the object referenced by #objectPtr and and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectPtrSelf pointer of the object.
[in]methodPtrPointer to the const member function.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [9/11]

static Delegate CreateByReference ( OBJECT &  object,
RESULT(OBJECT::*)(ARGS...)  methodPtr 
)
static

Returns a delegate for a virtual member function.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectReference of the object.
[in]methodPtrPointer to the member function.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [10/11]

static Delegate CreateByReference ( const OBJECT &  object,
RESULT(OBJECT::*)(ARGS...) const  methodPtr 
)
static

Returns a delegate for a const virtual member function.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
OBJECTThe object type.
Parameters
[in]objectReference of the const object.
[in]methodPtrPointer to the const member function.
Returns
Always succeeds and returns a Delegate.

◆ CreateByReference() [11/11]

static Delegate CreateByReference ( FN &&  fn)
static

Returns a delegate for an object with a callable.

Note
The caller retains ownership of object and must not release it before the Delegate is destructed.
Template Parameters
FNThe callable object type.
Parameters
[in]fnCallable
Returns
Always succeeds and returns a Delegate.

◆ Swap()

MAXON_ATTRIBUTE_FORCE_INLINE void Swap ( Delegate< RESULT(ARGS...)> &  other)

◆ operator==() [1/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== ( const Delegate< RESULT(ARGS...)> &  rhs) const

◆ operator!=() [1/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator!= ( const Delegate< RESULT(ARGS...)> &  rhs) const

◆ operator<()

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< ( const Delegate< RESULT(ARGS...)> &  rhs) const

◆ operator==() [2/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== ( const std::nullptr_t  ) const

◆ operator!=() [2/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator!= ( const std::nullptr_t  ) const

◆ operator Bool()

MAXON_ATTRIBUTE_FORCE_INLINE operator Bool ( ) const
explicit

◆ operator()()

MAXON_ATTRIBUTE_FORCE_INLINE RESULT operator() ( ARGS...  args) const

Forwards the call (invokes the stub function for a callable).

◆ ConstructFrom()

Delegate& ConstructFrom ( FN &&  fn)
private

Constructs the delegate when T is not a Delegate, but a function pointer, ptmf or a PTMFWrapper<OBJECT*, RESULT (OBJECT::*)(ARGS...)>.

◆ FunctionStub()

static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctionStub ( void *  objectPtr,
ARGS...  args 
)
staticprivate

Invokes a C style function via direct call (inlines it if possible).

◆ FunctionIndirectStub()

static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctionIndirectStub ( void *  objectPtr,
ARGS...  args 
)
staticprivate

Invokes a C style function via indirect function pointer call.

◆ MethodStub()

static MAXON_ATTRIBUTE_FORCE_INLINE RESULT MethodStub ( void *  objectPtr,
ARGS...  args 
)
staticprivate

Invokes a virtual member function.

◆ ConstMethodStub()

static MAXON_ATTRIBUTE_FORCE_INLINE RESULT ConstMethodStub ( void *  objectPtr,
ARGS...  args 
)
staticprivate

Invokes a const virtual member function.

◆ FunctorStub()

static MAXON_ATTRIBUTE_FORCE_INLINE RESULT FunctorStub ( void *  objectPtr,
ARGS...  args 
)
staticprivate