qxLib
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
qx::base_delegate< derived_t, return_t, args_t > Class Template Reference

Single or multicast delegate. Use the qx::delegate<> class in your code. More...

#include <delegate.h>

Public Member Functions

template<callable_c< return_t, args_t... > callable_t>
delegate_token_type add_free (callable_t callable, priority ePriority=priority::normal) noexcept
 Add a callable without any protection. More...
 
template<callable_c< return_t, args_t... > callable_t>
destruction_callback add_destruction_callback (callable_t callable, priority ePriority=priority::normal) noexcept
 Add a callable that will be removed from the delegate when its destruction callback is destroyed. More...
 
template<class object_t >
destruction_callback add_destruction_callback (object_t &object, return_t(object_t::*pMethod)(args_t...), priority ePriority=priority::normal) noexcept
 Add a callable that will be removed from the delegate when its destruction callback is destroyed. More...
 
template<class object_t , callable_c< return_t, args_t... > callable_t>
delegate_token_type add_weak (std::weak_ptr< object_t > pWeakObject, callable_t callable, priority ePriority=priority::normal) noexcept
 Add a callable that will be executed only if the appropriate weak object is valid. More...
 
template<class object_t >
delegate_token_type add_weak (std::weak_ptr< object_t > pWeakObject, return_t(object_t::*pMethod)(args_t...), priority ePriority=priority::normal) noexcept
 Add a callable that will be executed only if the appropriate weak object is valid. More...
 
bool remove (delegate_token_type token) noexcept
 Remove a callable using its token. More...
 
void clear () noexcept
 Clear all the callables in this delegate.
 

Static Public Member Functions

template<class... creation_args_t>
static derived_t create_singlecast (creation_args_t... args) noexcept
 Create a singlecast delegate. More...
 

Protected Member Functions

template<class invoke_single_t , class invoke_multiple_t >
return_t execute_internal (const invoke_single_t &invokeSingle, const invoke_multiple_t &invokeMultiple) const noexcept
 Execute all callables. More...
 

Detailed Description

template<class derived_t, delegate_return_c return_t, class... args_t>
class qx::base_delegate< derived_t, return_t, args_t >

Single or multicast delegate. Use the qx::delegate<> class in your code.

For a singlecast version:

  1. Create with qx::delegate<...>::create_singlecast(...)
  2. Call delegate.execute(...) For a multicast version:
  1. Default construct
  2. All various callbacks with delegate.add_xxx(...)
  1. Call delegate.execute(...)
    Template Parameters
    derived_t- CRTP derived class type
    return_t- the exact type that all the passed callables and execute() should return
    args_t- the exact type that all the passed callables and execute() should take
    Author
    Khrapov
    Date
    4.07.2025

Definition at line 53 of file delegate.h.

Member Function Documentation

◆ add_destruction_callback() [1/2]

template<class derived_t , delegate_return_c return_t, class... args_t>
template<callable_c< return_t, args_t... > callable_t>
destruction_callback qx::base_delegate< derived_t, return_t, args_t >::add_destruction_callback ( callable_t  callable,
priority  ePriority = priority::normal 
)
noexcept

Add a callable that will be removed from the delegate when its destruction callback is destroyed.

Template Parameters
callable_t- any callable type: lambda, function, static method pointer, ect
Parameters
callable- callable object
ePriority- callable priority. Callables will be called in order of priority, from highest to lowest, and in the order they were added.
Return values
-an object that removes this callable from the delegate on its destruction

Definition at line 88 of file delegate.inl.

◆ add_destruction_callback() [2/2]

template<class derived_t , delegate_return_c return_t, class... args_t>
template<class object_t >
destruction_callback qx::base_delegate< derived_t, return_t, args_t >::add_destruction_callback ( object_t &  object,
return_t(object_t::*)(args_t...)  pMethod,
priority  ePriority = priority::normal 
)
noexcept

Add a callable that will be removed from the delegate when its destruction callback is destroyed.

Template Parameters
object_t- object type
Parameters
object- object reference
pMethod- object's method pointer
ePriority- callable priority. Callables will be called in order of priority, from highest to lowest, and in the order they were added.
Return values
-an object that removes this callable from the delegate on its destruction

Definition at line 97 of file delegate.inl.

◆ add_free()

template<class derived_t , delegate_return_c return_t, class... args_t>
template<callable_c< return_t, args_t... > callable_t>
delegate_token_type qx::base_delegate< derived_t, return_t, args_t >::add_free ( callable_t  callable,
priority  ePriority = priority::normal 
)
noexcept

Add a callable without any protection.

Warning
If you capture this in the lambda passed and the object becomes invalid, it'll crash. Consider add_destruction_callback or add_weak in this case.
Template Parameters
callable_t- any callable type: lambda, function, static method pointer, ect
Parameters
callable- callable object
ePriority- callable priority. Callables will be called in order of priority, from highest to lowest, and in the order they were added.
Return values
-a token that can be used to remove this callable from the delegate

Definition at line 77 of file delegate.inl.

◆ add_weak() [1/2]

template<class derived_t , delegate_return_c return_t, class... args_t>
template<class object_t , callable_c< return_t, args_t... > callable_t>
delegate_token_type qx::base_delegate< derived_t, return_t, args_t >::add_weak ( std::weak_ptr< object_t >  pWeakObject,
callable_t  callable,
priority  ePriority = priority::normal 
)
noexcept

Add a callable that will be executed only if the appropriate weak object is valid.

Template Parameters
object_t- object type
callable_t- any callable type: lambda, function, static method pointer, ect
Parameters
pWeakObject- an object to track
callable- callable object
ePriority- callable priority. Callables will be called in order of priority, from highest to lowest, and in the order they were added.
Return values
-a token that can be used to remove this callable from the delegate

Definition at line 130 of file delegate.inl.

◆ add_weak() [2/2]

template<class derived_t , delegate_return_c return_t, class... args_t>
template<class object_t >
delegate_token_type qx::base_delegate< derived_t, return_t, args_t >::add_weak ( std::weak_ptr< object_t >  pWeakObject,
return_t(object_t::*)(args_t...)  pMethod,
priority  ePriority = priority::normal 
)
noexcept

Add a callable that will be executed only if the appropriate weak object is valid.

Template Parameters
object_t- object type
Parameters
pWeakObject- an object to track and to apply the method to
pMethod- object's method pointer
ePriority- callable priority. Callables will be called in order of priority, from highest to lowest, and in the order they were added.
Return values
-a token that can be used to remove this callable from the delegate

Definition at line 112 of file delegate.inl.

◆ create_singlecast()

template<class derived_t , delegate_return_c return_t, class... args_t>
template<class... creation_args_t>
derived_t qx::base_delegate< derived_t, return_t, args_t >::create_singlecast ( creation_args_t...  args)
staticnoexcept

Create a singlecast delegate.

Template Parameters
creation_args_t- any arguments that can be used in add_weak or add_free
Parameters
args- template parameter pack
Return values
-created delegate

Definition at line 62 of file delegate.inl.

◆ execute_internal()

template<class derived_t , delegate_return_c return_t, class... args_t>
template<class invoke_single_t , class invoke_multiple_t >
return_t qx::base_delegate< derived_t, return_t, args_t >::execute_internal ( const invoke_single_t &  invokeSingle,
const invoke_multiple_t &  invokeMultiple 
) const
protectednoexcept

Execute all callables.

Template Parameters
invoke_single_t- a callable type that executes a delegate callable by moving args into it
invoke_multiple_t- a callable type that executes a delegate callable by copying args into it
Parameters
invokeSingle- invoke_single_t object
invokeMultiple- invoke_multiple_t object
Return values
-void or piping result of all delegate callables

Definition at line 160 of file delegate.inl.

◆ remove()

template<class derived_t , delegate_return_c return_t, class... args_t>
bool qx::base_delegate< derived_t, return_t, args_t >::remove ( delegate_token_type  token)
noexcept

Remove a callable using its token.

Parameters
token- callable token
Return values
-true is a callable was deleted

Definition at line 147 of file delegate.inl.


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