From f19d52b6625f31222cde16684152a546ae29d371 Mon Sep 17 00:00:00 2001 From: Nomango Date: Sat, 10 Oct 2020 15:34:00 +0800 Subject: [PATCH] update EventListener --- .gitignore | 1 + src/kiwano/event/EventDispatcher.cpp | 39 +------------ src/kiwano/event/EventDispatcher.h | 15 ----- src/kiwano/event/EventListener.cpp | 66 ++++++++++++++------- src/kiwano/event/EventListener.h | 85 ++++++---------------------- src/kiwano/event/EventType.h | 79 +++++++++++++++++++++----- 6 files changed, 133 insertions(+), 152 deletions(-) diff --git a/.gitignore b/.gitignore index 7c3945ca..b0f0dad1 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ Release/ # Application folders .vs .idea +._Kiwano.sln # vs2010 ipch/ diff --git a/src/kiwano/event/EventDispatcher.cpp b/src/kiwano/event/EventDispatcher.cpp index 71abe117..b131819f 100644 --- a/src/kiwano/event/EventDispatcher.cpp +++ b/src/kiwano/event/EventDispatcher.cpp @@ -34,7 +34,7 @@ bool EventDispatcher::DispatchEvent(Event* evt) next = listener->GetNext(); if (listener->IsRunning()) - listener->Receive(evt); + listener->Handle(evt); if (listener->IsRemoveable()) listeners_.Remove(listener); @@ -58,13 +58,13 @@ EventListener* EventDispatcher::AddListener(EventListenerPtr listener) EventListener* EventDispatcher::AddListener(const String& name, EventType type, EventListener::Callback callback) { - auto lis = MakePtr(name, type, callback); + auto lis = EventListener::Create(name, type, callback); return AddListener(lis); } EventListener* EventDispatcher::AddListener(EventType type, EventListener::Callback callback) { - auto lis = MakePtr(type, callback); + auto lis = EventListener::Create(type, callback); return AddListener(lis); } @@ -101,39 +101,6 @@ void EventDispatcher::RemoveListeners(const String& name) } } -void EventDispatcher::StartListeners(const EventType& type) -{ - for (auto& listener : listeners_) - { - if (listener->GetEventType() == type) - { - listener->Start(); - } - } -} - -void EventDispatcher::StopListeners(const EventType& type) -{ - for (auto& listener : listeners_) - { - if (listener->GetEventType() == type) - { - listener->Stop(); - } - } -} - -void EventDispatcher::RemoveListeners(const EventType& type) -{ - for (auto& listener : listeners_) - { - if (listener->GetEventType() == type) - { - listener->Remove(); - } - } -} - void EventDispatcher::StartAllListeners() { for (auto& listener : listeners_) diff --git a/src/kiwano/event/EventDispatcher.h b/src/kiwano/event/EventDispatcher.h index 56be5fab..5c5e2952 100644 --- a/src/kiwano/event/EventDispatcher.h +++ b/src/kiwano/event/EventDispatcher.h @@ -90,21 +90,6 @@ public: /// @param name 监听器名称 void RemoveListeners(const String& name); - /// \~chinese - /// @brief 启动监听器 - /// @param type 监听的事件类型 - void StartListeners(const EventType& type); - - /// \~chinese - /// @brief 停止监听器 - /// @param type 监听的事件类型 - void StopListeners(const EventType& type); - - /// \~chinese - /// @brief 移除监听器 - /// @param type 监听的事件类型 - void RemoveListeners(const EventType& type); - /// \~chinese /// @brief 启动所有监听器 void StartAllListeners(); diff --git a/src/kiwano/event/EventListener.cpp b/src/kiwano/event/EventListener.cpp index 31800fb9..5821ebef 100644 --- a/src/kiwano/event/EventListener.cpp +++ b/src/kiwano/event/EventListener.cpp @@ -25,35 +25,63 @@ namespace kiwano { EventListener::EventListener() - : type_() - , callback_() - , running_(true) + : running_(true) , removeable_(false) , swallow_(false) { } -EventListener::EventListener(EventType type, const Callback& callback) - : EventListener() -{ - this->SetEventType(type); - this->SetCallback(callback); -} - -EventListener::EventListener(const String& name, EventType type, const Callback& callback) - : EventListener(type, callback) -{ - this->SetName(name); -} - EventListener::~EventListener() {} -void EventListener::Receive(Event* evt) +class CallbackEventListener : public EventListener { - if (ShouldHandle(evt) && callback_) +public: + CallbackEventListener(EventType type, const Callback& cb) + : type_(type) + , cb_(cb) { - callback_(evt); } + + void Handle(Event* evt) override + { + if (type_.IsNull() || type_ == evt->GetType()) + { + if (cb_) + { + cb_(evt); + } + } + } + +private: + EventType type_; + Callback cb_; +}; + +EventListenerPtr EventListener::Create(const Callback& callback) +{ + EventListenerPtr ptr = new CallbackEventListener(EventType(), callback); + return ptr; +} + +EventListenerPtr EventListener::Create(const String& name, const Callback& callback) +{ + EventListenerPtr ptr = new CallbackEventListener(EventType(), callback); + ptr->SetName(name); + return ptr; +} + +EventListenerPtr EventListener::Create(EventType type, const Callback& callback) +{ + EventListenerPtr ptr = new CallbackEventListener(type, callback); + return ptr; +} + +EventListenerPtr EventListener::Create(const String& name, EventType type, const Callback& callback) +{ + EventListenerPtr ptr = new CallbackEventListener(type, callback); + ptr->SetName(name); + return ptr; } } // namespace kiwano diff --git a/src/kiwano/event/EventListener.h b/src/kiwano/event/EventListener.h index d6383662..621619cd 100644 --- a/src/kiwano/event/EventListener.h +++ b/src/kiwano/event/EventListener.h @@ -48,20 +48,32 @@ public: EventListener(); + virtual ~EventListener(); + /// \~chinese /// @brief 创建监听器 - /// @param type 监听的事件类型 /// @param callback 回调函数 - EventListener(EventType type, const Callback& callback); + static EventListenerPtr Create(const Callback& callback); /// \~chinese /// @brief 创建监听器 /// @param name 监听器名称 /// @param type 监听的事件类型 /// @param callback 回调函数 - EventListener(const String& name, EventType type, const Callback& callback); + static EventListenerPtr Create(const String& name, const Callback& callback); - virtual ~EventListener(); + /// \~chinese + /// @brief 创建监听器 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + static EventListenerPtr Create(EventType type, const Callback& callback); + + /// \~chinese + /// @brief 创建监听器 + /// @param name 监听器名称 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + static EventListenerPtr Create(const String& name, EventType type, const Callback& callback); /// \~chinese /// @brief 启动监听器 @@ -93,45 +105,13 @@ public: void SetSwallowEnabled(bool enabled); /// \~chinese - /// @brief 获取回调函数 - const Callback& GetCallback() const; - - /// \~chinese - /// @brief 设置回调函数 - void SetCallback(const Callback& cb); - - /// \~chinese - /// @brief 获取监听的事件类型 - EventType GetEventType() const; - - /// \~chinese - /// @brief 设置监听的事件类型 - void SetEventType(const EventType& type); - - /// \~chinese - /// @brief 判断是否处理事件 - virtual bool ShouldHandle(Event* evt) const; - - /// \~chinese - /// @brief 设置监听的事件类型 - /// @tparam _EventTy 事件类型 - template - inline void SetEventType() - { - static_assert(std::is_base_of::value, "_EventTy is not an event type."); - SetEventType(KGE_EVENT(_EventTy)); - } - - /// \~chinese - /// @brief 接收消息 - void Receive(Event* evt); + /// @brief 处理消息 + virtual void Handle(Event* evt) = 0; private: bool running_; bool removeable_; bool swallow_; - EventType type_; - Callback callback_; }; inline void EventListener::Start() @@ -169,33 +149,4 @@ inline void EventListener::SetSwallowEnabled(bool enabled) swallow_ = enabled; } -inline const EventListener::Callback& EventListener::GetCallback() const -{ - return callback_; -} - -inline void EventListener::SetCallback(const Callback& cb) -{ - callback_ = cb; -} - -inline EventType EventListener::GetEventType() const -{ - return type_; -} - -inline void EventListener::SetEventType(const EventType& type) -{ - type_ = type; -} - -inline bool EventListener::ShouldHandle(Event* evt) const -{ - if (evt) - { - return evt->GetType() == type_; - } - return false; -} - } // namespace kiwano diff --git a/src/kiwano/event/EventType.h b/src/kiwano/event/EventType.h index 53dd8d24..ae104bc0 100644 --- a/src/kiwano/event/EventType.h +++ b/src/kiwano/event/EventType.h @@ -32,24 +32,28 @@ namespace kiwano /// \~chinese /// @brief 事件类型 -class EventType : public std::type_index +class EventType { - class Dummy - { - }; - public: - /// \~chinese - /// @brief 构建事件类型 EventType(); - using std::type_index::type_index; - using std::type_index::operator==; - using std::type_index::operator!=; - using std::type_index::operator<; - using std::type_index::operator>=; - using std::type_index::operator>; - using std::type_index::operator<=; + EventType(const std::type_index& type); + + /// \~chinese + /// @brief 是否是空类型 + bool IsNull() const; + + const std::type_index& GetType() const; + + bool operator==(const EventType& rhs) const; + bool operator!=(const EventType& rhs) const; + bool operator<(const EventType& rhs) const; + bool operator<=(const EventType& rhs) const; + bool operator>(const EventType& rhs) const; + bool operator>=(const EventType& rhs) const; + +private: + std::type_index type_; }; /** @} */ @@ -57,8 +61,53 @@ public: #define KGE_EVENT(EVENT_TYPE) ::kiwano::EventType(typeid(EVENT_TYPE)) inline EventType::EventType() - : std::type_index(typeid(EventType::Dummy)) + : type_(typeid(void)) { } +inline EventType::EventType(const std::type_index& type) + : type_(type) +{ +} + +inline bool EventType::IsNull() const +{ + return type_ == typeid(void); +} + +inline const std::type_index& EventType::GetType() const +{ + return type_; +} + +inline bool EventType::operator==(const EventType& rhs) const +{ + return type_ == rhs.type_; +} + +inline bool EventType::operator!=(const EventType& rhs) const +{ + return type_ != rhs.type_; +} + +inline bool EventType::operator<(const EventType& rhs) const +{ + return type_ < rhs.type_; +} + +inline bool EventType::operator<=(const EventType& rhs) const +{ + return type_ <= rhs.type_; +} + +inline bool EventType::operator>(const EventType& rhs) const +{ + return type_ > rhs.type_; +} + +inline bool EventType::operator>=(const EventType& rhs) const +{ + return type_ >= rhs.type_; +} + } // namespace kiwano