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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/array_internal.h
diff --git a/mojo/public/cpp/bindings/lib/array_internal.h b/mojo/public/cpp/bindings/lib/array_internal.h
index 99991c5c862b3035168496acc28d1c24a522e91e..b0a8908e77d19c7ee10883cd7c25dd7e993a58cf 100644
--- a/mojo/public/cpp/bindings/lib/array_internal.h
+++ b/mojo/public/cpp/bindings/lib/array_internal.h
@@ -290,6 +290,7 @@ template <typename T> struct ArrayTraits<T, false> {
typedef T StorageType;
typedef typename std::vector<T>::reference RefType;
typedef typename std::vector<T>::const_reference ConstRefType;
+ typedef ConstRefType ForwardType;
static inline void Initialize(std::vector<T>* vec) {
}
static inline void Finalize(std::vector<T>* vec) {
@@ -300,6 +301,12 @@ template <typename T> struct ArrayTraits<T, false> {
static inline RefType at(std::vector<T>* vec, size_t offset) {
return vec->at(offset);
}
+ static inline void Resize(std::vector<T>* vec, size_t size) {
+ vec->resize(size);
+ }
+ static inline void PushBack(std::vector<T>* vec, ForwardType value) {
+ vec->push_back(value);
+ }
};
template <typename T> struct ArrayTraits<T, true> {
@@ -308,6 +315,7 @@ template <typename T> struct ArrayTraits<T, true> {
};
typedef T& RefType;
typedef const T& ConstRefType;
+ typedef T ForwardType;
static inline void Initialize(std::vector<StorageType>* vec) {
for (size_t i = 0; i < vec->size(); ++i)
new (vec->at(i).buf) T();
@@ -323,6 +331,30 @@ template <typename T> struct ArrayTraits<T, true> {
static inline RefType at(std::vector<StorageType>* vec, size_t offset) {
return *reinterpret_cast<T*>(vec->at(offset).buf);
}
+ static inline void Resize(std::vector<StorageType>* vec, size_t size) {
+ size_t old_size = vec->size();
+ for (size_t i = size; i < old_size; i++)
+ reinterpret_cast<T*>(vec->at(i).buf)->~T();
+ ResizeStorage(vec, size);
+ for (size_t i = old_size; i < vec->size(); i++)
+ new (vec->at(i).buf) T();
+ }
+ static inline void PushBack(std::vector<StorageType>* vec, RefType value) {
+ size_t old_size = vec->size();
+ ResizeStorage(vec, old_size + 1);
+ new (vec->at(old_size).buf) T(value.Pass());
+ }
+ static inline void ResizeStorage(std::vector<StorageType>* vec, size_t size) {
+ if (size <= vec->capacity()) {
+ vec->resize(size);
+ return;
+ }
+ std::vector<StorageType> new_storage(size);
+ for (size_t i = 0; i < vec->size(); i++)
+ new (new_storage.at(i).buf) T(at(vec, i).Pass());
+ vec->swap(new_storage);
+ Finalize(&new_storage);
+ }
};
template <> struct WrapperTraits<String, false> {
« 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