From bd206060416168274282757a32ab66477b60db39 Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 25 Dec 2019 23:37:36 +0800 Subject: [PATCH] Update EventListener & EventDispatcher --- src/kiwano/2d/action/ActionManager.cpp | 10 +-- src/kiwano/2d/action/ActionManager.h | 2 +- src/kiwano/core/EventDispatcher.cpp | 56 +++++++++--- src/kiwano/core/EventDispatcher.h | 118 ++++++++++++++++--------- src/kiwano/core/EventListener.cpp | 1 + src/kiwano/core/EventListener.h | 95 ++++++++++++++++---- src/kiwano/core/Library.h | 25 +++++- src/kiwano/core/ObjectBase.h | 10 +++ src/kiwano/core/TimerManager.cpp | 12 ++- 9 files changed, 241 insertions(+), 88 deletions(-) diff --git a/src/kiwano/2d/action/ActionManager.cpp b/src/kiwano/2d/action/ActionManager.cpp index 349a78b1..ee792d04 100644 --- a/src/kiwano/2d/action/ActionManager.cpp +++ b/src/kiwano/2d/action/ActionManager.cpp @@ -58,12 +58,12 @@ namespace kiwano return action; } - Action* ActionManager::GetAction(String const & name) + ActionPtr ActionManager::GetAction(String const & name) { if (actions_.empty()) return nullptr; - for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) + for (auto& action : actions_) if (action->IsName(name)) return action; @@ -75,7 +75,7 @@ namespace kiwano if (actions_.empty()) return; - for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) + for (auto& action : actions_) { action->Resume(); } @@ -86,7 +86,7 @@ namespace kiwano if (actions_.empty()) return; - for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) + for (auto& action : actions_) { action->Pause(); } @@ -97,7 +97,7 @@ namespace kiwano if (actions_.empty()) return; - for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) + for (auto& action : actions_) { action->Stop(); } diff --git a/src/kiwano/2d/action/ActionManager.h b/src/kiwano/2d/action/ActionManager.h index a5d3858f..0d614ac7 100644 --- a/src/kiwano/2d/action/ActionManager.h +++ b/src/kiwano/2d/action/ActionManager.h @@ -48,7 +48,7 @@ namespace kiwano /// \~chinese /// @brief 获取指定名称的动画 /// @param name 动画名称 - Action* GetAction(String const& name); + ActionPtr GetAction(String const& name); /// \~chinese /// @brief 继续所有暂停动画 diff --git a/src/kiwano/core/EventDispatcher.cpp b/src/kiwano/core/EventDispatcher.cpp index bf54d608..54e7dd91 100644 --- a/src/kiwano/core/EventDispatcher.cpp +++ b/src/kiwano/core/EventDispatcher.cpp @@ -37,6 +37,11 @@ namespace kiwano { listener->callback_(evt); } + + if (listener->IsRemoveable()) + { + listeners_.remove(listener); + } } } @@ -70,7 +75,7 @@ namespace kiwano void EventDispatcher::StartListeners(String const & listener_name) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto& listener : listeners_) { if (listener->IsName(listener_name)) { @@ -81,7 +86,7 @@ namespace kiwano void EventDispatcher::StopListeners(String const & listener_name) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto& listener : listeners_) { if (listener->IsName(listener_name)) { @@ -92,21 +97,18 @@ namespace kiwano void EventDispatcher::RemoveListeners(String const & listener_name) { - EventListenerPtr next; - for (auto listener = listeners_.first_item(); listener; listener = next) + for (auto& listener : listeners_) { - next = listener->next_item(); - if (listener->IsName(listener_name)) { - listeners_.remove(listener); + listener->Remove(); } } } void EventDispatcher::StartListeners(const EventType& type) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto& listener : listeners_) { if (listener->type_ == type) { @@ -117,7 +119,7 @@ namespace kiwano void EventDispatcher::StopListeners(const EventType& type) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto& listener : listeners_) { if (listener->type_ == type) { @@ -128,16 +130,42 @@ namespace kiwano void EventDispatcher::RemoveListeners(const EventType& type) { - EventListenerPtr next; - for (auto listener = listeners_.first_item(); listener; listener = next) + for (auto& listener : listeners_) { - next = listener->next_item(); - if (listener->type_ == type) { - listeners_.remove(listener); + listener->Remove(); } } } + void EventDispatcher::StartAllListeners() + { + for (auto& listener : listeners_) + { + listener->Start(); + } + } + + void EventDispatcher::StopAllListeners() + { + for (auto& listener : listeners_) + { + listener->Stop(); + } + } + + void EventDispatcher::RemoveAllListeners() + { + for (auto& listener : listeners_) + { + listener->Remove(); + } + } + + const EventDispatcher::Listeners& EventDispatcher::GetAllListeners() const + { + return listeners_; + } + } diff --git a/src/kiwano/core/EventDispatcher.h b/src/kiwano/core/EventDispatcher.h index eda44e5c..0cbe879c 100644 --- a/src/kiwano/core/EventDispatcher.h +++ b/src/kiwano/core/EventDispatcher.h @@ -23,34 +23,40 @@ namespace kiwano { + /** + * \~chinese + * @brief 事件分发系统 + */ class KGE_API EventDispatcher { + public: using Listeners = IntrusiveList; - public: - // 添加监听器 - EventListener* AddListener( - EventListenerPtr listener - ); + /// \~chinese + /// @brief 添加监听器 + EventListener* AddListener(EventListenerPtr listener); - // 添加监听器 - EventListener* AddListener( - EventListener* listener - ); + /// \~chinese + /// @brief 添加监听器 + EventListener* AddListener(EventListener* listener); - // 添加监听器 - EventListener* AddListener( - EventType type, - EventListener::Callback callback - ); + /// \~chinese + /// @brief 添加监听器 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener* AddListener(EventType type, EventListener::Callback callback); - // 添加监听器 - EventListener* AddListener( - String const& name, - EventType type, - EventListener::Callback callback - ); + /// \~chinese + /// @brief 添加监听器 + /// @param name 监听器名称 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener* AddListener(String const& name, EventType type, EventListener::Callback callback); + /// \~chinese + /// @brief 添加监听器 + /// @tparam _EventTy 事件类型 + /// @param callback 回调函数 template < typename _EventTy, typename = typename std::enable_if::value, int>::type @@ -60,6 +66,11 @@ namespace kiwano return AddListener(KGE_EVENT(_EventTy), callback); } + /// \~chinese + /// @brief 添加监听器 + /// @tparam _EventTy 事件类型 + /// @param name 监听器名称 + /// @param callback 回调函数 template < typename _EventTy, typename = typename std::enable_if::value, int> @@ -69,36 +80,55 @@ namespace kiwano return AddListener(name, KGE_EVENT(_EventTy), callback); } - // 启动监听器 - void StartListeners( - String const& listener_name - ); + /// \~chinese + /// @brief 启动监听器 + /// @param name 监听器名称 + void StartListeners(String const& name); - // 停止监听器 - void StopListeners( - String const& listener_name - ); + /// \~chinese + /// @brief 停止监听器 + /// @param name 监听器名称 + void StopListeners(String const& name); - // 移除监听器 - void RemoveListeners( - String const& listener_name - ); + /// \~chinese + /// @brief 移除监听器 + /// @param name 监听器名称 + void RemoveListeners(String const& name); - // 启动监听器 - void StartListeners( - const EventType& type - ); + /// \~chinese + /// @brief 启动监听器 + /// @param type 监听的事件类型 + void StartListeners(const EventType& type); - // 停止监听器 - void StopListeners( - const EventType& type - ); + /// \~chinese + /// @brief 停止监听器 + /// @param type 监听的事件类型 + void StopListeners(const EventType& type); - // 移除监听器 - void RemoveListeners( - const EventType& type - ); + /// \~chinese + /// @brief 移除监听器 + /// @param type 监听的事件类型 + void RemoveListeners(const EventType& type); + /// \~chinese + /// @brief 启动所有监听器 + void StartAllListeners(); + + /// \~chinese + /// @brief 停止所有监听器 + void StopAllListeners(); + + /// \~chinese + /// @brief 移除所有监听器 + void RemoveAllListeners(); + + /// \~chinese + /// @brief 获取所有监听器 + const Listeners& GetAllListeners() const; + + /// \~chinese + /// @brief 分发事件 + /// @param evt 事件 virtual void Dispatch(Event& evt); private: diff --git a/src/kiwano/core/EventListener.cpp b/src/kiwano/core/EventListener.cpp index 1250a80e..872c62ca 100644 --- a/src/kiwano/core/EventListener.cpp +++ b/src/kiwano/core/EventListener.cpp @@ -27,6 +27,7 @@ namespace kiwano : type_() , callback_() , running_(true) + , removeable_(false) { } diff --git a/src/kiwano/core/EventListener.h b/src/kiwano/core/EventListener.h index b42c9ea1..147c22e4 100644 --- a/src/kiwano/core/EventListener.h +++ b/src/kiwano/core/EventListener.h @@ -30,7 +30,10 @@ namespace kiwano KGE_DECLARE_SMART_PTR(EventListener); - // 事件监听器 + /** + * \~chinese + * @brief 事件监听器 + */ class KGE_API EventListener : public ObjectBase , protected IntrusiveListItem @@ -39,57 +42,107 @@ namespace kiwano friend IntrusiveList; public: + /// \~chinese + /// @brief 监听器回调函数 using Callback = Function; + /// \~chinese + /// @brief 构造空监听器 EventListener(); - EventListener( - EventType type, - Callback const& callback - ); + /// \~chinese + /// @brief 构造监听器 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener(EventType type, Callback const& callback); - EventListener( - String const& name, - EventType type, - Callback const& callback - ); + /// \~chinese + /// @brief 构造监听器 + /// @param name 监听器名称 + /// @param type 监听的事件类型 + /// @param callback 回调函数 + EventListener(String const& name, EventType type, Callback const& callback); + /// \~chinese + /// @brief 构造监听器 + /// @tparam _EventTy 事件类型 + /// @param callback 回调函数 template < typename _EventTy, typename = typename std::enable_if::value, int>::type > - EventListener(Callback const& callback) + inline EventListener(Callback const& callback) : EventListener(KGE_EVENT(_EventTy), callback) { } + /// \~chinese + /// @brief 构造监听器 + /// @tparam _EventTy 事件类型 + /// @param name 监听器名称 + /// @param callback 回调函数 template < typename _EventTy, typename = typename std::enable_if::value, int>::type > - EventListener(String const& name, Callback const& callback) + inline EventListener(String const& name, Callback const& callback) : EventListener(name, KGE_EVENT(_EventTy), callback) { } virtual ~EventListener(); + /// \~chinese + /// @brief 启动监听器 void Start(); + /// \~chinese + /// @brief 停止监听器 void Stop(); + /// \~chinese + /// @brief 移除监听器 + void Remove(); + + /// \~chinese + /// @brief 是否正在运行 bool IsRunning() const; - Callback GetCallback() const; + /// \~chinese + /// @brief 是否可移除 + bool IsRemoveable() const; + /// \~chinese + /// @brief 获取回调函数 + const Callback& GetCallback() const; + + /// \~chinese + /// @brief 设置回调函数 void SetCallback(Callback const& cb); - EventType const& GetEventType() const; + /// \~chinese + /// @brief 获取监听的事件类型 + EventType GetEventType() const; + /// \~chinese + /// @brief 设置监听的事件类型 void SetEventType(EventType const& type); + /// \~chinese + /// @brief 设置监听的事件类型 + /// @tparam _EventTy 事件类型 + template < + typename _EventTy, + typename = typename std::enable_if::value, int>::type + > + inline void SetEventType() + { + SetEventType(KGE_EVENT(_EventTy)); + } + private: bool running_; + bool removeable_; EventType type_; Callback callback_; }; @@ -105,12 +158,22 @@ namespace kiwano running_ = false; } + inline void EventListener::Remove() + { + removeable_ = true; + } + inline bool EventListener::IsRunning() const { return running_; } - inline EventListener::Callback EventListener::GetCallback() const + inline bool EventListener::IsRemoveable() const + { + return removeable_; + } + + inline const EventListener::Callback& EventListener::GetCallback() const { return callback_; } @@ -120,7 +183,7 @@ namespace kiwano callback_ = cb; } - inline EventType const& EventListener::GetEventType() const + inline EventType EventListener::GetEventType() const { return type_; } diff --git a/src/kiwano/core/Library.h b/src/kiwano/core/Library.h index 08ad13b3..6b7b8351 100644 --- a/src/kiwano/core/Library.h +++ b/src/kiwano/core/Library.h @@ -24,22 +24,45 @@ namespace kiwano { - // DLL 库 + /** + * \~chinese + * @brief DLL库 + */ class KGE_API Library { public: + /// \~chinese + /// @brief 构造DLL库 Library(); + + /// \~chinese + /// @brief 构造DLL库 + /// @param lib DLL文件路径 Library(String const& lib); + virtual ~Library(); + /// \~chinese + /// @brief 加载DLL + /// @param lib DLL文件路径 bool Load(String const& lib); + /// \~chinese + /// @brief 是否有效 bool IsValid() const; + /// \~chinese + /// @brief 释放DLL void Free(); + /// \~chinese + /// @brief 检索指定的DLL中的输出库函数地址 + /// @param proc_name 函数名 FARPROC GetProcess(String const& proc_name); + /// \~chinese + /// @brief 检索指定的DLL中的输出库函数地址 + /// @param proc_name 函数名 template inline _Proc GetProcess(String const& proc_name) { diff --git a/src/kiwano/core/ObjectBase.h b/src/kiwano/core/ObjectBase.h index 4990f412..e4f3ee70 100644 --- a/src/kiwano/core/ObjectBase.h +++ b/src/kiwano/core/ObjectBase.h @@ -72,14 +72,24 @@ namespace kiwano String DumpObject(); public: + /// \~chinese + /// @brief 是否启用了内存泄漏追踪 static bool IsTracingLeaks(); + /// \~chinese + /// @brief 开始追踪内存泄漏 static void StartTracingLeaks(); + /// \~chinese + /// @brief 停止追踪内存泄漏 static void StopTracingLeaks(); + /// \~chinese + /// @brief 打印所有追踪中的对象信息 static void DumpTracingObjects(); + /// \~chinese + /// @brief 获取所有追踪中的对象 static Vector& GetTracingObjects(); private: diff --git a/src/kiwano/core/TimerManager.cpp b/src/kiwano/core/TimerManager.cpp index a03f207a..80e37119 100644 --- a/src/kiwano/core/TimerManager.cpp +++ b/src/kiwano/core/TimerManager.cpp @@ -69,7 +69,7 @@ namespace kiwano if (timers_.empty()) return; - for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) + for (auto& timer : timers_) { if (timer->IsName(name)) { @@ -83,7 +83,7 @@ namespace kiwano if (timers_.empty()) return; - for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) + for (auto& timer : timers_) { if (timer->IsName(name)) { @@ -97,10 +97,8 @@ namespace kiwano if (timers_.empty()) return; - TimerPtr next; - for (auto timer = timers_.first_item(); timer; timer = next) + for (auto& timer : timers_) { - next = timer->next_item(); if (timer->IsName(name)) { timer->Remove(); @@ -113,7 +111,7 @@ namespace kiwano if (timers_.empty()) return; - for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) + for (auto& timer : timers_) { timer->Stop(); } @@ -124,7 +122,7 @@ namespace kiwano if (timers_.empty()) return; - for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) + for (auto& timer : timers_) { timer->Start(); }