-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathbase_windows.h
453 lines (376 loc) · 11.9 KB
/
base_windows.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
namespace winrt::impl
{
#ifdef WINRT_DIAGNOSTICS
struct factory_diagnostics_info
{
bool is_agile{ true };
uint32_t requests{ 0 };
};
struct diagnostics_info
{
std::map<std::wstring_view, uint32_t> queries;
std::map<std::wstring_view, factory_diagnostics_info> factories;
};
struct diagnostics_cache
{
template <typename T>
void add_query()
{
slim_lock_guard const guard(m_lock);
++m_info.queries[name_of<T>()];
}
template <typename T>
void add_factory()
{
slim_lock_guard const guard(m_lock);
factory_diagnostics_info& factory = m_info.factories[name_of<T>()];
++factory.requests;
}
template <typename T>
void non_agile_factory()
{
slim_lock_guard const guard(m_lock);
factory_diagnostics_info& factory = m_info.factories[name_of<T>()];
factory.is_agile = false;
}
auto get()
{
slim_lock_guard const guard(m_lock);
return m_info;
}
auto detach()
{
slim_lock_guard const guard(m_lock);
return std::move(m_info);
}
private:
slim_mutex m_lock;
diagnostics_info m_info;
};
inline diagnostics_cache& get_diagnostics_info() noexcept
{
static diagnostics_cache info;
return info;
}
#endif
template <typename T>
using com_ref = std::conditional_t<std::is_base_of_v<Windows::Foundation::IUnknown, T>, T, com_ptr<T>>;
template <typename T, std::enable_if_t<is_implements_v<T>, int> = 0>
com_ref<T> wrap_as_result(void* result)
{
return { &static_cast<produce<T, typename default_interface<T>::type>*>(result)->shim(), take_ownership_from_abi };
}
template <typename T, std::enable_if_t<!is_implements_v<T>, int> = 0>
com_ref<T> wrap_as_result(void* result)
{
return { result, take_ownership_from_abi };
}
template<typename T>
struct is_classic_com_interface : std::conjunction<std::is_base_of<::IUnknown, T>, std::negation<is_implements<T>>> {};
template <typename T>
struct is_com_interface : std::disjunction<std::is_base_of<Windows::Foundation::IUnknown, T>, std::is_base_of<unknown_abi, T>, is_implements<T>, is_classic_com_interface<T>> {};
template <typename T>
inline constexpr bool is_com_interface_v = is_com_interface<T>::value;
// You must include <winrt/Windows.Foundation.h> to use this overload.
template <typename To, typename From, std::enable_if_t<!is_com_interface_v<To>, int> = 0>
auto as(From* ptr);
template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
com_ref<To> as(From* ptr)
{
#ifdef WINRT_DIAGNOSTICS
get_diagnostics_info().add_query<To>();
#endif
if (!ptr)
{
return nullptr;
}
void* result{};
check_hresult(ptr->QueryInterface(guid_of<To>(), &result));
return wrap_as_result<To>(result);
}
// You must include <winrt/Windows.Foundation.h> to use this overload.
template <typename To, typename From, std::enable_if_t<!is_com_interface_v<To>, int> = 0>
auto try_as(From* ptr) noexcept;
template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
com_ref<To> try_as(From* ptr) noexcept
{
#ifdef WINRT_DIAGNOSTICS
get_diagnostics_info().add_query<To>();
#endif
if (!ptr)
{
return nullptr;
}
void* result{};
ptr->QueryInterface(guid_of<To>(), &result);
return wrap_as_result<To>(result);
}
template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
com_ref<To> try_as_with_reason(From* ptr, hresult& code) noexcept
{
#ifdef WINRT_DIAGNOSTICS
get_diagnostics_info().add_query<To>();
#endif
if (!ptr)
{
code = 0;
return nullptr;
}
void* result{};
code = ptr->QueryInterface(guid_of<To>(), &result);
return wrap_as_result<To>(result);
}
template <typename To, typename From>
auto try_as_with_reason(From ptr, hresult& code) noexcept
{
return ptr->template try_as_with_reason<To>(code);
}
}
WINRT_EXPORT namespace winrt::Windows::Foundation
{
struct IUnknown
{
IUnknown() noexcept = default;
IUnknown(std::nullptr_t) noexcept {}
void* operator new(size_t) = delete;
IUnknown(void* ptr, take_ownership_from_abi_t) noexcept : m_ptr(static_cast<impl::unknown_abi*>(ptr))
{
}
IUnknown(IUnknown const& other) noexcept : m_ptr(other.m_ptr)
{
add_ref();
}
IUnknown(IUnknown&& other) noexcept : m_ptr(std::exchange(other.m_ptr, {}))
{
}
~IUnknown() noexcept
{
release_ref();
}
IUnknown& operator=(IUnknown const& other) noexcept
{
if (this != &other)
{
release_ref();
m_ptr = other.m_ptr;
add_ref();
}
return*this;
}
IUnknown& operator=(IUnknown&& other) noexcept
{
if (this != &other)
{
release_ref();
m_ptr = std::exchange(other.m_ptr, {});
}
return*this;
}
explicit operator bool() const noexcept
{
return nullptr != m_ptr;
}
IUnknown& operator=(std::nullptr_t) noexcept
{
release_ref();
return*this;
}
template <typename To>
auto as() const
{
return impl::as<To>(m_ptr);
}
template <typename To>
auto try_as() const noexcept
{
return impl::try_as<To>(m_ptr);
}
template <typename To>
auto try_as_with_reason(hresult& code) const noexcept
{
return impl::try_as_with_reason<To>(m_ptr, code);
}
template <typename To>
void as(To& to) const
{
to = as<impl::wrapped_type_t<To>>();
}
template <typename To>
bool try_as(To& to) const noexcept
{
if constexpr (impl::is_com_interface_v<To> || !std::is_same_v<To, impl::wrapped_type_t<To>>)
{
to = try_as<impl::wrapped_type_t<To>>();
return static_cast<bool>(to);
}
else
{
auto result = try_as<To>();
to = result.has_value() ? result.value() : impl::empty_value<To>();
return result.has_value();
}
}
hresult as(guid const& id, void** result) const noexcept
{
return m_ptr->QueryInterface(id, result);
}
friend void swap(IUnknown& left, IUnknown& right) noexcept
{
std::swap(left.m_ptr, right.m_ptr);
}
private:
void add_ref() const noexcept
{
if (m_ptr)
{
m_ptr->AddRef();
}
}
void release_ref() noexcept
{
if (m_ptr)
{
unconditional_release_ref();
}
}
WINRT_IMPL_NOINLINE void unconditional_release_ref() noexcept
{
std::exchange(m_ptr, {})->Release();
}
impl::unknown_abi* m_ptr{};
};
}
WINRT_EXPORT namespace winrt
{
template <typename T, std::enable_if_t<!std::is_base_of_v<Windows::Foundation::IUnknown, T>, int> = 0>
auto get_abi(T const& object) noexcept
{
return reinterpret_cast<impl::abi_t<T> const&>(object);
}
template <typename T, std::enable_if_t<!std::is_base_of_v<Windows::Foundation::IUnknown, T>, int> = 0>
auto put_abi(T& object) noexcept
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
object = {};
}
return reinterpret_cast<impl::abi_t<T>*>(&object);
}
template <typename T, typename V, std::enable_if_t<!std::is_base_of_v<Windows::Foundation::IUnknown, T>, int> = 0>
void copy_from_abi(T& object, V&& value)
{
object = reinterpret_cast<T const&>(value);
}
template <typename T, typename V, std::enable_if_t<!std::is_base_of_v<Windows::Foundation::IUnknown, T>, int> = 0>
void copy_to_abi(T const& object, V& value)
{
reinterpret_cast<T&>(value) = object;
}
template <typename T, std::enable_if_t<!std::is_base_of_v<Windows::Foundation::IUnknown, std::decay_t<T>> && !std::is_convertible_v<T, std::wstring_view>, int> = 0>
auto detach_abi(T&& object)
{
impl::abi_t<T> result{};
reinterpret_cast<T&>(result) = std::move(object);
return result;
}
inline void* get_abi(Windows::Foundation::IUnknown const& object) noexcept
{
return *(void**)(&object);
}
inline void** put_abi(Windows::Foundation::IUnknown& object) noexcept
{
object = nullptr;
return reinterpret_cast<void**>(&object);
}
inline void attach_abi(Windows::Foundation::IUnknown& object, void* value) noexcept
{
object = nullptr;
*put_abi(object) = value;
}
inline void* detach_abi(Windows::Foundation::IUnknown& object) noexcept
{
void* temp = get_abi(object);
*reinterpret_cast<void**>(&object) = nullptr;
return temp;
}
inline void* detach_abi(Windows::Foundation::IUnknown&& object) noexcept
{
void* temp = get_abi(object);
*reinterpret_cast<void**>(&object) = nullptr;
return temp;
}
constexpr void* detach_abi(std::nullptr_t) noexcept
{
return nullptr;
}
inline void copy_from_abi(Windows::Foundation::IUnknown& object, void* value) noexcept
{
object = nullptr;
if (value)
{
static_cast<impl::unknown_abi*>(value)->AddRef();
*put_abi(object) = value;
}
}
inline void copy_to_abi(Windows::Foundation::IUnknown const& object, void*& value) noexcept
{
WINRT_ASSERT(value == nullptr);
value = get_abi(object);
if (value)
{
static_cast<impl::unknown_abi*>(value)->AddRef();
}
}
inline ::IUnknown* get_unknown(Windows::Foundation::IUnknown const& object) noexcept
{
return static_cast<::IUnknown*>(get_abi(object));
}
}
WINRT_EXPORT namespace winrt::Windows::Foundation
{
inline bool operator==(IUnknown const& left, IUnknown const& right) noexcept
{
if (get_abi(left) == get_abi(right))
{
return true;
}
if (!left || !right)
{
return false;
}
return get_abi(left.try_as<IUnknown>()) == get_abi(right.try_as<IUnknown>());
}
inline bool operator!=(IUnknown const& left, IUnknown const& right) noexcept
{
return !(left == right);
}
inline bool operator<(IUnknown const& left, IUnknown const& right) noexcept
{
if (get_abi(left) == get_abi(right))
{
return false;
}
if (!left || !right)
{
return get_abi(left) < get_abi(right);
}
return get_abi(left.try_as<IUnknown>()) < get_abi(right.try_as<IUnknown>());
}
inline bool operator>(IUnknown const& left, IUnknown const& right) noexcept
{
return right < left;
}
inline bool operator<=(IUnknown const& left, IUnknown const& right) noexcept
{
return !(right < left);
}
inline bool operator>=(IUnknown const& left, IUnknown const& right) noexcept
{
return !(left < right);
}
struct IInspectable : IUnknown
{
IInspectable(std::nullptr_t = nullptr) noexcept {}
IInspectable(void* ptr, take_ownership_from_abi_t) noexcept : IUnknown(ptr, take_ownership_from_abi) {}
};
}