OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef PPAPI_CPP_OUTPUT_TRAITS_H_ | |
6 #define PPAPI_CPP_OUTPUT_TRAITS_H_ | |
7 | |
8 #include "ppapi/c/pp_resource.h" | |
9 #include "ppapi/cpp/array_output.h" | |
10 | |
11 /// @file | |
12 /// This file defines internal templates for defining how data is passed to the | |
13 /// browser via output parameters and how to convert that data to the | |
14 /// corresponding C++ object types. | |
15 /// | |
16 /// It is used by the callback system, it should not be necessary for end-users | |
17 /// to use these templates directly. | |
18 | |
19 struct PP_Var; | |
20 | |
21 namespace pp { | |
22 | |
23 class Resource; | |
24 class Var; | |
25 | |
26 namespace internal { | |
dmichael (off chromium)
2012/03/09 18:23:27
I've seen (and used) "subtle" for this kind of thi
brettw
2012/03/11 05:15:42
There's a difference between "subtle" and "interna
| |
27 | |
28 // This goop is a trick used to implement a template that can be used to | |
29 // determine if a given class is the base class of another given class. It is | |
30 // used in the resource object partial specialization below. | |
31 template<bool Condition, typename T = void> struct EnableIf { | |
32 typedef T Type; | |
33 }; | |
34 template<typename T> struct EnableIf<false, T> { | |
35 }; | |
dmichael (off chromium)
2012/03/09 18:23:27
You're not using EnableIf anymore.
| |
36 template<typename, typename> struct IsSame { | |
37 static bool const value = false; | |
38 }; | |
39 template<typename A> struct IsSame<A, A> { | |
40 static bool const value = true; | |
41 }; | |
42 template<typename Base, typename Derived> struct IsBaseOf { | |
43 static Derived* CreateDerived(); | |
44 static char (&Check(Base*))[1]; | |
45 static char (&Check(...))[2]; | |
46 static bool const value = sizeof Check(CreateDerived()) == 1 && | |
47 !IsSame<Base const, void const>::value; | |
48 }; | |
49 | |
50 // Template to optionally derive from a given base class T if the given | |
51 // predicate P is true. | |
52 template <class T, bool P> struct InheritIf {}; | |
53 template <class T> struct InheritIf<T, true> : public T {}; | |
54 | |
55 // Single output parameters ---------------------------------------------------- | |
56 | |
57 // Output traits for all "plain old data" (POD) types. It is implemented to | |
58 // pass a pointer to the browser as an output parameter. | |
59 // | |
60 // This is used as a base class for the general CallbackOutputTraits below in | |
61 // the case where T is not a resource. | |
62 template<typename T> | |
63 struct GenericCallbackOutputTraits { | |
64 // The type passed to the PPAPI C API for this parameter. For normal out | |
65 // params, we pass a pointer to the object so the browser can write into it. | |
66 typedef T* APIArgType; | |
67 | |
68 // The type used to store the value. This is used internally in asynchronous | |
69 // callbacks by the CompletionCallbackFactory to have the browser write into | |
70 // a temporary value associated with the callback, which is passed to the | |
71 // plugin code when the callback is issued. | |
72 typedef T StorageType; | |
73 | |
74 // Converts a "storage type" to a value that can be passed to the browser as | |
75 // an output parameter. This just takes the address to convert the value to | |
76 // a pointer. | |
77 static inline APIArgType StorageToAPIArg(StorageType& t) { return &t; } | |
78 | |
79 // Converts the "storage type" to the value we pass to the plugin for | |
80 // callbacks. This doesn't actually need to do anything in this case, | |
81 // it's needed for some of more complex template specializations below. | |
82 static inline T& StorageToPluginArg(StorageType& t) { return t; } | |
83 }; | |
84 | |
85 // Output traits for all resource types. It is implemented to pass a | |
86 // PP_Resource* as an output parameter to the browser, and convert to the | |
87 // given resource object type T when passing to the plugin. | |
88 // | |
89 // Note that this class is parameterized by the resource object, for example | |
90 // ResourceCallbackOutputTraits<pp::FileRef>. This is used as a base class for | |
91 // CallbackOutputTraits below for the case where T is a derived class of | |
92 // pp::Resource. | |
93 template<typename T> | |
94 struct ResourceCallbackOutputTraits { | |
95 // To call the browser, we just pass a PP_Resource pointer as the out param. | |
96 typedef PP_Resource* APIArgType; | |
97 typedef PP_Resource StorageType; | |
98 | |
99 static inline APIArgType StorageToAPIArg(StorageType& t) { | |
100 return &t; | |
101 } | |
102 | |
103 // When calling into the plugin, convert the PP_Resource into the requested | |
104 // resource object type. | |
105 static inline T StorageToPluginArg(StorageType& t) { | |
106 return T(::pp::PASS_REF, t); | |
dmichael (off chromium)
2012/03/09 18:23:27
I don't think ::pp:: is necessary here.
| |
107 } | |
108 }; | |
109 | |
110 // The general templatized base class for all CallbackOutputTraits. This class | |
111 // covers both resources and POD (ints, structs, etc.) by inheriting from the | |
112 // appropriate base class depending on whether the given type derives from | |
113 // pp::Resource. This trick allows us to do this once rather than writing | |
114 // specializations for every resource object type. | |
115 template<typename T> | |
116 struct CallbackOutputTraits | |
117 : public InheritIf<GenericCallbackOutputTraits<T>, | |
118 !IsBaseOf<Resource, T>::value>, | |
119 public InheritIf<ResourceCallbackOutputTraits<T>, | |
120 IsBaseOf<Resource, T>::value> { | |
121 }; | |
122 | |
123 // A specialization of CallbackOutputTraits for pp::Var output parameters. | |
124 // It passes a PP_Var* to the browser and converts to a pp::Var when passing | |
125 // to the plugin. | |
126 template<> | |
127 struct CallbackOutputTraits<Var> { | |
128 // To call the browser, we just pass a PP_Var* as an output param. | |
129 typedef PP_Var* APIArgType; | |
130 typedef PP_Var StorageType; | |
131 | |
132 static inline APIArgType StorageToAPIArg(StorageType& t) { | |
133 return &t; | |
134 } | |
135 | |
136 // Converts the PP_Var to a pp::Var object, passing any reference counted | |
137 // object along with it. This must only be called once since there will only | |
138 // be one reference that the browser has assigned to us for the out param! | |
dmichael (off chromium)
2012/03/09 18:23:27
(This comment also applies to resources.)
| |
139 static inline pp::Var StorageToPluginArg(StorageType& t) { | |
140 return Var(PASS_REF, t); | |
141 } | |
142 }; | |
143 | |
144 // Array output parameters ----------------------------------------------------- | |
145 | |
146 // Output traits for vectors of all "plain old data" (POD) types. It is | |
147 // implemented to pass a pointer to the browser as an output parameter. | |
148 // | |
149 // This is used as a base class for the general vector CallbackOutputTraits | |
150 // below in the case where T is not a resource. | |
151 template<typename T> | |
152 struct GenericVectorCallbackOutputTraits { | |
153 // All arrays are output via a PP_ArrayOutput type. | |
154 typedef PP_ArrayOutput* APIArgType; | |
155 | |
156 // We store the array as this adapter which combines the PP_ArrayOutput | |
157 // structure with the underlying std::vector that it will write into. | |
158 typedef ArrayOutputAdapterWithStorage<T> StorageType; | |
159 | |
160 // Retrieves the PP_ArrayOutput interface for our vector object that the | |
161 // browser will use to write into our code. | |
162 static inline APIArgType StorageToAPIArg(StorageType& t) { | |
163 return t.pp_array_output(); | |
164 } | |
165 | |
166 // Retrieves the underlying vector that can be passed to the plugin. | |
167 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { | |
168 return t.output(); | |
169 } | |
170 }; | |
171 | |
172 // Output traits for all vectors of resource types. It is implemented to pass | |
173 // a PP_Resource* as an output parameter to the browser, and convert to the | |
174 // given resource object type T when passing to the plugin. | |
175 // | |
176 // Note that this class is parameterized by the resource object, for example | |
177 // ResourceVectorCallbackOutputTraits<pp::FileRef>. This is used as a base | |
178 // class for CallbackOutputTraits below for the case where T is a derived | |
179 // class of pp::Resource. | |
180 template<typename T> | |
181 struct ResourceVectorCallbackOutputTraits { | |
182 typedef PP_ArrayOutput* APIArgType; | |
183 typedef ResourceArrayOutputAdapterWithStorage<T> StorageType; | |
184 | |
185 static inline APIArgType StorageToAPIArg(StorageType& t) { | |
186 return t.pp_array_output(); | |
187 } | |
188 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { | |
189 return t.output(); | |
190 } | |
191 }; | |
192 | |
193 // Specialization of CallbackOutputTraits for vectors. This struct covers both | |
194 // arrays of resources and arrays of POD (ints, structs, etc.) by inheriting | |
195 // from the appropriate base class depending on whether the given type derives | |
196 // from pp::Resource. This trick allows us to do this once rather than writing | |
197 // specilalizations for every resource object type. | |
198 template<typename T> | |
199 struct CallbackOutputTraits< std::vector<T> > | |
200 : public InheritIf<GenericVectorCallbackOutputTraits<T>, | |
201 !IsBaseOf<Resource, T>::value>, | |
202 public InheritIf<ResourceVectorCallbackOutputTraits<T>, | |
203 IsBaseOf<Resource, T>::value> { | |
204 }; | |
205 | |
206 // A specialization of CallbackOutputTraits to provide the callback system | |
207 // the information on how to handle vectors of pp::Var. Vectors of resources | |
208 // and plain data are handled separately. See the above definition for more. | |
209 template<> | |
210 struct CallbackOutputTraits< std::vector<pp::Var> > { | |
211 // All arrays are output via a PP_ArrayOutput type. | |
212 typedef PP_ArrayOutput* APIArgType; | |
213 | |
214 // We store the array as this adapter which combines the PP_ArrayOutput | |
215 // structure with the underlying std::vector that it will write into. | |
216 typedef VarArrayOutputAdapterWithStorage StorageType; | |
217 | |
218 // Retrieves the PP_ArrayOutput interface for our vector object that the | |
219 // browser will use to write into our code. | |
220 static inline APIArgType StorageToAPIArg(StorageType& t) { | |
221 return t.pp_array_output(); | |
222 } | |
223 | |
224 // Retrieves the underlying vector that can be passed to the plugin. | |
225 static inline std::vector<pp::Var>& StorageToPluginArg(StorageType& t) { | |
226 return t.output(); | |
227 } | |
228 }; | |
229 | |
230 } // namespace internal | |
231 } // namespace pp | |
232 | |
233 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_ | |
OLD | NEW |