Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / lib / bindings_internal.h
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_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
7
8 #include "mojo/public/cpp/system/core.h"
9
10 namespace mojo {
11 template <typename S> class InterfaceHandle;
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 kIsHandle = false;
88   static const bool kIsObject = true;
89 };
90 template <> struct TypeTraits<bool> {
91   static const bool kIsHandle = false;
92   static const bool kIsObject = false;
93 };
94 template <> struct TypeTraits<char> {
95   static const bool kIsHandle = false;
96   static const bool kIsObject = false;
97 };
98 template <> struct TypeTraits<int8_t> {
99   static const bool kIsHandle = false;
100   static const bool kIsObject = false;
101 };
102 template <> struct TypeTraits<int16_t> {
103   static const bool kIsHandle = false;
104   static const bool kIsObject = false;
105 };
106 template <> struct TypeTraits<int32_t> {
107   static const bool kIsHandle = false;
108   static const bool kIsObject = false;
109 };
110 template <> struct TypeTraits<int64_t> {
111   static const bool kIsHandle = false;
112   static const bool kIsObject = false;
113 };
114 template <> struct TypeTraits<uint8_t> {
115   static const bool kIsHandle = false;
116   static const bool kIsObject = false;
117 };
118 template <> struct TypeTraits<uint16_t> {
119   static const bool kIsHandle = false;
120   static const bool kIsObject = false;
121 };
122 template <> struct TypeTraits<uint32_t> {
123   static const bool kIsHandle = false;
124   static const bool kIsObject = false;
125 };
126 template <> struct TypeTraits<uint64_t> {
127   static const bool kIsHandle = false;
128   static const bool kIsObject = false;
129 };
130 template <> struct TypeTraits<float> {
131   static const bool kIsHandle = false;
132   static const bool kIsObject = false;
133 };
134 template <> struct TypeTraits<double> {
135   static const bool kIsHandle = false;
136   static const bool kIsObject = false;
137 };
138 template <> struct TypeTraits<Handle> {
139   static const bool kIsHandle = true;
140   static const bool kIsObject = false;
141 };
142 template <> struct TypeTraits<DataPipeConsumerHandle> {
143   static const bool kIsHandle = true;
144   static const bool kIsObject = false;
145 };
146 template <> struct TypeTraits<DataPipeProducerHandle> {
147   static const bool kIsHandle = true;
148   static const bool kIsObject = false;
149 };
150 template <> struct TypeTraits<MessagePipeHandle> {
151   static const bool kIsHandle = true;
152   static const bool kIsObject = false;
153 };
154 template <typename S> struct TypeTraits<InterfaceHandle<S> > {
155   static const bool kIsHandle = true;
156   static const bool kIsObject = false;
157 };
158
159 template <typename T> class ObjectTraits {};
160
161 }  // namespace internal
162 }  // namespace mojo
163
164 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_