ezEngine  Release 25.03
ezDelegate< T, DataSize > Struct Template Reference

A generic delegate class which supports static functions and member functions. More...

#include <Delegate.h>

Inheritance diagram for ezDelegate< T, DataSize >:

Additional Inherited Members

- Protected Attributes inherited from ezDelegateBase
InstancePtr m_Instance
 

Detailed Description

template<typename T, ezUInt32 DataSize = 16>
struct ezDelegate< T, DataSize >

A generic delegate class which supports static functions and member functions.

A delegate is a function pointer that may be used to call both simple C functions, as well as member functions of some class, which requires a 'this' pointer to go along with. The delegate can capture both these use cases dynamically, so this distinction does not need to be made statically, but is the choice of the user who assigns some function to the delegate.

Delegates have a rather strange syntax:

using SomeCallback = ezDelegate<void (ezUInt32, float)>;

This defines a type 'SomeCallback' that can call any function that returns void and takes two parameters, the first being ezUInt32, the second being a float parameter. Now you can use SomeCallback just like any other function pointer type.

Assigning a C function as the value to the delegate is straight-forward:

void SomeFunction(ezUInt32 i, float f);
SomeCallback callback = SomeFunction;

Assigning a member function to the delegate is a bit more complex:

class SomeClass {
void SomeFunction(ezUInt32 i, float f);
};
SomeClass instance;
SomeCallback callback = ezDelegate<void (ezUInt32, float)>(&SomeClass::SomeFunction, &instance);

Here you have to construct a delegate of the proper type and pass along both the member function pointer as well as the class instance to call the function on.

Using the delegate to call a function is straight forward again:

SomeCallback callback = ...;
if (callback.IsValid())
callback(4, 2.0f);

Just treat the delegate like a simple C function and call it. Internally it will dispatch the call to whatever function is bound to it, independent of whether it is a regular C function or a member function.

The check to 'IsValid' is only required when the delegate might have a nullptr bound (i.e. no function has been bound to it). When you know the delegate is always bound, this is not necessary.

Note
If you are wondering where the code is, the delegate is implemented with macro and template magic in Delegate_inl.h and DelegateHelper_inl.h.

The documentation for this struct was generated from the following file:
ezDelegate
A generic delegate class which supports static functions and member functions.
Definition: Delegate.h:75