add event provider interface

This commit is contained in:
Green Sky 2023-07-21 22:07:23 +02:00
parent 835e9c95b7
commit 6f2bc75bfa
No known key found for this signature in database
2 changed files with 36 additions and 0 deletions

View File

@ -8,6 +8,8 @@ add_library(solanaceae_util
./solanaceae/util/simple_config_model.hpp
./solanaceae/util/simple_config_model.cpp
./solanaceae/util/event_provider.hpp
)
target_include_directories(solanaceae_util PUBLIC .)

View File

@ -0,0 +1,34 @@
#pragma once
#include <array>
#include <vector>
template<typename EventI>
struct EventProviderI {
using enumType = typename EventI::enumType;
virtual ~EventProviderI(void) {};
// TODO: unsub
virtual void subscribe(EventI* object, const enumType event_type) {
_subscribers.at(size_t(event_type)).push_back(object);
}
protected:
template<typename T>
bool dispatch(enumType event_type, const T& event) {
for (auto* zei : _subscribers.at(size_t(event_type))) {
if (zei->onEvent(event)) {
return true;
}
}
return false;
}
protected:
std::array<
std::vector<EventI*>,
size_t(enumType::MAX)
> _subscribers;
};