-
Notifications
You must be signed in to change notification settings - Fork 247
/
base_implements.h
1578 lines (1310 loc) · 48.4 KB
/
base_implements.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if defined(_MSC_VER)
#if defined(_DEBUG) && !defined(WINRT_NO_MAKE_DETECTION)
#pragma detect_mismatch("C++/WinRT WINRT_NO_MAKE_DETECTION", "make detection enabled (DEBUG and !WINRT_NO_MAKE_DETECTION)")
#else
#pragma detect_mismatch("C++/WinRT WINRT_NO_MAKE_DETECTION", "make detection disabled (!DEBUG or WINRT_NO_MAKE_DETECTION)")
#endif
#endif
namespace winrt::impl
{
struct marker
{
marker() = delete;
};
}
WINRT_EXPORT namespace winrt
{
struct non_agile : impl::marker {};
struct no_weak_ref : impl::marker {};
struct composing : impl::marker {};
struct composable : impl::marker {};
struct no_module_lock : impl::marker {};
struct static_lifetime : impl::marker {};
template <typename Interface>
struct cloaked : Interface {};
template <typename D, typename... I>
struct implements;
}
namespace winrt::impl
{
template<typename...T>
using tuple_cat_t = decltype(std::tuple_cat(std::declval<T>()...));
template <template <typename> typename Condition, typename>
struct tuple_if_base;
template <template <typename> typename Condition, typename...T>
struct tuple_if_base<Condition, std::tuple<T...>> { using type = tuple_cat_t<typename std::conditional<Condition<T>::value, std::tuple<T>, std::tuple<>>::type...>; };
template <template <typename> typename Condition, typename T>
using tuple_if = typename tuple_if_base<Condition, T>::type;
template <typename T>
struct is_interface : std::disjunction<std::is_base_of<Windows::Foundation::IInspectable, T>, is_classic_com_interface<T>> {};
template <typename T>
struct is_marker : std::disjunction<std::is_base_of<marker, T>, std::is_void<T>> {};
template <typename T>
struct uncloak_base
{
using type = T;
};
template <typename T>
struct uncloak_base<cloaked<T>>
{
using type = T;
};
template <typename T>
using uncloak = typename uncloak_base<T>::type;
template <typename I>
struct is_cloaked : std::disjunction<
std::is_same<Windows::Foundation::IInspectable, I>,
std::negation<std::is_base_of<Windows::Foundation::IInspectable, I>>
> {};
template <typename I>
struct is_cloaked<cloaked<I>> : std::true_type {};
template <typename D, typename I, typename Enable = void>
struct producer;
template <typename D, typename T>
struct producers_base;
template <typename D, typename I, typename Enable = void>
struct producer_convert;
template <typename T>
struct producer_ref : T
{
producer_ref(producer_ref const&) = delete;
producer_ref& operator=(producer_ref const&) = delete;
producer_ref(producer_ref&&) = delete;
producer_ref& operator=(producer_ref&&) = delete;
producer_ref(void* ptr) noexcept : T(ptr, take_ownership_from_abi)
{
}
~producer_ref() noexcept
{
detach_abi(*this);
}
};
template <typename T>
struct producer_vtable
{
void* value;
};
template <typename D, typename I, typename Enable>
struct producer_convert : producer<D, typename default_interface<I>::type>
{
#ifdef __clang__
// This is sub-optimal in that it requires an AddRef and Release of the
// implementation type for every conversion, but it works around an
// issue where Clang ignores the conversion of producer_ref<I> const
// to I&& (an rvalue ref that cannot bind a const rvalue).
// See CWG rev. 110 active issue 2077, "Overload resolution and invalid
// rvalue-reference initialization"
operator I() const noexcept
{
I result{ nullptr };
copy_from_abi(result, (produce<D, typename default_interface<I>::type>*)this);
return result;
}
#else
operator producer_ref<I> const() const noexcept
{
return { (produce<D, typename default_interface<I>::type>*)this };
}
#endif
operator producer_vtable<I> const() const noexcept
{
return { (void*)this };
}
};
template <typename D, typename...T>
struct producers_base<D, std::tuple<T...>> : producer_convert<D, T>... {};
template <typename D, typename...T>
using producers = producers_base<D, tuple_if<is_interface, std::tuple<uncloak<T>...>>>;
template <typename D, typename... I>
struct root_implements;
template <typename T, typename = std::void_t<>>
struct unwrap_implements
{
using type = T;
};
template <typename T>
struct unwrap_implements<T, std::void_t<typename T::implements_type>>
{
using type = typename T::implements_type;
};
template <typename T>
using unwrap_implements_t = typename unwrap_implements<T>::type;
template <typename...>
struct nested_implements
{};
template <typename First, typename... Rest>
struct nested_implements<First, Rest...>
: std::conditional_t<is_implements_v<First>,
impl::identity<First>, nested_implements<Rest...>>
{
static_assert(!is_implements_v<First> || !std::disjunction_v<is_implements<Rest>...>,
"Duplicate nested implements found");
};
template <typename D, typename Dummy = std::void_t<>, typename... I>
struct base_implements_impl
: impl::identity<root_implements<D, I...>> {};
template <typename D, typename... I>
struct base_implements_impl<D, std::void_t<typename nested_implements<I...>::type>, I...>
: nested_implements<I...> {};
template <typename D, typename... I>
using base_implements = base_implements_impl<D, void, I...>;
template <typename T, typename = std::void_t<>>
struct has_composable : std::false_type {};
template <typename T>
struct has_composable<T, std::void_t<typename T::composable>> : std::true_type {};
template <typename T, typename = std::void_t<>>
struct has_class_type : std::false_type {};
template <typename T>
struct has_class_type<T, std::void_t<typename T::class_type>> : std::true_type {};
template <typename>
struct has_static_lifetime : std::false_type {};
template <typename D, typename...I>
struct has_static_lifetime<implements<D, I...>> : std::disjunction<std::is_same<static_lifetime, I>...> {};
template <typename D>
inline constexpr bool has_static_lifetime_v = has_static_lifetime<typename D::implements_type>::value;
template <typename T>
void clear_abi(T*) noexcept
{}
template <typename T>
void clear_abi(T** value) noexcept
{
*value = nullptr;
}
template <typename T>
void zero_abi([[maybe_unused]] void* ptr, [[maybe_unused]] uint32_t const capacity) noexcept
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
memset(ptr, 0, sizeof(T) * capacity);
}
}
template <typename T>
void zero_abi([[maybe_unused]] void* ptr) noexcept
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
memset(ptr, 0, sizeof(T));
}
}
}
WINRT_EXPORT namespace winrt
{
template <typename D, typename I>
D* get_self(I const& from) noexcept
{
return &static_cast<impl::produce<D, default_interface<I>>*>(get_abi(from))->shim();
}
template <typename D, typename I>
D* get_self(com_ptr<I> const& from) noexcept
{
return static_cast<D*>(static_cast<impl::producer<D, I>*>(from.get()));
}
template <typename D, typename I>
[[deprecated]] D* from_abi(I const& from) noexcept
{
return get_self<D>(from);
}
template <typename I, typename D>
impl::abi_t<I>* to_abi(impl::producer<D, I> const* from) noexcept
{
return reinterpret_cast<impl::abi_t<I>*>(const_cast<impl::producer<D, I>*>(from));
}
template <typename I, typename D>
impl::abi_t<I>* to_abi(impl::producer_convert<D, I> const* from) noexcept
{
return reinterpret_cast<impl::abi_t<I>*>((impl::producer<D, default_interface<I>>*)from);
}
}
namespace winrt::impl
{
template <typename...> struct interface_list;
template <>
struct interface_list<>
{
template <typename Traits>
static constexpr auto find(Traits const& traits) noexcept
{
return traits.not_found();
}
};
template <typename First, typename ... Rest>
struct interface_list<First, Rest...>
{
template <typename Traits>
static constexpr auto find(Traits const& traits) noexcept
{
if (traits.template test<First>())
{
return traits.template found<First>();
}
return interface_list<Rest...>::find(traits);
}
using first_interface = First;
};
template <typename, typename> struct interface_list_append_impl;
template <typename... T, typename... U>
struct interface_list_append_impl<interface_list<T...>, interface_list<U...>>
{
using type = interface_list<T..., U...>;
};
template <template <typename> class, typename...>
struct filter_impl;
template <template <typename> class Predicate, typename... T>
using filter = typename filter_impl<Predicate, unwrap_implements_t<T>...>::type;
template <template <typename> class Predicate>
struct filter_impl<Predicate>
{
using type = interface_list<>;
};
template <template <typename> class Predicate, typename T, typename... Rest>
struct filter_impl<Predicate, T, Rest...>
{
using type = typename interface_list_append_impl<
std::conditional_t<
Predicate<T>::value,
interface_list<winrt::impl::uncloak<T>>,
interface_list<>
>,
typename filter_impl<Predicate, Rest...>::type
>::type;
};
template <template <typename> class Predicate, typename ... T, typename ... Rest>
struct filter_impl<Predicate, interface_list<T...>, Rest...>
{
using type = typename interface_list_append_impl<
filter<Predicate, T...>,
filter<Predicate, Rest...>
>::type;
};
template <template <typename> class Predicate, typename D, typename ... I, typename ... Rest>
struct filter_impl<Predicate, winrt::implements<D, I...>, Rest...>
{
using type = typename interface_list_append_impl<
filter<Predicate, I...>,
filter<Predicate, Rest...>
>::type;
};
template <typename T>
using implemented_interfaces = filter<is_interface, typename T::implements_type>;
template <typename T>
struct is_uncloaked_interface : std::conjunction<is_interface<T>, std::negation<winrt::impl::is_cloaked<T>>> {};
template <typename T>
using uncloaked_interfaces = filter<is_uncloaked_interface, typename T::implements_type>;
template <typename T>
struct uncloaked_iids;
template <typename ... T>
struct uncloaked_iids<interface_list<T...>>
{
#ifdef _MSC_VER
#pragma warning(suppress: 4307)
#endif
static constexpr std::array<guid, sizeof...(T)> value{ winrt::guid_of<T>() ... };
};
template <typename T, typename = void>
struct implements_default_interface
{
using type = typename default_interface<typename implemented_interfaces<T>::first_interface>::type;
};
template <typename T>
struct implements_default_interface<T, std::void_t<typename T::class_type>>
{
using type = winrt::default_interface<typename T::class_type>;
};
template <typename T>
struct default_interface<T, std::void_t<typename T::implements_type>>
{
using type = typename implements_default_interface<T>::type;
};
template<typename T>
struct find_iid_traits
{
T const* m_object;
guid const& m_guid;
template <typename I>
constexpr bool test() const noexcept
{
return is_guid_of<typename default_interface<I>::type>(m_guid);
}
template <typename I>
constexpr void* found() const noexcept
{
return to_abi<I>(m_object);
}
static constexpr void* not_found() noexcept
{
return nullptr;
}
};
template <typename T>
auto find_iid(T const* obj, guid const& iid) noexcept
{
return static_cast<unknown_abi*>(implemented_interfaces<T>::find(find_iid_traits<T>{ obj, iid }));
}
template <typename I>
struct has_interface_traits
{
template <typename T>
constexpr bool test() const noexcept
{
return std::is_same_v<T, I>;
}
template <typename>
static constexpr bool found() noexcept
{
return true;
}
static constexpr bool not_found() noexcept
{
return false;
}
};
template <typename T, typename I>
constexpr bool has_interface() noexcept
{
return impl::implemented_interfaces<T>::find(has_interface_traits<I>{});
}
template<typename T>
struct find_inspectable_traits
{
T const* m_object;
template <typename I>
static constexpr bool test() noexcept
{
return std::is_base_of_v<inspectable_abi, abi_t<I>>;
}
template <typename I>
constexpr void* found() const noexcept
{
return to_abi<I>(m_object);
}
static constexpr void* not_found() noexcept
{
return nullptr;
}
};
template <typename T>
inspectable_abi* find_inspectable(T const* obj) noexcept
{
using default_interface = typename implements_default_interface<T>::type;
if constexpr (std::is_base_of_v<inspectable_abi, abi_t<default_interface>>)
{
return to_abi<default_interface>(obj);
}
else
{
return static_cast<inspectable_abi*>(implemented_interfaces<T>::find(find_inspectable_traits<T>{ obj }));
}
}
template <typename I, typename = std::void_t<>>
struct runtime_class_name
{
static hstring get()
{
throw hresult_not_implemented{};
}
};
template <typename I>
struct runtime_class_name<I, std::void_t<decltype(name_v<I>)>>
{
static hstring get()
{
return hstring{ name_of<I>() };
}
};
template <>
struct runtime_class_name<Windows::Foundation::IInspectable>
{
static hstring get()
{
return {};
}
};
template <typename D, typename I, typename Enable>
struct producer
{
private:
produce<D, I> vtable;
};
template <typename D, typename I, typename Enable>
struct produce_base : abi_t<I>
{
D& shim() noexcept
{
return*static_cast<D*>(reinterpret_cast<producer<D, I>*>(this));
}
int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept override
{
return shim().QueryInterface(id, object);
}
uint32_t __stdcall AddRef() noexcept override
{
return shim().AddRef();
}
uint32_t __stdcall Release() noexcept override
{
return shim().Release();
}
int32_t __stdcall GetIids(uint32_t* count, guid** array) noexcept override
{
return shim().GetIids(reinterpret_cast<count_type*>(count), reinterpret_cast<guid_type**>(array));
}
int32_t __stdcall GetRuntimeClassName(void** name) noexcept override
{
return shim().abi_GetRuntimeClassName(name);
}
int32_t __stdcall GetTrustLevel(Windows::Foundation::TrustLevel* trustLevel) noexcept final
{
return shim().abi_GetTrustLevel(trustLevel);
}
};
template <typename D, typename I>
struct producer<D, I, std::enable_if_t<is_classic_com_interface<I>::value>> : I
{
#ifndef WINRT_IMPL_IUNKNOWN_DEFINED
static_assert(std::is_void_v<I> /* dependent_false */, "To implement classic COM interfaces, you must #include <unknwn.h> before including C++/WinRT headers.");
#endif
};
template <typename D, typename I>
struct producer_convert<D, I, std::enable_if_t<is_classic_com_interface<I>::value>> : producer<D, I>
{
};
struct INonDelegatingInspectable : Windows::Foundation::IUnknown
{
INonDelegatingInspectable(std::nullptr_t = nullptr) noexcept {}
};
template <> struct abi<INonDelegatingInspectable>
{
using type = inspectable_abi;
};
template <typename D>
struct produce<D, INonDelegatingInspectable> : produce_base<D, INonDelegatingInspectable>
{
int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
{
return this->shim().NonDelegatingQueryInterface(id, object);
}
uint32_t __stdcall AddRef() noexcept final
{
return this->shim().NonDelegatingAddRef();
}
uint32_t __stdcall Release() noexcept final
{
return this->shim().NonDelegatingRelease();
}
int32_t __stdcall GetIids(uint32_t* count, guid** array) noexcept final
{
return this->shim().NonDelegatingGetIids(count, array);
}
int32_t __stdcall GetRuntimeClassName(void** name) noexcept final
{
return this->shim().NonDelegatingGetRuntimeClassName(name);
}
};
template <bool Agile, bool UseModuleLock>
struct weak_ref;
template <bool Agile, bool UseModuleLock>
struct weak_source_producer;
template <bool Agile, bool UseModuleLock>
struct weak_source final : IWeakReferenceSource, module_lock_updater<UseModuleLock>
{
weak_ref<Agile, UseModuleLock>* that() noexcept
{
return static_cast<weak_ref<Agile, UseModuleLock>*>(reinterpret_cast<weak_source_producer<Agile, UseModuleLock>*>(this));
}
int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
{
if (is_guid_of<IWeakReferenceSource>(id))
{
*object = static_cast<IWeakReferenceSource*>(this);
that()->increment_strong();
return 0;
}
return that()->m_object->QueryInterface(id, object);
}
uint32_t __stdcall AddRef() noexcept final
{
return that()->increment_strong();
}
uint32_t __stdcall Release() noexcept final
{
return that()->m_object->Release();
}
int32_t __stdcall GetWeakReference(IWeakReference** weakReference) noexcept final
{
*weakReference = that();
that()->AddRef();
return 0;
}
};
template <bool Agile, bool UseModuleLock>
struct weak_source_producer
{
protected:
weak_source<Agile, UseModuleLock> m_source;
};
template <bool Agile, bool UseModuleLock>
struct weak_ref final : IWeakReference, weak_source_producer<Agile, UseModuleLock>
{
weak_ref(unknown_abi* object, uint32_t const strong) noexcept :
m_object(object),
m_strong(strong)
{
WINRT_ASSERT(object);
}
int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept final
{
if (is_guid_of<IWeakReference>(id) || is_guid_of<Windows::Foundation::IUnknown>(id))
{
*object = static_cast<IWeakReference*>(this);
AddRef();
return 0;
}
if constexpr (Agile)
{
if (is_guid_of<IAgileObject>(id))
{
*object = static_cast<unknown_abi*>(this);
AddRef();
return 0;
}
if (is_guid_of<IMarshal>(id))
{
return make_marshaler(this, object);
}
}
*object = nullptr;
return error_no_interface;
}
uint32_t __stdcall AddRef() noexcept final
{
return 1 + m_weak.fetch_add(1, std::memory_order_relaxed);
}
uint32_t __stdcall Release() noexcept final
{
uint32_t const target = m_weak.fetch_sub(1, std::memory_order_relaxed) - 1;
if (target == 0)
{
delete this;
}
return target;
}
int32_t __stdcall Resolve(guid const& id, void** objectReference) noexcept final
{
uint32_t target = m_strong.load(std::memory_order_relaxed);
while (true)
{
if (target == 0)
{
*objectReference = nullptr;
return 0;
}
if (m_strong.compare_exchange_weak(target, target + 1, std::memory_order_acquire, std::memory_order_relaxed))
{
int32_t hr = m_object->QueryInterface(id, objectReference);
m_strong.fetch_sub(1, std::memory_order_relaxed);
return hr;
}
}
}
void set_strong(uint32_t const count) noexcept
{
m_strong = count;
}
uint32_t increment_strong() noexcept
{
return 1 + m_strong.fetch_add(1, std::memory_order_relaxed);
}
uint32_t decrement_strong() noexcept
{
uint32_t const target = m_strong.fetch_sub(1, std::memory_order_release) - 1;
if (target == 0)
{
Release();
}
return target;
}
IWeakReferenceSource* get_source() noexcept
{
increment_strong();
return &this->m_source;
}
private:
template <bool T, bool U>
friend struct weak_source;
static_assert(sizeof(weak_source_producer<Agile, UseModuleLock>) == sizeof(weak_source<Agile, UseModuleLock>));
unknown_abi* m_object{};
std::atomic<uint32_t> m_strong{ 1 };
std::atomic<uint32_t> m_weak{ 1 };
};
template <bool>
struct WINRT_IMPL_EMPTY_BASES root_implements_composing_outer
{
protected:
static constexpr bool is_composing = false;
static constexpr inspectable_abi* m_inner = nullptr;
};
template <>
struct WINRT_IMPL_EMPTY_BASES root_implements_composing_outer<true>
{
template <typename Qi>
auto try_as() const noexcept
{
return m_inner.try_as<Qi>();
}
explicit operator bool() const noexcept
{
return m_inner.operator bool();
}
template <typename To, typename From>
friend auto winrt::impl::try_as_with_reason(From ptr, hresult& code) noexcept;
protected:
static constexpr bool is_composing = true;
Windows::Foundation::IInspectable m_inner;
private:
template <typename Qi>
auto try_as_with_reason(hresult& code) const noexcept
{
return m_inner.try_as_with_reason<Qi>(code);
}
};
template <typename D, bool>
struct WINRT_IMPL_EMPTY_BASES root_implements_composable_inner
{
protected:
static inspectable_abi* outer() noexcept { return nullptr; }
template <typename, typename, typename>
friend class produce_dispatch_to_overridable_base;
};
template <typename D>
struct WINRT_IMPL_EMPTY_BASES root_implements_composable_inner<D, true> : producer<D, INonDelegatingInspectable>
{
protected:
inspectable_abi* outer() noexcept { return m_outer; }
private:
inspectable_abi* m_outer = nullptr;
template <typename, typename, typename>
friend class produce_dispatch_to_overridable_base;
template <typename>
friend struct composable_factory;
};
template <typename D, typename... I>
struct WINRT_IMPL_NOVTABLE root_implements
: root_implements_composing_outer<std::disjunction_v<std::is_same<composing, I>...>>
, root_implements_composable_inner<D, std::disjunction_v<std::is_same<composable, I>...>>
, module_lock_updater<!std::disjunction_v<std::is_same<no_module_lock, I>...>>
{
using IInspectable = Windows::Foundation::IInspectable;
using root_implements_type = root_implements;
int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept
{
if (this->outer())
{
return this->outer()->QueryInterface(id, object);
}
int32_t result = query_interface(id, object);
if (result == error_no_interface && this->m_inner)
{
result = static_cast<unknown_abi*>(get_abi(this->m_inner))->QueryInterface(id, object);
}
return result;
}
uint32_t __stdcall AddRef() noexcept
{
if (this->outer())
{
return this->outer()->AddRef();
}
return NonDelegatingAddRef();
}
uint32_t __stdcall Release() noexcept
{
if (this->outer())
{
return this->outer()->Release();
}
return NonDelegatingRelease();
}
struct abi_guard
{
abi_guard(D& derived) :
m_derived(derived)
{
m_derived.abi_enter();
}
~abi_guard()
{
m_derived.abi_exit();
}
private:
D& m_derived;
};
void abi_enter() const noexcept {}
void abi_exit() const noexcept {}
#if defined(_DEBUG) && !defined(WINRT_NO_MAKE_DETECTION)
// Please use winrt::make<T>(args...) to avoid allocating a C++/WinRT implementation type on the stack.
virtual void use_make_function_to_create_this_object() = 0;
#endif
protected:
virtual int32_t query_interface_tearoff(guid const&, void**) const noexcept
{
return error_no_interface;
}
root_implements() noexcept
{
}
virtual ~root_implements() noexcept
{
// If a weak reference is created during destruction, this ensures that it is also destroyed.
subtract_final_reference();
}
int32_t __stdcall GetIids(uint32_t* count, guid** array) noexcept
{
if (this->outer())
{
return this->outer()->GetIids(count, array);
}
return NonDelegatingGetIids(count, array);
}
int32_t __stdcall abi_GetRuntimeClassName(void** name) noexcept
{
if (this->outer())
{
return this->outer()->GetRuntimeClassName(name);
}
return NonDelegatingGetRuntimeClassName(name);
}
int32_t __stdcall abi_GetTrustLevel(Windows::Foundation::TrustLevel* trustLevel) noexcept
{
if (this->outer())
{
return this->outer()->GetTrustLevel(trustLevel);
}
return NonDelegatingGetTrustLevel(trustLevel);
}
uint32_t __stdcall NonDelegatingAddRef() noexcept
{
if constexpr (is_weak_ref_source::value)
{
uintptr_t count_or_pointer = m_references.load(std::memory_order_relaxed);
while (true)
{
if (is_weak_ref(count_or_pointer))
{
return decode_weak_ref(count_or_pointer)->increment_strong();
}
uintptr_t const target = count_or_pointer + 1;
if (m_references.compare_exchange_weak(count_or_pointer, target, std::memory_order_relaxed))
{
return static_cast<uint32_t>(target);
}
}
}
else
{
return 1 + m_references.fetch_add(1, std::memory_order_relaxed);
}
}
uint32_t __stdcall NonDelegatingRelease() noexcept
{
uint32_t const target = subtract_reference();
if (target == 0)
{
if constexpr (has_final_release::value)
{
D::final_release(std::unique_ptr<D>(static_cast<D*>(this)));
}
else
{
delete this;
}
}
return target;
}