Home | History | Annotate | Download | only in lib
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_INTERNAL_H_
      6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_INTERNAL_H_
      7 
      8 #include "mojo/public/system/core_cpp.h"
      9 
     10 namespace mojo {
     11 template <typename T, typename U> class TypeConverter {};
     12 
     13 namespace internal {
     14 template <typename T> class Array_Data;
     15 
     16 #pragma pack(push, 1)
     17 
     18 struct StructHeader {
     19   uint32_t num_bytes;
     20   uint32_t num_fields;
     21 };
     22 MOJO_COMPILE_ASSERT(sizeof(StructHeader) == 8, bad_sizeof_StructHeader);
     23 
     24 struct ArrayHeader {
     25   uint32_t num_bytes;
     26   uint32_t num_elements;
     27 };
     28 MOJO_COMPILE_ASSERT(sizeof(ArrayHeader) == 8, bad_sizeof_ArrayHeader);
     29 
     30 template <typename T>
     31 union StructPointer {
     32   uint64_t offset;
     33   T* ptr;
     34 };
     35 MOJO_COMPILE_ASSERT(sizeof(StructPointer<char>) == 8, bad_sizeof_StructPointer);
     36 
     37 template <typename T>
     38 union ArrayPointer {
     39   uint64_t offset;
     40   Array_Data<T>* ptr;
     41 };
     42 MOJO_COMPILE_ASSERT(sizeof(ArrayPointer<char>) == 8, bad_sizeof_ArrayPointer);
     43 
     44 union StringPointer {
     45   uint64_t offset;
     46   Array_Data<char>* ptr;
     47 };
     48 MOJO_COMPILE_ASSERT(sizeof(StringPointer) == 8, bad_sizeof_StringPointer);
     49 
     50 #pragma pack(pop)
     51 
     52 template <typename T>
     53 void ResetIfNonNull(T* ptr) {
     54   if (ptr)
     55     *ptr = T();
     56 }
     57 
     58 template <typename T>
     59 T FetchAndReset(T* ptr) {
     60   T temp = *ptr;
     61   *ptr = T();
     62   return temp;
     63 }
     64 
     65 template <typename T>
     66 class WrapperHelper {
     67  public:
     68   static const T Wrap(const typename T::Data* data) {
     69     return T(typename T::Wrap(), const_cast<typename T::Data*>(data));
     70   }
     71   static typename T::Data* Unwrap(const T& object) {
     72     return const_cast<typename T::Data*>(object.data_);
     73   }
     74 };
     75 
     76 template <typename Data>
     77 inline const typename Data::Wrapper Wrap(const Data* data) {
     78   return WrapperHelper<typename Data::Wrapper>::Wrap(data);
     79 }
     80 
     81 template <typename T>
     82 inline typename T::Data* Unwrap(const T& object) {
     83   return WrapperHelper<T>::Unwrap(object);
     84 }
     85 
     86 template <typename T> struct TypeTraits {
     87   static const bool kIsObject = true;
     88 };
     89 template <> struct TypeTraits<bool> {
     90   static const bool kIsObject = false;
     91 };
     92 template <> struct TypeTraits<char> {
     93   static const bool kIsObject = false;
     94 };
     95 template <> struct TypeTraits<int8_t> {
     96   static const bool kIsObject = false;
     97 };
     98 template <> struct TypeTraits<int16_t> {
     99   static const bool kIsObject = false;
    100 };
    101 template <> struct TypeTraits<int32_t> {
    102   static const bool kIsObject = false;
    103 };
    104 template <> struct TypeTraits<int64_t> {
    105   static const bool kIsObject = false;
    106 };
    107 template <> struct TypeTraits<uint8_t> {
    108   static const bool kIsObject = false;
    109 };
    110 template <> struct TypeTraits<uint16_t> {
    111   static const bool kIsObject = false;
    112 };
    113 template <> struct TypeTraits<uint32_t> {
    114   static const bool kIsObject = false;
    115 };
    116 template <> struct TypeTraits<uint64_t> {
    117   static const bool kIsObject = false;
    118 };
    119 template <> struct TypeTraits<float> {
    120   static const bool kIsObject = false;
    121 };
    122 template <> struct TypeTraits<double> {
    123   static const bool kIsObject = false;
    124 };
    125 template <> struct TypeTraits<Handle> {
    126   static const bool kIsObject = false;
    127 };
    128 template <> struct TypeTraits<MessagePipeHandle> {
    129   static const bool kIsObject = false;
    130 };
    131 
    132 template <typename T> class ObjectTraits {};
    133 
    134 }  // namespace internal
    135 }  // namespace mojo
    136 
    137 #endif  // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_INTERNAL_H_
    138