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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // Moves the contents of |other| into this array. | 49 // Moves the contents of |other| into this array. |
50 Array(Array&& other) : is_null_(true) { Take(&other); } | 50 Array(Array&& other) : is_null_(true) { Take(&other); } |
51 Array& operator=(Array&& other) { | 51 Array& operator=(Array&& other) { |
52 Take(&other); | 52 Take(&other); |
53 return *this; | 53 return *this; |
54 } | 54 } |
55 | 55 |
56 // Creates a non-null array of the specified size. The elements will be | 56 // Creates a non-null array of the specified size. The elements will be |
57 // value-initialized (meaning that they will be initialized by their default | 57 // value-initialized (meaning that they will be initialized by their default |
58 // constructor, if any, or else zero-initialized). | 58 // constructor, if any, or else zero-initialized). |
59 static Array New(size_t size) { return Array(size).Pass(); } | 59 static Array New(size_t size) { |
| 60 Array ret; |
| 61 ret.resize(size); |
| 62 return ret; |
| 63 } |
60 | 64 |
61 // Creates a new array with a copy of the contents of |other|. | 65 // Creates a new array with a copy of the contents of |other|. |
62 template <typename U> | 66 template <typename U> |
63 static Array From(const U& other) { | 67 static Array From(const U& other) { |
64 return TypeConverter<Array, U>::Convert(other); | 68 return TypeConverter<Array, U>::Convert(other); |
65 } | 69 } |
66 | 70 |
67 // Copies the contents of this array to a new object of type |U|. | 71 // Copies the contents of this array to a new object of type |U|. |
68 template <typename U> | 72 template <typename U> |
69 U To() const { | 73 U To() const { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 std::vector<StorageType> vec_; | 253 std::vector<StorageType> vec_; |
250 bool is_null_; | 254 bool is_null_; |
251 }; | 255 }; |
252 | 256 |
253 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 257 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
254 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | 258 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each |
255 // element. The returned array will always be non-null. | 259 // element. The returned array will always be non-null. |
256 template <typename T, typename E> | 260 template <typename T, typename E> |
257 struct TypeConverter<Array<T>, std::vector<E>> { | 261 struct TypeConverter<Array<T>, std::vector<E>> { |
258 static Array<T> Convert(const std::vector<E>& input) { | 262 static Array<T> Convert(const std::vector<E>& input) { |
259 Array<T> result(input.size()); | 263 auto result = Array<T>::New(input.size()); |
260 for (size_t i = 0; i < input.size(); ++i) | 264 for (size_t i = 0; i < input.size(); ++i) |
261 result[i] = TypeConverter<T, E>::Convert(input[i]); | 265 result[i] = TypeConverter<T, E>::Convert(input[i]); |
262 return result.Pass(); | 266 return result.Pass(); |
263 } | 267 } |
264 }; | 268 }; |
265 | 269 |
266 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of | 270 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of |
267 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 271 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
268 // element. If the input array is null, the output vector will be empty. | 272 // element. If the input array is null, the output vector will be empty. |
269 template <typename E, typename T> | 273 template <typename E, typename T> |
270 struct TypeConverter<std::vector<E>, Array<T>> { | 274 struct TypeConverter<std::vector<E>, Array<T>> { |
271 static std::vector<E> Convert(const Array<T>& input) { | 275 static std::vector<E> Convert(const Array<T>& input) { |
272 std::vector<E> result; | 276 std::vector<E> result; |
273 if (!input.is_null()) { | 277 if (!input.is_null()) { |
274 result.resize(input.size()); | 278 result.resize(input.size()); |
275 for (size_t i = 0; i < input.size(); ++i) | 279 for (size_t i = 0; i < input.size(); ++i) |
276 result[i] = TypeConverter<E, T>::Convert(input[i]); | 280 result[i] = TypeConverter<E, T>::Convert(input[i]); |
277 } | 281 } |
278 return result; | 282 return result; |
279 } | 283 } |
280 }; | 284 }; |
281 | 285 |
282 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 286 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
283 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each | 287 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each |
284 // element. The returned array will always be non-null. | 288 // element. The returned array will always be non-null. |
285 template <typename T, typename E> | 289 template <typename T, typename E> |
286 struct TypeConverter<Array<T>, std::set<E>> { | 290 struct TypeConverter<Array<T>, std::set<E>> { |
287 static Array<T> Convert(const std::set<E>& input) { | 291 static Array<T> Convert(const std::set<E>& input) { |
288 Array<T> result(0u); | 292 Array<T> result = Array<T>::New(0u); |
289 for (auto i : input) | 293 for (auto i : input) |
290 result.push_back(TypeConverter<T, E>::Convert(i)); | 294 result.push_back(TypeConverter<T, E>::Convert(i)); |
291 return result.Pass(); | 295 return result.Pass(); |
292 } | 296 } |
293 }; | 297 }; |
294 | 298 |
295 // A |TypeConverter| that will create an |std::set<E>| containing a copy of | 299 // A |TypeConverter| that will create an |std::set<E>| containing a copy of |
296 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 300 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
297 // element. If the input array is null, the output set will be empty. | 301 // element. If the input array is null, the output set will be empty. |
298 template <typename E, typename T> | 302 template <typename E, typename T> |
299 struct TypeConverter<std::set<E>, Array<T>> { | 303 struct TypeConverter<std::set<E>, Array<T>> { |
300 static std::set<E> Convert(const Array<T>& input) { | 304 static std::set<E> Convert(const Array<T>& input) { |
301 std::set<E> result; | 305 std::set<E> result; |
302 if (!input.is_null()) { | 306 if (!input.is_null()) { |
303 for (size_t i = 0; i < input.size(); ++i) | 307 for (size_t i = 0; i < input.size(); ++i) |
304 result.insert(TypeConverter<E, T>::Convert(input[i])); | 308 result.insert(TypeConverter<E, T>::Convert(input[i])); |
305 } | 309 } |
306 return result; | 310 return result; |
307 } | 311 } |
308 }; | 312 }; |
309 | 313 |
310 } // namespace mojo | 314 } // namespace mojo |
311 | 315 |
312 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 316 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
OLD | NEW |