diff --git a/CMakeLists.txt b/CMakeLists.txt index 7217b41..17a0929 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 .) diff --git a/solanaceae/util/event_provider.hpp b/solanaceae/util/event_provider.hpp new file mode 100644 index 0000000..410d54e --- /dev/null +++ b/solanaceae/util/event_provider.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +template +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 + 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, + size_t(enumType::MAX) + > _subscribers; +}; +