-
Notifications
You must be signed in to change notification settings - Fork 47
/
collection.h
executable file
·2185 lines (1613 loc) · 71.6 KB
/
collection.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
/***
* collection.h - Windows Runtime Collection/Iterator Wrappers
*
* Copyright (c) Microsoft Corporation. All rights reserved.
****/
#pragma once
#ifndef _COLLECTION_H_
#define _COLLECTION_H_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#ifndef __cplusplus_winrt
#error collection.h requires the /ZW compiler option.
#endif
#include <stddef.h>
#include <algorithm>
#include <array>
#include <exception>
#include <functional>
#include <iterator>
#include <map>
#include <memory>
#include <new>
#include <string> // for source compatibility, as <unordered_map> included most of <string> before microsoft/STL#2996
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include <agile.h>
#define _COLLECTION_ATTRIBUTES [::Platform::Metadata::RuntimeClassName] [::Windows::Foundation::Metadata::Default]
#define _COLLECTION_TRANSLATE \
} catch (const ::std::bad_alloc&) { \
throw ref new OutOfMemoryException; \
} catch (const ::std::exception&) { \
throw ref new FailureException; \
}
#ifndef _COLLECTION_WUXI
#define _COLLECTION_WUXI 1
#endif
#ifdef _WIN64
#pragma pack(push, 16)
#else
#pragma pack(push, 8)
#endif
#pragma warning(push, 4)
#pragma warning(disable: 4451) // Usage of ref class 'Meow' inside this context can lead to invalid marshaling of object across contexts
namespace Platform {
namespace Collections {
namespace Details {
namespace WFC = ::Windows::Foundation::Collections;
#if _COLLECTION_WUXI
namespace WUXI = ::Windows::UI::Xaml::Interop;
#endif // _COLLECTION_WUXI
typedef ::Windows::Foundation::EventRegistrationToken Token;
inline void ValidateBounds(bool b) {
if (!b) {
throw ref new OutOfBoundsException;
}
}
inline void ValidateCounter(const ::std::shared_ptr<unsigned int>& ctr, unsigned int good_ctr) {
if (*ctr != good_ctr) {
throw ref new ChangedStateException;
}
}
inline void ValidateSize(size_t n) {
if (n > 0x7FFFFFFFUL) {
throw ref new OutOfMemoryException;
}
}
template <typename T> struct AlwaysFalse
: public ::std::false_type { };
template <typename T> struct Wrap {
typedef T type;
};
template <typename T> struct Wrap<T^>
: public ::std::conditional<__is_winrt_agile(T), T^, Agile<T^>> { };
template <typename T> inline const T& MakeWrap(const T& t) {
return t;
}
template <typename T> inline typename ::std::enable_if<!__is_winrt_agile(T), Agile<T^>>::type MakeWrap(T^ const & t) {
return Agile<T^>(t);
}
template <typename T> inline const T& Unwrap(const T& t) {
return t;
}
template <typename T> inline T^ Unwrap(const Agile<T^>& a) {
return a.Get();
}
template <typename T, typename U> struct VectorEnableIf
: public ::std::enable_if< ::std::is_same<T, U>::value || ::std::is_same<typename Wrap<T>::type, U>::value, void **> { };
template <typename X> inline void Init(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp) {
try {
ctr = ::std::make_shared<unsigned int>(0);
sp = ::std::make_shared<X>();
_COLLECTION_TRANSLATE
}
template <typename X, typename A> inline void Init(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, A&& a) {
try {
ctr = ::std::make_shared<unsigned int>(0);
sp = ::std::make_shared<X>(::std::forward<A>(a));
ValidateSize(sp->size());
_COLLECTION_TRANSLATE
}
template <typename X, typename A, typename B> inline void Init(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, A&& a, B&& b) {
try {
ctr = ::std::make_shared<unsigned int>(0);
sp = ::std::make_shared<X>(::std::forward<A>(a), ::std::forward<B>(b));
ValidateSize(sp->size());
_COLLECTION_TRANSLATE
}
template <typename X, typename A, typename B, typename C> inline void Init(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, A&& a, B&& b, C&& c) {
try {
ctr = ::std::make_shared<unsigned int>(0);
sp = ::std::make_shared<X>(::std::forward<A>(a), ::std::forward<B>(b), ::std::forward<C>(c));
ValidateSize(sp->size());
_COLLECTION_TRANSLATE
}
template <typename X, typename A> inline void InitMoveVector(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, A&& a) {
Init(ctr, sp, a.begin(), a.end());
}
template <typename X> inline void InitMoveVector(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, X&& x) {
Init(ctr, sp, ::std::move(x));
}
template <typename K, typename V, typename M, typename InIt> inline void EmplaceWrappedRange(M& m, InIt first, InIt last) {
for ( ; first != last; ++first) {
::std::pair<const K, V> p(*first);
m.emplace(MakeWrap(p.first), MakeWrap(p.second));
}
}
template <typename K, typename V, typename X, typename A> inline void InitMoveMap(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, A&& a) {
Init(ctr, sp, a.key_comp());
EmplaceWrappedRange<K, V>(*sp, a.begin(), a.end());
}
template <typename K, typename V, typename X> inline void InitMoveMap(::std::shared_ptr<unsigned int>& ctr, ::std::shared_ptr<X>& sp, X&& x) {
Init(ctr, sp, ::std::move(x));
}
inline void IncrementCounter(::std::shared_ptr<unsigned int>& ctr) {
if (++*ctr == static_cast<unsigned int>(-1)) {
// Wraparound is imminent! Create a fresh counter.
ctr = ::std::make_shared<unsigned int>(0);
}
}
ref class VectorChangedEventArgs sealed : public _COLLECTION_ATTRIBUTES WFC::IVectorChangedEventArgs {
internal:
VectorChangedEventArgs(WFC::CollectionChange change, unsigned int index)
: m_change(change), m_index(index) { }
public:
virtual property WFC::CollectionChange CollectionChange {
virtual WFC::CollectionChange get() {
return m_change;
}
}
virtual property unsigned int Index {
virtual unsigned int get() {
if (m_change == WFC::CollectionChange::Reset) {
throw ref new FailureException;
}
return m_index;
}
}
private:
WFC::CollectionChange m_change;
unsigned int m_index;
};
template <typename K> ref class MapChangedEventArgsReset sealed : public _COLLECTION_ATTRIBUTES WFC::IMapChangedEventArgs<K> {
public:
virtual property WFC::CollectionChange CollectionChange {
virtual WFC::CollectionChange get() {
return WFC::CollectionChange::Reset;
}
}
virtual property K Key {
virtual K get() {
throw ref new FailureException;
}
}
};
template <typename K> ref class MapChangedEventArgs sealed : public _COLLECTION_ATTRIBUTES WFC::IMapChangedEventArgs<K> {
internal:
MapChangedEventArgs(WFC::CollectionChange change, K key)
: m_change(change), m_key(key) { }
public:
virtual property WFC::CollectionChange CollectionChange {
virtual WFC::CollectionChange get() {
return m_change;
}
}
virtual property K Key {
virtual K get() {
return Unwrap(m_key);
}
}
private:
WFC::CollectionChange m_change;
typename Wrap<K>::type m_key;
};
template <typename T, typename E> inline bool VectorIndexOf(const ::std::vector<typename Wrap<T>::type>& v, T value, unsigned int * index) {
auto pred = [&](const typename Wrap<T>::type& elem) { return E()(Unwrap(elem), value); };
*index = static_cast<unsigned int>(::std::find_if(v.begin(), v.end(), pred) - v.begin());
return *index < v.size();
}
#if _COLLECTION_WUXI
template <typename T> struct is_hat : public ::std::false_type { };
template <typename U> struct is_hat<U^> : public ::std::true_type { };
template <typename T, typename E> inline bool VectorBindableIndexOf(::std::false_type, const ::std::vector<typename Wrap<T>::type>& v, Object^ o, unsigned int * index) {
IBox<T>^ ib = dynamic_cast<IBox<T>^>(o);
if (ib) {
return VectorIndexOf<T, E>(v, ib->Value, index);
} else {
*index = static_cast<unsigned int>(v.size());
return false;
}
}
template <typename T, typename E> inline bool VectorBindableIndexOf(::std::true_type, const ::std::vector<typename Wrap<T>::type>& v, Object^ o, unsigned int * index) {
T t = dynamic_cast<T>(o);
if (!o || t) {
return VectorIndexOf<T, E>(v, t, index);
} else {
*index = static_cast<unsigned int>(v.size());
return false;
}
}
template <typename T, typename E> inline bool VectorBindableIndexOf(const ::std::vector<typename Wrap<T>::type>& v, Object^ o, unsigned int * index) {
return VectorBindableIndexOf<T, E>(is_hat<T>(), v, o, index);
}
#endif // _COLLECTION_WUXI
template <typename T> inline unsigned int VectorGetMany(const ::std::vector<typename Wrap<T>::type>& v, unsigned int startIndex, WriteOnlyArray<T>^ dest) {
unsigned int capacity = dest->Length;
unsigned int actual = static_cast<unsigned int>(v.size()) - startIndex;
if (actual > capacity) {
actual = capacity;
}
for (unsigned int i = 0; i < actual; ++i) {
dest->set(i, Unwrap(v[startIndex + i]));
}
return actual;
}
template <typename T> ref class IteratorForVectorView sealed
: public _COLLECTION_ATTRIBUTES WFC::IIterator<T>
#if _COLLECTION_WUXI
, public WUXI::IBindableIterator
#endif // _COLLECTION_WUXI
{
private:
typedef ::std::vector<typename Wrap<T>::type> WrappedVector;
typedef WFC::IIterator<T> WFC_Base;
#if _COLLECTION_WUXI
typedef WUXI::IBindableIterator WUXI_Base;
#endif // _COLLECTION_WUXI
internal:
IteratorForVectorView(const ::std::shared_ptr<unsigned int>& ctr, const ::std::shared_ptr<WrappedVector>& vec)
: m_ctr(ctr), m_vec(vec), m_good_ctr(*ctr), m_index(0) { }
public:
virtual property T Current {
virtual T get() = WFC_Base::Current::get {
ValidateCounter(m_ctr, m_good_ctr);
ValidateBounds(m_index < m_vec->size());
return Unwrap((*m_vec)[m_index]);
}
}
virtual property bool HasCurrent {
virtual bool get() {
ValidateCounter(m_ctr, m_good_ctr);
return m_index < m_vec->size();
}
}
virtual bool MoveNext() {
ValidateCounter(m_ctr, m_good_ctr);
ValidateBounds(m_index < m_vec->size());
++m_index;
return m_index < m_vec->size();
}
virtual unsigned int GetMany(WriteOnlyArray<T>^ dest) {
ValidateCounter(m_ctr, m_good_ctr);
unsigned int actual = VectorGetMany(*m_vec, m_index, dest);
m_index += actual;
return actual;
}
private:
#if _COLLECTION_WUXI
virtual Object^ BindableCurrent() = WUXI_Base::Current::get {
return Current;
}
#endif // _COLLECTION_WUXI
::std::shared_ptr<unsigned int> m_ctr;
::std::shared_ptr<WrappedVector> m_vec;
unsigned int m_good_ctr;
unsigned int m_index;
};
} // namespace Details
template <typename T, typename E = ::std::equal_to<T>, bool = __is_valid_winrt_type(T)> ref class Vector;
template <typename T, typename E = ::std::equal_to<T>, bool = __is_valid_winrt_type(T)> ref class VectorView;
template <typename T, typename E> ref class VectorView<T, E, false> {
static_assert(Details::AlwaysFalse<T>::value, "Platform::Collections::VectorView<T, E> requires T to be a valid Windows Runtime type.");
};
template <typename T, typename E, bool> ref class VectorView sealed
: public _COLLECTION_ATTRIBUTES Details::WFC::IVectorView<T>
#if _COLLECTION_WUXI
, public Details::WUXI::IBindableVectorView
#endif // _COLLECTION_WUXI
{
private:
typedef ::std::vector<typename Details::Wrap<T>::type> WrappedVector;
typedef Details::WFC::IVectorView<T> WFC_Base;
#if _COLLECTION_WUXI
typedef Details::WUXI::IBindableVectorView WUXI_Base;
#endif // _COLLECTION_WUXI
internal:
VectorView() {
Details::Init(m_ctr, m_vec);
m_good_ctr = 0;
}
explicit VectorView(unsigned int size) {
Details::Init(m_ctr, m_vec, size);
m_good_ctr = 0;
}
VectorView(unsigned int size, T value) {
Details::Init(m_ctr, m_vec, size, Details::MakeWrap(value));
m_good_ctr = 0;
}
template <typename U> explicit VectorView(const ::std::vector<U>& v, typename Details::VectorEnableIf<T, U>::type = nullptr) {
Details::Init(m_ctr, m_vec, v.begin(), v.end());
m_good_ctr = 0;
}
template <typename U> explicit VectorView(::std::vector<U>&& v, typename Details::VectorEnableIf<T, U>::type = nullptr) {
Details::InitMoveVector(m_ctr, m_vec, ::std::move(v));
m_good_ctr = 0;
}
VectorView(const T * ptr, unsigned int size) {
Details::Init(m_ctr, m_vec, ptr, ptr + size);
m_good_ctr = 0;
}
template <size_t N> explicit VectorView(const T (&arr)[N]) {
Details::Init(m_ctr, m_vec, arr, arr + N);
m_good_ctr = 0;
}
template <size_t N> explicit VectorView(const ::std::array<T, N>& a) {
Details::Init(m_ctr, m_vec, a.begin(), a.end());
m_good_ctr = 0;
}
explicit VectorView(const Array<T>^ arr) {
Details::Init(m_ctr, m_vec, arr->begin(), arr->end());
m_good_ctr = 0;
}
template <typename InIt> VectorView(InIt first, InIt last) {
// SFINAE is unnecessary here.
Details::Init(m_ctr, m_vec, first, last);
m_good_ctr = 0;
}
VectorView(::std::initializer_list<T> il) {
Details::Init(m_ctr, m_vec, il.begin(), il.end());
m_good_ctr = 0;
}
public:
virtual Details::WFC::IIterator<T>^ First() = WFC_Base::First {
Details::ValidateCounter(m_ctr, m_good_ctr);
return ref new Details::IteratorForVectorView<T>(m_ctr, m_vec);
}
virtual T GetAt(unsigned int index) = WFC_Base::GetAt {
Details::ValidateCounter(m_ctr, m_good_ctr);
Details::ValidateBounds(index < m_vec->size());
return Details::Unwrap((*m_vec)[index]);
}
virtual property unsigned int Size {
virtual unsigned int get() {
Details::ValidateCounter(m_ctr, m_good_ctr);
return static_cast<unsigned int>(m_vec->size());
}
}
virtual bool IndexOf(T value, unsigned int * index) = WFC_Base::IndexOf {
*index = 0;
Details::ValidateCounter(m_ctr, m_good_ctr);
return Details::VectorIndexOf<T, E>(*m_vec, value, index);
}
virtual unsigned int GetMany(unsigned int startIndex, WriteOnlyArray<T>^ dest) {
Details::ValidateCounter(m_ctr, m_good_ctr);
Details::ValidateBounds(startIndex <= m_vec->size());
return Details::VectorGetMany(*m_vec, startIndex, dest);
}
private:
friend ref class Vector<T, E>;
VectorView(const ::std::shared_ptr<unsigned int>& ctr, const ::std::shared_ptr<WrappedVector>& vec)
: m_ctr(ctr), m_vec(vec), m_good_ctr(*ctr) { }
#if _COLLECTION_WUXI
virtual Details::WUXI::IBindableIterator^ BindableFirst() = WUXI_Base::First {
return safe_cast<Details::WUXI::IBindableIterator^>(First());
}
virtual Object^ BindableGetAt(unsigned int index) = WUXI_Base::GetAt {
return GetAt(index);
}
virtual bool BindableIndexOf(Object^ value, unsigned int * index) = WUXI_Base::IndexOf {
*index = 0;
Details::ValidateCounter(m_ctr, m_good_ctr);
return Details::VectorBindableIndexOf<T, E>(*m_vec, value, index);
}
#endif // _COLLECTION_WUXI
::std::shared_ptr<unsigned int> m_ctr;
::std::shared_ptr<WrappedVector> m_vec;
unsigned int m_good_ctr;
};
template <typename T, typename E> ref class Vector<T, E, false> {
static_assert(Details::AlwaysFalse<T>::value, "Platform::Collections::Vector<T, E> requires T to be a valid Windows Runtime type.");
};
template <typename T, typename E, bool> ref class Vector sealed
: public _COLLECTION_ATTRIBUTES Details::WFC::IObservableVector<T>
#if _COLLECTION_WUXI
, public Details::WUXI::IBindableObservableVector
#endif // _COLLECTION_WUXI
{
private:
typedef ::std::vector<typename Details::Wrap<T>::type> WrappedVector;
typedef Details::WFC::IObservableVector<T> WFC_Base;
typedef Details::WFC::VectorChangedEventHandler<T> WFC_Handler;
#if _COLLECTION_WUXI
typedef Details::WUXI::IBindableObservableVector WUXI_Base;
typedef Details::WUXI::BindableVectorChangedEventHandler WUXI_Handler;
#endif // _COLLECTION_WUXI
internal:
Vector() {
Details::Init(m_ctr, m_vec);
m_observed = false;
}
explicit Vector(unsigned int size) {
Details::Init(m_ctr, m_vec, size);
m_observed = false;
}
Vector(unsigned int size, T value) {
Details::Init(m_ctr, m_vec, size, Details::MakeWrap(value));
m_observed = false;
}
template <typename U> explicit Vector(const ::std::vector<U>& v, typename Details::VectorEnableIf<T, U>::type = nullptr) {
Details::Init(m_ctr, m_vec, v.begin(), v.end());
m_observed = false;
}
template <typename U> explicit Vector(::std::vector<U>&& v, typename Details::VectorEnableIf<T, U>::type = nullptr) {
Details::InitMoveVector(m_ctr, m_vec, ::std::move(v));
m_observed = false;
}
Vector(const T * ptr, unsigned int size) {
Details::Init(m_ctr, m_vec, ptr, ptr + size);
m_observed = false;
}
template <size_t N> explicit Vector(const T (&arr)[N]) {
Details::Init(m_ctr, m_vec, arr, arr + N);
m_observed = false;
}
template <size_t N> explicit Vector(const ::std::array<T, N>& a) {
Details::Init(m_ctr, m_vec, a.begin(), a.end());
m_observed = false;
}
explicit Vector(const Array<T>^ arr) {
Details::Init(m_ctr, m_vec, arr->begin(), arr->end());
m_observed = false;
}
template <typename InIt> Vector(InIt first, InIt last) {
// SFINAE is unnecessary here.
Details::Init(m_ctr, m_vec, first, last);
m_observed = false;
}
Vector(::std::initializer_list<T> il) {
Details::Init(m_ctr, m_vec, il.begin(), il.end());
m_observed = false;
}
public:
virtual Details::WFC::IIterator<T>^ First() = WFC_Base::First {
return ref new Details::IteratorForVectorView<T>(m_ctr, m_vec);
}
virtual T GetAt(unsigned int index) = WFC_Base::GetAt {
Details::ValidateBounds(index < m_vec->size());
return Details::Unwrap((*m_vec)[index]);
}
virtual property unsigned int Size {
virtual unsigned int get() {
return static_cast<unsigned int>(m_vec->size());
}
}
virtual bool IndexOf(T value, unsigned int * index) = WFC_Base::IndexOf {
*index = 0;
return Details::VectorIndexOf<T, E>(*m_vec, value, index);
}
virtual unsigned int GetMany(unsigned int startIndex, WriteOnlyArray<T>^ dest) {
Details::ValidateBounds(startIndex <= m_vec->size());
return Details::VectorGetMany(*m_vec, startIndex, dest);
}
virtual Details::WFC::IVectorView<T>^ GetView() = WFC_Base::GetView {
return ref new VectorView<T, E>(m_ctr, m_vec);
}
virtual void SetAt(unsigned int index, T item) = WFC_Base::SetAt {
try {
Details::IncrementCounter(m_ctr);
Details::ValidateBounds(index < m_vec->size());
(*m_vec)[index] = item;
NotifyChanged(index);
_COLLECTION_TRANSLATE
}
virtual void InsertAt(unsigned int index, T item) = WFC_Base::InsertAt {
try {
Details::IncrementCounter(m_ctr);
Details::ValidateBounds(index <= m_vec->size());
Details::ValidateSize(m_vec->size() + 1);
Emplace(m_vec->begin() + index, item, ::std::is_same<T, bool>());
NotifyInserted(index);
_COLLECTION_TRANSLATE
}
virtual void Append(T item) = WFC_Base::Append {
try {
Details::IncrementCounter(m_ctr);
size_t n = m_vec->size();
Details::ValidateSize(n + 1);
EmplaceBack(item, ::std::is_same<T, bool>());
NotifyInserted(static_cast<unsigned int>(n));
_COLLECTION_TRANSLATE
}
virtual void RemoveAt(unsigned int index) {
try {
Details::IncrementCounter(m_ctr);
Details::ValidateBounds(index < m_vec->size());
m_vec->erase(m_vec->begin() + index);
NotifyRemoved(index);
_COLLECTION_TRANSLATE
}
virtual void RemoveAtEnd() {
try {
Details::IncrementCounter(m_ctr);
Details::ValidateBounds(!m_vec->empty());
m_vec->pop_back();
NotifyRemoved(static_cast<unsigned int>(m_vec->size()));
_COLLECTION_TRANSLATE
}
virtual void Clear() {
try {
Details::IncrementCounter(m_ctr);
m_vec->clear();
NotifyReset();
_COLLECTION_TRANSLATE
}
virtual void ReplaceAll(const Array<T>^ arr) {
try {
Details::IncrementCounter(m_ctr);
Details::ValidateSize(arr->Length);
m_vec->assign(arr->begin(), arr->end());
NotifyReset();
_COLLECTION_TRANSLATE
}
virtual event WFC_Handler^ VectorChanged {
virtual Details::Token add(WFC_Handler^ e) = WFC_Base::VectorChanged::add {
m_observed = true;
return m_wfc_event += e;
}
virtual void remove(Details::Token t) = WFC_Base::VectorChanged::remove {
m_wfc_event -= t;
}
};
private:
template <typename A, typename B> void Emplace(A&& a, B&& b, ::std::false_type) {
m_vec->emplace(::std::forward<A>(a), ::std::forward<B>(b));
}
template <typename A, typename B> void Emplace(A&& a, B&& b, ::std::true_type) {
m_vec->insert(::std::forward<A>(a), ::std::forward<B>(b));
}
template <typename A> void EmplaceBack(A&& a, ::std::false_type) {
m_vec->emplace_back(::std::forward<A>(a));
}
template <typename A> void EmplaceBack(A&& a, ::std::true_type) {
m_vec->push_back(::std::forward<A>(a));
}
void Notify(Details::WFC::CollectionChange change, unsigned int index) {
if (m_observed) {
auto args = ref new Details::VectorChangedEventArgs(change, index);
m_wfc_event(this, args);
#if _COLLECTION_WUXI
m_wuxi_event(this, args);
#endif // _COLLECTION_WUXI
}
}
void NotifyReset() {
Notify(Details::WFC::CollectionChange::Reset, 0);
}
void NotifyInserted(unsigned int index) {
Notify(Details::WFC::CollectionChange::ItemInserted, index);
}
void NotifyRemoved(unsigned int index) {
Notify(Details::WFC::CollectionChange::ItemRemoved, index);
}
void NotifyChanged(unsigned int index) {
Notify(Details::WFC::CollectionChange::ItemChanged, index);
}
#if _COLLECTION_WUXI
virtual Details::WUXI::IBindableIterator^ BindableFirst() = WUXI_Base::First {
return safe_cast<Details::WUXI::IBindableIterator^>(First());
}
virtual Object^ BindableGetAt(unsigned int index) = WUXI_Base::GetAt {
return GetAt(index);
}
virtual bool BindableIndexOf(Object^ value, unsigned int * index) = WUXI_Base::IndexOf {
*index = 0;
return Details::VectorBindableIndexOf<T, E>(*m_vec, value, index);
}
virtual Details::WUXI::IBindableVectorView^ BindableGetView() = WUXI_Base::GetView {
return safe_cast<Details::WUXI::IBindableVectorView^>(GetView());
}
virtual void BindableSetAt(unsigned int index, Object^ item) = WUXI_Base::SetAt {
SetAt(index, safe_cast<T>(item));
}
virtual void BindableInsertAt(unsigned int index, Object^ item) = WUXI_Base::InsertAt {
InsertAt(index, safe_cast<T>(item));
}
virtual void BindableAppend(Object^ item) = WUXI_Base::Append {
Append(safe_cast<T>(item));
}
virtual Details::Token BindableEventAdd(WUXI_Handler^ e) = WUXI_Base::VectorChanged::add {
m_observed = true;
return m_wuxi_event += e;
}
virtual void BindableEventRemove(Details::Token t) = WUXI_Base::VectorChanged::remove {
m_wuxi_event -= t;
}
#endif // _COLLECTION_WUXI
::std::shared_ptr<unsigned int> m_ctr;
::std::shared_ptr<WrappedVector> m_vec;
bool m_observed;
event WFC_Handler^ m_wfc_event;
#if _COLLECTION_WUXI
event WUXI_Handler^ m_wuxi_event;
#endif // _COLLECTION_WUXI
};
namespace Details {
template <typename K, typename V> ref class KeyValuePair sealed : public _COLLECTION_ATTRIBUTES WFC::IKeyValuePair<K, V> {
internal:
KeyValuePair(const typename Wrap<K>::type& key, const typename Wrap<V>::type& value)
: m_key(key), m_value(value) { }
public:
virtual property K Key {
virtual K get() {
return Unwrap(m_key);
}
}
virtual property V Value {
virtual V get() {
return Unwrap(m_value);
}
}
private:
typename Wrap<K>::type m_key;
typename Wrap<V>::type m_value;
};
template <typename K, typename F, bool = ::std::is_same<typename Wrap<K>::type, K>::value> class WrapFunc {
public:
typedef F type;
};
template <typename K, typename F> class WrapFunc<K, F, false> {
public:
typedef WrapFunc type;
WrapFunc(const F& func)
: m_func(func) { }
size_t operator()(const Agile<K>& k) const {
return m_func(k.Get());
}
bool operator()(const Agile<K>& l, const Agile<K>& r) const {
return m_func(l.Get(), r.Get());
}
private:
F m_func;
};
template <typename K, typename V, typename C> struct WrapMap {
typedef ::std::map<typename Wrap<K>::type, typename Wrap<V>::type, typename WrapFunc<K, C>::type> type;
};
template <typename K, typename V, typename H, typename P> struct WrapUnorderedMap {
typedef ::std::unordered_map<typename Wrap<K>::type, typename Wrap<V>::type, typename WrapFunc<K, H>::type, typename WrapFunc<K, P>::type> type;
};
template <typename K, typename V, typename WrappedMap> ref class IteratorForAnyMapView sealed : public _COLLECTION_ATTRIBUTES WFC::IIterator<WFC::IKeyValuePair<K, V>^> {
internal:
IteratorForAnyMapView(const ::std::shared_ptr<unsigned int>& ctr, const ::std::shared_ptr<WrappedMap>& m)
: m_ctr(ctr), m_map(m), m_good_ctr(*ctr), m_iter(m->begin()) { }
public:
virtual property WFC::IKeyValuePair<K, V>^ Current {
virtual WFC::IKeyValuePair<K, V>^ get() {
ValidateCounter(m_ctr, m_good_ctr);
ValidateBounds(m_iter != m_map->end());
return ref new KeyValuePair<K, V>(m_iter->first, m_iter->second);
}
}
virtual property bool HasCurrent {
virtual bool get() {
ValidateCounter(m_ctr, m_good_ctr);
return m_iter != m_map->end();
}
}
virtual bool MoveNext() {
ValidateCounter(m_ctr, m_good_ctr);
ValidateBounds(m_iter != m_map->end());
++m_iter;
return m_iter != m_map->end();
}
virtual unsigned int GetMany(WriteOnlyArray<WFC::IKeyValuePair<K, V>^>^ dest) {
ValidateCounter(m_ctr, m_good_ctr);
unsigned int capacity = dest->Length;
unsigned int actual = 0;
while (capacity > 0 && m_iter != m_map->end()) {
dest->set(actual, ref new KeyValuePair<K, V>(m_iter->first, m_iter->second));
++m_iter;
--capacity;
++actual;
}
return actual;
}
private:
::std::shared_ptr<unsigned int> m_ctr;
::std::shared_ptr<WrappedMap> m_map;
unsigned int m_good_ctr;
typename WrappedMap::const_iterator m_iter;
};
} // namespace Details
template <typename K, typename V, typename C = ::std::less<K>, bool = __is_valid_winrt_type(K), bool = __is_valid_winrt_type(V)> ref class Map;
template <typename K, typename V, typename C = ::std::less<K>, bool = __is_valid_winrt_type(K), bool = __is_valid_winrt_type(V)> ref class MapView;
template <typename K, typename V, typename H = ::std::hash<K>, typename P = ::std::equal_to<K>, bool = __is_valid_winrt_type(K), bool = __is_valid_winrt_type(V)> ref class UnorderedMap;
template <typename K, typename V, typename H = ::std::hash<K>, typename P = ::std::equal_to<K>, bool = __is_valid_winrt_type(K), bool = __is_valid_winrt_type(V)> ref class UnorderedMapView;
template <typename K, typename V, typename C> ref class MapView<K, V, C, false, false> {
static_assert(Details::AlwaysFalse<K>::value, "Platform::Collections::MapView<K, V, C> requires K and V to be valid Windows Runtime types.");
};
template <typename K, typename V, typename C> ref class MapView<K, V, C, false, true> {
static_assert(Details::AlwaysFalse<K>::value, "Platform::Collections::MapView<K, V, C> requires K to be a valid Windows Runtime type.");
};
template <typename K, typename V, typename C> ref class MapView<K, V, C, true, false> {
static_assert(Details::AlwaysFalse<K>::value, "Platform::Collections::MapView<K, V, C> requires V to be a valid Windows Runtime type.");
};
template <typename K, typename V, typename C, bool, bool> ref class MapView sealed : public _COLLECTION_ATTRIBUTES Details::WFC::IMapView<K, V> {
private:
typedef typename Details::WrapMap<K, V, C>::type WrappedMap;
typedef Details::IteratorForAnyMapView<K, V, WrappedMap> MyIterator;
friend ref class Map<K, V, C>;
internal:
explicit MapView(const C& comp = C()) {
Details::Init(m_ctr, m_map, comp);
m_good_ctr = 0;
}
explicit MapView(const ::std::map<K, V, C>& m) {
Details::Init(m_ctr, m_map, m.key_comp());
Details::EmplaceWrappedRange<K, V>(*m_map, m.begin(), m.end());