| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | |
| 11 #include <algorithm> | 10 #include <algorithm> |
| 12 #include <set> | 11 #include <set> |
| 13 #include <string> | 12 #include <string> |
| 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 16 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 18 #include "mojo/public/cpp/bindings/lib/template_util.h" | 18 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 19 #include "mojo/public/cpp/bindings/lib/value_traits.h" | 19 #include "mojo/public/cpp/bindings/lib/value_traits.h" |
| 20 #include "mojo/public/cpp/bindings/type_converter.h" | 20 #include "mojo/public/cpp/bindings/type_converter.h" |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 | 23 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 50 // Moves the contents of |other| into this array. | 50 // Moves the contents of |other| into this array. |
| 51 Array(Array&& other) : is_null_(true) { Take(&other); } | 51 Array(Array&& other) : is_null_(true) { Take(&other); } |
| 52 Array& operator=(Array&& other) { | 52 Array& operator=(Array&& other) { |
| 53 Take(&other); | 53 Take(&other); |
| 54 return *this; | 54 return *this; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Creates a non-null array of the specified size. The elements will be | 57 // Creates a non-null array of the specified size. The elements will be |
| 58 // value-initialized (meaning that they will be initialized by their default | 58 // value-initialized (meaning that they will be initialized by their default |
| 59 // constructor, if any, or else zero-initialized). | 59 // constructor, if any, or else zero-initialized). |
| 60 static Array New(size_t size) { return Array(size).Pass(); } | 60 static Array New(size_t size) { return std::move(Array(size)); } |
| 61 | 61 |
| 62 // Creates a new array with a copy of the contents of |other|. | 62 // Creates a new array with a copy of the contents of |other|. |
| 63 template <typename U> | 63 template <typename U> |
| 64 static Array From(const U& other) { | 64 static Array From(const U& other) { |
| 65 return TypeConverter<Array, U>::Convert(other); | 65 return TypeConverter<Array, U>::Convert(other); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Copies the contents of this array to a new object of type |U|. | 68 // Copies the contents of this array to a new object of type |U|. |
| 69 template <typename U> | 69 template <typename U> |
| 70 U To() const { | 70 U To() const { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // calling the |Clone| method of the source element, which should make a copy | 139 // calling the |Clone| method of the source element, which should make a copy |
| 140 // of the element. | 140 // of the element. |
| 141 // | 141 // |
| 142 // Please note that calling this method will fail compilation if the element | 142 // Please note that calling this method will fail compilation if the element |
| 143 // type cannot be cloned (which usually means that it is a Mojo handle type or | 143 // type cannot be cloned (which usually means that it is a Mojo handle type or |
| 144 // a type contains Mojo handles). | 144 // a type contains Mojo handles). |
| 145 Array Clone() const { | 145 Array Clone() const { |
| 146 Array result; | 146 Array result; |
| 147 result.is_null_ = is_null_; | 147 result.is_null_ = is_null_; |
| 148 Traits::Clone(vec_, &result.vec_); | 148 Traits::Clone(vec_, &result.vec_); |
| 149 return result.Pass(); | 149 return std::move(result); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Indicates whether the contents of this array are equal to |other|. A null | 152 // Indicates whether the contents of this array are equal to |other|. A null |
| 153 // array is only equal to another null array. Elements are compared using the | 153 // array is only equal to another null array. Elements are compared using the |
| 154 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method | 154 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method |
| 155 // of the element. | 155 // of the element. |
| 156 bool Equals(const Array& other) const { | 156 bool Equals(const Array& other) const { |
| 157 if (is_null() != other.is_null()) | 157 if (is_null() != other.is_null()) |
| 158 return false; | 158 return false; |
| 159 if (size() != other.size()) | 159 if (size() != other.size()) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 190 | 190 |
| 191 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 191 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
| 192 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each | 192 // contents of an |std::vector<E>|, using |TypeConverter<T, E>| to copy each |
| 193 // element. The returned array will always be non-null. | 193 // element. The returned array will always be non-null. |
| 194 template <typename T, typename E> | 194 template <typename T, typename E> |
| 195 struct TypeConverter<Array<T>, std::vector<E>> { | 195 struct TypeConverter<Array<T>, std::vector<E>> { |
| 196 static Array<T> Convert(const std::vector<E>& input) { | 196 static Array<T> Convert(const std::vector<E>& input) { |
| 197 Array<T> result(input.size()); | 197 Array<T> result(input.size()); |
| 198 for (size_t i = 0; i < input.size(); ++i) | 198 for (size_t i = 0; i < input.size(); ++i) |
| 199 result[i] = TypeConverter<T, E>::Convert(input[i]); | 199 result[i] = TypeConverter<T, E>::Convert(input[i]); |
| 200 return result.Pass(); | 200 return std::move(result); |
| 201 } | 201 } |
| 202 }; | 202 }; |
| 203 | 203 |
| 204 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of | 204 // A |TypeConverter| that will create an |std::vector<E>| containing a copy of |
| 205 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 205 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
| 206 // element. If the input array is null, the output vector will be empty. | 206 // element. If the input array is null, the output vector will be empty. |
| 207 template <typename E, typename T> | 207 template <typename E, typename T> |
| 208 struct TypeConverter<std::vector<E>, Array<T>> { | 208 struct TypeConverter<std::vector<E>, Array<T>> { |
| 209 static std::vector<E> Convert(const Array<T>& input) { | 209 static std::vector<E> Convert(const Array<T>& input) { |
| 210 std::vector<E> result; | 210 std::vector<E> result; |
| 211 if (!input.is_null()) { | 211 if (!input.is_null()) { |
| 212 result.resize(input.size()); | 212 result.resize(input.size()); |
| 213 for (size_t i = 0; i < input.size(); ++i) | 213 for (size_t i = 0; i < input.size(); ++i) |
| 214 result[i] = TypeConverter<E, T>::Convert(input[i]); | 214 result[i] = TypeConverter<E, T>::Convert(input[i]); |
| 215 } | 215 } |
| 216 return result; | 216 return result; |
| 217 } | 217 } |
| 218 }; | 218 }; |
| 219 | 219 |
| 220 // A |TypeConverter| that will create an |Array<T>| containing a copy of the | 220 // A |TypeConverter| that will create an |Array<T>| containing a copy of the |
| 221 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each | 221 // contents of an |std::set<E>|, using |TypeConverter<T, E>| to copy each |
| 222 // element. The returned array will always be non-null. | 222 // element. The returned array will always be non-null. |
| 223 template <typename T, typename E> | 223 template <typename T, typename E> |
| 224 struct TypeConverter<Array<T>, std::set<E>> { | 224 struct TypeConverter<Array<T>, std::set<E>> { |
| 225 static Array<T> Convert(const std::set<E>& input) { | 225 static Array<T> Convert(const std::set<E>& input) { |
| 226 Array<T> result(0u); | 226 Array<T> result(0u); |
| 227 for (auto i : input) | 227 for (auto i : input) |
| 228 result.push_back(TypeConverter<T, E>::Convert(i)); | 228 result.push_back(TypeConverter<T, E>::Convert(i)); |
| 229 return result.Pass(); | 229 return std::move(result); |
| 230 } | 230 } |
| 231 }; | 231 }; |
| 232 | 232 |
| 233 // A |TypeConverter| that will create an |std::set<E>| containing a copy of | 233 // A |TypeConverter| that will create an |std::set<E>| containing a copy of |
| 234 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each | 234 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each |
| 235 // element. If the input array is null, the output set will be empty. | 235 // element. If the input array is null, the output set will be empty. |
| 236 template <typename E, typename T> | 236 template <typename E, typename T> |
| 237 struct TypeConverter<std::set<E>, Array<T>> { | 237 struct TypeConverter<std::set<E>, Array<T>> { |
| 238 static std::set<E> Convert(const Array<T>& input) { | 238 static std::set<E> Convert(const Array<T>& input) { |
| 239 std::set<E> result; | 239 std::set<E> result; |
| 240 if (!input.is_null()) { | 240 if (!input.is_null()) { |
| 241 for (size_t i = 0; i < input.size(); ++i) | 241 for (size_t i = 0; i < input.size(); ++i) |
| 242 result.insert(TypeConverter<E, T>::Convert(input[i])); | 242 result.insert(TypeConverter<E, T>::Convert(input[i])); |
| 243 } | 243 } |
| 244 return result; | 244 return result; |
| 245 } | 245 } |
| 246 }; | 246 }; |
| 247 | 247 |
| 248 } // namespace mojo | 248 } // namespace mojo |
| 249 | 249 |
| 250 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 250 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
| OLD | NEW |