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