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