OLD | NEW |
---|---|
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 Loading... | |
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 |
341 // ---- | |
342 | |
343 template <typename T, bool kIsMoveOnlyType> struct ArrayTraits {}; | |
344 | |
345 template <typename T> struct ArrayTraits<T, false> { | |
346 typedef T StorageType; | |
347 typedef typename std::vector<T>::reference RefType; | |
348 typedef typename std::vector<T>::const_reference ConstRefType; | |
349 static inline void Initialize(std::vector<T>* vec) { | |
350 } | |
351 static inline void Finalize(std::vector<T>* vec) { | |
352 } | |
353 static inline ConstRefType at(const std::vector<T>* vec, size_t offset) { | |
354 return vec->at(offset); | |
355 } | |
356 static inline RefType at(std::vector<T>* vec, size_t offset) { | |
357 return vec->at(offset); | |
358 } | |
359 }; | |
360 | |
361 template <typename T> struct ArrayTraits<T, true> { | |
362 struct StorageType { | |
363 char buf[sizeof(T)]; | |
364 }; | |
365 typedef T& RefType; | |
366 typedef const T& ConstRefType; | |
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(); | |
yzshen1
2014/05/22 17:13:53
Is there potential alignment issue here?
One alte
| |
370 } | |
371 static inline void Finalize(std::vector<StorageType>* vec) { | |
372 for (size_t i = 0; i < vec->size(); ++i) | |
373 reinterpret_cast<T*>(vec->at(i).buf)->~T(); | |
374 } | |
375 static inline ConstRefType at(const std::vector<StorageType>* vec, | |
376 size_t offset) { | |
377 return *reinterpret_cast<const T*>(vec->at(offset).buf); | |
378 } | |
379 static inline RefType at(std::vector<StorageType>* vec, size_t offset) { | |
380 return *reinterpret_cast<T*>(vec->at(offset).buf); | |
381 } | |
382 }; | |
383 | |
384 #if 0 | |
385 template <typename E> struct ArrayTraits<Array<E>, true> { | |
386 typedef S* StorageType; | |
387 static const T& at(const std::vector<StorageType>& vec, size_t offset) const { | |
388 const S*& slot = vec.at(offset); | |
389 return *reinterpret_cast<const T*>(reinterpret_cast<const char**>(&slot); | |
390 } | |
391 static T& at(std::vector<StorageType>& vec, size_t offset) { | |
392 S*& slot = vec.at(offset); | |
393 return *reinterpret_cast<T*>(reinterpret_cast<char**>(&slot); | |
394 } | |
395 }; | |
396 #endif | |
397 | |
398 #if 0 | |
340 template <typename T, bool kIsObject, bool kIsHandle> struct ArrayTraits {}; | 399 template <typename T, bool kIsObject, bool kIsHandle> struct ArrayTraits {}; |
341 | 400 |
342 // When T is an object type: | 401 // When T is an object type: |
343 template <typename T> struct ArrayTraits<T, true, false> { | 402 template <typename T> struct ArrayTraits<T, true, false> { |
344 typedef Array_Data<typename T::Data*> DataType; | 403 typedef Array_Data<typename T::Data*> DataType; |
345 typedef const T& ConstRef; | 404 typedef const T& ConstRef; |
346 typedef T& Ref; | 405 typedef T& Ref; |
347 static Buffer::Destructor GetDestructor() { | 406 static Buffer::Destructor GetDestructor() { |
348 return NULL; | 407 return NULL; |
349 } | 408 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 return &DataType::Destructor; | 457 return &DataType::Destructor; |
399 } | 458 } |
400 static H ToArrayElement(const H& value) { | 459 static H ToArrayElement(const H& value) { |
401 return value; | 460 return value; |
402 } | 461 } |
403 static Ref ToRef(H& data) { return Ref(&data); } | 462 static Ref ToRef(H& data) { return Ref(&data); } |
404 static ConstRef ToConstRef(const H& data) { | 463 static ConstRef ToConstRef(const H& data) { |
405 return ConstRef(const_cast<H*>(&data)); | 464 return ConstRef(const_cast<H*>(&data)); |
406 } | 465 } |
407 }; | 466 }; |
467 #endif | |
408 | 468 |
409 } // namespace internal | 469 } // namespace internal |
410 } // namespace mojo | 470 } // namespace mojo |
411 | 471 |
412 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ | 472 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_INTERNAL_H_ |
OLD | NEW |