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

Unified Diff: mojo/public/cpp/bindings/lib/array_internal.h

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: serialization Created 6 years, 7 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/lib/array.cc ('k') | mojo/public/cpp/bindings/lib/array_serialization.h » ('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 865bf1cc8647e82a949ade03dac505887266c184..963b9d3de4cb1cbe13c9567a2d5e4d663887bcb9 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,72 +338,46 @@ 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 kIsObject, bool kIsHandle> struct ArrayTraits {};
+// ----
-// When T is an object type:
-template <typename T> struct ArrayTraits<T, true, false> {
- typedef Array_Data<typename T::Data*> DataType;
- typedef const T& ConstRef;
- typedef T& Ref;
- static Buffer::Destructor GetDestructor() {
- return NULL;
- }
- static typename T::Data* ToArrayElement(const T& value) {
- return Unwrap(value);
- }
- // Something sketchy is indeed happening here...
- static Ref ToRef(typename T::Data*& data) {
- return *reinterpret_cast<T*>(&data);
- }
- static ConstRef ToConstRef(typename T::Data* const& data) {
- return *reinterpret_cast<const T*>(&data);
- }
-};
+template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {};
-// When T is a primitive (non-bool) type:
-template <typename T> struct ArrayTraits<T, false, false> {
- typedef Array_Data<T> DataType;
- typedef const T& ConstRef;
- typedef T& Ref;
- static Buffer::Destructor GetDestructor() {
- return NULL;
+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 T ToArrayElement(const T& value) {
- return value;
+ static inline void Finalize(std::vector<T>* vec) {
}
- static Ref ToRef(T& data) { return data; }
- static ConstRef ToConstRef(const T& data) { return data; }
-};
-
-// When T is a bool type:
-template <> struct ArrayTraits<bool, false, false> {
- typedef Array_Data<bool> DataType;
- typedef bool ConstRef;
- typedef ArrayDataTraits<bool>::Ref Ref;
- static Buffer::Destructor GetDestructor() {
- return NULL;
+ static inline ConstRefType at(const std::vector<T>* vec, size_t offset) {
+ return vec->at(offset);
}
- static bool ToArrayElement(const bool& value) {
- return value;
+ static inline RefType at(std::vector<T>* vec, size_t offset) {
+ return vec->at(offset);
}
- static Ref ToRef(const Ref& data) { return data; }
- static ConstRef ToConstRef(ConstRef data) { return data; }
};
-// When T is a handle type:
-template <typename H> struct ArrayTraits<H, false, true> {
- typedef Array_Data<H> DataType;
- typedef Passable<H> ConstRef;
- typedef AssignableAndPassable<H> Ref;
- static Buffer::Destructor GetDestructor() {
- return &DataType::Destructor;
- }
- static H ToArrayElement(const H& value) {
- return value;
- }
- static Ref ToRef(H& data) { return Ref(&data); }
- static ConstRef ToConstRef(const H& data) {
- return ConstRef(const_cast<H*>(&data));
+template <typename T> struct ArrayTraits<T, true> {
+ struct StorageType {
+ char buf[sizeof(T) + (8 - (sizeof(T) % 8)) % 8]; // Make 8-byte aligned.
yzshen1 2014/05/23 17:45:05 What I was trying to ask is that with |vec| of typ
+ };
+ 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();
+ }
+ 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);
}
};
« no previous file with comments | « mojo/public/cpp/bindings/lib/array.cc ('k') | mojo/public/cpp/bindings/lib/array_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698