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 28a0a0d8d6accd24b7a4baf4c3d442260c82f373..99ccc89cd4cf1d07ce37b19ab425a386843e3312 100644 |
--- a/mojo/public/cpp/bindings/lib/array_internal.h |
+++ b/mojo/public/cpp/bindings/lib/array_internal.h |
@@ -280,6 +280,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 SinkType; |
static inline void Initialize(std::vector<T>* vec) { |
} |
static inline void Finalize(std::vector<T>* vec) { |
@@ -290,6 +291,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, SinkType value) { |
darin (slow to review)
2014/06/10 22:33:13
I don't quite understand the use of the term "Sink
Sam McNally
2014/06/10 23:55:17
Done.
|
+ vec->push_back(value); |
+ } |
}; |
template <typename T> struct ArrayTraits<T, true> { |
@@ -298,6 +305,7 @@ template <typename T> struct ArrayTraits<T, true> { |
}; |
typedef T& RefType; |
typedef const T& ConstRefType; |
+ typedef T SinkType; |
static inline void Initialize(std::vector<StorageType>* vec) { |
for (size_t i = 0; i < vec->size(); ++i) |
new (vec->at(i).buf) T(); |
@@ -313,6 +321,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> { |