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/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/lib/value_traits.h" | |
16 #include "mojo/public/cpp/bindings/type_converter.h" | 15 #include "mojo/public/cpp/bindings/type_converter.h" |
17 #include "third_party/WebKit/Source/wtf/Vector.h" | 16 #include "third_party/WebKit/Source/wtf/Vector.h" |
18 | 17 |
19 namespace mojo { | 18 namespace mojo { |
20 | 19 |
21 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, | 20 // Represents an array backed by WTF::Vector. Comparing with WTF::Vector, |
22 // mojo::WTFArray is move-only and can be null. | 21 // mojo::WTFArray is move-only and can be null. |
23 // 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>: |
24 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a | 23 // - constructor WTFArray(WTF::Vector<T>&&) takes the contents of a |
25 // WTF::Vector<T>; | 24 // WTF::Vector<T>; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 136 |
138 // Swaps the contents of this array with the specified vector, making this | 137 // Swaps the contents of this array with the specified vector, making this |
139 // array non-null. Since the vector cannot represent null, it will just be | 138 // array non-null. Since the vector cannot represent null, it will just be |
140 // made empty if this array is null. | 139 // made empty if this array is null. |
141 void Swap(WTF::Vector<T>* other) { | 140 void Swap(WTF::Vector<T>* other) { |
142 is_null_ = false; | 141 is_null_ = false; |
143 vec_.swap(*other); | 142 vec_.swap(*other); |
144 } | 143 } |
145 | 144 |
146 // Returns a copy of the array where each value of the new array has been | 145 // Returns a copy of the array where each value of the new array has been |
147 // "cloned" from the corresponding value of this array. If this array contains | 146 // "cloned" from the corresponding value of this array. If the element type |
148 // primitive data types, this is equivalent to simply copying the contents. | 147 // defines a Clone() method, it will be used; otherwise copy |
149 // However, if the array contains objects, then each new element is created by | 148 // constructor/assignment will be used. |
150 // calling the |Clone| method of the source element, which should make a copy | |
151 // of the element. | |
152 // | 149 // |
153 // Please note that calling this method will fail compilation if the element | 150 // Please note that calling this method will fail compilation if the element |
154 // type cannot be cloned (which usually means that it is a Mojo handle type or | 151 // type cannot be cloned (which usually means that it is a Mojo handle type or |
155 // a type contains Mojo handles). | 152 // a type containing Mojo handles). |
156 WTFArray Clone() const { | 153 WTFArray Clone() const { |
157 WTFArray result; | 154 WTFArray result; |
158 result.is_null_ = is_null_; | 155 result.is_null_ = is_null_; |
159 CloneTraits<T>::Clone(vec_, &result.vec_); | 156 result.vec_.reserveCapacity(vec_.size()); |
160 return std::move(result); | 157 for (const auto& element : vec_) |
| 158 result.vec_.append(internal::Clone(element)); |
| 159 return result; |
161 } | 160 } |
162 | 161 |
163 // Indicates whether the contents of this array are equal to |other|. A null | 162 // Indicates whether the contents of this array are equal to |other|. A null |
164 // array is only equal to another null array. Elements are compared using the | 163 // array is only equal to another null array. If the element type defines an |
165 // |ValueTraits::Equals| method, which in most cases calls the |Equals| method | 164 // Equals() method, it will be used; otherwise == operator will be used. |
166 // of the element. | |
167 bool Equals(const WTFArray& other) const { | 165 bool Equals(const WTFArray& other) const { |
168 if (is_null() != other.is_null()) | 166 if (is_null() != other.is_null()) |
169 return false; | 167 return false; |
170 if (size() != other.size()) | 168 if (size() != other.size()) |
171 return false; | 169 return false; |
172 for (size_t i = 0; i < size(); ++i) { | 170 for (size_t i = 0; i < size(); ++i) { |
173 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) | 171 if (!internal::Equals(at(i), other.at(i))) |
174 return false; | 172 return false; |
175 } | 173 } |
176 return true; | 174 return true; |
177 } | 175 } |
178 | 176 |
179 private: | 177 private: |
180 typedef WTF::Vector<T> WTFArray::*Testable; | 178 typedef WTF::Vector<T> WTFArray::*Testable; |
181 | 179 |
182 public: | 180 public: |
183 operator Testable() const { | 181 operator Testable() const { |
184 // 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 |
185 // contain any elements. | 183 // contain any elements. |
186 DCHECK(!is_null_ || vec_.isEmpty()); | 184 DCHECK(!is_null_ || vec_.isEmpty()); |
187 return is_null_ ? 0 : &WTFArray::vec_; | 185 return is_null_ ? 0 : &WTFArray::vec_; |
188 } | 186 } |
189 | 187 |
190 private: | 188 private: |
191 // Forbid the == and != operators explicitly, otherwise WTFArray will be | 189 // Forbid the == and != operators explicitly, otherwise WTFArray will be |
192 // converted to Testable to do == or != comparison. | 190 // converted to Testable to do == or != comparison. |
193 template <typename U> | 191 template <typename U> |
194 bool operator==(const WTFArray<U>& other) const = delete; | 192 bool operator==(const WTFArray<U>& other) const = delete; |
195 template <typename U> | 193 template <typename U> |
196 bool operator!=(const WTFArray<U>& other) const = delete; | 194 bool operator!=(const WTFArray<U>& other) const = delete; |
197 | 195 |
198 template <typename U, | |
199 bool is_move_only_type = internal::IsMoveOnlyType<U>::value> | |
200 struct CloneTraits {}; | |
201 | |
202 template <typename U> | |
203 struct CloneTraits<U, false> { | |
204 static inline void Clone(const WTF::Vector<T>& src_vec, | |
205 WTF::Vector<T>* dest_vec) { | |
206 DCHECK(dest_vec->isEmpty()); | |
207 dest_vec->reserveCapacity(src_vec.size()); | |
208 for (const auto& element : src_vec) | |
209 dest_vec->append(element); | |
210 } | |
211 }; | |
212 | |
213 template <typename U> | |
214 struct CloneTraits<U, true> { | |
215 static inline void Clone(const WTF::Vector<T>& src_vec, | |
216 WTF::Vector<T>* dest_vec) { | |
217 DCHECK(dest_vec->isEmpty()); | |
218 dest_vec->reserveCapacity(src_vec.size()); | |
219 for (const auto& element : src_vec) | |
220 dest_vec->append(element.Clone()); | |
221 } | |
222 }; | |
223 | |
224 void Take(WTFArray* other) { | 196 void Take(WTFArray* other) { |
225 operator=(nullptr); | 197 operator=(nullptr); |
226 Swap(other); | 198 Swap(other); |
227 } | 199 } |
228 | 200 |
229 WTF::Vector<T> vec_; | 201 WTF::Vector<T> vec_; |
230 bool is_null_; | 202 bool is_null_; |
231 }; | 203 }; |
232 | 204 |
233 } // namespace mojo | 205 } // namespace mojo |
234 | 206 |
235 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ | 207 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_ARRAY_H_ |
OLD | NEW |