| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_WTF_ARRAY_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/move.h" |
| 12 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 12 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 14 #include "mojo/public/cpp/bindings/lib/template_util.h" | 14 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 15 #include "mojo/public/cpp/bindings/type_converter.h" | 15 #include "mojo/public/cpp/bindings/type_converter.h" |
| 16 #include "third_party/WebKit/Source/wtf/Vector.h" | 16 #include "third_party/WebKit/Source/wtf/Vector.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 | 19 |
| 20 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, | 20 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, |
| 21 // mojo::WTFArray is move-only and can be null. | 21 // mojo::WTFArray is move-only and can be null. |
| 22 // It is easy to convert between WTF::Vector<T> and mojo::WTFArray<T>: | 22 // It is easy to convert between WTF::Vector<T> and mojo::WTFArray<T>: |
| 23 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a | 23 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a |
| 24 // WTF::Vector<T>; | 24 // WTF::Vector<T>; |
| 25 // - method PassStorage() passes the underlying WTF::Vector. | 25 // - method PassStorage() passes the underlying WTF::Vector. |
| 26 template <typename T> | 26 template <typename T> |
| 27 class WTFArray { | 27 class WTFArray { |
| 28 MOVE_ONLY_TYPE_FOR_CPP_03(WTFArray); |
| 29 |
| 28 public: | 30 public: |
| 29 using Data_ = internal::Array_Data< | 31 using Data_ = internal::Array_Data< |
| 30 typename internal::GetDataTypeAsArrayElement<T>::Data>; | 32 typename internal::GetDataTypeAsArrayElement<T>::Data>; |
| 31 using Element = T; | 33 using Element = T; |
| 32 | 34 |
| 33 // Constructs an empty array. | 35 // Constructs an empty array. |
| 34 WTFArray() : is_null_(false) {} | 36 WTFArray() : is_null_(false) {} |
| 35 // Constructs a null array. | 37 // Constructs a null array. |
| 36 WTFArray(std::nullptr_t null_pointer) : is_null_(true) {} | 38 WTFArray(std::nullptr_t null_pointer) : is_null_(true) {} |
| 37 | 39 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 if (size() != other.size()) | 168 if (size() != other.size()) |
| 167 return false; | 169 return false; |
| 168 for (size_t i = 0; i < size(); ++i) { | 170 for (size_t i = 0; i < size(); ++i) { |
| 169 if (!internal::Equals(at(i), other.at(i))) | 171 if (!internal::Equals(at(i), other.at(i))) |
| 170 return false; | 172 return false; |
| 171 } | 173 } |
| 172 return true; | 174 return true; |
| 173 } | 175 } |
| 174 | 176 |
| 175 private: | 177 private: |
| 176 // TODO(dcheng): Use an explicit conversion operator. | |
| 177 typedef WTF::Vector<T> WTFArray::*Testable; | 178 typedef WTF::Vector<T> WTFArray::*Testable; |
| 178 | 179 |
| 179 public: | 180 public: |
| 180 operator Testable() const { | 181 operator Testable() const { |
| 181 // When the array is set to null, the underlying storage |vec_| shouldn't | 182 // When the array is set to null, the underlying storage |vec_| shouldn't |
| 182 // contain any elements. | 183 // contain any elements. |
| 183 DCHECK(!is_null_ || vec_.isEmpty()); | 184 DCHECK(!is_null_ || vec_.isEmpty()); |
| 184 return is_null_ ? 0 : &WTFArray::vec_; | 185 return is_null_ ? 0 : &WTFArray::vec_; |
| 185 } | 186 } |
| 186 | 187 |
| 187 private: | 188 private: |
| 188 // Forbid the == and != operators explicitly, otherwise WTFArray will be | 189 // Forbid the == and != operators explicitly, otherwise WTFArray will be |
| 189 // converted to Testable to do == or != comparison. | 190 // converted to Testable to do == or != comparison. |
| 190 template <typename U> | 191 template <typename U> |
| 191 bool operator==(const WTFArray<U>& other) const = delete; | 192 bool operator==(const WTFArray<U>& other) const = delete; |
| 192 template <typename U> | 193 template <typename U> |
| 193 bool operator!=(const WTFArray<U>& other) const = delete; | 194 bool operator!=(const WTFArray<U>& other) const = delete; |
| 194 | 195 |
| 195 void Take(WTFArray* other) { | 196 void Take(WTFArray* other) { |
| 196 operator=(nullptr); | 197 operator=(nullptr); |
| 197 Swap(other); | 198 Swap(other); |
| 198 } | 199 } |
| 199 | 200 |
| 200 WTF::Vector<T> vec_; | 201 WTF::Vector<T> vec_; |
| 201 bool is_null_; | 202 bool is_null_; |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(WTFArray); | |
| 204 }; | 203 }; |
| 205 | 204 |
| 206 } // namespace mojo | 205 } // namespace mojo |
| 207 | 206 |
| 208 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ | 207 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
| OLD | NEW |