template<typename Ret, typename... Args>
class makVre::DtDelegate< Ret, Args >
- Template Parameters
-
| Ret | Return type of the callback function |
| Args | Parameter 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.
template<typename Ret, typename... Args>
template<typename T, typename Method>
Constructor binding a class member function.
- Template Parameters
-
| T | Type of the object containing the member function |
- Parameters
-
| object | Pointer to the object instance |
| method | Pointer to the member function |
Creates a delegate that will invoke the specified member function on the given object instance when called.
template<typename Ret, typename... Args>
template<typename T, typename Method>
Constructor binding a const class member function.
- Template Parameters
-
| T | Type of the object containing the member function |
- Parameters
-
| object | Pointer to the const object instance |
| method | Pointer to the const member function |
Creates a delegate that will invoke the specified const member function on the given const object instance when called.
template<typename Ret, typename... Args>
template<typename Callable, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Callable>, DtDelegate>>>
Constructor for any callable (lambda, functor, etc.)
- Template Parameters
-
| Callable | Type of the callable object |
- Parameters
-
| callable | The 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.
template<typename Ret, typename... Args>
template<typename... CallArgs, typename = std::enable_if_t<!std::is_void_v<Ret>>>
Function call operator with perfect forwarding (non-void return type)
- Template Parameters
-
| CallArgs | Types of the arguments being passed to the delegate |
- Parameters
-
| args | Arguments 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.
template<typename Ret, typename... Args>
template<typename... CallArgs, typename = std::enable_if_t<std::is_void_v<Ret>>, typename = void>
Function call operator with perfect forwarding (void return type)
- Template Parameters
-
| CallArgs | Types of the arguments being passed to the delegate |
- Parameters
-
| args | Arguments 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.
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
-
| Method | Type of the method pointer |
- Parameters
-
| method | The 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.
template<typename Ret, typename... Args>
template<typename PtrType>
Helper function to hash any pointer (object or function)
- Template Parameters
-
| PtrType | Type of the pointer |
- Parameters
-
- 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.
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
-
| Callable | Type of the callable object |
- Parameters
-
| callable | The 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.
template<typename Ret, typename... Args>
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.