| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_ARRAY_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // calling the |Clone| method of the source element, which should make a copy | 132 // calling the |Clone| method of the source element, which should make a copy |
| 133 // of the element. | 133 // of the element. |
| 134 // | 134 // |
| 135 // Please note that calling this method will fail compilation if the element | 135 // Please note that calling this method will fail compilation if the element |
| 136 // type cannot be cloned (which usually means that it is a Mojo handle type or | 136 // type cannot be cloned (which usually means that it is a Mojo handle type or |
| 137 // a type contains Mojo handles). | 137 // a type contains Mojo handles). |
| 138 Array Clone() const { | 138 Array Clone() const { |
| 139 Array result; | 139 Array result; |
| 140 result.is_null_ = is_null_; | 140 result.is_null_ = is_null_; |
| 141 Traits::Clone(vec_, &result.vec_); | 141 Traits::Clone(vec_, &result.vec_); |
| 142 return result.Pass(); | 142 return result; |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Indicates whether the contents of this array are equal to |other|. A null | 145 // Indicates whether the contents of this array are equal to |other|. A null |
| 146 // array is only equal to another null array. Elements are compared using the | 146 // array is only equal to another null array. Elements are compared using the |
| 147 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method | 147 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method |
| 148 // of the element. | 148 // of the element. |
| 149 bool Equals(const Array& other) const { | 149 bool Equals(const Array& other) const { |
| 150 if (is_null() != other.is_null()) | 150 if (is_null() != other.is_null()) |
| 151 return false; | 151 return false; |
| 152 if (size() != other.size()) | 152 if (size() != other.size()) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 248 |
| 249 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 249 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
| 250 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | 250 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each |
| 251 // element. The returned array will always be non-null. | 251 // element. The returned array will always be non-null. |
| 252 template <typename T, typename E> | 252 template <typename T, typename E> |
| 253 struct TypeConverter<Array<T>, std::vector<E>> { | 253 struct TypeConverter<Array<T>, std::vector<E>> { |
| 254 static Array<T> Convert(const std::vector<E>& input) { | 254 static Array<T> Convert(const std::vector<E>& input) { |
| 255 auto result = Array<T>::New(input.size()); | 255 auto result = Array<T>::New(input.size()); |
| 256 for (size_t i = 0; i < input.size(); ++i) | 256 for (size_t i = 0; i < input.size(); ++i) |
| 257 result[i] = TypeConverter<T, E>::Convert(input[i]); | 257 result[i] = TypeConverter<T, E>::Convert(input[i]); |
| 258 return result.Pass(); | 258 return result; |
| 259 } | 259 } |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of | 262 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of |
| 263 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 263 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
| 264 // element. If the input array is null, the output vector will be empty. | 264 // element. If the input array is null, the output vector will be empty. |
| 265 template <typename E, typename T> | 265 template <typename E, typename T> |
| 266 struct TypeConverter<std::vector<E>, Array<T>> { | 266 struct TypeConverter<std::vector<E>, Array<T>> { |
| 267 static std::vector<E> Convert(const Array<T>& input) { | 267 static std::vector<E> Convert(const Array<T>& input) { |
| 268 std::vector<E> result; | 268 std::vector<E> result; |
| 269 if (!input.is_null()) { | 269 if (!input.is_null()) { |
| 270 result.resize(input.size()); | 270 result.resize(input.size()); |
| 271 for (size_t i = 0; i < input.size(); ++i) | 271 for (size_t i = 0; i < input.size(); ++i) |
| 272 result[i] = TypeConverter<E, T>::Convert(input[i]); | 272 result[i] = TypeConverter<E, T>::Convert(input[i]); |
| 273 } | 273 } |
| 274 return result; | 274 return result; |
| 275 } | 275 } |
| 276 }; | 276 }; |
| 277 | 277 |
| 278 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 278 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
| 279 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each | 279 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each |
| 280 // element. The returned array will always be non-null. | 280 // element. The returned array will always be non-null. |
| 281 template <typename T, typename E> | 281 template <typename T, typename E> |
| 282 struct TypeConverter<Array<T>, std::set<E>> { | 282 struct TypeConverter<Array<T>, std::set<E>> { |
| 283 static Array<T> Convert(const std::set<E>& input) { | 283 static Array<T> Convert(const std::set<E>& input) { |
| 284 Array<T> result = Array<T>::New(0u); | 284 Array<T> result = Array<T>::New(0u); |
| 285 for (auto i : input) | 285 for (auto i : input) |
| 286 result.push_back(TypeConverter<T, E>::Convert(i)); | 286 result.push_back(TypeConverter<T, E>::Convert(i)); |
| 287 return result.Pass(); | 287 return result; |
| 288 } | 288 } |
| 289 }; | 289 }; |
| 290 | 290 |
| 291 // A |TypeConverter| that will create an |std::set<E>| containing a copy of | 291 // A |TypeConverter| that will create an |std::set<E>| containing a copy of |
| 292 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 292 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
| 293 // element. If the input array is null, the output set will be empty. | 293 // element. If the input array is null, the output set will be empty. |
| 294 template <typename E, typename T> | 294 template <typename E, typename T> |
| 295 struct TypeConverter<std::set<E>, Array<T>> { | 295 struct TypeConverter<std::set<E>, Array<T>> { |
| 296 static std::set<E> Convert(const Array<T>& input) { | 296 static std::set<E> Convert(const Array<T>& input) { |
| 297 std::set<E> result; | 297 std::set<E> result; |
| 298 if (!input.is_null()) { | 298 if (!input.is_null()) { |
| 299 for (size_t i = 0; i < input.size(); ++i) | 299 for (size_t i = 0; i < input.size(); ++i) |
| 300 result.insert(TypeConverter<E, T>::Convert(input[i])); | 300 result.insert(TypeConverter<E, T>::Convert(input[i])); |
| 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_ARRAY_H_ | 308 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| OLD | NEW |