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