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

Side by Side Diff: mojo/public/cpp/bindings/lib/array_internal.h

Issue 311273003: Mojo: Add resize and push_back to Array. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ForwardType Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « mojo/public/cpp/bindings/array.h ('k') | mojo/public/cpp/bindings/tests/array_unittest.cc » ('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_LIB_ARRAY_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_
7 7
8 #include <new> 8 #include <new>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 283
284 // UTF-8 encoded 284 // UTF-8 encoded
285 typedef Array_Data<char> String_Data; 285 typedef Array_Data<char> String_Data;
286 286
287 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {}; 287 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {};
288 288
289 template <typename T> struct ArrayTraits<T, false> { 289 template <typename T> struct ArrayTraits<T, false> {
290 typedef T StorageType; 290 typedef T StorageType;
291 typedef typename std::vector<T>::reference RefType; 291 typedef typename std::vector<T>::reference RefType;
292 typedef typename std::vector<T>::const_reference ConstRefType; 292 typedef typename std::vector<T>::const_reference ConstRefType;
293 typedef ConstRefType ForwardType;
293 static inline void Initialize(std::vector<T>* vec) { 294 static inline void Initialize(std::vector<T>* vec) {
294 } 295 }
295 static inline void Finalize(std::vector<T>* vec) { 296 static inline void Finalize(std::vector<T>* vec) {
296 } 297 }
297 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) { 298 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) {
298 return vec->at(offset); 299 return vec->at(offset);
299 } 300 }
300 static inline RefType at(std::vector<T>* vec, size_t offset) { 301 static inline RefType at(std::vector<T>* vec, size_t offset) {
301 return vec->at(offset); 302 return vec->at(offset);
302 } 303 }
304 static inline void Resize(std::vector<T>* vec, size_t size) {
305 vec->resize(size);
306 }
307 static inline void PushBack(std::vector<T>* vec, ForwardType value) {
308 vec->push_back(value);
309 }
303 }; 310 };
304 311
305 template <typename T> struct ArrayTraits<T, true> { 312 template <typename T> struct ArrayTraits<T, true> {
306 struct StorageType { 313 struct StorageType {
307 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned. 314 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned.
308 }; 315 };
309 typedef T& RefType; 316 typedef T& RefType;
310 typedef const T& ConstRefType; 317 typedef const T& ConstRefType;
318 typedef T ForwardType;
311 static inline void Initialize(std::vector<StorageType>* vec) { 319 static inline void Initialize(std::vector<StorageType>* vec) {
312 for (size_t i = 0; i < vec->size(); ++i) 320 for (size_t i = 0; i < vec->size(); ++i)
313 new (vec->at(i).buf) T(); 321 new (vec->at(i).buf) T();
314 } 322 }
315 static inline void Finalize(std::vector<StorageType>* vec) { 323 static inline void Finalize(std::vector<StorageType>* vec) {
316 for (size_t i = 0; i < vec->size(); ++i) 324 for (size_t i = 0; i < vec->size(); ++i)
317 reinterpret_cast<T*>(vec->at(i).buf)->~T(); 325 reinterpret_cast<T*>(vec->at(i).buf)->~T();
318 } 326 }
319 static inline ConstRefType at(const std::vector<StorageType>* vec, 327 static inline ConstRefType at(const std::vector<StorageType>* vec,
320 size_t offset) { 328 size_t offset) {
321 return *reinterpret_cast<const T*>(vec->at(offset).buf); 329 return *reinterpret_cast<const T*>(vec->at(offset).buf);
322 } 330 }
323 static inline RefType at(std::vector<StorageType>* vec, size_t offset) { 331 static inline RefType at(std::vector<StorageType>* vec, size_t offset) {
324 return *reinterpret_cast<T*>(vec->at(offset).buf); 332 return *reinterpret_cast<T*>(vec->at(offset).buf);
325 } 333 }
334 static inline void Resize(std::vector<StorageType>* vec, size_t size) {
335 size_t old_size = vec->size();
336 for (size_t i = size; i < old_size; i++)
337 reinterpret_cast<T*>(vec->at(i).buf)->~T();
338 ResizeStorage(vec, size);
339 for (size_t i = old_size; i < vec->size(); i++)
340 new (vec->at(i).buf) T();
341 }
342 static inline void PushBack(std::vector<StorageType>* vec, RefType value) {
343 size_t old_size = vec->size();
344 ResizeStorage(vec, old_size + 1);
345 new (vec->at(old_size).buf) T(value.Pass());
346 }
347 static inline void ResizeStorage(std::vector<StorageType>* vec, size_t size) {
348 if (size <= vec->capacity()) {
349 vec->resize(size);
350 return;
351 }
352 std::vector<StorageType> new_storage(size);
353 for (size_t i = 0; i < vec->size(); i++)
354 new (new_storage.at(i).buf) T(at(vec, i).Pass());
355 vec->swap(new_storage);
356 Finalize(&new_storage);
357 }
326 }; 358 };
327 359
328 template <> struct WrapperTraits<String, false> { 360 template <> struct WrapperTraits<String, false> {
329 typedef String_Data* DataType; 361 typedef String_Data* DataType;
330 }; 362 };
331 363
332 } // namespace internal 364 } // namespace internal
333 } // namespace mojo 365 } // namespace mojo
334 366
335 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ 367 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/array.h ('k') | mojo/public/cpp/bindings/tests/array_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698