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 public:
135 using TDelegate = Delegate<TRet(Args...)>;
136
141
142 public:
147 template <typename TOwner>
149 {
150 assert(initializer._owner != nullptr);
151 assert(initializer._accessor != nullptr);
152
153 SetOwner(initializer._owner);
154 _accessor = reinterpret_cast<void *>(initializer._accessor);
155
156 _extractor = [](void *owner, void *accessor) -> TDelegate & {
157 return reinterpret_cast<TDelegate &(*)(TOwner *)>(accessor)(reinterpret_cast<TOwner *>(owner));
158 };
159 }
160
166 {
167 assert(initializer._accessor != nullptr);
168
169 SetOwner(nullptr);
170 _accessor = reinterpret_cast<void *>(initializer._accessor);
171
172 _extractor = [](void * /*owner*/, void *accessor) -> TDelegate & {
173 return reinterpret_cast<TDelegate &(*)()>(accessor)();
174 };
175 }
176
181 template <typename T>
182 auto operator+=(T &&handler) const
183 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
184 {
185 this->GetDelegate() += std::forward<T>(handler);
186 }
187
192 template <typename T>
193 auto operator-=(T &&handler) const
194 -> typename std::enable_if<_DelegateCanAddSubtract<TDelegate, T>::value>::type
195 {
196 this->GetDelegate() -= std::forward<T>(handler);
197 }
198
199 public:
203 template <typename TOwner>
208
216
217 private:
221 static constexpr std::ptrdiff_t _STATICOFFSET =
222 (std::numeric_limits<std::ptrdiff_t>::max)();
223
227 std::ptrdiff_t _offset;
228
232 void *_accessor;
233
237 TDelegate &(*_extractor)(void *owner, void *accessor);
238
242 bool IsStatic() const noexcept
243 {
244 return _offset == _STATICOFFSET;
245 }
246
251 void SetOwner(void *owner) noexcept
252 {
253 if (owner == nullptr) {
254 _offset = _STATICOFFSET;
255 } else {
256 _offset = reinterpret_cast<uint8_t *>(owner) - reinterpret_cast<uint8_t *>(this);
257 }
258 }
259
263 void *GetOwner() const noexcept
264 {
265 if (IsStatic()) {
266 return nullptr;
267 } else {
268 return const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(this)) + _offset;
269 }
270 }
271
275 TDelegate &GetDelegate() const
276 {
277 return _extractor(GetOwner(), _accessor);
278 }
279 };
280
281 /*================================================================================*/
282
286 struct EventArgs {};
287
291 template <typename TSender, typename TEventArgs = EventArgs>
293}
委托类,类似于C::中的委托,支持存储和调用任意可调用对象
Definition Delegate.h:391
Definition Delegate.h:21
auto operator+=(T &&handler) const -> typename std::enable_if< _DelegateCanAddSubtract< TDelegate, T >::value >::type
添加事件处理程序
Definition Event.h:182
Event(const StaticEventInitializer< TDelegate > &initializer)
构造静态事件
Definition Event.h:165
auto operator-=(T &&handler) const -> typename std::enable_if< _DelegateCanAddSubtract< TDelegate, T >::value >::type
移除事件处理程序
Definition Event.h:193
Event(const MemberEventInitializer< TOwner, TDelegate > &initializer)
构造成员事件
Definition Event.h:148
static StaticEventInitializer< TDelegate > Init()
初始化静态事件
Definition Event.h:212
static MemberEventInitializer< TOwner, TDelegate > Init(TOwner *owner)
初始化成员事件
Definition Event.h:204
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:286