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

Side by Side Diff: ppapi/cpp/output_traits.h

Issue 116963003: App APIs in Pepper: C++ APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 PPAPI_CPP_OUTPUT_TRAITS_H_ 5 #ifndef PPAPI_CPP_OUTPUT_TRAITS_H_
6 #define PPAPI_CPP_OUTPUT_TRAITS_H_ 6 #define PPAPI_CPP_OUTPUT_TRAITS_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "ppapi/c/pp_resource.h" 10 #include "ppapi/c/pp_resource.h"
11 #include "ppapi/cpp/array_output.h" 11 #include "ppapi/cpp/array_output.h"
12 #include "ppapi/cpp/dev/array_dev.h"
13 #include "ppapi/cpp/dev/struct_wrapper_base_dev.h"
12 #include "ppapi/cpp/resource.h" 14 #include "ppapi/cpp/resource.h"
13 15
14 /// @file 16 /// @file
15 /// This file defines internal templates for defining how data is passed to the 17 /// This file defines internal templates for defining how data is passed to the
16 /// browser via output parameters and how to convert that data to the 18 /// browser via output parameters and how to convert that data to the
17 /// corresponding C++ object types. 19 /// corresponding C++ object types.
18 /// 20 ///
19 /// It is used by the callback system, it should not be necessary for end-users 21 /// It is used by the callback system, it should not be necessary for end-users
20 /// to use these templates directly. 22 /// to use these templates directly.
21 23
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // resource object type. 118 // resource object type.
117 static inline T StorageToPluginArg(StorageType& t) { 119 static inline T StorageToPluginArg(StorageType& t) {
118 return T(PASS_REF, t); 120 return T(PASS_REF, t);
119 } 121 }
120 122
121 static inline void Initialize(StorageType* t) { 123 static inline void Initialize(StorageType* t) {
122 *t = 0; 124 *t = 0;
123 } 125 }
124 }; 126 };
125 127
128 // This class is parameterized by derived classes of
129 // pp::StructWrapperIdentifier, for example,
130 // StructWrapperCallbackOutputTraits<pp::alarms::Alarm_Dev>. It is used as a
131 // base class for CallbackOutputTraits below.
132 template<typename T>
133 struct StructWrapperCallbackOutputTraits {
134 typedef typename T::COutputType APIArgType;
135 typedef T StorageType;
136
137 // Returns the underlying C struct of |t|, which can be passed to the browser
138 // as an output parameter.
139 static inline APIArgType StorageToAPIArg(StorageType& t) {
140 return t.StartRawUpdate();
141 }
142
143 // For each |t| passed into StorageToAPIArg(), this method must be called
144 // exactly once in order to match StartRawUpdate() and EndRawUpdate().
145 static inline T& StorageToPluginArg(StorageType& t) {
146 t.EndRawUpdate();
147 return t;
148 }
149
150 static inline void Initialize(StorageType* /* t */) {}
151 };
152
126 // The general templatized base class for all CallbackOutputTraits. This class 153 // The general templatized base class for all CallbackOutputTraits. This class
127 // covers both resources and POD (ints, structs, etc.) by inheriting from the 154 // covers resources, struct wrapper objects and POD (ints, structs, etc.)
128 // appropriate base class depending on whether the given type derives from 155 // by inheriting from the appropriate base class depending on whether the given
129 // pp::Resource. This trick allows us to do this once rather than writing 156 // type derives from pp::Resource or pp::StructWrapperIdentifier. This trick
130 // specializations for every resource object type. 157 // allows us to do this once rather than writing specializations for every
158 // object type.
131 template<typename T> 159 template<typename T>
132 struct CallbackOutputTraits 160 struct CallbackOutputTraits
133 : public InheritIf<GenericCallbackOutputTraits<T>, 161 : public InheritIf<ResourceCallbackOutputTraits<T>,
134 !IsBaseOf<Resource, T>::value>, 162 IsBaseOf<Resource, T>::value>,
135 public InheritIf<ResourceCallbackOutputTraits<T>, 163 public InheritIf<StructWrapperCallbackOutputTraits<T>,
136 IsBaseOf<Resource, T>::value> { 164 IsBaseOf<StructWrapperIdentifier, T>::value>,
165 public InheritIf<GenericCallbackOutputTraits<T>,
166 !IsBaseOf<Resource, T>::value &&
167 !IsBaseOf<StructWrapperIdentifier, T>::value> {
137 }; 168 };
138 169
139 // A specialization of CallbackOutputTraits for pp::Var output parameters. 170 // A specialization of CallbackOutputTraits for pp::Var output parameters.
140 // It passes a PP_Var* to the browser and converts to a pp::Var when passing 171 // It passes a PP_Var* to the browser and converts to a pp::Var when passing
141 // to the plugin. 172 // to the plugin.
142 template<> 173 template<>
143 struct CallbackOutputTraits<Var> { 174 struct CallbackOutputTraits<Var> {
144 // To call the browser, we just pass a PP_Var* as an output param. 175 // To call the browser, we just pass a PP_Var* as an output param.
145 typedef PP_Var* APIArgType; 176 typedef PP_Var* APIArgType;
146 typedef PP_Var StorageType; 177 typedef PP_Var StorageType;
(...skipping 30 matching lines...) Expand all
177 // Converts the PP_Bool to a bool object. 208 // Converts the PP_Bool to a bool object.
178 static inline bool StorageToPluginArg(StorageType& t) { 209 static inline bool StorageToPluginArg(StorageType& t) {
179 return PP_ToBool(t); 210 return PP_ToBool(t);
180 } 211 }
181 212
182 static inline void Initialize(StorageType* t) { 213 static inline void Initialize(StorageType* t) {
183 *t = PP_FALSE; 214 *t = PP_FALSE;
184 } 215 }
185 }; 216 };
186 217
218 // A specialization of CallbackOutputTraits for pp::Array parameters.
219 template<typename T>
220 struct CallbackOutputTraits<Array<T> > {
dmichael (off chromium) 2013/12/17 18:03:25 Would it be practical to put this in the header fo
yzshen1 2013/12/17 23:52:21 It seems we put most output traits in this file. F
221 typedef typename Array<T>::COutputType APIArgType;
222 typedef Array<T> StorageType;
223
224 // Returns the underlying C struct of |t|, which can be passed to the browser
225 // as an output parameter.
226 static inline APIArgType StorageToAPIArg(StorageType& t) {
227 return t.StartRawUpdate();
228 }
229
230 // For each |t| passed into StorageToAPIArg(), this method must be called
231 // exactly once in order to match StartRawUpdate() and EndRawUpdate().
232 static inline Array<T>& StorageToPluginArg(StorageType& t) {
233 t.EndRawUpdate();
234 return t;
235 }
236
237 static inline void Initialize(StorageType* /* t */) {}
238 };
239
187 // Array output parameters ----------------------------------------------------- 240 // Array output parameters -----------------------------------------------------
188 241
189 // Output traits for vectors of all "plain old data" (POD) types. It is 242 // Output traits for vectors of all "plain old data" (POD) types. It is
190 // implemented to pass a pointer to the browser as an output parameter. 243 // implemented to pass a pointer to the browser as an output parameter.
191 // 244 //
192 // This is used as a base class for the general vector CallbackOutputTraits 245 // This is used as a base class for the general vector CallbackOutputTraits
193 // below in the case where T is not a resource. 246 // below in the case where T is not a resource.
194 template<typename T> 247 template<typename T>
195 struct GenericVectorCallbackOutputTraits { 248 struct GenericVectorCallbackOutputTraits {
196 // All arrays are output via a PP_ArrayOutput type. 249 // All arrays are output via a PP_ArrayOutput type.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 return t.output(); 326 return t.output();
274 } 327 }
275 328
276 static inline void Initialize(StorageType* /* t */) {} 329 static inline void Initialize(StorageType* /* t */) {}
277 }; 330 };
278 331
279 } // namespace internal 332 } // namespace internal
280 } // namespace pp 333 } // namespace pp
281 334
282 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_ 335 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698