OLD | NEW |
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" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 78 |
79 // Converts a "storage type" to a value that can be passed to the browser as | 79 // Converts a "storage type" to a value that can be passed to the browser as |
80 // an output parameter. This just takes the address to convert the value to | 80 // an output parameter. This just takes the address to convert the value to |
81 // a pointer. | 81 // a pointer. |
82 static inline APIArgType StorageToAPIArg(StorageType& t) { return &t; } | 82 static inline APIArgType StorageToAPIArg(StorageType& t) { return &t; } |
83 | 83 |
84 // Converts the "storage type" to the value we pass to the plugin for | 84 // Converts the "storage type" to the value we pass to the plugin for |
85 // callbacks. This doesn't actually need to do anything in this case, | 85 // callbacks. This doesn't actually need to do anything in this case, |
86 // it's needed for some of more complex template specializations below. | 86 // it's needed for some of more complex template specializations below. |
87 static inline T& StorageToPluginArg(StorageType& t) { return t; } | 87 static inline T& StorageToPluginArg(StorageType& t) { return t; } |
| 88 |
| 89 // Initializes the "storage type" to a default value, if necessary. Here, |
| 90 // we do nothing, assuming that the default constructor for T suffices. |
| 91 static inline void Initialize(StorageType* /* t */) {} |
88 }; | 92 }; |
89 | 93 |
90 // Output traits for all resource types. It is implemented to pass a | 94 // Output traits for all resource types. It is implemented to pass a |
91 // PP_Resource* as an output parameter to the browser, and convert to the | 95 // PP_Resource* as an output parameter to the browser, and convert to the |
92 // given resource object type T when passing to the plugin. | 96 // given resource object type T when passing to the plugin. |
93 // | 97 // |
94 // Note that this class is parameterized by the resource object, for example | 98 // Note that this class is parameterized by the resource object, for example |
95 // ResourceCallbackOutputTraits<pp::FileRef>. This is used as a base class for | 99 // ResourceCallbackOutputTraits<pp::FileRef>. This is used as a base class for |
96 // CallbackOutputTraits below for the case where T is a derived class of | 100 // CallbackOutputTraits below for the case where T is a derived class of |
97 // pp::Resource. | 101 // pp::Resource. |
98 template<typename T> | 102 template<typename T> |
99 struct ResourceCallbackOutputTraits { | 103 struct ResourceCallbackOutputTraits { |
100 // To call the browser, we just pass a PP_Resource pointer as the out param. | 104 // To call the browser, we just pass a PP_Resource pointer as the out param. |
101 typedef PP_Resource* APIArgType; | 105 typedef PP_Resource* APIArgType; |
102 typedef PP_Resource StorageType; | 106 typedef PP_Resource StorageType; |
103 | 107 |
104 static inline APIArgType StorageToAPIArg(StorageType& t) { | 108 static inline APIArgType StorageToAPIArg(StorageType& t) { |
105 return &t; | 109 return &t; |
106 } | 110 } |
107 | 111 |
108 // Converts the PP_Resource to a pp::* object, passing any reference counted | 112 // Converts the PP_Resource to a pp::* object, passing any reference counted |
109 // object along with it. This must only be called once since there will only | 113 // object along with it. This must only be called once since there will only |
110 // be one reference that the browser has assigned to us for the out param! | 114 // be one reference that the browser has assigned to us for the out param! |
111 // When calling into the plugin, convert the PP_Resource into the requested | 115 // When calling into the plugin, convert the PP_Resource into the requested |
112 // resource object type. | 116 // resource object type. |
113 static inline T StorageToPluginArg(StorageType& t) { | 117 static inline T StorageToPluginArg(StorageType& t) { |
114 return T(PASS_REF, t); | 118 return T(PASS_REF, t); |
115 } | 119 } |
| 120 |
| 121 static inline void Initialize(StorageType* t) { |
| 122 *t = 0; |
| 123 } |
116 }; | 124 }; |
117 | 125 |
118 // The general templatized base class for all CallbackOutputTraits. This class | 126 // The general templatized base class for all CallbackOutputTraits. This class |
119 // covers both resources and POD (ints, structs, etc.) by inheriting from the | 127 // covers both resources and POD (ints, structs, etc.) by inheriting from the |
120 // appropriate base class depending on whether the given type derives from | 128 // appropriate base class depending on whether the given type derives from |
121 // pp::Resource. This trick allows us to do this once rather than writing | 129 // pp::Resource. This trick allows us to do this once rather than writing |
122 // specializations for every resource object type. | 130 // specializations for every resource object type. |
123 template<typename T> | 131 template<typename T> |
124 struct CallbackOutputTraits | 132 struct CallbackOutputTraits |
125 : public InheritIf<GenericCallbackOutputTraits<T>, | 133 : public InheritIf<GenericCallbackOutputTraits<T>, |
(...skipping 14 matching lines...) Expand all Loading... |
140 static inline APIArgType StorageToAPIArg(StorageType& t) { | 148 static inline APIArgType StorageToAPIArg(StorageType& t) { |
141 return &t; | 149 return &t; |
142 } | 150 } |
143 | 151 |
144 // Converts the PP_Var to a pp::Var object, passing any reference counted | 152 // Converts the PP_Var to a pp::Var object, passing any reference counted |
145 // object along with it. This must only be called once since there will only | 153 // object along with it. This must only be called once since there will only |
146 // be one reference that the browser has assigned to us for the out param! | 154 // be one reference that the browser has assigned to us for the out param! |
147 static inline pp::Var StorageToPluginArg(StorageType& t) { | 155 static inline pp::Var StorageToPluginArg(StorageType& t) { |
148 return Var(PASS_REF, t); | 156 return Var(PASS_REF, t); |
149 } | 157 } |
| 158 |
| 159 static inline void Initialize(StorageType* t) { |
| 160 *t = PP_MakeUndefined(); |
| 161 } |
150 }; | 162 }; |
151 | 163 |
152 // Array output parameters ----------------------------------------------------- | 164 // Array output parameters ----------------------------------------------------- |
153 | 165 |
154 // Output traits for vectors of all "plain old data" (POD) types. It is | 166 // Output traits for vectors of all "plain old data" (POD) types. It is |
155 // implemented to pass a pointer to the browser as an output parameter. | 167 // implemented to pass a pointer to the browser as an output parameter. |
156 // | 168 // |
157 // This is used as a base class for the general vector CallbackOutputTraits | 169 // This is used as a base class for the general vector CallbackOutputTraits |
158 // below in the case where T is not a resource. | 170 // below in the case where T is not a resource. |
159 template<typename T> | 171 template<typename T> |
160 struct GenericVectorCallbackOutputTraits { | 172 struct GenericVectorCallbackOutputTraits { |
161 // All arrays are output via a PP_ArrayOutput type. | 173 // All arrays are output via a PP_ArrayOutput type. |
162 typedef PP_ArrayOutput APIArgType; | 174 typedef PP_ArrayOutput APIArgType; |
163 | 175 |
164 // We store the array as this adapter which combines the PP_ArrayOutput | 176 // We store the array as this adapter which combines the PP_ArrayOutput |
165 // structure with the underlying std::vector that it will write into. | 177 // structure with the underlying std::vector that it will write into. |
166 typedef ArrayOutputAdapterWithStorage<T> StorageType; | 178 typedef ArrayOutputAdapterWithStorage<T> StorageType; |
167 | 179 |
168 // Retrieves the PP_ArrayOutput interface for our vector object that the | 180 // Retrieves the PP_ArrayOutput interface for our vector object that the |
169 // browser will use to write into our code. | 181 // browser will use to write into our code. |
170 static inline APIArgType StorageToAPIArg(StorageType& t) { | 182 static inline APIArgType StorageToAPIArg(StorageType& t) { |
171 return t.pp_array_output(); | 183 return t.pp_array_output(); |
172 } | 184 } |
173 | 185 |
174 // Retrieves the underlying vector that can be passed to the plugin. | 186 // Retrieves the underlying vector that can be passed to the plugin. |
175 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { | 187 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { |
176 return t.output(); | 188 return t.output(); |
177 } | 189 } |
| 190 |
| 191 static inline void Initialize(StorageType* /* t */) {} |
178 }; | 192 }; |
179 | 193 |
180 // Output traits for all vectors of resource types. It is implemented to pass | 194 // Output traits for all vectors of resource types. It is implemented to pass |
181 // a PP_ArrayOutput parameter to the browser, and convert the returned resources | 195 // a PP_ArrayOutput parameter to the browser, and convert the returned resources |
182 // to a vector of the given resource object type T when passing to the plugin. | 196 // to a vector of the given resource object type T when passing to the plugin. |
183 // | 197 // |
184 // Note that this class is parameterized by the resource object, for example | 198 // Note that this class is parameterized by the resource object, for example |
185 // ResourceVectorCallbackOutputTraits<pp::FileRef>. This is used as a base | 199 // ResourceVectorCallbackOutputTraits<pp::FileRef>. This is used as a base |
186 // class for CallbackOutputTraits below for the case where T is a derived | 200 // class for CallbackOutputTraits below for the case where T is a derived |
187 // class of pp::Resource. | 201 // class of pp::Resource. |
188 template<typename T> | 202 template<typename T> |
189 struct ResourceVectorCallbackOutputTraits { | 203 struct ResourceVectorCallbackOutputTraits { |
190 typedef PP_ArrayOutput APIArgType; | 204 typedef PP_ArrayOutput APIArgType; |
191 typedef ResourceArrayOutputAdapterWithStorage<T> StorageType; | 205 typedef ResourceArrayOutputAdapterWithStorage<T> StorageType; |
192 | 206 |
193 static inline APIArgType StorageToAPIArg(StorageType& t) { | 207 static inline APIArgType StorageToAPIArg(StorageType& t) { |
194 return t.pp_array_output(); | 208 return t.pp_array_output(); |
195 } | 209 } |
196 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { | 210 static inline std::vector<T>& StorageToPluginArg(StorageType& t) { |
197 return t.output(); | 211 return t.output(); |
198 } | 212 } |
| 213 |
| 214 static inline void Initialize(StorageType* /* t */) {} |
199 }; | 215 }; |
200 | 216 |
201 // Specialization of CallbackOutputTraits for vectors. This struct covers both | 217 // Specialization of CallbackOutputTraits for vectors. This struct covers both |
202 // arrays of resources and arrays of POD (ints, structs, etc.) by inheriting | 218 // arrays of resources and arrays of POD (ints, structs, etc.) by inheriting |
203 // from the appropriate base class depending on whether the given type derives | 219 // from the appropriate base class depending on whether the given type derives |
204 // from pp::Resource. This trick allows us to do this once rather than writing | 220 // from pp::Resource. This trick allows us to do this once rather than writing |
205 // specializations for every resource object type. | 221 // specializations for every resource object type. |
206 template<typename T> | 222 template<typename T> |
207 struct CallbackOutputTraits< std::vector<T> > | 223 struct CallbackOutputTraits< std::vector<T> > |
208 : public InheritIf<GenericVectorCallbackOutputTraits<T>, | 224 : public InheritIf<GenericVectorCallbackOutputTraits<T>, |
(...skipping 17 matching lines...) Expand all Loading... |
226 // Retrieves the PP_ArrayOutput interface for our vector object that the | 242 // Retrieves the PP_ArrayOutput interface for our vector object that the |
227 // browser will use to write into our code. | 243 // browser will use to write into our code. |
228 static inline APIArgType StorageToAPIArg(StorageType& t) { | 244 static inline APIArgType StorageToAPIArg(StorageType& t) { |
229 return t.pp_array_output(); | 245 return t.pp_array_output(); |
230 } | 246 } |
231 | 247 |
232 // Retrieves the underlying vector that can be passed to the plugin. | 248 // Retrieves the underlying vector that can be passed to the plugin. |
233 static inline std::vector<pp::Var>& StorageToPluginArg(StorageType& t) { | 249 static inline std::vector<pp::Var>& StorageToPluginArg(StorageType& t) { |
234 return t.output(); | 250 return t.output(); |
235 } | 251 } |
| 252 |
| 253 static inline void Initialize(StorageType* /* t */) {} |
236 }; | 254 }; |
237 | 255 |
238 } // namespace internal | 256 } // namespace internal |
239 } // namespace pp | 257 } // namespace pp |
240 | 258 |
241 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_ | 259 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_ |
OLD | NEW |