CreatureLib/src/Battling/EventHooks/EventHook.hpp

28 lines
794 B
C++
Raw Normal View History

2019-12-15 11:24:08 +00:00
#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); }
2020-03-09 16:43:36 +00:00
void TriggerEvent(EventData* eventData) const {
2020-03-22 12:42:26 +00:00
AssertNotNull(eventData)
2019-12-15 11:24:08 +00:00
for (auto listener : _listeners) {
listener(eventData);
}
2019-12-15 11:27:56 +00:00
delete eventData;
2019-12-15 11:24:08 +00:00
}
};
}
#endif // CREATURELIB_EVENTHOOK_HPP