Kiwano Engine v1.3.x
EventDispatcher.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/event/listener/EventListener.h>
23
24namespace kiwano
25{
26
29typedef IntrusiveList<RefPtr<EventListener>> ListenerList;
30
35class KGE_API EventDispatcher : protected IntrusiveListValue<EventDispatcher*>
36{
38
39public:
42 EventListener* AddListener(RefPtr<EventListener> listener);
43
48 EventListener* AddListener(EventType type, EventListener::Callback callback);
49
55 EventListener* AddListener(StringView name, EventType type, EventListener::Callback callback);
56
61 template <typename _EventTy>
63 {
64 static_assert(std::is_base_of<Event, _EventTy>::value, "_EventTy is not an event type.");
65 return AddListener(KGE_EVENT(_EventTy), callback);
66 }
67
73 template <typename _EventTy>
75 {
76 static_assert(std::is_base_of<Event, _EventTy>::value, "_EventTy is not an event type.");
77 return AddListener(name, KGE_EVENT(_EventTy), callback);
78 }
79
83 void StartListeners(StringView name);
84
88 void StopListeners(StringView name);
89
93 void RemoveListeners(StringView name);
94
97 void StartAllListeners();
98
101 void StopAllListeners();
102
105 void RemoveAllListeners();
106
109 const ListenerList& GetAllListeners() const;
110
115 virtual bool DispatchEvent(Event* evt);
116
117private:
118 ListenerList listeners_;
119};
120} // namespace kiwano
事件分发器
Definition: EventDispatcher.h:36
EventListener * AddListener(StringView name, EventListener::Callback callback)
添加监听器
Definition: EventDispatcher.h:74
EventListener * AddListener(EventListener::Callback callback)
添加监听器
Definition: EventDispatcher.h:62
事件监听器
Definition: EventListener.h:48
事件类型
Definition: EventType.h:36
事件
Definition: Event.h:43
Definition: Function.h:228
侵入式链表元素
Definition: IntrusiveList.hpp:434
侵入式链表
Definition: IntrusiveList.hpp:34
引用计数智能指针
Definition: RefBasePtr.hpp:35