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/bindings/lib/template_util.h"
9 #include "mojo/public/cpp/system/core.h"
10
11 namespace mojo {
12 class String;
13
14 namespace internal {
15 template <typename T> class Array_Data;
16
17 #pragma pack(push, 1)
18
19 struct StructHeader {
20 uint32_t num_bytes;
21 uint32_t num_fields;
22 };
23 MOJO_COMPILE_ASSERT(sizeof(StructHeader) == 8, bad_sizeof_StructHeader);
24
25 struct ArrayHeader {
26 uint32_t num_bytes;
27 uint32_t num_elements;
28 };
29 MOJO_COMPILE_ASSERT(sizeof(ArrayHeader) == 8, bad_sizeof_ArrayHeader);
30
31 template <typename T>
32 union StructPointer {
33 uint64_t offset;
34 T* ptr;
35 };
36 MOJO_COMPILE_ASSERT(sizeof(StructPointer<char>) == 8, bad_sizeof_StructPointer);
37
38 template <typename T>
39 union ArrayPointer {
40 uint64_t offset;
41 Array_Data<T>* ptr;
42 };
43 MOJO_COMPILE_ASSERT(sizeof(ArrayPointer<char>) == 8, bad_sizeof_ArrayPointer);
44
45 union StringPointer {
46 uint64_t offset;
47 Array_Data<char>* ptr;
48 };
49 MOJO_COMPILE_ASSERT(sizeof(StringPointer) == 8, bad_sizeof_StringPointer);
50
51 #pragma pack(pop)
52
53 template <typename T>
ResetIfNonNull(T * ptr)54 void ResetIfNonNull(T* ptr) {
55 if (ptr)
56 *ptr = T();
57 }
58
59 template <typename T>
FetchAndReset(T * ptr)60 T FetchAndReset(T* ptr) {
61 T temp = *ptr;
62 *ptr = T();
63 return temp;
64 }
65
66 template <typename H> struct IsHandle {
67 static const bool value = IsBaseOf<Handle, H>::value;
68 };
69
70 template <typename T, bool move_only = IsMoveOnlyType<T>::value>
71 struct WrapperTraits;
72
73 template <typename T> struct WrapperTraits<T, false> {
74 typedef T DataType;
75 };
76 template <typename H> struct WrapperTraits<ScopedHandleBase<H>, true> {
77 typedef H DataType;
78 };
79 template <typename S> struct WrapperTraits<S, true> {
80 typedef typename S::Data_* DataType;
81 };
82
83 } // namespace internal
84 } // namespace mojo
85
86 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
87