| 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 <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 // Returns a new Map that contains a copy of the contents of this map. If the | 130 // Returns a new Map that contains a copy of the contents of this map. If the |
| 131 // values are of a type that is designated move-only, they will be cloned | 131 // values are of a type that is designated move-only, they will be cloned |
| 132 // using the Clone() method of the type. Please note that calling this method | 132 // using the Clone() method of the type. Please note that calling this method |
| 133 // will fail compilation if the value type cannot be cloned (which usually | 133 // will fail compilation if the value type cannot be cloned (which usually |
| 134 // means that it is a Mojo handle type or a type that contains Mojo handles). | 134 // means that it is a Mojo handle type or a type that contains Mojo handles). |
| 135 Map Clone() const { | 135 Map Clone() const { |
| 136 Map result; | 136 Map result; |
| 137 result.is_null_ = is_null_; | 137 result.is_null_ = is_null_; |
| 138 Traits::Clone(map_, &result.map_); | 138 Traits::Clone(map_, &result.map_); |
| 139 return result.Pass(); | 139 return result; |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Indicates whether the contents of this map are equal to those of another | 142 // Indicates whether the contents of this map are equal to those of another |
| 143 // Map (including nullness). Keys are compared by the != operator. Values are | 143 // Map (including nullness). Keys are compared by the != operator. Values are |
| 144 // compared as follows: | 144 // compared as follows: |
| 145 // - Map, Array, Struct, or StructPtr values are compared by their Equals() | 145 // - Map, Array, Struct, or StructPtr values are compared by their Equals() |
| 146 // method. | 146 // method. |
| 147 // - ScopedHandleBase-derived types are compared by their handles. | 147 // - ScopedHandleBase-derived types are compared by their handles. |
| 148 // - Values of other types are compared by their "==" operator. | 148 // - Values of other types are compared by their "==" operator. |
| 149 bool Equals(const Map& other) const { | 149 bool Equals(const Map& other) const { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 typename STLValue> | 267 typename STLValue> |
| 268 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { | 268 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { |
| 269 static Map<MojoKey, MojoValue> Convert( | 269 static Map<MojoKey, MojoValue> Convert( |
| 270 const std::map<STLKey, STLValue>& input) { | 270 const std::map<STLKey, STLValue>& input) { |
| 271 Map<MojoKey, MojoValue> result; | 271 Map<MojoKey, MojoValue> result; |
| 272 result.mark_non_null(); | 272 result.mark_non_null(); |
| 273 for (auto& pair : input) { | 273 for (auto& pair : input) { |
| 274 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), | 274 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), |
| 275 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); | 275 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); |
| 276 } | 276 } |
| 277 return result.Pass(); | 277 return result; |
| 278 } | 278 } |
| 279 }; | 279 }; |
| 280 | 280 |
| 281 // Copies the contents of a Map to an std::map, optionally changing the types of | 281 // Copies the contents of a Map to an std::map, optionally changing the types of |
| 282 // the keys and values along the way using TypeConverter. | 282 // the keys and values along the way using TypeConverter. |
| 283 template <typename MojoKey, | 283 template <typename MojoKey, |
| 284 typename MojoValue, | 284 typename MojoValue, |
| 285 typename STLKey, | 285 typename STLKey, |
| 286 typename STLValue> | 286 typename STLValue> |
| 287 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> { | 287 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> { |
| 288 static std::map<STLKey, STLValue> Convert( | 288 static std::map<STLKey, STLValue> Convert( |
| 289 const Map<MojoKey, MojoValue>& input) { | 289 const Map<MojoKey, MojoValue>& input) { |
| 290 std::map<STLKey, STLValue> result; | 290 std::map<STLKey, STLValue> result; |
| 291 if (!input.is_null()) { | 291 if (!input.is_null()) { |
| 292 for (auto it = input.cbegin(); it != input.cend(); ++it) { | 292 for (auto it = input.cbegin(); it != input.cend(); ++it) { |
| 293 result.insert(std::make_pair( | 293 result.insert(std::make_pair( |
| 294 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), | 294 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), |
| 295 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); | 295 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); |
| 296 } | 296 } |
| 297 } | 297 } |
| 298 return result; | 298 return result; |
| 299 } | 299 } |
| 300 }; | 300 }; |
| 301 | 301 |
| 302 } // namespace mojo | 302 } // namespace mojo |
| 303 | 303 |
| 304 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | 304 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
| OLD | NEW |