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/move.h" | 11 #include "base/macros.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 | |
30 public: | 28 public: |
31 using Data_ = internal::Array_Data< | 29 using Data_ = internal::Array_Data< |
32 typename internal::GetDataTypeAsArrayElement<T>::Data>; | 30 typename internal::GetDataTypeAsArrayElement<T>::Data>; |
33 using Element = T; | 31 using Element = T; |
34 | 32 |
35 // Constructs an empty array. | 33 // Constructs an empty array. |
36 WTFArray() : is_null_(false) {} | 34 WTFArray() : is_null_(false) {} |
37 // Constructs a null array. | 35 // Constructs a null array. |
38 WTFArray(std::nullptr_t null_pointer) : is_null_(true) {} | 36 WTFArray(std::nullptr_t null_pointer) : is_null_(true) {} |
39 | 37 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 if (size() != other.size()) | 166 if (size() != other.size()) |
169 return false; | 167 return false; |
170 for (size_t i = 0; i < size(); ++i) { | 168 for (size_t i = 0; i < size(); ++i) { |
171 if (!internal::Equals(at(i), other.at(i))) | 169 if (!internal::Equals(at(i), other.at(i))) |
172 return false; | 170 return false; |
173 } | 171 } |
174 return true; | 172 return true; |
175 } | 173 } |
176 | 174 |
177 private: | 175 private: |
| 176 // TODO(dcheng): Use an explicit conversion operator. |
178 typedef WTF::Vector<T> WTFArray::*Testable; | 177 typedef WTF::Vector<T> WTFArray::*Testable; |
179 | 178 |
180 public: | 179 public: |
181 operator Testable() const { | 180 operator Testable() const { |
182 // When the array is set to null, the underlying storage |vec_| shouldn't | 181 // When the array is set to null, the underlying storage |vec_| shouldn't |
183 // contain any elements. | 182 // contain any elements. |
184 DCHECK(!is_null_ || vec_.isEmpty()); | 183 DCHECK(!is_null_ || vec_.isEmpty()); |
185 return is_null_ ? 0 : &WTFArray::vec_; | 184 return is_null_ ? 0 : &WTFArray::vec_; |
186 } | 185 } |
187 | 186 |
188 private: | 187 private: |
189 // Forbid the == and != operators explicitly, otherwise WTFArray will be | 188 // Forbid the == and != operators explicitly, otherwise WTFArray will be |
190 // converted to Testable to do == or != comparison. | 189 // converted to Testable to do == or != comparison. |
191 template <typename U> | 190 template <typename U> |
192 bool operator==(const WTFArray<U>& other) const = delete; | 191 bool operator==(const WTFArray<U>& other) const = delete; |
193 template <typename U> | 192 template <typename U> |
194 bool operator!=(const WTFArray<U>& other) const = delete; | 193 bool operator!=(const WTFArray<U>& other) const = delete; |
195 | 194 |
196 void Take(WTFArray* other) { | 195 void Take(WTFArray* other) { |
197 operator=(nullptr); | 196 operator=(nullptr); |
198 Swap(other); | 197 Swap(other); |
199 } | 198 } |
200 | 199 |
201 WTF::Vector<T> vec_; | 200 WTF::Vector<T> vec_; |
202 bool is_null_; | 201 bool is_null_; |
| 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(WTFArray); |
203 }; | 204 }; |
204 | 205 |
205 } // namespace mojo | 206 } // namespace mojo |
206 | 207 |
207 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ | 208 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
OLD | NEW |