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
base / win / map.h [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_MAP_H_
#define BASE_WIN_MAP_H_
#include <windows.foundation.collections.h>
#include <wrl/implements.h>
#include <map>
#include <utility>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/notimplemented.h"
#include "base/win/vector.h"
#include "base/win/winrt_foundation_helpers.h"
namespace base {
namespace win {
template <typename K, typename V>
class Map;
namespace internal {
// Template tricks needed to dispatch to the correct implementation.
// See base/win/winrt_foundation_helpers.h for explanation.
template <typename K, typename V>
using ComplexK =
typename ABI::Windows::Foundation::Collections::IMap<K, V>::K_complex;
template <typename K, typename V>
using ComplexV =
typename ABI::Windows::Foundation::Collections::IMap<K, V>::V_complex;
template <typename K, typename V>
using LogicalK = LogicalType<ComplexK<K, V>>;
template <typename K, typename V>
using LogicalV = LogicalType<ComplexV<K, V>>;
template <typename K, typename V>
using AbiK = AbiType<ComplexK<K, V>>;
template <typename K, typename V>
using AbiV = AbiType<ComplexV<K, V>>;
template <typename K, typename V>
using StorageK = StorageType<ComplexK<K, V>>;
template <typename K, typename V>
using StorageV = StorageType<ComplexV<K, V>>;
template <typename K, typename V>
class KeyValuePair : public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRtClassicComMix |
Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::Collections::
IKeyValuePair<LogicalK<K, V>, LogicalV<K, V>>> {
public:
using AbiK = AbiK<K, V>;
using AbiV = AbiV<K, V>;
using StorageK = StorageK<K, V>;
using StorageV = StorageV<K, V>;
KeyValuePair(StorageK key, StorageV value)
: key_(std::move(key)), value_(std::move(value)) {}
// ABI::Windows::Foundation::Collections::IKeyValuePair:
IFACEMETHODIMP get_Key(AbiK* key) { return CopyTo(key_, key); }
IFACEMETHODIMP get_Value(AbiV* value) { return CopyTo(value_, value); }
private:
StorageK key_;
StorageV value_;
};
template <typename K>
class MapChangedEventArgs
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRtClassicComMix |
Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::Collections::IMapChangedEventArgs<K>> {
public:
MapChangedEventArgs(
ABI::Windows::Foundation::Collections::CollectionChange change,
K key)
: change_(change), key_(std::move(key)) {}
~MapChangedEventArgs() override = default;
// ABI::Windows::Foundation::Collections::IMapChangedEventArgs:
IFACEMETHODIMP get_CollectionChange(
ABI::Windows::Foundation::Collections::CollectionChange* value) override {
*value = change_;
return S_OK;
}
IFACEMETHODIMP get_Key(K* value) override {
*value = key_;
return S_OK;
}
private:
const ABI::Windows::Foundation::Collections::CollectionChange change_;
K key_;
};
} // namespace internal
// This file provides an implementation of Windows::Foundation::IMap. It
// functions as a thin wrapper around an std::map, and dispatches
// method calls to either the corresponding std::map API or
// appropriate std algorithms. Furthermore, it notifies its observers whenever
// its observable state changes, and is iterable. Please notice also that if the
// map is modified while iterating over it, iterator methods will return
// E_CHANGED_STATE. A base::win::Map can be constructed for any types <K,V>, and
// is implicitly constructible from a std::map. In the case where K or V is a
// pointer derived from IUnknown, the std::map needs to be of type
// Microsoft::WRL::ComPtr<K> or Microsoft::WRL::ComPtr<V>. This enforces proper
// reference counting and improves safety.
template <typename K, typename V>
class Map
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::Collections::IMap<internal::LogicalK<K, V>,
internal::LogicalV<K, V>>,
ABI::Windows::Foundation::Collections::IObservableMap<
internal::LogicalK<K, V>,
internal::LogicalV<K, V>>,
ABI::Windows::Foundation::Collections::IIterable<
ABI::Windows::Foundation::Collections::IKeyValuePair<
internal::LogicalK<K, V>,
internal::LogicalV<K, V>>*>> {
public:
using LogicalK = internal::LogicalK<K, V>;
using LogicalV = internal::LogicalV<K, V>;
using AbiK = internal::AbiK<K, V>;
using AbiV = internal::AbiV<K, V>;
using StorageK = internal::StorageK<K, V>;
using StorageV = internal::StorageV<K, V>;
private:
class MapView;
// Iterates over base::win::Map.
// Its methods return E_CHANGED_STATE is the map is modified.
// TODO(crbug.com/40637532): Refactor MapIterator to leverage
// std::map::iterator.
class MapIterator
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRtClassicComMix |
Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::Collections::IIterator<
ABI::Windows::Foundation::Collections::IKeyValuePair<
internal::LogicalK<K, V>,
internal::LogicalV<K, V>>*>> {
public:
explicit MapIterator(Microsoft::WRL::ComPtr<MapView> view)
: view_(std::move(view)) {
DCHECK(view_->ValidState());
ConvertMapToVectorIterator();
}
// ABI::Windows::Foundation::Collections::IIterator:
IFACEMETHODIMP get_Current(
ABI::Windows::Foundation::Collections::IKeyValuePair<LogicalK,
LogicalV>**
current) override {
return view_->ValidState() ? iterator_->get_Current(current)
: E_CHANGED_STATE;
}
IFACEMETHODIMP get_HasCurrent(boolean* has_current) override {
return view_->ValidState() ? iterator_->get_HasCurrent(has_current)
: E_CHANGED_STATE;
}
IFACEMETHODIMP MoveNext(boolean* has_current) override {
return view_->ValidState() ? iterator_->MoveNext(has_current)
: E_CHANGED_STATE;
}
IFACEMETHODIMP GetMany(
unsigned capacity,
ABI::Windows::Foundation::Collections::IKeyValuePair<LogicalK,
LogicalV>** value,
unsigned* actual) override {
return view_->ValidState() ? iterator_->GetMany(capacity, value, actual)
: E_CHANGED_STATE;
}
private:
// Helper for iteration:
void ConvertMapToVectorIterator() {
// Create a vector that will hold Map's key-value pairs.
auto vector = Microsoft::WRL::Make<
Vector<ABI::Windows::Foundation::Collections::IKeyValuePair<
LogicalK, LogicalV>*>>();
// Fill the vector with container data.
for (const auto& pair : view_->get_map()) {
auto key_value_pair =
Microsoft::WRL::Make<internal::KeyValuePair<AbiK, AbiV>>(
pair.first, pair.second);
vector->Append(key_value_pair.Get());
}
// Return an iterator to that vector.
// Iterator is immutable (wraps an IVectorView) and Vector's lifecycle is
// ensured cause the view holds a reference to the vector, and iterator
// holds a reference to the view.
HRESULT hr = vector->First(&iterator_);
DCHECK(SUCCEEDED(hr));
}
Microsoft::WRL::ComPtr<MapView> view_;
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::Collections::IIterator<
ABI::Windows::Foundation::Collections::IKeyValuePair<LogicalK,
LogicalV>*>>
iterator_;
};
class MapView
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRtClassicComMix |
Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::Collections::
IMapView<internal::LogicalK<K, V>, internal::LogicalV<K, V>>,
ABI::Windows::Foundation::Collections::IIterable<
ABI::Windows::Foundation::Collections::IKeyValuePair<
internal::LogicalK<K, V>,
internal::LogicalV<K, V>>*>,
ABI::Windows::Foundation::Collections::MapChangedEventHandler<
internal::LogicalK<K, V>,
internal::LogicalV<K, V>>> {
public:
explicit MapView(Microsoft::WRL::ComPtr<Map<LogicalK, LogicalV>> map)
: map_(std::move(map)) {
map_->add_MapChanged(this, &map_changed_token_);
}
~MapView() override {
if (map_)
map_->remove_MapChanged(map_changed_token_);
}
// ABI::Windows::Foundation::Collections::IMapView:
IFACEMETHODIMP Lookup(AbiK key, AbiV* value) override {
return map_ ? map_->Lookup(key, value) : E_CHANGED_STATE;
}
IFACEMETHODIMP get_Size(unsigned int* size) override {
return map_ ? map_->get_Size(size) : E_CHANGED_STATE;
}
IFACEMETHODIMP HasKey(AbiK key, boolean* found) override {
return map_ ? map_->HasKey(key, found) : E_CHANGED_STATE;
}
IFACEMETHODIMP Split(
ABI::Windows::Foundation::Collections::IMapView<LogicalK, LogicalV>**
first_partition,
ABI::Windows::Foundation::Collections::IMapView<LogicalK, LogicalV>**
second_partition) override {
NOTIMPLEMENTED();
return E_NOTIMPL;
}
// ABI::Windows::Foundation::Collections::IIterable:
IFACEMETHODIMP First(
ABI::Windows::Foundation::Collections::IIterator<
ABI::Windows::Foundation::Collections::IKeyValuePair<LogicalK,
LogicalV>*>**
first) override {
return map_ ? map_->First(first) : E_CHANGED_STATE;
}
// ABI::Windows::Foundation::Collections::MapChangedEventHandler:
IFACEMETHODIMP Invoke(
ABI::Windows::Foundation::Collections::IObservableMap<LogicalK,
LogicalV>* sender,
ABI::Windows::Foundation::Collections::IMapChangedEventArgs<LogicalK>*
e) override {
DCHECK_EQ(map_.Get(), sender);
map_.Reset();
sender->remove_MapChanged(map_changed_token_);
return S_OK;
}
// Accessor used in MapIterator for iterating over Map's container.
// Will remain valid during the entire iteration.
const std::map<StorageK, StorageV, internal::Less>& get_map() {
DCHECK(map_);
return map_->map_;
}
bool ValidState() const { return map_; }
private:
Microsoft::WRL::ComPtr<Map<LogicalK, LogicalV>> map_;
EventRegistrationToken map_changed_token_;
};
public:
Map() = default;
explicit Map(const std::map<StorageK, StorageV, internal::Less>& map)
: map_(map) {}
explicit Map(std::map<StorageK, StorageV, internal::Less>&& map)
: map_(std::move(map)) {}
// ABI::Windows::Foundation::Collections::IMap:
IFACEMETHODIMP Lookup(AbiK key, AbiV* value) override {
auto it = map_.find(key);
if (it == map_.cend())
return E_BOUNDS;
return internal::CopyTo(it->second, value);
}
IFACEMETHODIMP get_Size(unsigned int* size) override {
*size = map_.size();
return S_OK;
}
IFACEMETHODIMP HasKey(AbiK key, boolean* found) override {
*found = Contains(map_, key);
return S_OK;
}
IFACEMETHODIMP GetView(
ABI::Windows::Foundation::Collections::IMapView<LogicalK, LogicalV>**
view) override {
return Microsoft::WRL::Make<MapView>(this).CopyTo(view);
}
IFACEMETHODIMP Insert(AbiK key, AbiV value, boolean* replaced) override {
auto [it, inserted] = map_.insert_or_assign(key, std::move(value));
*replaced = !inserted;
NotifyMapChanged(*replaced ? ABI::Windows::Foundation::Collections::
CollectionChange_ItemChanged
: ABI::Windows::Foundation::Collections::
CollectionChange_ItemInserted,
key);
return S_OK;
}
IFACEMETHODIMP Remove(AbiK key) override {
if (!map_.erase(key))
return E_BOUNDS;
NotifyMapChanged(
ABI::Windows::Foundation::Collections::CollectionChange_ItemRemoved,
key);
return S_OK;
}
IFACEMETHODIMP Clear() override {
map_.clear();
NotifyMapChanged(
ABI::Windows::Foundation::Collections::CollectionChange_Reset, 0);
return S_OK;
}
// ABI::Windows::Foundation::Collections::IObservableMap:
IFACEMETHODIMP add_MapChanged(
ABI::Windows::Foundation::Collections::MapChangedEventHandler<LogicalK,
LogicalV>*
handler,
EventRegistrationToken* token) override {
token->value = handler_id_++;
handlers_.emplace_hint(handlers_.end(), token->value, handler);
return S_OK;
}
IFACEMETHODIMP remove_MapChanged(EventRegistrationToken token) override {
return handlers_.erase(token.value) ? S_OK : E_BOUNDS;
}
// ABI::Windows::Foundation::Collections::IIterable:
IFACEMETHODIMP First(
ABI::Windows::Foundation::Collections::IIterator<
ABI::Windows::Foundation::Collections::IKeyValuePair<LogicalK,
LogicalV>*>**
first) override {
return Microsoft::WRL::Make<MapIterator>(
Microsoft::WRL::Make<MapView>(this))
.CopyTo(first);
}
private:
~Map() override {
// Handlers should not outlive the Map. Furthermore, they must ensure
// they are unregistered before the handler is destroyed. This implies
// there should be no handlers left when the Map is destructed.
DCHECK(handlers_.empty());
}
void NotifyMapChanged(
ABI::Windows::Foundation::Collections::CollectionChange change,
AbiK key) {
auto args =
Microsoft::WRL::Make<internal::MapChangedEventArgs<AbiK>>(change, key);
// Invoking the handlers could result in mutations to the map, thus we make
// a copy beforehand.
auto handlers = handlers_;
for (auto& handler : handlers)
handler.second->Invoke(this, args.Get());
}
std::map<StorageK, StorageV, internal::Less> map_;
base::flat_map<int64_t,
ABI::Windows::Foundation::Collections::
MapChangedEventHandler<LogicalK, LogicalV>*>
handlers_;
int64_t handler_id_ = 0;
};
} // namespace win
} // namespace base
#endif // BASE_WIN_MAP_H_