Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: mojo/public/cpp/bindings/lib/bindings_internal.h

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix windows bustage Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_BINDINGS_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
7 7
8 #include "mojo/public/cpp/bindings/lib/template_util.h"
8 #include "mojo/public/cpp/system/core.h" 9 #include "mojo/public/cpp/system/core.h"
9 10
10 namespace mojo { 11 namespace mojo {
11 template <typename S> class InterfaceHandle; 12 class String;
12 13
13 namespace internal { 14 namespace internal {
14 template <typename T> class Array_Data; 15 template <typename T> class Array_Data;
15 16
16 #pragma pack(push, 1) 17 #pragma pack(push, 1)
17 18
18 struct StructHeader { 19 struct StructHeader {
19 uint32_t num_bytes; 20 uint32_t num_bytes;
20 uint32_t num_fields; 21 uint32_t num_fields;
21 }; 22 };
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 *ptr = T(); 56 *ptr = T();
56 } 57 }
57 58
58 template <typename T> 59 template <typename T>
59 T FetchAndReset(T* ptr) { 60 T FetchAndReset(T* ptr) {
60 T temp = *ptr; 61 T temp = *ptr;
61 *ptr = T(); 62 *ptr = T();
62 return temp; 63 return temp;
63 } 64 }
64 65
65 template <typename T> 66 template <typename T> struct IsHandle {
66 class WrapperHelper { 67 static const bool value = false;
67 public: 68 };
68 static const T Wrap(const typename T::Data* data) { 69 template <> struct IsHandle<Handle> {
69 return T(typename T::Wrap(), const_cast<typename T::Data*>(data)); 70 static const bool value = true;
70 } 71 };
71 static typename T::Data* Unwrap(const T& object) { 72 template <> struct IsHandle<DataPipeConsumerHandle> {
yzshen1 2014/05/27 22:16:59 It might make sense to define IsHandle<> with the
72 return const_cast<typename T::Data*>(object.data_); 73 static const bool value = true;
73 } 74 };
75 template <> struct IsHandle<DataPipeProducerHandle> {
76 static const bool value = true;
77 };
78 template <> struct IsHandle<MessagePipeHandle> {
79 static const bool value = true;
80 };
81 template <> struct IsHandle<SharedBufferHandle> {
82 static const bool value = true;
74 }; 83 };
75 84
76 template <typename Data> 85 template <typename T, bool kMoveOnly = IsMoveOnlyType<T>::value>
77 inline const typename Data::Wrapper Wrap(const Data* data) { 86 struct WrapperTraits;
78 return WrapperHelper<typename Data::Wrapper>::Wrap(data);
79 }
80 87
81 template <typename T> 88 template <typename T> struct WrapperTraits<T, false> {
82 inline typename T::Data* Unwrap(const T& object) { 89 typedef T DataType;
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 };
90 template <> struct TypeTraits<bool> { 91 template <typename H> struct WrapperTraits<ScopedHandleBase<H>, true> {
91 static const bool kIsHandle = false; 92 typedef H DataType;
92 static const bool kIsObject = false;
93 }; 93 };
94 template <> struct TypeTraits<char> { 94 template <typename S> struct WrapperTraits<S, true> {
95 static const bool kIsHandle = false; 95 typedef typename S::Data_* DataType;
96 static const bool kIsObject = false;
97 }; 96 };
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 97
161 } // namespace internal 98 } // namespace internal
162 } // namespace mojo 99 } // namespace mojo
163 100
164 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 101 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698