Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Event

Add this in your code:

#include <engine/event_system/event_system.h>

Description

This class gives the possibility to bind a function to an event.
When a event object is destroyed, all bound functions are unbound.

This class is using templates.

warning

If your bound functions reference an object that is about to be destroyed, make sure to unbind them manually beforehand.
Failing to do so may lead to undefined behavior such as accessing dangling pointers or crashing the application.

template<typename... Args>
class Event

Event without argument:

Event<> myEvent;

Event with arguments:

Event<int, const std::string&> myEvent;

Public methods


Bind

Bind a simple function.

Parameters:

  • function: Pointer to the function to bind
void Bind(void(*function)(Args...))

Code sample:

void MyFunction(int a, const std::string& b) { }

class MyClass
{
public:
    static void MyStaticFunction(int a, const std::string& b) { }
};

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyFunction);
myEvent.Bind(&MyClass::MyStaticFunction);

Bind

Bind a function linked to an object.

Parameters:

  • function: Pointer to the function to bind
  • obj: Pointer to the object
template<typename ObjType>
void Bind(void(ObjType::* function)(Args...), ObjType* obj)

Code sample:

class MyClass
{
public:
    void MyFunction(int a, const std::string& b) { }
};

MyClass myClassInstance;

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyClass::MyFunction, &myClassInstance);

Unbind

Unbind a simple function.

Parameters:

  • function: Pointer to the function to unbind
void Unbind(void(*function)(Args...))

Code sample:

void MyFunction(int a, const std::string& b) { }

class MyClass
{
public:
    static void MyStaticFunction(int a, const std::string& b) { }
};

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyFunction);
myEvent.Bind(&MyClass::MyStaticFunction);
//...
myEvent.Unbind(&MyFunction);
myEvent.Unbind(&MyClass::MyStaticFunction);

Unbind

Unbind a function linked to an object.

Parameters:

  • function: Pointer to the function to unbind
  • obj: Pointer to the object
template<typename ObjType>
void Unbind(void(ObjType::* function)(Args...), ObjType* obj)

Code sample:

class MyClass
{
public:
    void MyFunction(int a, const std::string& b) { }
};

MyClass myClassInstance;

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyClass::MyFunction, &myClassInstance);
//...
myEvent.Unbind(&MyClass::MyFunction, &myClassInstance);

UnbindAll

Unbind all bound function.

void UnbindAll()

Code sample:

void MyFunction(int a, const std::string b) { }

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyFunction);
//...
myEvent.UnbindAll();

Trigger

Call all bound functions.

Parameters:

  • args: All arguments to send
void Trigger(Args... args)

Code sample:

void MyFunction(int a, const std::string b) { }

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyFunction);
myEvent.Trigger(1, "Hello World!");

GetBoundFunctionCount

Get the number of listener.

size_t GetBoundFunctionCount()

Code sample:

void MyFunction(int a, const std::string b) { }
void MyFunction2(int a, const std::string b) { }

Event<int, const std::string&> myEvent;
myEvent.Bind(&MyFunction);
myEvent.Bind(&MyFunction2);

size_t count = myEvent.GetBoundFunctionCount(); // count = 2