aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/HandlerRegistry.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
committercrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
commit7f15a1ff9a2007e119798053083a0a87d042990a (patch)
treecb35c01a7eaee867376d959b96c9bbd15df939e5 /include/cru/common/HandlerRegistry.hpp
parent74956951ee663012df0c3fe4ebe29799cb2f7732 (diff)
parent7703063a5816b089483e78ccd74bb9902ccfbea8 (diff)
downloadcru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.gz
cru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.bz2
cru-7f15a1ff9a2007e119798053083a0a87d042990a.zip
Merge branch 'master' of https://github.com/crupest/CruUI
Diffstat (limited to 'include/cru/common/HandlerRegistry.hpp')
-rw-r--r--include/cru/common/HandlerRegistry.hpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/include/cru/common/HandlerRegistry.hpp b/include/cru/common/HandlerRegistry.hpp
new file mode 100644
index 00000000..bd74a9e0
--- /dev/null
+++ b/include/cru/common/HandlerRegistry.hpp
@@ -0,0 +1,86 @@
+#pragma once
+#include "Base.hpp"
+
+#include <algorithm>
+#include <functional>
+#include <utility>
+#include <vector>
+
+namespace cru {
+
+template <typename T>
+class HandlerRegistryIterator {
+ public:
+ using RawIterator =
+ typename std::vector<std::pair<int, std::function<T>>>::const_iterator;
+
+ explicit HandlerRegistryIterator(RawIterator raw) : raw_(std::move(raw)) {}
+
+ CRU_DELETE_COPY(HandlerRegistryIterator)
+ CRU_DELETE_MOVE(HandlerRegistryIterator)
+
+ ~HandlerRegistryIterator() = default;
+
+ const std::function<T>& operator*() const { return raw_->second; }
+ const std::function<T>* operator->() const { return &raw_->second; }
+
+ HandlerRegistryIterator& operator++() {
+ ++raw_;
+ return *this;
+ }
+
+ HandlerRegistryIterator operator++(int) {
+ auto c = *this;
+ this->operator++();
+ return c;
+ }
+
+ bool operator==(const HandlerRegistryIterator<T>& other) const {
+ return this->raw_ == other.raw_;
+ }
+
+ bool operator!=(const HandlerRegistryIterator<T>& other) const {
+ return !this->operator==(other);
+ }
+
+ private:
+ RawIterator raw_;
+};
+
+template <typename T>
+class HandlerRegistry final {
+ public:
+ HandlerRegistry() = default;
+ CRU_DEFAULT_COPY(HandlerRegistry)
+ CRU_DEFAULT_MOVE(HandlerRegistry)
+ ~HandlerRegistry() = default;
+
+ public:
+ int AddHandler(std::function<T> handler) {
+ auto id = current_id_++;
+ handler_list_.push_back({id, std::move(handler)});
+ }
+
+ void RemoveHandler(int id) {
+ auto result = std::find_if(handler_list_.cbegin(), handler_list_.cend(),
+ [id](const std::pair<int, std::function<T>>& d) {
+ return d.first == id;
+ });
+ if (result != handler_list_.cend()) {
+ handler_list_.erase(result);
+ }
+ }
+
+ HandlerRegistryIterator<T> begin() const {
+ return HandlerRegistryIterator<T>(handler_list_.begin());
+ }
+
+ HandlerRegistryIterator<T> end() const {
+ return HandlerRegistryIterator<T>(handler_list_.begin());
+ }
+
+ private:
+ int current_id_ = 1;
+ std::vector<std::pair<int, std::function<T>>> handler_list_;
+};
+} // namespace cru