Kiwano Engine v1.3.x
Event.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/base/RefObject.h>
23#include <kiwano/base/RefPtr.h>
24#include <kiwano/event/EventType.h>
25#include <kiwano/math/Math.h>
26
27namespace kiwano
28{
29
42class KGE_API Event : public RefObject
43{
44public:
47 Event(const EventType& type);
48
49 virtual ~Event();
50
53 const EventType& GetType() const;
54
58 template <typename _Ty>
59 bool IsType() const;
60
64 template <typename _Ty>
65 const _Ty* Cast() const;
66
70 template <typename _Ty>
71 _Ty* Cast();
72
73private:
74 const EventType type_;
75};
76
79template <typename _Ty>
81{
82 inline bool operator()(const Event* evt) const
83 {
84 static_assert(std::is_base_of<Event, _Ty>::value, "_Ty is not an event type.");
85 return evt->GetType() == KGE_EVENT(_Ty);
86 }
87};
88
91inline const EventType& Event::GetType() const
92{
93 return type_;
94}
95
96template <typename _Ty>
97inline bool Event::IsType() const
98{
99 static_assert(std::is_base_of<Event, _Ty>::value, "_Ty is not an event type.");
100 return IsSameEventType<_Ty>()(this);
101}
102
103template <typename _Ty>
104inline const _Ty* Event::Cast() const
105{
106 return const_cast<Event*>(this)->Cast<_Ty>();
107}
108
109template <typename _Ty>
110inline _Ty* Event::Cast()
111{
112 if (!this->IsType<_Ty>())
113 return nullptr;
114 return dynamic_cast<_Ty*>(this);
115}
116
117} // namespace kiwano
事件类型
Definition: EventType.h:36
事件
Definition: Event.h:43
const EventType & GetType() const
获取类型事件
Definition: Event.h:91
bool IsType() const
判断事件类型
Definition: Event.h:97
const _Ty * Cast() const
转换为其他类型事件
Definition: Event.h:104
引用计数器
Definition: RefObject.h:33
事件特性:判断事件类型是否相同
Definition: Event.h:81