add event provider interface
This commit is contained in:
parent
835e9c95b7
commit
6f2bc75bfa
@ -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 .)
|
||||
|
34
solanaceae/util/event_provider.hpp
Normal file
34
solanaceae/util/event_provider.hpp
Normal 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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user