From 26c3b5c7a509d0123719f2a6537399c332a48011 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 1 Jan 2020 23:13:46 +0800 Subject: ... --- include/cru/common/event.hpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'include/cru') diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index 55a00d7e..bc8670df 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -3,11 +3,12 @@ #include "self_resolvable.hpp" +#include #include -#include #include #include #include +#include namespace cru { class EventRevoker; @@ -132,17 +133,13 @@ class Event : public details::EventBase, public IEvent { EventRevoker AddHandler(const EventHandler& handler) override { const auto token = current_token_++; - this->handler_data_list_.emplace_after(this->last_handler_iterator_, token, - handler); - ++(this->last_handler_iterator_); + this->handler_data_list_.emplace_back(token, handler); return CreateRevoker(token); } EventRevoker AddHandler(EventHandler&& handler) override { const auto token = current_token_++; - this->handler_data_list_.emplace_after(this->last_handler_iterator_, token, - std::move(handler)); - ++(this->last_handler_iterator_); + this->handler_data_list_.emplace_back(token, std::move(handler)); return CreateRevoker(token); } @@ -153,11 +150,10 @@ class Event : public details::EventBase, public IEvent { // is called, so even if you delete a handler during this period, all handlers // in the snapshot will be executed. void Raise(typename IEvent::EventArgs args) { - std::forward_list handlers; - auto iter = handlers.cbefore_begin(); + std::vector handlers; + handlers.reserve(this->handler_data_list_.size()); for (const auto& data : this->handler_data_list_) { - handlers.insert_after(iter, data.handler); - iter++; + handlers.push_back(data.handler); } for (const auto& handler : handlers) { handler(args); @@ -166,16 +162,16 @@ class Event : public details::EventBase, public IEvent { protected: void RemoveHandler(EventHandlerToken token) override { - this->handler_data_list_.remove_if( + const auto find_result = std::find_if( + this->handler_data_list_.cbegin(), this->handler_data_list_.cend(), [token](const HandlerData& data) { return data.token == token; }); + if (find_result != this->handler_data_list_.cend()) { + this->handler_data_list_.erase(find_result); + } } private: - std::forward_list handler_data_list_{}; - typename std::forward_list< - HandlerData>::const_iterator last_handler_iterator_ = - this->handler_data_list_ - .cbefore_begin(); // remember the last handler to make push back O(1) + std::vector handler_data_list_; EventHandlerToken current_token_ = 0; }; -- cgit v1.2.3