From 6f2bc75bfa5e24ccedc9f94581dc9edccb6448f3 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Fri, 21 Jul 2023 22:07:23 +0200 Subject: [PATCH] add event provider interface --- CMakeLists.txt | 2 ++ solanaceae/util/event_provider.hpp | 34 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 solanaceae/util/event_provider.hpp 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; +}; +