Chromium Code Reviews| 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 865bf1cc8647e82a949ade03dac505887266c184..679642627d8af3b16ec47e23b206ad775e4b1664 100644 |
| --- a/mojo/public/cpp/bindings/lib/array_internal.h |
| +++ b/mojo/public/cpp/bindings/lib/array_internal.h |
| @@ -6,6 +6,7 @@ |
| #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
| #include <new> |
| +#include <vector> |
| #include "mojo/public/cpp/bindings/buffer.h" |
| #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| @@ -337,6 +338,64 @@ MOJO_COMPILE_ASSERT(sizeof(Array_Data<char>) == 8, bad_sizeof_Array_Data); |
| // UTF-8 encoded |
| typedef Array_Data<char> String_Data; |
| +// ---- |
| + |
| +template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {}; |
| + |
| +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; |
| + static inline void Initialize(std::vector<T>* vec) { |
| + } |
| + static inline void Finalize(std::vector<T>* vec) { |
| + } |
| + static inline ConstRefType at(const std::vector<T>* vec, size_t offset) { |
| + return vec->at(offset); |
| + } |
| + static inline RefType at(std::vector<T>* vec, size_t offset) { |
| + return vec->at(offset); |
| + } |
| +}; |
| + |
| +template <typename T> struct ArrayTraits<T, true> { |
| + struct StorageType { |
| + char buf[sizeof(T)]; |
| + }; |
| + typedef T& RefType; |
| + typedef const T& ConstRefType; |
| + static inline void Initialize(std::vector<StorageType>* vec) { |
| + for (size_t i = 0; i < vec->size(); ++i) |
| + new (vec->at(i).buf) T(); |
|
yzshen1
2014/05/22 17:13:53
Is there potential alignment issue here?
One alte
|
| + } |
| + static inline void Finalize(std::vector<StorageType>* vec) { |
| + for (size_t i = 0; i < vec->size(); ++i) |
| + reinterpret_cast<T*>(vec->at(i).buf)->~T(); |
| + } |
| + static inline ConstRefType at(const std::vector<StorageType>* vec, |
| + size_t offset) { |
| + return *reinterpret_cast<const T*>(vec->at(offset).buf); |
| + } |
| + static inline RefType at(std::vector<StorageType>* vec, size_t offset) { |
| + return *reinterpret_cast<T*>(vec->at(offset).buf); |
| + } |
| +}; |
| + |
| +#if 0 |
| +template <typename E> struct ArrayTraits<Array<E>, true> { |
| + typedef S* StorageType; |
| + static const T& at(const std::vector<StorageType>& vec, size_t offset) const { |
| + const S*& slot = vec.at(offset); |
| + return *reinterpret_cast<const T*>(reinterpret_cast<const char**>(&slot); |
| + } |
| + static T& at(std::vector<StorageType>& vec, size_t offset) { |
| + S*& slot = vec.at(offset); |
| + return *reinterpret_cast<T*>(reinterpret_cast<char**>(&slot); |
| + } |
| +}; |
| +#endif |
| + |
| +#if 0 |
| template <typename T, bool kIsObject, bool kIsHandle> struct ArrayTraits {}; |
| // When T is an object type: |
| @@ -405,6 +464,7 @@ template <typename H> struct ArrayTraits<H, false, true> { |
| return ConstRef(const_cast<H*>(&data)); |
| } |
| }; |
| +#endif |
| } // namespace internal |
| } // namespace mojo |