SimpleWindow
载入中...
搜索中...
未找到
Event.h
1#pragma once
2
3#include "Delegate.h"
4#include "Internal.h"
5#include <cassert>
6#include <cstddef>
7#include <limits>
8
9namespace sw
10{
11 // 向前声明
12 template <typename>
13 class Event;
14
15 /*================================================================================*/
16
20 template <typename TOwner, typename TDelegate>
22 {
23 template <typename>
24 friend class Event;
25
26 private:
30 TOwner *_owner;
31
35 TDelegate &(*_accessor)(TOwner *);
36
37 public:
42 : _owner(owner), _accessor(nullptr)
43 {
44 }
45
50 {
51 _accessor = accessor;
52 return *this;
53 }
54
58 template <TDelegate (TOwner::*accessor)()>
60 {
61 return Delegate([](TOwner *owner) -> TDelegate & {
62 return (owner->*accessor)();
63 });
64 }
65
69 template <TDelegate (TOwner::*accessor)() const>
71 {
72 return Delegate([](TOwner *owner) -> TDelegate & {
73 return (owner->*accessor)();
74 });
75 }
76
80 template <TDelegate TOwner::*field>
82 {
83 return Delegate([](TOwner *owner) -> TDelegate & {
84 return owner->*field;
85 });
86 }
87 };
88
92 template <typename TDelegate>
94 {
95 template <typename>
96 friend class Event;
97
98 private:
102 TDelegate &(*_accessor)();
103
104 public:
109 : _accessor(nullptr)
110 {
111 }
112
117 {
118 _accessor = accessor;
119 return *this;
120 }
121 };
122
123 /*================================================================================*/
124
128 template <typename TRet, typename... Args>
129 class Event<Delegate<TRet(Args...)>> final
130 {
131 private:
137 using TFuncPtr = void (*)();
138
142 static constexpr std::ptrdiff_t _STATICOFFSET =
143 (std::numeric_limits<std::ptrdiff_t>::max)();
144
145 public:
149 using TDelegate = Delegate<TRet(Args...)>;
150
155
156 public:
161 template <typename TOwner>
163 {
164 assert(initializer._owner != nullptr);
165 assert(initializer._accessor != nullptr);
166
167 SetOwner(initializer._owner);
168 _accessor = reinterpret_cast<TFuncPtr>(initializer._accessor);
169
170 _extractor = [](void *owner, TFuncPtr accessor) -> TDelegate & {
171 return reinterpret_cast<TDelegate &(*)(TOwner *)>(accessor)(reinterpret_cast<TOwner *>(owner));
172 };
173 }
174
180 {
181 assert(initializer._accessor != nullptr);
182
183 SetOwner(nullptr);
184 _accessor = reinterpret_cast<TFuncPtr>(initializer._accessor);
185
186 _extractor = [](void * /*owner*/, TFuncPtr accessor) -> TDelegate & {
187 return reinterpret_cast<TDelegate &(*)()>(accessor)();
188 };
189 }
190
195 template <typename T>
196 auto operator+=(T &&handler) const
197 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
198 {
199 this->GetDelegate() += std::forward<T>(handler);
200 }
201
206 template <typename T>
207 auto operator-=(T &&handler) const
208 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
209 {
210 this->GetDelegate() -= std::forward<T>(handler);
211 }
212
213 public:
217 template <typename TOwner>
222
230
231 private:
235 std::ptrdiff_t _offset;
236
240 TFuncPtr _accessor;
241
245 TDelegate &(*_extractor)(void *owner, TFuncPtr accessor);
246
250 bool IsStatic() const noexcept
251 {
252 return _offset == _STATICOFFSET;
253 }
254
259 void SetOwner(void *owner) noexcept
260 {
261 if (owner == nullptr) {
262 _offset = _STATICOFFSET;
263 } else {
264 _offset = reinterpret_cast<uint8_t *>(owner) - reinterpret_cast<uint8_t *>(this);
265 }
266 }
267
271 void *GetOwner() const noexcept
272 {
273 if (IsStatic()) {
274 return nullptr;
275 } else {
276 return const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(this)) + _offset;
277 }
278 }
279
283 TDelegate &GetDelegate() const
284 {
285 return _extractor(GetOwner(), _accessor);
286 }
287 };
288
289 /*================================================================================*/
290
294 struct EventArgs {};
295
299 template <typename TSender, typename TEventArgs = EventArgs>
301}
委托类,类似于C::中的委托,支持存储和调用任意可调用对象
Definition Delegate.h:407
Definition Delegate.h:21
auto operator+=(T &&handler) const -> typename std::enable_if< _DelegateCanAddSubtract< TDelegate, T >::value >::type
添加事件处理程序
Definition Event.h:196
Event(const StaticEventInitializer< TDelegate > &initializer)
构造静态事件
Definition Event.h:179
auto operator-=(T &&handler) const -> typename std::enable_if< _DelegateCanAddSubtract< TDelegate, T >::value >::type
移除事件处理程序
Definition Event.h:207
Event(const MemberEventInitializer< TOwner, TDelegate > &initializer)
构造成员事件
Definition Event.h:162
static StaticEventInitializer< TDelegate > Init()
初始化静态事件
Definition Event.h:226
static MemberEventInitializer< TOwner, TDelegate > Init(TOwner *owner)
初始化成员事件
Definition Event.h:218
Definition Event.h:13
值转换器接口
Definition IValueConverter.h:14
成员事件初始化器
Definition Event.h:22
MemberEventInitializer & Delegate(TDelegate &(*accessor)(TOwner *))
设置委托访问器
Definition Event.h:49
MemberEventInitializer(TOwner *owner)
构造成员事件初始化器
Definition Event.h:41
MemberEventInitializer & Delegate()
使用成员函数获取委托
Definition Event.h:59
静态事件初始化器
Definition Event.h:94
StaticEventInitializer()
构造静态事件初始化器
Definition Event.h:108
StaticEventInitializer & Delegate(TDelegate &(*accessor)())
设置委托访问器
Definition Event.h:116
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
事件参数结构体
Definition Event.h:294