Update EventListener & EventDispatcher

This commit is contained in:
Nomango 2019-12-25 23:37:36 +08:00
parent 11c43a0731
commit bd20606041
9 changed files with 241 additions and 88 deletions

View File

@ -58,12 +58,12 @@ namespace kiwano
return action; return action;
} }
Action* ActionManager::GetAction(String const & name) ActionPtr ActionManager::GetAction(String const & name)
{ {
if (actions_.empty()) if (actions_.empty())
return nullptr; return nullptr;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) for (auto& action : actions_)
if (action->IsName(name)) if (action->IsName(name))
return action; return action;
@ -75,7 +75,7 @@ namespace kiwano
if (actions_.empty()) if (actions_.empty())
return; return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) for (auto& action : actions_)
{ {
action->Resume(); action->Resume();
} }
@ -86,7 +86,7 @@ namespace kiwano
if (actions_.empty()) if (actions_.empty())
return; return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) for (auto& action : actions_)
{ {
action->Pause(); action->Pause();
} }
@ -97,7 +97,7 @@ namespace kiwano
if (actions_.empty()) if (actions_.empty())
return; return;
for (auto action = actions_.first_item().get(); action; action = action->next_item().get()) for (auto& action : actions_)
{ {
action->Stop(); action->Stop();
} }

View File

@ -48,7 +48,7 @@ namespace kiwano
/// \~chinese /// \~chinese
/// @brief 获取指定名称的动画 /// @brief 获取指定名称的动画
/// @param name 动画名称 /// @param name 动画名称
Action* GetAction(String const& name); ActionPtr GetAction(String const& name);
/// \~chinese /// \~chinese
/// @brief 继续所有暂停动画 /// @brief 继续所有暂停动画

View File

@ -37,6 +37,11 @@ namespace kiwano
{ {
listener->callback_(evt); listener->callback_(evt);
} }
if (listener->IsRemoveable())
{
listeners_.remove(listener);
}
} }
} }
@ -70,7 +75,7 @@ namespace kiwano
void EventDispatcher::StartListeners(String const & listener_name) 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)) if (listener->IsName(listener_name))
{ {
@ -81,7 +86,7 @@ namespace kiwano
void EventDispatcher::StopListeners(String const & listener_name) 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)) if (listener->IsName(listener_name))
{ {
@ -92,21 +97,18 @@ namespace kiwano
void EventDispatcher::RemoveListeners(String const & listener_name) void EventDispatcher::RemoveListeners(String const & listener_name)
{ {
EventListenerPtr next; for (auto& listener : listeners_)
for (auto listener = listeners_.first_item(); listener; listener = next)
{ {
next = listener->next_item();
if (listener->IsName(listener_name)) if (listener->IsName(listener_name))
{ {
listeners_.remove(listener); listener->Remove();
} }
} }
} }
void EventDispatcher::StartListeners(const EventType& type) 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) if (listener->type_ == type)
{ {
@ -117,7 +119,7 @@ namespace kiwano
void EventDispatcher::StopListeners(const EventType& type) 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) if (listener->type_ == type)
{ {
@ -128,16 +130,42 @@ namespace kiwano
void EventDispatcher::RemoveListeners(const EventType& type) void EventDispatcher::RemoveListeners(const EventType& type)
{ {
EventListenerPtr next; for (auto& listener : listeners_)
for (auto listener = listeners_.first_item(); listener; listener = next)
{ {
next = listener->next_item();
if (listener->type_ == type) 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_;
}
} }

View File

@ -23,34 +23,40 @@
namespace kiwano namespace kiwano
{ {
/**
* \~chinese
* @brief
*/
class KGE_API EventDispatcher class KGE_API EventDispatcher
{ {
public:
using Listeners = IntrusiveList<EventListenerPtr>; using Listeners = IntrusiveList<EventListenerPtr>;
public: /// \~chinese
// 添加监听器 /// @brief 添加监听器
EventListener* AddListener( EventListener* AddListener(EventListenerPtr listener);
EventListenerPtr listener
);
// 添加监听器 /// \~chinese
EventListener* AddListener( /// @brief 添加监听器
EventListener* listener EventListener* AddListener(EventListener* listener);
);
// 添加监听器 /// \~chinese
EventListener* AddListener( /// @brief 添加监听器
EventType type, /// @param type 监听的事件类型
EventListener::Callback callback /// @param callback 回调函数
); EventListener* AddListener(EventType type, EventListener::Callback callback);
// 添加监听器 /// \~chinese
EventListener* AddListener( /// @brief 添加监听器
String const& name, /// @param name 监听器名称
EventType type, /// @param type 监听的事件类型
EventListener::Callback callback /// @param callback 回调函数
); EventListener* AddListener(String const& name, EventType type, EventListener::Callback callback);
/// \~chinese
/// @brief 添加监听器
/// @tparam _EventTy 事件类型
/// @param callback 回调函数
template < template <
typename _EventTy, typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
@ -60,6 +66,11 @@ namespace kiwano
return AddListener(KGE_EVENT(_EventTy), callback); return AddListener(KGE_EVENT(_EventTy), callback);
} }
/// \~chinese
/// @brief 添加监听器
/// @tparam _EventTy 事件类型
/// @param name 监听器名称
/// @param callback 回调函数
template < template <
typename _EventTy, typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int> typename = typename std::enable_if<IsEvent<_EventTy>::value, int>
@ -69,36 +80,55 @@ namespace kiwano
return AddListener(name, KGE_EVENT(_EventTy), callback); return AddListener(name, KGE_EVENT(_EventTy), callback);
} }
// 启动监听器 /// \~chinese
void StartListeners( /// @brief 启动监听器
String const& listener_name /// @param name 监听器名称
); void StartListeners(String const& name);
// 停止监听器 /// \~chinese
void StopListeners( /// @brief 停止监听器
String const& listener_name /// @param name 监听器名称
); void StopListeners(String const& name);
// 移除监听器 /// \~chinese
void RemoveListeners( /// @brief 移除监听器
String const& listener_name /// @param name 监听器名称
); void RemoveListeners(String const& name);
// 启动监听器 /// \~chinese
void StartListeners( /// @brief 启动监听器
const EventType& type /// @param type 监听的事件类型
); void StartListeners(const EventType& type);
// 停止监听器 /// \~chinese
void StopListeners( /// @brief 停止监听器
const EventType& type /// @param type 监听的事件类型
); void StopListeners(const EventType& type);
// 移除监听器 /// \~chinese
void RemoveListeners( /// @brief 移除监听器
const EventType& type /// @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); virtual void Dispatch(Event& evt);
private: private:

View File

@ -27,6 +27,7 @@ namespace kiwano
: type_() : type_()
, callback_() , callback_()
, running_(true) , running_(true)
, removeable_(false)
{ {
} }

View File

@ -30,7 +30,10 @@ namespace kiwano
KGE_DECLARE_SMART_PTR(EventListener); KGE_DECLARE_SMART_PTR(EventListener);
// ʼþ¼àÌýÆ÷ /**
* \~chinese
* @brief
*/
class KGE_API EventListener class KGE_API EventListener
: public ObjectBase : public ObjectBase
, protected IntrusiveListItem<EventListenerPtr> , protected IntrusiveListItem<EventListenerPtr>
@ -39,57 +42,107 @@ namespace kiwano
friend IntrusiveList<EventListenerPtr>; friend IntrusiveList<EventListenerPtr>;
public: public:
/// \~chinese
/// @brief 监听器回调函数
using Callback = Function<void(Event&)>; using Callback = Function<void(Event&)>;
/// \~chinese
/// @brief 构造空监听器
EventListener(); EventListener();
EventListener( /// \~chinese
EventType type, /// @brief 构造监听器
Callback const& callback /// @param type 监听的事件类型
); /// @param callback 回调函数
EventListener(EventType type, Callback const& callback);
EventListener( /// \~chinese
String const& name, /// @brief 构造监听器
EventType type, /// @param name 监听器名称
Callback const& callback /// @param type 监听的事件类型
); /// @param callback 回调函数
EventListener(String const& name, EventType type, Callback const& callback);
/// \~chinese
/// @brief 构造监听器
/// @tparam _EventTy 事件类型
/// @param callback 回调函数
template < template <
typename _EventTy, typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
> >
EventListener(Callback const& callback) inline EventListener(Callback const& callback)
: EventListener(KGE_EVENT(_EventTy), callback) : EventListener(KGE_EVENT(_EventTy), callback)
{ {
} }
/// \~chinese
/// @brief 构造监听器
/// @tparam _EventTy 事件类型
/// @param name 监听器名称
/// @param callback 回调函数
template < template <
typename _EventTy, typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
> >
EventListener(String const& name, Callback const& callback) inline EventListener(String const& name, Callback const& callback)
: EventListener(name, KGE_EVENT(_EventTy), callback) : EventListener(name, KGE_EVENT(_EventTy), callback)
{ {
} }
virtual ~EventListener(); virtual ~EventListener();
/// \~chinese
/// @brief 启动监听器
void Start(); void Start();
/// \~chinese
/// @brief 停止监听器
void Stop(); void Stop();
/// \~chinese
/// @brief 移除监听器
void Remove();
/// \~chinese
/// @brief 是否正在运行
bool IsRunning() const; bool IsRunning() const;
Callback GetCallback() const; /// \~chinese
/// @brief 是否可移除
bool IsRemoveable() const;
/// \~chinese
/// @brief 获取回调函数
const Callback& GetCallback() const;
/// \~chinese
/// @brief 设置回调函数
void SetCallback(Callback const& cb); void SetCallback(Callback const& cb);
EventType const& GetEventType() const; /// \~chinese
/// @brief 获取监听的事件类型
EventType GetEventType() const;
/// \~chinese
/// @brief 设置监听的事件类型
void SetEventType(EventType const& type); void SetEventType(EventType const& type);
/// \~chinese
/// @brief 设置监听的事件类型
/// @tparam _EventTy 事件类型
template <
typename _EventTy,
typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type
>
inline void SetEventType()
{
SetEventType(KGE_EVENT(_EventTy));
}
private: private:
bool running_; bool running_;
bool removeable_;
EventType type_; EventType type_;
Callback callback_; Callback callback_;
}; };
@ -105,12 +158,22 @@ namespace kiwano
running_ = false; running_ = false;
} }
inline void EventListener::Remove()
{
removeable_ = true;
}
inline bool EventListener::IsRunning() const inline bool EventListener::IsRunning() const
{ {
return running_; return running_;
} }
inline EventListener::Callback EventListener::GetCallback() const inline bool EventListener::IsRemoveable() const
{
return removeable_;
}
inline const EventListener::Callback& EventListener::GetCallback() const
{ {
return callback_; return callback_;
} }
@ -120,7 +183,7 @@ namespace kiwano
callback_ = cb; callback_ = cb;
} }
inline EventType const& EventListener::GetEventType() const inline EventType EventListener::GetEventType() const
{ {
return type_; return type_;
} }

View File

@ -24,22 +24,45 @@
namespace kiwano namespace kiwano
{ {
// DLL ¿â /**
* \~chinese
* @brief DLL库
*/
class KGE_API Library class KGE_API Library
{ {
public: public:
/// \~chinese
/// @brief 构造DLL库
Library(); Library();
/// \~chinese
/// @brief 构造DLL库
/// @param lib DLL文件路径
Library(String const& lib); Library(String const& lib);
virtual ~Library(); virtual ~Library();
/// \~chinese
/// @brief 加载DLL
/// @param lib DLL文件路径
bool Load(String const& lib); bool Load(String const& lib);
/// \~chinese
/// @brief 是否有效
bool IsValid() const; bool IsValid() const;
/// \~chinese
/// @brief 释放DLL
void Free(); void Free();
/// \~chinese
/// @brief 检索指定的DLL中的输出库函数地址
/// @param proc_name 函数名
FARPROC GetProcess(String const& proc_name); FARPROC GetProcess(String const& proc_name);
/// \~chinese
/// @brief 检索指定的DLL中的输出库函数地址
/// @param proc_name 函数名
template <typename _Proc> template <typename _Proc>
inline _Proc GetProcess(String const& proc_name) inline _Proc GetProcess(String const& proc_name)
{ {

View File

@ -72,14 +72,24 @@ namespace kiwano
String DumpObject(); String DumpObject();
public: public:
/// \~chinese
/// @brief 是否启用了内存泄漏追踪
static bool IsTracingLeaks(); static bool IsTracingLeaks();
/// \~chinese
/// @brief 开始追踪内存泄漏
static void StartTracingLeaks(); static void StartTracingLeaks();
/// \~chinese
/// @brief 停止追踪内存泄漏
static void StopTracingLeaks(); static void StopTracingLeaks();
/// \~chinese
/// @brief 打印所有追踪中的对象信息
static void DumpTracingObjects(); static void DumpTracingObjects();
/// \~chinese
/// @brief 获取所有追踪中的对象
static Vector<ObjectBase*>& GetTracingObjects(); static Vector<ObjectBase*>& GetTracingObjects();
private: private:

View File

@ -69,7 +69,7 @@ namespace kiwano
if (timers_.empty()) if (timers_.empty())
return; return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) for (auto& timer : timers_)
{ {
if (timer->IsName(name)) if (timer->IsName(name))
{ {
@ -83,7 +83,7 @@ namespace kiwano
if (timers_.empty()) if (timers_.empty())
return; return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) for (auto& timer : timers_)
{ {
if (timer->IsName(name)) if (timer->IsName(name))
{ {
@ -97,10 +97,8 @@ namespace kiwano
if (timers_.empty()) if (timers_.empty())
return; return;
TimerPtr next; for (auto& timer : timers_)
for (auto timer = timers_.first_item(); timer; timer = next)
{ {
next = timer->next_item();
if (timer->IsName(name)) if (timer->IsName(name))
{ {
timer->Remove(); timer->Remove();
@ -113,7 +111,7 @@ namespace kiwano
if (timers_.empty()) if (timers_.empty())
return; return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) for (auto& timer : timers_)
{ {
timer->Stop(); timer->Stop();
} }
@ -124,7 +122,7 @@ namespace kiwano
if (timers_.empty()) if (timers_.empty())
return; return;
for (auto timer = timers_.first_item().get(); timer; timer = timer->next_item().get()) for (auto& timer : timers_)
{ {
timer->Start(); timer->Start();
} }