VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtDelegate< Ret, Args > Class Template Reference

Detailed Description

template<typename Ret, typename... Args>
class makVre::DtDelegate< Ret, Args >
Template Parameters
RetReturn type of the callback function
ArgsParameter types for the callback function (variadic)

This class provides a unified interface for binding and invoking different types of callables:

  • Static/global functions
  • Member functions bound to objects
  • Lambda expressions
  • Functors (classes with operator())

The delegate uses type erasure via std::function internally while maintaining the ability to compare and hash different delegate instances, which is useful for containers like std::unordered_map or for implementing event systems.

Note
Delegates are hashable and comparable, but due to limitations in C++, lambdas with identical code but at different source locations will be considered different delegates.

#include <delegate.h>

Inheritance diagram for makVre::DtDelegate< Ret, Args >:
[legend]

Public Types

enum class  DelegateType { Empty , Static , Member , Lambda }
 

Public Member Functions

 DtDelegate ()=default
 
 DtDelegate (Ret(*func)(Args...))
 
template<typename T, typename Method>
 DtDelegate (T *object, Method method)
 
template<typename T, typename Method>
 DtDelegate (const T *object, Method method)
 
template<typename Callable, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Callable>, DtDelegate>>>
 DtDelegate (Callable &&callable)
 
 ~DtDelegate ()=default
 
 DtDelegate (const DtDelegate &other)
 
 DtDelegate (DtDelegate &&other) noexcept
 
DtDelegateoperator= (const DtDelegate &other)
 
DtDelegateoperator= (DtDelegate &&other) noexcept
 
void reset () noexcept
 
template<typename... CallArgs, typename = std::enable_if_t<!std::is_void_v<Ret>>>
Ret operator() (CallArgs &&... args) const
 
template<typename... CallArgs, typename = std::enable_if_t<std::is_void_v<Ret>>, typename = void>
void operator() (CallArgs &&... args) const
 
 operator bool () const noexcept
 
std::size_t hash () const noexcept
 
bool operator== (const DtDelegate &rhs) const noexcept
 
bool operator!= (const DtDelegate &rhs) const noexcept
 
DelegateType getType () const noexcept
 

Static Public Member Functions

template<typename Method>
static std::size_t hashMethodPtr (const Method &method) noexcept
 
template<typename PtrType>
static std::size_t hashPtr (PtrType ptr) noexcept
 
template<typename Callable>
static std::size_t hashLambda (const Callable &callable) noexcept
 

Protected Member Functions

void computeHash () noexcept
 
void resetHash () const noexcept
 

Protected Attributes

std::function< Ret(Args...)> myFunction = nullptr
 
DelegateType myType = DelegateType::Empty
 
std::size_t myObjectHash = 0
 
std::size_t myStaticFuncHash = 0
 
std::size_t myMethodHash = 0
 
std::size_t myLambdaHash = 0
 
std::size_t myHash = 0
 
bool myHashComputed = false
 

Member Enumeration Documentation

◆ DelegateType

template<typename Ret, typename... Args>
enum class makVre::DtDelegate::DelegateType
strong

Enumeration of delegate types.

Categorizes the different types of callables that can be bound to a delegate.

Enumerator
Empty 

Empty delegate with no bound callable.

Static 

Static or global function.

Member 

Member function bound to an object.

Lambda 

Lambda, functor, or other callable object.

Constructor & Destructor Documentation

◆ DtDelegate() [1/7]

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::DtDelegate ( )
default

Default constructor - creates an empty delegate.

Initializes a delegate with no bound callable. Calling an empty delegate will return a default-constructed instance of the return type.

◆ DtDelegate() [2/7]

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::DtDelegate ( Ret(* func )(Args...))

Constructor binding a static or global function.

Parameters
funcPointer to the static or global function to bind

Creates a delegate that will invoke the specified static or global function when called.

◆ DtDelegate() [3/7]

template<typename Ret, typename... Args>
template<typename T, typename Method>
makVre::DtDelegate< Ret, Args >::DtDelegate ( T * object,
Method method )

Constructor binding a class member function.

Template Parameters
TType of the object containing the member function
Parameters
objectPointer to the object instance
methodPointer to the member function

Creates a delegate that will invoke the specified member function on the given object instance when called.

◆ DtDelegate() [4/7]

template<typename Ret, typename... Args>
template<typename T, typename Method>
makVre::DtDelegate< Ret, Args >::DtDelegate ( const T * object,
Method method )

Constructor binding a const class member function.

Template Parameters
TType of the object containing the member function
Parameters
objectPointer to the const object instance
methodPointer to the const member function

Creates a delegate that will invoke the specified const member function on the given const object instance when called.

◆ DtDelegate() [5/7]

template<typename Ret, typename... Args>
template<typename Callable, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Callable>, DtDelegate>>>
makVre::DtDelegate< Ret, Args >::DtDelegate ( Callable && callable)

Constructor for any callable (lambda, functor, etc.)

Template Parameters
CallableType of the callable object
Parameters
callableThe callable object to bind

Creates a delegate that will invoke the specified lambda or functor when called. The callable is stored by value, so any state within the callable is preserved.

Note
SFINAE is used to prevent this constructor from matching when a DtDelegate is passed, ensuring proper copy/move construction.

◆ ~DtDelegate()

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::~DtDelegate ( )
default

Destructor.

◆ DtDelegate() [6/7]

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::DtDelegate ( const DtDelegate< Ret, Args > & other)

Copy constructor.

Parameters
otherThe delegate to copy from

Creates a new delegate that is a copy of the given delegate, including its bound callable and all associated hash information.

◆ DtDelegate() [7/7]

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::DtDelegate ( DtDelegate< Ret, Args > && other)
noexcept

Move constructor.

Parameters
otherThe delegate to move from

Creates a new delegate by moving the contents of the given delegate, leaving the source delegate in an empty state.

Member Function Documentation

◆ operator=() [1/2]

template<typename Ret, typename... Args>
DtDelegate & makVre::DtDelegate< Ret, Args >::operator= ( const DtDelegate< Ret, Args > & other)

Copy assignment operator.

Parameters
otherThe delegate to copy from
Returns
Reference to this delegate after assignment

Assigns the contents of the given delegate to this delegate, including its bound callable and all associated hash information.

◆ operator=() [2/2]

template<typename Ret, typename... Args>
DtDelegate & makVre::DtDelegate< Ret, Args >::operator= ( DtDelegate< Ret, Args > && other)
noexcept

Move assignment operator.

Parameters
otherThe delegate to move from
Returns
Reference to this delegate after assignment

Moves the contents of the given delegate to this delegate, leaving the source delegate in an empty state.

◆ reset()

template<typename Ret, typename... Args>
void makVre::DtDelegate< Ret, Args >::reset ( )
noexcept

Reset the delegate to an empty state.

Clears the bound callable and resets all internal state, effectively returning the delegate to the same state as if it were default constructed.

◆ operator()() [1/2]

template<typename Ret, typename... Args>
template<typename... CallArgs, typename = std::enable_if_t<!std::is_void_v<Ret>>>
Ret makVre::DtDelegate< Ret, Args >::operator() ( CallArgs &&... args) const

Function call operator with perfect forwarding (non-void return type)

Template Parameters
CallArgsTypes of the arguments being passed to the delegate
Parameters
argsArguments to forward to the bound callable
Returns
The result of invoking the bound callable with the given arguments

Invokes the bound callable with the given arguments. If the delegate is empty:

  • For default constructible types: returns default-constructed instance (Ret{})
  • For non-default constructible types: throws std::runtime_error This overload is enabled only when the return type is not void.

◆ operator()() [2/2]

template<typename Ret, typename... Args>
template<typename... CallArgs, typename = std::enable_if_t<std::is_void_v<Ret>>, typename = void>
void makVre::DtDelegate< Ret, Args >::operator() ( CallArgs &&... args) const

Function call operator with perfect forwarding (void return type)

Template Parameters
CallArgsTypes of the arguments being passed to the delegate
Parameters
argsArguments to forward to the bound callable

Invokes the bound callable with the given arguments. If the delegate is empty, this function simply returns without doing anything (silent no-op). This overload is enabled only when the return type is void.

◆ operator bool()

template<typename Ret, typename... Args>
makVre::DtDelegate< Ret, Args >::operator bool ( ) const
explicitnoexcept

Check if the delegate has a bound callable.

Returns
true if a callable is bound, false otherwise

Allows the delegate to be used in boolean contexts to check if it has a bound callable.

◆ hash()

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::hash ( ) const
noexcept

Get hash value for this delegate.

Returns
Hash value representing the delegate's bound callable

Computes and returns a hash value that uniquely identifies the bound callable. This value is cached after computation for efficiency.

Note
For static functions and member functions, this will reliably reflect the identity of the function. For lambdas and functors, the hash is based on the object's address and type, which may differ between identical lambdas defined at different source locations.

◆ operator==()

template<typename Ret, typename... Args>
bool makVre::DtDelegate< Ret, Args >::operator== ( const DtDelegate< Ret, Args > & rhs) const
noexcept

Equality comparison operator using hash values.

Parameters
rhsRight-hand side delegate to compare with
Returns
true if delegates are equivalent, false otherwise

Compares this delegate with another for equality based on their hash values. Two delegates are considered equivalent if they have the same type and:

  • For static functions: they point to the same function
  • For member functions: they point to the same method of the same object
  • For lambdas/functors: they are the same instance (by address and type)
Note
std::function does not support deep comparison of bound callables, so this comparison is based on the computed hash values.

◆ operator!=()

template<typename Ret, typename... Args>
bool makVre::DtDelegate< Ret, Args >::operator!= ( const DtDelegate< Ret, Args > & rhs) const
noexcept

Inequality comparison operator.

Parameters
rhsRight-hand side delegate to compare with
Returns
true if delegates are not equivalent, false otherwise

Inverts the result of the equality operator.

◆ getType()

template<typename Ret, typename... Args>
DelegateType makVre::DtDelegate< Ret, Args >::getType ( ) const
noexcept

Get the type of this delegate.

Returns
The type of delegate (Empty, Static, Member, or Lambda)

This function returns the type of the delegate, which can be useful for debugging and introspection. The type indicates what kind of callable entity is bound to the delegate:

  • Empty: No callable is bound
  • Static: A static or global function is bound
  • Member: A member function bound to an object is bound
  • Lambda: A lambda, functor, or other callable is bound

◆ hashMethodPtr()

template<typename Ret, typename... Args>
template<typename Method>
static std::size_t makVre::DtDelegate< Ret, Args >::hashMethodPtr ( const Method & method)
staticnoexcept

Helper function to hash method pointers.

Template Parameters
MethodType of the method pointer
Parameters
methodThe method pointer to hash
Returns
Hash value for the method pointer

Creates a hash value from a method pointer by converting it to a byte sequence and hashing that sequence. This allows distinguishing between different methods of the same class.

◆ hashPtr()

template<typename Ret, typename... Args>
template<typename PtrType>
static std::size_t makVre::DtDelegate< Ret, Args >::hashPtr ( PtrType ptr)
staticnoexcept

Helper function to hash any pointer (object or function)

Template Parameters
PtrTypeType of the pointer
Parameters
ptrThe pointer to hash
Returns
Hash value for the pointer

Creates a hash value from a pointer by using std::hash<const void*>. This is used to hash object pointers and function pointers.

◆ hashLambda()

template<typename Ret, typename... Args>
template<typename Callable>
static std::size_t makVre::DtDelegate< Ret, Args >::hashLambda ( const Callable & callable)
staticnoexcept

Helper function to hash lambda objects.

Template Parameters
CallableType of the callable object
Parameters
callableThe callable object to hash
Returns
Hash value for the callable object

Creates a hash value for a lambda or functor by combining the object's address and its type information. This allows distinguishing between different lambda expressions, even if they have identical code.

◆ computeHash()

template<typename Ret, typename... Args>
void makVre::DtDelegate< Ret, Args >::computeHash ( )
protectednoexcept

Compute hash for the current delegate state.

Calculates a hash value based on the delegate's type and the specific bound callable. The hash computation differs based on the delegate type:

  • Empty: Hash is 0
  • Static: Hash is derived from the function pointer
  • Member: Hash combines the object pointer and method pointer
  • Lambda: Hash combines the lambda instance and its type

Once computed, the hash is cached for future use.

◆ resetHash()

template<typename Ret, typename... Args>
void makVre::DtDelegate< Ret, Args >::resetHash ( ) const
protectednoexcept

Reset hash computation state.

Clears the cached hash value and marks it as not computed, ensuring that the next call to hash() will recompute the value.

Member Data Documentation

◆ myFunction

template<typename Ret, typename... Args>
std::function<Ret(Args...)> makVre::DtDelegate< Ret, Args >::myFunction = nullptr
protected

Function object to store any callable.

Uses std::function for type erasure, allowing the delegate to store and invoke any callable with the matching signature.

◆ myType

template<typename Ret, typename... Args>
DelegateType makVre::DtDelegate< Ret, Args >::myType = DelegateType::Empty
protected

Type of delegate.

Stores the category of the bound callable (Empty, Static, Member, or Lambda).

◆ myObjectHash

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::myObjectHash = 0
protected

Hash of object pointer for member function delegates.

Stores the hash of the object pointer when binding a member function. Used to differentiate delegates bound to different object instances.

◆ myStaticFuncHash

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::myStaticFuncHash = 0
protected

Hash of static function pointer for static function delegates.

Stores the hash of the function pointer when binding a static function. Used to identify the specific function being called.

◆ myMethodHash

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::myMethodHash = 0
protected

Hash value for method pointer to distinguish different member functions.

Stores the hash of the method pointer when binding a member function. Used to differentiate between different methods of the same class.

◆ myLambdaHash

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::myLambdaHash = 0
protected

Hash value for lambda objects.

Stores the hash of the lambda or functor when binding a callable object. Used to identify the specific callable being invoked.

◆ myHash

template<typename Ret, typename... Args>
std::size_t makVre::DtDelegate< Ret, Args >::myHash = 0
mutableprotected

Hash value for the delegate's bound callable.

Caches the computed hash value for efficient repeated access. This value combines information based on the delegate type.

◆ myHashComputed

template<typename Ret, typename... Args>
bool makVre::DtDelegate< Ret, Args >::myHashComputed = false
mutableprotected

Flag to track if hash has been computed.

Indicates whether the hash value has been computed and cached. Used to avoid unnecessary recomputation of the hash.


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