Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: mojo/public/cpp/bindings/array.h

Issue 2136733002: Mojo C++ bindings: add a new mode to generator to use native STL/WTF types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@67_new
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/array_traits_stl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 8 #include <stddef.h>
9 #include <string.h> 9 #include <string.h>
10 #include <algorithm> 10 #include <algorithm>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "mojo/public/cpp/bindings/lib/array_internal.h" 17 #include "mojo/public/cpp/bindings/lib/array_internal.h"
18 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 18 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
19 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h"
19 #include "mojo/public/cpp/bindings/lib/template_util.h" 20 #include "mojo/public/cpp/bindings/lib/template_util.h"
20 #include "mojo/public/cpp/bindings/type_converter.h" 21 #include "mojo/public/cpp/bindings/type_converter.h"
21 22
22 namespace mojo { 23 namespace mojo {
23 24
24 // Represents a moveable array with contents of type |T|. The array can be null, 25 // Represents a moveable array with contents of type |T|. The array can be null,
25 // meaning that no value has been assigned to it. Null is distinct from empty. 26 // meaning that no value has been assigned to it. Null is distinct from empty.
26 template <typename T> 27 template <typename T>
27 class Array { 28 class Array {
28 public: 29 public:
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 131 }
131 132
132 // Sets the array to empty (even if previously it was null.) 133 // Sets the array to empty (even if previously it was null.)
133 void SetToEmpty() { resize(0); } 134 void SetToEmpty() { resize(0); }
134 135
135 // Returns a const reference to the |std::vector| managed by this class. If 136 // Returns a const reference to the |std::vector| managed by this class. If
136 // the array is null, this will be an empty vector. 137 // the array is null, this will be an empty vector.
137 const std::vector<T>& storage() const { return vec_; } 138 const std::vector<T>& storage() const { return vec_; }
138 139
139 // Passes the underlying storage and resets this array to null. 140 // Passes the underlying storage and resets this array to null.
140 //
141 // TODO(yzshen): Consider changing this to a rvalue-ref-qualified conversion
142 // to std::vector<T> after we move to MSVC 2015.
143 std::vector<T> PassStorage() { 141 std::vector<T> PassStorage() {
144 is_null_ = true; 142 is_null_ = true;
145 return std::move(vec_); 143 return std::move(vec_);
146 } 144 }
147 145
146 operator const std::vector<T>&() const { return vec_; }
147
148 void Swap(Array* other) { 148 void Swap(Array* other) {
149 std::swap(is_null_, other->is_null_); 149 std::swap(is_null_, other->is_null_);
150 vec_.swap(other->vec_); 150 vec_.swap(other->vec_);
151 } 151 }
152 152
153 // Swaps the contents of this array with the specified vector, making this 153 // Swaps the contents of this array with the specified vector, making this
154 // array non-null. Since the vector cannot represent null, it will just be 154 // array non-null. Since the vector cannot represent null, it will just be
155 // made empty if this array is null. 155 // made empty if this array is null.
156 void Swap(std::vector<T>* other) { 156 void Swap(std::vector<T>* other) {
157 is_null_ = false; 157 is_null_ = false;
158 vec_.swap(*other); 158 vec_.swap(*other);
159 } 159 }
160 160
161 // Returns a copy of the array where each value of the new array has been 161 // Returns a copy of the array where each value of the new array has been
162 // "cloned" from the corresponding value of this array. If the element type 162 // "cloned" from the corresponding value of this array. If the element type
163 // defines a Clone() method, it will be used; otherwise copy 163 // defines a Clone() method, it will be used; otherwise copy
164 // constructor/assignment will be used. 164 // constructor/assignment will be used.
165 // 165 //
166 // Please note that calling this method will fail compilation if the element 166 // Please note that calling this method will fail compilation if the element
167 // type cannot be cloned (which usually means that it is a Mojo handle type or 167 // type cannot be cloned (which usually means that it is a Mojo handle type or
168 // a type containing Mojo handles). 168 // a type containing Mojo handles).
169 Array Clone() const { 169 Array Clone() const {
170 Array result; 170 Array result;
171 result.is_null_ = is_null_; 171 result.is_null_ = is_null_;
172 result.vec_.reserve(vec_.size()); 172 result.vec_ = internal::Clone(vec_);
173 for (const auto& element : vec_) 173 return result;
174 result.vec_.push_back(internal::Clone(element));
175 return std::move(result);
176 } 174 }
177 175
178 // Indicates whether the contents of this array are equal to |other|. A null 176 // Indicates whether the contents of this array are equal to |other|. A null
179 // array is only equal to another null array. If the element type defines an 177 // array is only equal to another null array. If the element type defines an
180 // Equals() method, it will be used; otherwise == operator will be used. 178 // Equals() method, it will be used; otherwise == operator will be used.
181 bool Equals(const Array& other) const { 179 bool Equals(const Array& other) const {
182 if (is_null() != other.is_null()) 180 if (is_null() != other.is_null())
183 return false; 181 return false;
184 if (size() != other.size()) 182 return internal::Equals(vec_, other.vec_);
185 return false;
186 for (size_t i = 0; i < size(); ++i) {
187 if (!internal::Equals(at(i), other.at(i)))
188 return false;
189 }
190 return true;
191 } 183 }
192 184
193 private: 185 private:
194 typedef std::vector<T> Array::*Testable; 186 typedef std::vector<T> Array::*Testable;
195 187
196 public: 188 public:
197 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } 189 operator Testable() const { return is_null_ ? 0 : &Array::vec_; }
198 190
199 private: 191 private:
200 // Forbid the == and != operators explicitly, otherwise Array will be 192 // Forbid the == and != operators explicitly, otherwise Array will be
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 if (a.is_null()) 270 if (a.is_null())
279 return !b.is_null(); 271 return !b.is_null();
280 if (b.is_null()) 272 if (b.is_null())
281 return false; 273 return false;
282 return a.storage() < b.storage(); 274 return a.storage() < b.storage();
283 } 275 }
284 276
285 } // namespace mojo 277 } // namespace mojo
286 278
287 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ 279 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/array_traits_stl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698