aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/event.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-01 17:38:45 +0800
committercrupest <crupest@outlook.com>2020-01-01 17:38:45 +0800
commitae6f797561cdfa438ebef1fbbf94d784d315e655 (patch)
tree33659ea0448a6068c4d07b15ca488ab5495906ce /include/cru/common/event.hpp
parentf3719a5128ef911ec61c43a6eed34203f3b9bbb4 (diff)
downloadcru-ae6f797561cdfa438ebef1fbbf94d784d315e655.tar.gz
cru-ae6f797561cdfa438ebef1fbbf94d784d315e655.tar.bz2
cru-ae6f797561cdfa438ebef1fbbf94d784d315e655.zip
...
Diffstat (limited to 'include/cru/common/event.hpp')
-rw-r--r--include/cru/common/event.hpp17
1 files changed, 12 insertions, 5 deletions
diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp
index 33b2e3f6..55a00d7e 100644
--- a/include/cru/common/event.hpp
+++ b/include/cru/common/event.hpp
@@ -3,6 +3,7 @@
#include "self_resolvable.hpp"
+#include <cassert>
#include <forward_list>
#include <functional>
#include <memory>
@@ -99,7 +100,8 @@ struct IEvent {
IEvent(IEvent&& other) = delete;
IEvent& operator=(const IEvent& other) = delete;
IEvent& operator=(IEvent&& other) = delete;
- ~IEvent() = default;
+ ~IEvent() = default; // Note that user can't destroy a Event via IEvent. So
+ // destructor should be protected.
public:
virtual EventRevoker AddHandler(const EventHandler& handler) = 0;
@@ -130,14 +132,16 @@ class Event : public details::EventBase, public IEvent<TEventArgs> {
EventRevoker AddHandler(const EventHandler& handler) override {
const auto token = current_token_++;
- this->handler_data_list_.emplace_after(this->last_handler_iterator_ ,token, handler);
+ this->handler_data_list_.emplace_after(this->last_handler_iterator_, token,
+ handler);
++(this->last_handler_iterator_);
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->handler_data_list_.emplace_after(this->last_handler_iterator_, token,
+ std::move(handler));
++(this->last_handler_iterator_);
return CreateRevoker(token);
}
@@ -163,12 +167,15 @@ class Event : public details::EventBase, public IEvent<TEventArgs> {
protected:
void RemoveHandler(EventHandlerToken token) override {
this->handler_data_list_.remove_if(
- [token](const HandlerData& data) { return data.token == token; });
+ [token](const HandlerData& data) { return data.token == token; });
}
private:
std::forward_list<HandlerData> 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)
+ 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)
EventHandlerToken current_token_ = 0;
};