-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathcppwinrt_authoring.h
More file actions
454 lines (402 loc) · 14.9 KB
/
cppwinrt_authoring.h
File metadata and controls
454 lines (402 loc) · 14.9 KB
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT.
//
//*********************************************************
//! @file
//! Helpers that make authoring C++/WinRT components easier.
namespace wil
{
#ifndef __WIL_CPPWINRT_AUTHORING_PROPERTIES_INCLUDED
/// @cond
#define __WIL_CPPWINRT_AUTHORING_PROPERTIES_INCLUDED
namespace details
{
template <typename T>
struct single_threaded_property_storage
{
T m_value{};
single_threaded_property_storage() = default;
single_threaded_property_storage(const T& value) : m_value(value)
{
}
operator T&()
{
return m_value;
}
operator T const&() const
{
return m_value;
}
template <typename Q>
auto operator=(Q&& q)
{
m_value = wistd::forward<Q>(q);
return *this;
}
};
} // namespace details
/// @endcond
template <typename T>
struct single_threaded_property
: std::conditional_t<std::is_scalar_v<T> || std::is_final_v<T>, wil::details::single_threaded_property_storage<T>, T>
{
single_threaded_property() = default;
template <typename... TArgs>
single_threaded_property(TArgs&&... value) : base_type(std::forward<TArgs>(value)...)
{
}
using base_type =
std::conditional_t<std::is_scalar_v<T> || std::is_final_v<T>, wil::details::single_threaded_property_storage<T>, T>;
T operator()() const
{
return *this;
}
// This is the only setter exposed. We don't expose `operator()(Q&& q)`,
// since that is what C++/WinRT uses to implement public setters. Since
// single_threaded_property is intended for readonly properties, we
// don't want to expose that.
//
// To set the value of this property *internally* (within your
// implementation), use this `operator=`:
//
// MyProperty = 42;
// // MyProperty(42); // won't work
//
// For settable properties, use single_threaded_rw_property<T> instead.
template <typename Q>
auto& operator=(Q&& q)
{
static_cast<base_type&>(*this) = std::forward<Q>(q);
return *this;
}
};
template <typename T>
struct single_threaded_rw_property : single_threaded_property<T>
{
using base_type = single_threaded_property<T>;
template <typename... TArgs>
single_threaded_rw_property(TArgs&&... value) : base_type(std::forward<TArgs>(value)...)
{
}
using base_type::operator();
// needed in lieu of deducing-this
template <typename Q>
auto& operator()(Q&& q)
{
return *this = std::forward<Q>(q);
}
// needed in lieu of deducing-this
template <typename Q>
auto& operator=(Q&& q)
{
base_type::operator=(std::forward<Q>(q));
return *this;
}
};
#endif // __WIL_CPPWINRT_AUTHORING_PROPERTIES_INCLUDED
#if (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY) && defined(__IClassFactory_INTERFACE_DEFINED__)) || \
defined(WIL_DOXYGEN) // class factory
/// @cond
#define __WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY
/// @endcond
template <typename T, typename... Rest>
struct class_factory : winrt::implements<class_factory<T, Rest...>, IClassFactory, winrt::no_weak_ref, Rest...>
{
HRESULT __stdcall CreateInstance(IUnknown* outer, GUID const& iid, void** result) noexcept final
try
{
*result = nullptr;
if (!outer)
{
return winrt::make_self<T>().as(iid, result);
}
else
{
return CLASS_E_NOAGGREGATION;
}
}
CATCH_RETURN()
HRESULT __stdcall LockServer(BOOL lock) noexcept final
try
{
if (lock)
{
++winrt::get_module_lock();
}
else
{
--winrt::get_module_lock();
}
return S_OK;
}
CATCH_RETURN()
};
#endif // !defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY) && defined(__IClassFactory_INTERFACE_DEFINED__)
#if (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_COM_SERVER) && defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY) && defined(_COMBASEAPI_H_)) || \
defined(WIL_DOXYGEN) // COM server
/// @cond
#define __WIL_CPPWINRT_AUTHORING_INCLUDED_COM_SERVER
/// @endcond
template <typename T>
WI_NODISCARD_REASON("The class is unregistered when the returned value is destructed")
unique_com_class_object_cookie
register_com_server(GUID const& guid, DWORD context = CLSCTX_LOCAL_SERVER, DWORD flags = REGCLS_MULTIPLEUSE)
{
unique_com_class_object_cookie registration;
winrt::check_hresult(CoRegisterClassObject(
guid, winrt::make<class_factory<T, winrt::no_module_lock>>().get(), context, flags, registration.put()));
return registration;
}
template <typename... Types>
struct clsid_array
{
std::array<GUID, sizeof...(Types)> value;
};
template <typename... Ts>
WI_NODISCARD_REASON("The classes are unregistered when the returned value is destructed")
std::vector<unique_com_class_object_cookie> register_com_server(
wil::clsid_array<Ts...> const& clsids, DWORD context = CLSCTX_LOCAL_SERVER, DWORD flags = REGCLS_MULTIPLEUSE)
{
std::vector<wil::unique_com_class_object_cookie> registrations;
registrations.reserve(sizeof...(Ts));
std::size_t i = 0;
(registrations.push_back(wil::register_com_server<Ts>(clsids.value[i++], context, flags | REGCLS_SUSPENDED)), ...);
// allow the user to keep class objects suspended if they've explicitly passed REGCLS_SUSPENDED.
if (!WI_IsFlagSet(flags, REGCLS_SUSPENDED))
{
winrt::check_hresult(CoResumeClassObjects());
}
return registrations;
}
namespace details
{
template <typename T>
struct has_iid
{
template <typename U = T, std::enable_if_t<std::is_same_v<GUID, std::decay_t<decltype(__uuidof(U))>>, int> = 0>
static std::true_type invoke(int);
template <typename U = T>
static std::false_type invoke(float);
static constexpr bool value = decltype(invoke(0))::value;
};
template <typename T>
constexpr bool has_iid_v = has_iid<T>::value;
}
template <typename... Types>
constexpr auto make_array_of_uuid()
{
return clsid_array<Types...>{std::array<GUID, sizeof...(Types)>{__uuidof(Types)...}};
}
template <typename... Types>
constexpr auto make_array_of_guid()
{
static_assert(!(... && details::has_iid_v<Types>), "Use make_array_of_uuid for COM class instead");
return clsid_array<Types...>{std::array<GUID, sizeof...(Types)>{winrt::guid_of<Types>()...}};
}
#endif // (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_COM_SERVER) && defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_ICLASSFACTORY) && defined(_COMBASEAPI_H_))
#if (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_FOUNDATION) && defined(WINRT_Windows_Foundation_H)) || \
defined(WIL_DOXYGEN) // WinRT / XAML helpers
/// @cond
#define __WIL_CPPWINRT_AUTHORING_INCLUDED_FOUNDATION
namespace details
{
template <typename T>
struct event_base
{
winrt::event_token operator()(const T& handler)
{
return m_handler.add(handler);
}
auto operator()(const winrt::event_token& token) noexcept
{
return m_handler.remove(token);
}
template <typename... TArgs>
auto invoke(TArgs&&... args)
{
return m_handler(std::forward<TArgs>(args)...);
}
private:
winrt::event<T> m_handler;
};
} // namespace details
/// @endcond
/**
* @brief A default event handler that maps to
* [Windows.Foundation.EventHandler](https://docs.microsoft.com/uwp/api/windows.foundation.eventhandler-1).
* @tparam T The event data type.
*/
template <typename T>
struct untyped_event : wil::details::event_base<winrt::Windows::Foundation::EventHandler<T>>
{
};
/**
* @brief A default event handler that maps to
* [Windows.Foundation.TypedEventHandler](https://docs.microsoft.com/uwp/api/windows.foundation.typedeventhandler-2).
* @tparam T The event data type.
* @details Usage example:
* @code
* // In IDL, this corresponds to:
* // event Windows.Foundation.TypedEventHandler<ModalPage, String> OkClicked;
* wil::typed_event<MarkupSample::ModalPage, winrt::hstring> OkClicked;
* @endcode
*/
template <typename TSender, typename TArgs>
struct typed_event : wil::details::event_base<winrt::Windows::Foundation::TypedEventHandler<TSender, TArgs>>
{
};
#endif // !defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_FOUNDATION) && defined(WINRT_Windows_Foundation_H)
#if (!defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_XAML_DATA) && (defined(WINRT_Microsoft_UI_Xaml_Data_H) || defined(WINRT_Windows_UI_Xaml_Data_H))) || \
defined(WIL_DOXYGEN) // INotifyPropertyChanged helpers
/// @cond
#define __WIL_CPPWINRT_AUTHORING_INCLUDED_XAML_DATA
namespace details
{
#ifdef WINRT_Microsoft_UI_Xaml_Data_H
using Xaml_Data_PropertyChangedEventHandler = winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler;
using Xaml_Data_PropertyChangedEventArgs = winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventArgs;
#elif defined(WINRT_Windows_UI_Xaml_Data_H)
using Xaml_Data_PropertyChangedEventHandler = winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler;
using Xaml_Data_PropertyChangedEventArgs = winrt::Windows::UI::Xaml::Data::PropertyChangedEventArgs;
#endif
} // namespace details
/// @endcond
/**
* @brief Helper base class to inherit from to have a simple implementation of
* [INotifyPropertyChanged](https://docs.microsoft.com/uwp/api/windows.ui.xaml.data.inotifypropertychanged).
* @tparam T CRTP type
* @details When you declare your class, make this class a base class and pass your class as a template parameter:
* @code
* struct MyPage : MyPageT<MyPage>, wil::notify_property_changed_base<MyPage>
* {
* wil::single_threaded_notifying_property<int> MyInt;
* MyPage() : INIT_NOTIFYING_PROPERTY(MyInt, 42) { }
* // or
* WIL_NOTIFYING_PROPERTY(int, MyInt, 42);
* };
* @endcode
*/
template <typename T, typename Xaml_Data_PropertyChangedEventHandler = wil::details::Xaml_Data_PropertyChangedEventHandler, typename Xaml_Data_PropertyChangedEventArgs = wil::details::Xaml_Data_PropertyChangedEventArgs>
struct notify_property_changed_base
{
using Type = T;
auto PropertyChanged(Xaml_Data_PropertyChangedEventHandler const& value)
{
return m_propertyChanged.add(value);
}
void PropertyChanged(winrt::event_token const& token)
{
m_propertyChanged.remove(token);
}
Type& self()
{
return *static_cast<Type*>(this);
}
/**
* @brief Raises a property change notification event
* @param name The name of the property
* @return
* @details Usage example\n
* C++
* @code
* void MyPage::DoSomething()
* {
* // modify MyInt
* // MyInt = ...
*
* // now send a notification to update the bound UI elements
* RaisePropertyChanged(L"MyInt");
* }
* @endcode
*/
auto RaisePropertyChanged(std::wstring_view name)
{
return m_propertyChanged(self(), Xaml_Data_PropertyChangedEventArgs{name});
}
protected:
winrt::event<Xaml_Data_PropertyChangedEventHandler> m_propertyChanged;
};
/**
* @brief Implements a property type with notifications
* @tparam T the property type
* @details Use the INIT_NOTIFY_PROPERTY macro to initialize this property in your class constructor. This will set up the
* right property name, and bind it to the `notify_property_changed_base` implementation.
*/
template <typename T, typename Xaml_Data_PropertyChangedEventHandler = wil::details::Xaml_Data_PropertyChangedEventHandler, typename Xaml_Data_PropertyChangedEventArgs = wil::details::Xaml_Data_PropertyChangedEventArgs>
struct single_threaded_notifying_property : single_threaded_rw_property<T>
{
using Type = T;
using base_type = single_threaded_rw_property<T>;
using base_type::operator();
template <typename Q>
auto& operator()(Q&& q)
{
return *this = std::forward<Q>(q);
}
template <typename Q>
auto& operator=(Q&& q)
{
if (q != this->operator()())
{
static_cast<base_type&>(*this) = std::forward<Q>(q);
if (auto strong = m_sender.get(); (m_npc != nullptr) && (strong != nullptr))
{
(*m_npc)(strong, Xaml_Data_PropertyChangedEventArgs{m_name});
}
}
return *this;
}
template <typename... TArgs>
single_threaded_notifying_property(
winrt::event<Xaml_Data_PropertyChangedEventHandler>* npc,
const winrt::Windows::Foundation::IInspectable& sender,
std::wstring_view name,
TArgs&&... args) :
single_threaded_rw_property<T>(std::forward<TArgs...>(args)...), m_name(name), m_npc(npc), m_sender(sender)
{
}
single_threaded_notifying_property(const single_threaded_notifying_property&) = default;
single_threaded_notifying_property(single_threaded_notifying_property&&) = default;
std::wstring_view Name() const noexcept
{
return m_name;
}
private:
std::wstring_view m_name;
winrt::event<Xaml_Data_PropertyChangedEventHandler>* m_npc;
winrt::weak_ref<winrt::Windows::Foundation::IInspectable> m_sender;
};
/**
* @def WIL_NOTIFYING_PROPERTY
* @brief use this to stamp out a property that calls RaisePropertyChanged upon changing its value. This is a zero-storage
* alternative to wil::single_threaded_notifying_property<T>.
* @details You can pass an initializer list for the initial property value in the variadic arguments to this macro.
*/
#define WIL_NOTIFYING_PROPERTY(type, name, ...) \
type m_##name{__VA_ARGS__}; \
auto name() const noexcept \
{ \
return m_##name; \
} \
auto& name(type value) \
{ \
if (m_##name != value) \
{ \
m_##name = std::move(value); \
RaisePropertyChanged(L"" #name); \
} \
return *this; \
}
/**
* @def INIT_NOTIFYING_PROPERTY
* @brief use this to initialize a wil::single_threaded_notifying_property in your class constructor.
*/
#define INIT_NOTIFYING_PROPERTY(NAME, VALUE) NAME(&m_propertyChanged, *this, L"" #NAME, VALUE)
#endif // !defined(__WIL_CPPWINRT_AUTHORING_INCLUDED_XAML_DATA) && (defined(WINRT_Microsoft_UI_Xaml_Data_H) || defined(WINRT_Windows_UI_Xaml_Data_H))
} // namespace wil