Files
CreatureLib/src/Battling/EventHooks/EventHook.hpp
Deukhoofd db002c784f
All checks were successful
continuous-integration/drone/push Build is passing
Resolve memory issue with Event Hooks.
2019-12-15 12:27:56 +01:00

27 lines
751 B
C++

#ifndef CREATURELIB_EVENTHOOK_HPP
#define CREATURELIB_EVENTHOOK_HPP
#include <vector>
#include "EventData.hpp"
#define EVENT_HOOK_FUNC(name) void (*name)(const EventData*)
namespace CreatureLib::Battling {
/// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces
/// for it.
class EventHook {
std::vector<EVENT_HOOK_FUNC()> _listeners;
public:
void RegisterListener(EVENT_HOOK_FUNC(func)) { _listeners.push_back(func); }
void TriggerEvent(EventData* eventData) {
for (auto listener : _listeners) {
listener(eventData);
}
delete eventData;
}
};
}
#endif // CREATURELIB_EVENTHOOK_HPP