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/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/lib/wtf_clone_equals_util.h" |
15 #include "mojo/public/cpp/bindings/type_converter.h" | 16 #include "mojo/public/cpp/bindings/type_converter.h" |
16 #include "third_party/WebKit/Source/wtf/Vector.h" | 17 #include "third_party/WebKit/Source/wtf/Vector.h" |
17 | 18 |
18 namespace mojo { | 19 namespace mojo { |
19 | 20 |
20 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, | 21 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, |
21 // mojo::WTFArray is move-only and can be null. | 22 // mojo::WTFArray is move-only and can be null. |
22 // It is easy to convert between WTF::Vector<T> and mojo::WTFArray<T>: | 23 // It is easy to convert between WTF::Vector<T> and mojo::WTFArray<T>: |
23 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a | 24 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a |
24 // WTF::Vector<T>; | 25 // WTF::Vector<T>; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // "cloned" from the corresponding value of this array. If the element type | 141 // "cloned" from the corresponding value of this array. If the element type |
141 // defines a Clone() method, it will be used; otherwise copy | 142 // defines a Clone() method, it will be used; otherwise copy |
142 // constructor/assignment will be used. | 143 // constructor/assignment will be used. |
143 // | 144 // |
144 // Please note that calling this method will fail compilation if the element | 145 // Please note that calling this method will fail compilation if the element |
145 // type cannot be cloned (which usually means that it is a Mojo handle type or | 146 // type cannot be cloned (which usually means that it is a Mojo handle type or |
146 // a type containing Mojo handles). | 147 // a type containing Mojo handles). |
147 WTFArray Clone() const { | 148 WTFArray Clone() const { |
148 WTFArray result; | 149 WTFArray result; |
149 result.is_null_ = is_null_; | 150 result.is_null_ = is_null_; |
150 result.vec_.reserveCapacity(vec_.size()); | 151 result.vec_ = internal::Clone(vec_); |
151 for (const auto& element : vec_) | |
152 result.vec_.append(internal::Clone(element)); | |
153 return result; | 152 return result; |
154 } | 153 } |
155 | 154 |
156 // Indicates whether the contents of this array are equal to |other|. A null | 155 // Indicates whether the contents of this array are equal to |other|. A null |
157 // array is only equal to another null array. If the element type defines an | 156 // array is only equal to another null array. If the element type defines an |
158 // Equals() method, it will be used; otherwise == operator will be used. | 157 // Equals() method, it will be used; otherwise == operator will be used. |
159 bool Equals(const WTFArray& other) const { | 158 bool Equals(const WTFArray& other) const { |
160 if (is_null() != other.is_null()) | 159 if (is_null() != other.is_null()) |
161 return false; | 160 return false; |
162 if (size() != other.size()) | 161 return internal::Equals(vec_, other.vec_); |
163 return false; | |
164 for (size_t i = 0; i < size(); ++i) { | |
165 if (!internal::Equals(at(i), other.at(i))) | |
166 return false; | |
167 } | |
168 return true; | |
169 } | 162 } |
170 | 163 |
171 private: | 164 private: |
172 // TODO(dcheng): Use an explicit conversion operator. | 165 // TODO(dcheng): Use an explicit conversion operator. |
173 typedef WTF::Vector<T> WTFArray::*Testable; | 166 typedef WTF::Vector<T> WTFArray::*Testable; |
174 | 167 |
175 public: | 168 public: |
176 operator Testable() const { | 169 operator Testable() const { |
177 // When the array is set to null, the underlying storage |vec_| shouldn't | 170 // When the array is set to null, the underlying storage |vec_| shouldn't |
178 // contain any elements. | 171 // contain any elements. |
(...skipping 16 matching lines...) Expand all Loading... |
195 | 188 |
196 WTF::Vector<T> vec_; | 189 WTF::Vector<T> vec_; |
197 bool is_null_; | 190 bool is_null_; |
198 | 191 |
199 DISALLOW_COPY_AND_ASSIGN(WTFArray); | 192 DISALLOW_COPY_AND_ASSIGN(WTFArray); |
200 }; | 193 }; |
201 | 194 |
202 } // namespace mojo | 195 } // namespace mojo |
203 | 196 |
204 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ | 197 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
OLD | NEW |