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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
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 10
10 #include "mojo/public/cpp/bindings/buffer.h" 11 #include "mojo/public/cpp/bindings/buffer.h"
11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 12 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
12 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" 13 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
13 #include "mojo/public/cpp/bindings/passable.h" 14 #include "mojo/public/cpp/bindings/passable.h"
14 15
15 namespace mojo { 16 namespace mojo {
16 template <typename T> class Array; 17 template <typename T> class Array;
17 18
18 namespace internal { 19 namespace internal {
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 331
331 internal::ArrayHeader header_; 332 internal::ArrayHeader header_;
332 333
333 // Elements of type internal::ArrayDataTraits<T>::StorageType follow. 334 // Elements of type internal::ArrayDataTraits<T>::StorageType follow.
334 }; 335 };
335 MOJO_COMPILE_ASSERT(sizeof(Array_Data<char>) == 8, bad_sizeof_Array_Data); 336 MOJO_COMPILE_ASSERT(sizeof(Array_Data<char>) == 8, bad_sizeof_Array_Data);
336 337
337 // UTF-8 encoded 338 // UTF-8 encoded
338 typedef Array_Data<char> String_Data; 339 typedef Array_Data<char> String_Data;
339 340
340 template <typename T, bool kIsObject, bool kIsHandle> struct ArrayTraits {}; 341 // ----
341 342
342 // When T is an object type: 343 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {};
343 template <typename T> struct ArrayTraits<T, true, false> { 344
344 typedef Array_Data<typename T::Data*> DataType; 345 template <typename T> struct ArrayTraits<T, false> {
345 typedef const T& ConstRef; 346 typedef T StorageType;
346 typedef T& Ref; 347 typedef typename std::vector<T>::reference RefType;
347 static Buffer::Destructor GetDestructor() { 348 typedef typename std::vector<T>::const_reference ConstRefType;
348 return NULL; 349 static inline void Initialize(std::vector<T>* vec) {
349 } 350 }
350 static typename T::Data* ToArrayElement(const T& value) { 351 static inline void Finalize(std::vector<T>* vec) {
351 return Unwrap(value);
352 } 352 }
353 // Something sketchy is indeed happening here... 353 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) {
354 static Ref ToRef(typename T::Data*& data) { 354 return vec->at(offset);
355 return *reinterpret_cast<T*>(&data);
356 } 355 }
357 static ConstRef ToConstRef(typename T::Data* const& data) { 356 static inline RefType at(std::vector<T>* vec, size_t offset) {
358 return *reinterpret_cast<const T*>(&data); 357 return vec->at(offset);
359 } 358 }
360 }; 359 };
361 360
362 // When T is a primitive (non-bool) type: 361 template <typename T> struct ArrayTraits<T, true> {
363 template <typename T> struct ArrayTraits<T, false, false> { 362 struct StorageType {
364 typedef Array_Data<T> DataType; 363 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
365 typedef const T& ConstRef; 364 };
366 typedef T& Ref; 365 typedef T& RefType;
367 static Buffer::Destructor GetDestructor() { 366 typedef const T& ConstRefType;
368 return NULL; 367 static inline void Initialize(std::vector<StorageType>* vec) {
368 for (size_t i = 0; i < vec->size(); ++i)
369 new (vec->at(i).buf) T();
369 } 370 }
370 static T ToArrayElement(const T& value) { 371 static inline void Finalize(std::vector<StorageType>* vec) {
371 return value; 372 for (size_t i = 0; i < vec->size(); ++i)
373 reinterpret_cast<T*>(vec->at(i).buf)->~T();
372 } 374 }
373 static Ref ToRef(T& data) { return data; } 375 static inline ConstRefType at(const std::vector<StorageType>* vec,
374 static ConstRef ToConstRef(const T& data) { return data; } 376 size_t offset) {
375 }; 377 return *reinterpret_cast<const T*>(vec->at(offset).buf);
376
377 // When T is a bool type:
378 template <> struct ArrayTraits<bool, false, false> {
379 typedef Array_Data<bool> DataType;
380 typedef bool ConstRef;
381 typedef ArrayDataTraits<bool>::Ref Ref;
382 static Buffer::Destructor GetDestructor() {
383 return NULL;
384 } 378 }
385 static bool ToArrayElement(const bool& value) { 379 static inline RefType at(std::vector<StorageType>* vec, size_t offset) {
386 return value; 380 return *reinterpret_cast<T*>(vec->at(offset).buf);
387 }
388 static Ref ToRef(const Ref& data) { return data; }
389 static ConstRef ToConstRef(ConstRef data) { return data; }
390 };
391
392 // When T is a handle type:
393 template <typename H> struct ArrayTraits<H, false, true> {
394 typedef Array_Data<H> DataType;
395 typedef Passable<H> ConstRef;
396 typedef AssignableAndPassable<H> Ref;
397 static Buffer::Destructor GetDestructor() {
398 return &DataType::Destructor;
399 }
400 static H ToArrayElement(const H& value) {
401 return value;
402 }
403 static Ref ToRef(H& data) { return Ref(&data); }
404 static ConstRef ToConstRef(const H& data) {
405 return ConstRef(const_cast<H*>(&data));
406 } 381 }
407 }; 382 };
408 383
409 } // namespace internal 384 } // namespace internal
410 } // namespace mojo 385 } // namespace mojo
411 386
412 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ 387 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_
OLDNEW
« 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