-
Notifications
You must be signed in to change notification settings - Fork 247
/
base_events.h
528 lines (434 loc) · 13.8 KB
/
base_events.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
WINRT_EXPORT namespace winrt
{
struct event_token
{
int64_t value{};
explicit operator bool() const noexcept
{
return value != 0;
}
};
inline bool operator==(event_token const& left, event_token const& right) noexcept
{
return left.value == right.value;
}
struct auto_revoke_t {};
inline constexpr auto_revoke_t auto_revoke{};
template <typename I>
struct event_revoker
{
using method_type = int32_t(__stdcall impl::abi_t<I>::*)(winrt::event_token);
event_revoker() noexcept = default;
event_revoker(event_revoker const&) = delete;
event_revoker& operator=(event_revoker const&) = delete;
event_revoker(event_revoker&&) noexcept = default;
event_revoker& operator=(event_revoker&& other) noexcept
{
if (this != &other)
{
revoke();
m_object = std::move(other.m_object);
m_method = other.m_method;
m_token = other.m_token;
}
return *this;
}
template <typename U>
event_revoker(U&& object, method_type method, event_token token) :
m_object(std::forward<U>(object)),
m_method(method),
m_token(token)
{}
~event_revoker() noexcept
{
revoke();
}
void revoke() noexcept
{
if (I object = std::exchange(m_object, {}).get())
{
((*reinterpret_cast<impl::abi_t<I>**>(&object))->*(m_method))(m_token);
}
}
explicit operator bool() const noexcept
{
return m_object ? true : false;
}
private:
weak_ref<I> m_object;
method_type m_method{};
event_token m_token{};
};
template <typename I>
struct factory_event_revoker
{
using method_type = int32_t(__stdcall impl::abi_t<I>::*)(winrt::event_token);
factory_event_revoker() noexcept = default;
factory_event_revoker(factory_event_revoker const&) = delete;
factory_event_revoker& operator=(factory_event_revoker const&) = delete;
factory_event_revoker(factory_event_revoker&&) noexcept = default;
factory_event_revoker& operator=(factory_event_revoker&& other) noexcept
{
if (this != &other)
{
revoke();
m_object = std::move(other.m_object);
m_method = other.m_method;
m_token = other.m_token;
}
return *this;
}
template <typename U>
factory_event_revoker(U&& object, method_type method, event_token token) noexcept :
m_object(std::forward<U>(object)),
m_method(method),
m_token(token)
{}
~factory_event_revoker() noexcept
{
revoke();
}
void revoke() noexcept
{
if (auto object = std::exchange(m_object, {}))
{
((*reinterpret_cast<impl::abi_t<I>**>(&object))->*(m_method))(m_token);
}
}
explicit operator bool() const noexcept
{
return m_object ? true : false;
}
private:
I m_object;
method_type m_method{};
event_token m_token{};
};
}
namespace winrt::impl
{
template <typename I, auto Method>
struct event_revoker
{
event_revoker() noexcept = default;
event_revoker(event_revoker const&) = delete;
event_revoker& operator=(event_revoker const&) = delete;
event_revoker(event_revoker&&) noexcept = default;
event_revoker& operator=(event_revoker&& other) noexcept
{
event_revoker(std::move(other)).swap(*this);
return *this;
}
event_revoker(I const& object, event_token token)
: m_object(object)
, m_token(token)
{}
operator winrt::event_revoker<I>() && noexcept
{
return { std::move(m_object), Method, m_token };
}
~event_revoker() noexcept
{
if (m_object)
{
revoke_impl(m_object.get());
}
}
void swap(event_revoker& other) noexcept
{
std::swap(m_object, other.m_object);
std::swap(m_token, other.m_token);
}
void revoke() noexcept
{
revoke_impl(std::exchange(m_object, {}).get());
}
explicit operator bool() const noexcept
{
return bool{ m_object };
}
private:
void revoke_impl(I object) noexcept
{
if (object)
{
((*reinterpret_cast<impl::abi_t<I>**>(&object))->*(Method))(m_token);
}
}
winrt::weak_ref<I> m_object{};
event_token m_token{};
};
template <typename I, auto Method>
struct factory_event_revoker
{
factory_event_revoker() noexcept = default;
factory_event_revoker(factory_event_revoker const&) = delete;
factory_event_revoker& operator=(factory_event_revoker const&) = delete;
factory_event_revoker(factory_event_revoker&&) noexcept = default;
factory_event_revoker& operator=(factory_event_revoker&& other) noexcept
{
factory_event_revoker(std::move(other)).swap(*this);
return *this;
}
factory_event_revoker(I const& object, event_token token)
: m_object(object)
, m_token(token)
{}
operator winrt::factory_event_revoker<I>() && noexcept
{
return { std::move(m_object), Method, m_token };
}
~factory_event_revoker() noexcept
{
if (m_object)
{
revoke_impl(m_object);
}
}
void swap(factory_event_revoker& other) noexcept
{
std::swap(m_object, other.m_object);
std::swap(m_token, other.m_token);
}
void revoke() noexcept
{
revoke_impl(std::exchange(m_object, {}));
}
explicit operator bool() const noexcept
{
return bool{ m_object };
}
private:
void revoke_impl(I object) noexcept
{
if (object)
{
((*reinterpret_cast<impl::abi_t<I>**>(&object))->*(Method))(m_token);
}
}
private:
I m_object;
event_token m_token{};
};
template <typename D, typename Revoker, typename S>
Revoker make_event_revoker(S source, event_token token)
{
return { static_cast<D const&>(*source), token };
}
template <typename T>
struct event_array
{
using value_type = T;
using reference = value_type&;
using pointer = value_type*;
using iterator = value_type*;
explicit event_array(uint32_t const count) noexcept : m_size(count)
{
std::uninitialized_fill_n(data(), count, value_type());
}
unsigned long AddRef() noexcept
{
return ++m_references;
}
unsigned long Release() noexcept
{
auto const remaining = --m_references;
if (remaining == 0)
{
this->~event_array();
::operator delete(static_cast<void*>(this));
}
return remaining;
}
reference back() noexcept
{
WINRT_ASSERT(m_size > 0);
return*(data() + m_size - 1);
}
iterator begin() noexcept
{
return data();
}
iterator end() noexcept
{
return data() + m_size;
}
uint32_t size() const noexcept
{
return m_size;
}
~event_array() noexcept
{
std::destroy(begin(), end());
}
private:
pointer data() noexcept
{
return reinterpret_cast<pointer>(this + 1);
}
atomic_ref_count m_references{ 1 };
uint32_t m_size{ 0 };
};
template <typename T>
com_ptr<event_array<T>> make_event_array(uint32_t const capacity)
{
void* raw = ::operator new(sizeof(event_array<T>) + (sizeof(T)* capacity));
#ifdef _MSC_VER
#pragma warning(suppress: 6386)
#endif
return { new(raw) event_array<T>(capacity), take_ownership_from_abi };
}
WINRT_IMPL_NOINLINE inline bool report_failed_invoke()
{
int32_t const code = to_hresult();
WINRT_IMPL_RoTransformError(code, 0, nullptr);
if (code == static_cast<int32_t>(0x80010108) || // RPC_E_DISCONNECTED
code == static_cast<int32_t>(0x800706BA) || // HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE)
code == static_cast<int32_t>(0x89020001)) // JSCRIPT_E_CANTEXECUTE
{
return false;
}
return true;
}
template <typename Delegate, typename... Arg>
bool invoke(Delegate const& delegate, Arg const&... args) noexcept
{
try
{
delegate(args...);
}
catch (...)
{
return report_failed_invoke();
}
return true;
}
}
WINRT_EXPORT namespace winrt
{
template <typename Delegate>
struct event
{
using delegate_type = Delegate;
event() = default;
event(event const&) = delete;
event& operator =(event const&) = delete;
explicit operator bool() const noexcept
{
return m_targets != nullptr;
}
event_token add(delegate_type const& delegate)
{
return add_agile(impl::make_agile_delegate(delegate));
}
void remove(event_token const token)
{
// Extends life of old targets array to release delegates outside of lock.
delegate_array temp_targets;
{
slim_lock_guard const change_guard(m_change);
if (!m_targets)
{
return;
}
uint32_t available_slots = m_targets->size() - 1;
delegate_array new_targets;
bool removed = false;
if (available_slots == 0)
{
if (get_token(*m_targets->begin()) == token)
{
removed = true;
}
}
else
{
new_targets = impl::make_event_array<delegate_type>(available_slots);
auto new_iterator = new_targets->begin();
for (delegate_type const& element : *m_targets)
{
if (!removed && token == get_token(element))
{
removed = true;
continue;
}
if (available_slots == 0)
{
WINRT_ASSERT(!removed);
break;
}
*new_iterator = element;
++new_iterator;
--available_slots;
}
}
if (removed)
{
slim_lock_guard const swap_guard(m_swap);
temp_targets = std::exchange(m_targets, std::move(new_targets));
}
}
}
void clear()
{
// Extends life of old targets array to release delegates outside of lock.
delegate_array temp_targets;
{
slim_lock_guard const change_guard(m_change);
if (!m_targets)
{
return;
}
slim_lock_guard const swap_guard(m_swap);
temp_targets = std::exchange(m_targets, nullptr);
}
}
template<typename...Arg>
void operator()(Arg const&... args)
{
delegate_array temp_targets;
{
slim_lock_guard const swap_guard(m_swap);
temp_targets = m_targets;
}
if (temp_targets)
{
for (delegate_type const& element : *temp_targets)
{
if (!impl::invoke(element, args...))
{
remove(get_token(element));
}
}
}
}
private:
WINRT_IMPL_NOINLINE event_token add_agile(delegate_type delegate)
{
event_token token{};
// Extends life of old targets array to release delegates outside of lock.
delegate_array temp_targets;
{
slim_lock_guard const change_guard(m_change);
delegate_array new_targets = impl::make_event_array<delegate_type>((!m_targets) ? 1 : m_targets->size() + 1);
if (m_targets)
{
std::copy_n(m_targets->begin(), m_targets->size(), new_targets->begin());
}
new_targets->back() = std::move(delegate);
token = get_token(new_targets->back());
slim_lock_guard const swap_guard(m_swap);
temp_targets = std::exchange(m_targets, std::move(new_targets));
}
return token;
}
event_token get_token(delegate_type const& delegate) const noexcept
{
return event_token{ reinterpret_cast<int64_t>(WINRT_IMPL_EncodePointer(get_abi(delegate))) };
}
using delegate_array = com_ptr<impl::event_array<delegate_type>>;
delegate_array m_targets;
slim_mutex m_swap;
slim_mutex m_change;
};
}