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

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

Issue 2380643003: Mojo C++ bindings: Add mojo::Array<T>::reserve() (Closed)
Patch Set: Created 4 years, 2 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 | « no previous file | no next file » | 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>
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // Resizes the array to |size| and makes it non-null. Otherwise, works just 126 // Resizes the array to |size| and makes it non-null. Otherwise, works just
127 // like the resize method of |std::vector|. 127 // like the resize method of |std::vector|.
128 void resize(size_t size) { 128 void resize(size_t size) {
129 is_null_ = false; 129 is_null_ = false;
130 vec_.resize(size); 130 vec_.resize(size);
131 } 131 }
132 132
133 // Sets the array to empty (even if previously it was null.) 133 // Sets the array to empty (even if previously it was null.)
134 void SetToEmpty() { resize(0); } 134 void SetToEmpty() { resize(0); }
135 135
136 // Ensures the underlying storage can store up to |size| elements without
137 // performing reallocations. This works like the reserve method of
138 // |std::vector|.
139 void reserve(size_t size) { vec_.reserve(size); }
140
136 // Returns a const reference to the |std::vector| managed by this class. If 141 // Returns a const reference to the |std::vector| managed by this class. If
137 // the array is null, this will be an empty vector. 142 // the array is null, this will be an empty vector.
138 const std::vector<T>& storage() const { return vec_; } 143 const std::vector<T>& storage() const { return vec_; }
139 144
140 // Passes the underlying storage and resets this array to null. 145 // Passes the underlying storage and resets this array to null.
141 std::vector<T> PassStorage() { 146 std::vector<T> PassStorage() {
142 is_null_ = true; 147 is_null_ = true;
143 return std::move(vec_); 148 return std::move(vec_);
144 } 149 }
145 150
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 if (a.is_null()) 275 if (a.is_null())
271 return !b.is_null(); 276 return !b.is_null();
272 if (b.is_null()) 277 if (b.is_null())
273 return false; 278 return false;
274 return a.storage() < b.storage(); 279 return a.storage() < b.storage();
275 } 280 }
276 281
277 } // namespace mojo 282 } // namespace mojo
278 283
279 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ 284 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698