-
Notifications
You must be signed in to change notification settings - Fork 247
/
base_collections_vector.h
339 lines (274 loc) · 11.5 KB
/
base_collections_vector.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
namespace winrt::impl
{
template <typename T, typename Container>
using multi_threaded_vector = vector_impl<T, Container, multi_threaded_collection_base>;
template <typename Container, typename ThreadingBase = single_threaded_collection_base>
struct inspectable_observable_vector :
observable_vector_base<inspectable_observable_vector<Container, ThreadingBase>, Windows::Foundation::IInspectable>,
implements<inspectable_observable_vector<Container, ThreadingBase>,
wfc::IObservableVector<Windows::Foundation::IInspectable>, wfc::IVector<Windows::Foundation::IInspectable>, wfc::IVectorView<Windows::Foundation::IInspectable>, wfc::IIterable<Windows::Foundation::IInspectable>>,
ThreadingBase
{
static_assert(std::is_same_v<Container, std::remove_reference_t<Container>>, "Must be constructed with rvalue.");
explicit inspectable_observable_vector(Container&& values) : m_values(std::forward<Container>(values))
{
}
auto& get_container() noexcept
{
return m_values;
}
auto& get_container() const noexcept
{
return m_values;
}
using ThreadingBase::acquire_shared;
using ThreadingBase::acquire_exclusive;
private:
Container m_values;
};
template <typename Container>
using multi_threaded_inspectable_observable_vector = inspectable_observable_vector<Container, multi_threaded_collection_base>;
template <typename T, typename Container, typename ThreadingBase = single_threaded_collection_base>
struct convertible_observable_vector :
observable_vector_base<convertible_observable_vector<T, Container, ThreadingBase>, T>,
implements<convertible_observable_vector<T, Container, ThreadingBase>,
wfc::IObservableVector<T>, wfc::IVector<T>, wfc::IVectorView<T>, wfc::IIterable<T>,
wfc::IObservableVector<Windows::Foundation::IInspectable>, wfc::IVector<Windows::Foundation::IInspectable>, wfc::IVectorView<Windows::Foundation::IInspectable>, wfc::IIterable<Windows::Foundation::IInspectable>>,
ThreadingBase
{
static_assert(!std::is_same_v<T, Windows::Foundation::IInspectable>);
static_assert(std::is_same_v<Container, std::remove_reference_t<Container>>, "Must be constructed with rvalue.");
using container_type = convertible_observable_vector<T, Container, ThreadingBase>;
using base_type = observable_vector_base<convertible_observable_vector<T, Container, ThreadingBase>, T>;
explicit convertible_observable_vector(Container&& values) : m_values(std::forward<Container>(values))
{
}
auto& get_container() noexcept
{
return m_values;
}
auto& get_container() const noexcept
{
return m_values;
}
using ThreadingBase::acquire_shared;
using ThreadingBase::acquire_exclusive;
auto First()
{
struct result
{
container_type* container;
operator wfc::IIterator<T>()
{
return static_cast<base_type*>(container)->First();
}
operator wfc::IIterator<Windows::Foundation::IInspectable>()
{
[[maybe_unused]] auto guard = container->acquire_shared();
return make<iterator>(container);
}
};
return result{ this };
}
auto GetAt(uint32_t const index) const
{
struct result
{
base_type const* container;
uint32_t const index;
operator T() const
{
return container->GetAt(index);
}
operator Windows::Foundation::IInspectable() const
{
return box_value(container->GetAt(index));
}
};
return result{ this, index };
}
using base_type::IndexOf;
bool IndexOf(Windows::Foundation::IInspectable const& value, uint32_t& index) const
{
if constexpr (is_com_interface_v<T>)
{
if (!value)
{
return base_type::IndexOf(nullptr, index);
}
else if (auto as = value.try_as<T>())
{
return base_type::IndexOf(as, index);
}
}
else
{
if (auto as = value.try_as<T>())
{
return base_type::IndexOf(as.value(), index);
}
}
return false;
}
using base_type::GetMany;
uint32_t GetMany(uint32_t const startIndex, array_view<Windows::Foundation::IInspectable> values) const
{
[[maybe_unused]] auto guard = this->acquire_shared();
if (startIndex >= m_values.size())
{
return 0;
}
uint32_t const actual = (std::min)(static_cast<uint32_t>(m_values.size() - startIndex), values.size());
std::transform(m_values.begin() + startIndex, m_values.begin() + startIndex + actual, values.begin(), [&](auto && value)
{
return box_value(value);
});
return actual;
}
auto GetView() const noexcept
{
struct result
{
container_type const* container;
operator wfc::IVectorView<T>() const
{
return *container;
}
operator wfc::IVectorView<Windows::Foundation::IInspectable>() const
{
return *container;
}
};
return result{ this };
}
using base_type::SetAt;
void SetAt(uint32_t const index, Windows::Foundation::IInspectable const& value)
{
SetAt(index, unbox_value<T>(value));
}
using base_type::InsertAt;
void InsertAt(uint32_t const index, Windows::Foundation::IInspectable const& value)
{
InsertAt(index, unbox_value<T>(value));
}
using base_type::Append;
void Append(Windows::Foundation::IInspectable const& value)
{
Append(unbox_value<T>(value));
}
using base_type::ReplaceAll;
void ReplaceAll(array_view<Windows::Foundation::IInspectable const> values)
{
Container new_values;
new_values.reserve(values.size());
std::transform(values.begin(), values.end(), std::back_inserter(new_values), [&](auto && value)
{
return unbox_value<T>(value);
});
base_type::ReplaceAll(std::move(new_values));
}
using base_type::VectorChanged;
event_token VectorChanged(wfc::VectorChangedEventHandler<Windows::Foundation::IInspectable> const& handler)
{
return base_type::VectorChanged([handler](auto && sender, auto && args)
{
handler(sender.template try_as<wfc::IObservableVector<Windows::Foundation::IInspectable>>(), args);
});
}
private:
struct iterator :
impl::collection_version::iterator_type,
implements<iterator, Windows::Foundation::Collections::IIterator<Windows::Foundation::IInspectable>>
{
explicit iterator(container_type* const container) noexcept :
impl::collection_version::iterator_type(*container),
m_current(container->get_container().begin()),
m_end(container->get_container().end())
{
m_owner.copy_from(container);
}
Windows::Foundation::IInspectable Current() const
{
[[maybe_unused]] auto guard = m_owner->acquire_shared();
check_version(*m_owner);
if (m_current == m_end)
{
throw hresult_out_of_bounds();
}
return box_value(*m_current);
}
bool HasCurrent() const
{
[[maybe_unused]] auto guard = m_owner->acquire_shared();
check_version(*m_owner);
return m_current != m_end;
}
bool MoveNext()
{
[[maybe_unused]] auto guard = m_owner->acquire_exclusive();
check_version(*m_owner);
if (m_current != m_end)
{
++m_current;
}
return m_current != m_end;
}
uint32_t GetMany(array_view<Windows::Foundation::IInspectable> values)
{
[[maybe_unused]] auto guard = m_owner->acquire_exclusive();
check_version(*m_owner);
uint32_t const actual = (std::min)(static_cast<uint32_t>(std::distance(m_current, m_end)), values.size());
std::transform(m_current, m_current + actual, values.begin(), [&](auto && value)
{
return box_value(value);
});
std::advance(m_current, actual);
return actual;
}
private:
com_ptr<container_type> m_owner;
decltype(m_owner->get_container().begin()) m_current;
decltype(m_owner->get_container().end()) const m_end;
};
Container m_values;
};
template <typename T, typename Container>
using multi_threaded_convertible_observable_vector = convertible_observable_vector<T, Container, multi_threaded_collection_base>;
}
WINRT_EXPORT namespace winrt
{
template <typename T, typename Allocator = std::allocator<T>>
Windows::Foundation::Collections::IVector<T> single_threaded_vector(std::vector<T, Allocator>&& values = {})
{
return make<impl::input_vector<T, std::vector<T, Allocator>>>(std::move(values));
}
template <typename T, typename Allocator = std::allocator<T>>
Windows::Foundation::Collections::IVector<T> multi_threaded_vector(std::vector<T, Allocator>&& values = {})
{
return make<impl::multi_threaded_vector<T, std::vector<T, Allocator>>>(std::move(values));
}
template <typename T, typename Allocator = std::allocator<T>>
Windows::Foundation::Collections::IObservableVector<T> single_threaded_observable_vector(std::vector<T, Allocator>&& values = {})
{
if constexpr (std::is_same_v<T, Windows::Foundation::IInspectable>)
{
return make<impl::inspectable_observable_vector<std::vector<T, Allocator>>>(std::move(values));
}
else
{
return make<impl::convertible_observable_vector<T, std::vector<T, Allocator>>>(std::move(values));
}
}
template <typename T, typename Allocator = std::allocator<T>>
Windows::Foundation::Collections::IObservableVector<T> multi_threaded_observable_vector(std::vector<T, Allocator>&& values = {})
{
if constexpr (std::is_same_v<T, Windows::Foundation::IInspectable>)
{
return make<impl::multi_threaded_inspectable_observable_vector<std::vector<T, Allocator>>>(std::move(values));
}
else
{
return make<impl::multi_threaded_convertible_observable_vector<T, std::vector<T, Allocator>>>(std::move(values));
}
}
}