OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/move.h" | 13 #include "base/macros.h" |
14 #include "mojo/public/cpp/bindings/array.h" | 14 #include "mojo/public/cpp/bindings/array.h" |
15 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 15 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
16 #include "mojo/public/cpp/bindings/lib/template_util.h" | 16 #include "mojo/public/cpp/bindings/lib/template_util.h" |
17 #include "mojo/public/cpp/bindings/type_converter.h" | 17 #include "mojo/public/cpp/bindings/type_converter.h" |
18 | 18 |
19 namespace mojo { | 19 namespace mojo { |
20 | 20 |
21 // A move-only map that can handle move-only values. Map has the following | 21 // A move-only map that can handle move-only values. Map has the following |
22 // characteristics: | 22 // characteristics: |
23 // - The map itself can be null, and this is distinct from empty. | 23 // - The map itself can be null, and this is distinct from empty. |
24 // - Keys must not be move-only. | 24 // - Keys must not be move-only. |
25 // - The Key-type's "<" operator is used to sort the entries, and also is | 25 // - The Key-type's "<" operator is used to sort the entries, and also is |
26 // used to determine equality of the key values. | 26 // used to determine equality of the key values. |
27 // - There can only be one entry per unique key. | 27 // - There can only be one entry per unique key. |
28 // - Values of move-only types will be moved into the Map when they are added | 28 // - Values of move-only types will be moved into the Map when they are added |
29 // using the insert() method. | 29 // using the insert() method. |
30 template <typename K, typename V> | 30 template <typename K, typename V> |
31 class Map { | 31 class Map { |
32 MOVE_ONLY_TYPE_FOR_CPP_03(Map); | |
33 | |
34 public: | 32 public: |
35 using Key = K; | 33 using Key = K; |
36 using Value = V; | 34 using Value = V; |
37 | 35 |
38 // Map keys cannot be move only classes. | 36 // Map keys cannot be move only classes. |
39 static_assert(!internal::IsMoveOnlyType<Key>::value, | 37 static_assert(!internal::IsMoveOnlyType<Key>::value, |
40 "Map keys cannot be move only types."); | 38 "Map keys cannot be move only types."); |
41 | 39 |
42 using Iterator = typename std::map<Key, Value>::iterator; | 40 using Iterator = typename std::map<Key, Value>::iterator; |
43 using ConstIterator = typename std::map<Key, Value>::const_iterator; | 41 using ConstIterator = typename std::map<Key, Value>::const_iterator; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 template <typename T, typename U> | 253 template <typename T, typename U> |
256 bool operator!=(const Map<T, U>& other) const = delete; | 254 bool operator!=(const Map<T, U>& other) const = delete; |
257 | 255 |
258 void Take(Map* other) { | 256 void Take(Map* other) { |
259 operator=(nullptr); | 257 operator=(nullptr); |
260 Swap(other); | 258 Swap(other); |
261 } | 259 } |
262 | 260 |
263 std::map<Key, Value> map_; | 261 std::map<Key, Value> map_; |
264 bool is_null_; | 262 bool is_null_; |
| 263 |
| 264 DISALLOW_COPY_AND_ASSIGN(Map); |
265 }; | 265 }; |
266 | 266 |
267 // Copies the contents of an std::map to a new Map, optionally changing the | 267 // Copies the contents of an std::map to a new Map, optionally changing the |
268 // types of the keys and values along the way using TypeConverter. | 268 // types of the keys and values along the way using TypeConverter. |
269 template <typename MojoKey, | 269 template <typename MojoKey, |
270 typename MojoValue, | 270 typename MojoValue, |
271 typename STLKey, | 271 typename STLKey, |
272 typename STLValue> | 272 typename STLValue> |
273 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { | 273 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { |
274 static Map<MojoKey, MojoValue> Convert( | 274 static Map<MojoKey, MojoValue> Convert( |
(...skipping 24 matching lines...) Expand all Loading... |
299 TypeConverter<STLValue, MojoValue>::Convert(it->second))); | 299 TypeConverter<STLValue, MojoValue>::Convert(it->second))); |
300 } | 300 } |
301 } | 301 } |
302 return result; | 302 return result; |
303 } | 303 } |
304 }; | 304 }; |
305 | 305 |
306 } // namespace mojo | 306 } // namespace mojo |
307 | 307 |
308 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | 308 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
OLD | NEW |