-
Notifications
You must be signed in to change notification settings - Fork 0
SFME::Mediator
Sztergbaum Roman edited this page Feb 5, 2018
·
66 revisions
Welcome to the page of the SFME::Mediator module
A complete example can be found here
This class represents the SFME EventManager, you will be able to send and receive events through his class.
template <typename TEvent, typename ... Args>
void emit(Args &&... args) noexcept;This function is used to emit a certain type of event to which all those who have subscribed to this event will receive it.
- TEvent: The type of event you wish to broadcast.
- Args: The arguments possibly necessary to the creation of the TEvent object.
- TEvent must be a class derived from BaseEvent class.
#include <SFME/mediator/mediator.hpp>
//! User Event with non empty constructor
struct InputEvent : sfme::mediator::BaseEvent
{
InputEvent(char _keycode) noexcept : keycode(_keycode)
{
}
char keycode;
};
//! User Event default constructible.
struct ShutdownEvent : sfme::mediator::BaseEvent
{
};
int main()
{
sfme::mediator::EventManager evtMgr;
evtMgr.emit<InputEvent>('a');
evtMgr.emit<ShutdownEvent>();
return 0;
}
template <typename TEvent, typename TReceiver>
void subscribe(TReceiver &receiver) noexcept;- TEvent: Represents the type of event you want to subscribe.
- Receiver: Represents the instance of the class that wishes to subscribe to this type of event.
- TEvent must be a class derived from BaseEvent class.
- The instance that uses the subscribe member function must be derived from the Receiver class. The use of the CRTP is here only for readability.
#include <SFME/mediator/mediator.hpp>
struct InputEvent : sfme::mediator::BaseEvent
{
};
class Example : public sfme::mediator::Receiver<Example>
{
public:
Example(sfme::mediator::EventManager &evtMgr) noexcept
{
evtMgr.subscribe<InputEvent>(*this);
}
void receive([[maybe_unused]] const InputEvent& evt) noexcept
{
}
};
int main()
{
sfme::mediator::EventManager evtMgr;
Example ex(evtMgr);
return 0;
}This class is the parent class of all SFME events, so if you want to create your own events you must inherit from this class.
void showEvents(std::string_view eventName) const noexcept;- eventName is the name of the event to display in the logger, when you compile in release no display is performed.
Content for Receiver.
Content of the examples.