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 <map> | 8 #include <map> |
| 9 #include <utility> |
9 | 10 |
10 #include "mojo/public/cpp/bindings/lib/map_internal.h" | 11 #include "mojo/public/cpp/bindings/lib/map_internal.h" |
11 #include "mojo/public/cpp/bindings/lib/value_traits.h" | 12 #include "mojo/public/cpp/bindings/lib/value_traits.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 | 15 |
15 // A move-only map that can handle move-only values. Map has the following | 16 // A move-only map that can handle move-only values. Map has the following |
16 // characteristics: | 17 // characteristics: |
17 // - The map itself can be null, and this is distinct from empty. | 18 // - The map itself can be null, and this is distinct from empty. |
18 // - Keys must not be move-only. | 19 // - Keys must not be move-only. |
(...skipping 27 matching lines...) Expand all Loading... |
46 typedef internal::Map_Data<typename internal::WrapperTraits<Key>::DataType, | 47 typedef internal::Map_Data<typename internal::WrapperTraits<Key>::DataType, |
47 typename internal::WrapperTraits<Value>::DataType> | 48 typename internal::WrapperTraits<Value>::DataType> |
48 Data_; | 49 Data_; |
49 | 50 |
50 Map() : is_null_(true) {} | 51 Map() : is_null_(true) {} |
51 | 52 |
52 // Constructs a non-null Map containing the specified |keys| mapped to the | 53 // Constructs a non-null Map containing the specified |keys| mapped to the |
53 // corresponding |values|. | 54 // corresponding |values|. |
54 Map(mojo::Array<Key> keys, mojo::Array<Value> values) : is_null_(false) { | 55 Map(mojo::Array<Key> keys, mojo::Array<Value> values) : is_null_(false) { |
55 MOJO_DCHECK(keys.size() == values.size()); | 56 MOJO_DCHECK(keys.size() == values.size()); |
56 Traits::InitializeFrom(&map_, keys.Pass(), values.Pass()); | 57 Traits::InitializeFrom(&map_, std::move(keys), std::move(values)); |
57 } | 58 } |
58 | 59 |
59 ~Map() { Traits::Finalize(&map_); } | 60 ~Map() { Traits::Finalize(&map_); } |
60 | 61 |
61 Map(Map&& other) : is_null_(true) { Take(&other); } | 62 Map(Map&& other) : is_null_(true) { Take(&other); } |
62 Map& operator=(Map&& other) { | 63 Map& operator=(Map&& other) { |
63 Take(&other); | 64 Take(&other); |
64 return *this; | 65 return *this; |
65 } | 66 } |
66 | 67 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 150 |
150 // Returns a new Map that contains a copy of the contents of this map. If the | 151 // Returns a new Map that contains a copy of the contents of this map. If the |
151 // values are of a type that is designated move-only, they will be cloned | 152 // values are of a type that is designated move-only, they will be cloned |
152 // using the Clone() method of the type. Please note that calling this method | 153 // using the Clone() method of the type. Please note that calling this method |
153 // will fail compilation if the value type cannot be cloned (which usually | 154 // will fail compilation if the value type cannot be cloned (which usually |
154 // means that it is a Mojo handle type or a type that contains Mojo handles). | 155 // means that it is a Mojo handle type or a type that contains Mojo handles). |
155 Map Clone() const { | 156 Map Clone() const { |
156 Map result; | 157 Map result; |
157 result.is_null_ = is_null_; | 158 result.is_null_ = is_null_; |
158 Traits::Clone(map_, &result.map_); | 159 Traits::Clone(map_, &result.map_); |
159 return result.Pass(); | 160 return std::move(result); |
160 } | 161 } |
161 | 162 |
162 // Indicates whether the contents of this map are equal to those of another | 163 // Indicates whether the contents of this map are equal to those of another |
163 // Map (including nullness). Keys are compared by the != operator. Values are | 164 // Map (including nullness). Keys are compared by the != operator. Values are |
164 // compared as follows: | 165 // compared as follows: |
165 // - Map, Array, Struct, or StructPtr values are compared by their Equals() | 166 // - Map, Array, Struct, or StructPtr values are compared by their Equals() |
166 // method. | 167 // method. |
167 // - ScopedHandleBase-derived types are compared by their handles. | 168 // - ScopedHandleBase-derived types are compared by their handles. |
168 // - Values of other types are compared by their "==" operator. | 169 // - Values of other types are compared by their "==" operator. |
169 bool Equals(const Map& other) const { | 170 bool Equals(const Map& other) const { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 typename STLValue> | 260 typename STLValue> |
260 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { | 261 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { |
261 static Map<MojoKey, MojoValue> Convert( | 262 static Map<MojoKey, MojoValue> Convert( |
262 const std::map<STLKey, STLValue>& input) { | 263 const std::map<STLKey, STLValue>& input) { |
263 Map<MojoKey, MojoValue> result; | 264 Map<MojoKey, MojoValue> result; |
264 result.mark_non_null(); | 265 result.mark_non_null(); |
265 for (auto& pair : input) { | 266 for (auto& pair : input) { |
266 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), | 267 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), |
267 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); | 268 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); |
268 } | 269 } |
269 return result.Pass(); | 270 return std::move(result); |
270 } | 271 } |
271 }; | 272 }; |
272 | 273 |
273 // Copies the contents of a Map to an std::map, optionally changing the types of | 274 // Copies the contents of a Map to an std::map, optionally changing the types of |
274 // the keys and values along the way using TypeConverter. | 275 // the keys and values along the way using TypeConverter. |
275 template <typename MojoKey, | 276 template <typename MojoKey, |
276 typename MojoValue, | 277 typename MojoValue, |
277 typename STLKey, | 278 typename STLKey, |
278 typename STLValue> | 279 typename STLValue> |
279 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> { | 280 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> { |
280 static std::map<STLKey, STLValue> Convert( | 281 static std::map<STLKey, STLValue> Convert( |
281 const Map<MojoKey, MojoValue>& input) { | 282 const Map<MojoKey, MojoValue>& input) { |
282 std::map<STLKey, STLValue> result; | 283 std::map<STLKey, STLValue> result; |
283 if (!input.is_null()) { | 284 if (!input.is_null()) { |
284 for (auto it = input.begin(); it != input.end(); ++it) { | 285 for (auto it = input.begin(); it != input.end(); ++it) { |
285 result.insert(std::make_pair( | 286 result.insert(std::make_pair( |
286 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), | 287 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), |
287 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); | 288 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); |
288 } | 289 } |
289 } | 290 } |
290 return result; | 291 return result; |
291 } | 292 } |
292 }; | 293 }; |
293 | 294 |
294 } // namespace mojo | 295 } // namespace mojo |
295 | 296 |
296 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | 297 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
OLD | NEW |