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

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

Issue 615063003: Mojo cpp bindings: support Clone() for structs and arrays which don't contain handles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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
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 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } 444 }
445 static inline RefType at(std::vector<T>* vec, size_t offset) { 445 static inline RefType at(std::vector<T>* vec, size_t offset) {
446 return vec->at(offset); 446 return vec->at(offset);
447 } 447 }
448 static inline void Resize(std::vector<T>* vec, size_t size) { 448 static inline void Resize(std::vector<T>* vec, size_t size) {
449 vec->resize(size); 449 vec->resize(size);
450 } 450 }
451 static inline void PushBack(std::vector<T>* vec, ForwardType value) { 451 static inline void PushBack(std::vector<T>* vec, ForwardType value) {
452 vec->push_back(value); 452 vec->push_back(value);
453 } 453 }
454 static inline void Clone(const std::vector<T>& src_vec,
455 std::vector<T>* dest_vec) {
456 dest_vec->assign(src_vec.begin(), src_vec.end());
457 }
454 }; 458 };
455 459
456 template <typename T> struct ArrayTraits<T, true> { 460 template <typename T> struct ArrayTraits<T, true> {
457 struct StorageType { 461 struct StorageType {
458 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned. 462 char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned.
459 }; 463 };
460 typedef T& RefType; 464 typedef T& RefType;
461 typedef const T& ConstRefType; 465 typedef const T& ConstRefType;
462 typedef T ForwardType; 466 typedef T ForwardType;
463 static inline void Initialize(std::vector<StorageType>* vec) { 467 static inline void Initialize(std::vector<StorageType>* vec) {
(...skipping 28 matching lines...) Expand all
492 if (size <= vec->capacity()) { 496 if (size <= vec->capacity()) {
493 vec->resize(size); 497 vec->resize(size);
494 return; 498 return;
495 } 499 }
496 std::vector<StorageType> new_storage(size); 500 std::vector<StorageType> new_storage(size);
497 for (size_t i = 0; i < vec->size(); i++) 501 for (size_t i = 0; i < vec->size(); i++)
498 new (new_storage.at(i).buf) T(at(vec, i).Pass()); 502 new (new_storage.at(i).buf) T(at(vec, i).Pass());
499 vec->swap(new_storage); 503 vec->swap(new_storage);
500 Finalize(&new_storage); 504 Finalize(&new_storage);
501 } 505 }
506 static inline void Clone(const std::vector<StorageType>& src_vec,
507 std::vector<StorageType>* dest_vec) {
508 Resize(dest_vec, src_vec.size());
509 for (size_t i = 0; i < src_vec.size(); ++i)
510 at(dest_vec, i) = at(&src_vec, i).Clone();
511 }
502 }; 512 };
503 513
504 template <> struct WrapperTraits<String, false> { 514 template <> struct WrapperTraits<String, false> {
505 typedef String_Data* DataType; 515 typedef String_Data* DataType;
506 }; 516 };
507 517
508 } // namespace internal 518 } // namespace internal
509 } // namespace mojo 519 } // namespace mojo
510 520
511 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ 521 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698