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 template <typename>
12 class Event; // 向前声明
13
14 /*================================================================================*/
15
19 template <typename TOwner, typename TDelegate>
21 {
22 template <typename>
23 friend class Event;
24
25 private:
29 TOwner *_owner;
30
34 TDelegate &(*_accessor)(TOwner *);
35
36 public:
41 : _owner(owner), _accessor(nullptr)
42 {
43 }
44
49 {
50 _accessor = accessor;
51 return *this;
52 }
53
57 template <TDelegate (TOwner::*accessor)()>
59 {
60 return Delegate([](TOwner *owner) -> TDelegate & {
61 return (owner->*accessor)();
62 });
63 }
64
68 template <TDelegate (TOwner::*accessor)() const>
70 {
71 return Delegate([](TOwner *owner) -> TDelegate & {
72 return (owner->*accessor)();
73 });
74 }
75
79 template <TDelegate TOwner::*field>
81 {
82 return Delegate([](TOwner *owner) -> TDelegate & {
83 return owner->*field;
84 });
85 }
86 };
87
91 template <typename TDelegate>
93 {
94 template <typename>
95 friend class Event;
96
97 private:
101 TDelegate &(*_accessor)();
102
103 public:
108 : _accessor(nullptr)
109 {
110 }
111
116 {
117 _accessor = accessor;
118 return *this;
119 }
120 };
121
122 /*================================================================================*/
123
127 template <typename TRet, typename... Args>
128 class Event<Delegate<TRet(Args...)>> final
129 {
130 private:
136 using TFuncPtr = void (*)();
137
141 static constexpr std::ptrdiff_t _STATICOFFSET =
142 (std::numeric_limits<std::ptrdiff_t>::max)();
143
144 public:
148 using TDelegate = Delegate<TRet(Args...)>;
149
154
155 public:
160 template <typename TOwner>
162 {
163 assert(initializer._owner != nullptr);
164 assert(initializer._accessor != nullptr);
165
166 SetOwner(initializer._owner);
167 _accessor = reinterpret_cast<TFuncPtr>(initializer._accessor);
168
169 _extractor = [](void *owner, TFuncPtr accessor) -> TDelegate & {
170 return reinterpret_cast<TDelegate &(*)(TOwner *)>(accessor)(reinterpret_cast<TOwner *>(owner));
171 };
172 }
173
179 {
180 assert(initializer._accessor != nullptr);
181
182 SetOwner(nullptr);
183 _accessor = reinterpret_cast<TFuncPtr>(initializer._accessor);
184
185 _extractor = [](void * /*owner*/, TFuncPtr accessor) -> TDelegate & {
186 return reinterpret_cast<TDelegate &(*)()>(accessor)();
187 };
188 }
189
194 template <typename T>
195 auto operator+=(T &&handler) const
196 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
197 {
198 this->GetDelegate() += std::forward<T>(handler);
199 }
200
205 template <typename T>
206 auto operator-=(T &&handler) const
207 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
208 {
209 this->GetDelegate() -= std::forward<T>(handler);
210 }
211
212 public:
216 template <typename TOwner>
221
229
230 private:
234 std::ptrdiff_t _offset;
235
239 TFuncPtr _accessor;
240
244 TDelegate &(*_extractor)(void *owner, TFuncPtr accessor);
245
249 bool IsStatic() const noexcept
250 {
251 return _offset == _STATICOFFSET;
252 }
253
258 void SetOwner(void *owner) noexcept
259 {
260 if (owner == nullptr) {
261 _offset = _STATICOFFSET;
262 } else {
263 _offset = reinterpret_cast<uint8_t *>(owner) - reinterpret_cast<uint8_t *>(this);
264 }
265 }
266
270 void *GetOwner() const noexcept
271 {
272 if (IsStatic()) {
273 return nullptr;
274 } else {
275 return const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(this)) + _offset;
276 }
277 }
278
282 TDelegate &GetDelegate() const
283 {
284 return _extractor(GetOwner(), _accessor);
285 }
286 };
287
288 /*================================================================================*/
289
293 struct EventArgs {
294 };
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:195
Event(const StaticEventInitializer< TDelegate > &initializer)
构造静态事件
Definition Event.h:178
auto operator-=(T &&handler) const -> typename std::enable_if< _DelegateCanAddSubtract< TDelegate, T >::value >::type
移除事件处理程序
Definition Event.h:206
Event(const MemberEventInitializer< TOwner, TDelegate > &initializer)
构造成员事件
Definition Event.h:161
static StaticEventInitializer< TDelegate > Init()
初始化静态事件
Definition Event.h:225
static MemberEventInitializer< TOwner, TDelegate > Init(TOwner *owner)
初始化成员事件
Definition Event.h:217
Definition Event.h:12
值转换器接口
Definition IValueConverter.h:14
成员事件初始化器
Definition Event.h:21
MemberEventInitializer & Delegate(TDelegate &(*accessor)(TOwner *))
设置委托访问器
Definition Event.h:48
MemberEventInitializer(TOwner *owner)
构造成员事件初始化器
Definition Event.h:40
MemberEventInitializer & Delegate()
使用成员函数获取委托
Definition Event.h:58
静态事件初始化器
Definition Event.h:93
StaticEventInitializer()
构造静态事件初始化器
Definition Event.h:107
StaticEventInitializer & Delegate(TDelegate &(*accessor)())
设置委托访问器
Definition Event.h:115
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
事件参数结构体
Definition Event.h:293