Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ppapi / cpp / output_traits.h
blob889c6128098ebb183a6a65f21c092ffab4a96326
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.
5 #ifndef PPAPI_CPP_OUTPUT_TRAITS_H_
6 #define PPAPI_CPP_OUTPUT_TRAITS_H_
8 #include <vector>
10 #include "ppapi/c/pp_resource.h"
11 #include "ppapi/cpp/array_output.h"
12 #include "ppapi/cpp/dev/directory_entry_dev.h"
13 #include "ppapi/cpp/extensions/ext_output_traits.h"
14 #include "ppapi/cpp/resource.h"
16 /// @file
17 /// This file defines internal templates for defining how data is passed to the
18 /// browser via output parameters and how to convert that data to the
19 /// corresponding C++ object types.
20 ///
21 /// It is used by the callback system, it should not be necessary for end-users
22 /// to use these templates directly.
24 struct PP_Var;
26 namespace pp {
28 class Var;
30 namespace internal {
32 // This goop is a trick used to implement a template that can be used to
33 // determine if a given class is the base class of another given class. It is
34 // used in the resource object partial specialization below.
35 template<typename, typename> struct IsSame {
36 static bool const value = false;
38 template<typename A> struct IsSame<A, A> {
39 static bool const value = true;
41 template<typename Base, typename Derived> struct IsBaseOf {
42 private:
43 // This class doesn't work correctly with forward declarations.
44 // Because sizeof cannot be applied to incomplete types, this line prevents us
45 // from passing in forward declarations.
46 typedef char (*EnsureTypesAreComplete)[sizeof(Base) + sizeof(Derived)];
48 static Derived* CreateDerived();
49 static char (&Check(Base*))[1];
50 static char (&Check(...))[2];
52 public:
53 static bool const value = sizeof Check(CreateDerived()) == 1 &&
54 !IsSame<Base const, void const>::value;
57 // Template to optionally derive from a given base class T if the given
58 // predicate P is true.
59 template <class T, bool P> struct InheritIf {};
60 template <class T> struct InheritIf<T, true> : public T {};
62 // Single output parameters ----------------------------------------------------
64 // Output traits for all "plain old data" (POD) types. It is implemented to
65 // pass a pointer to the browser as an output parameter.
67 // This is used as a base class for the general CallbackOutputTraits below in
68 // the case where T is not a resource.
69 template<typename T>
70 struct GenericCallbackOutputTraits {
71 // The type passed to the PPAPI C API for this parameter. For normal out
72 // params, we pass a pointer to the object so the browser can write into it.
73 typedef T* APIArgType;
75 // The type used to store the value. This is used internally in asynchronous
76 // callbacks by the CompletionCallbackFactory to have the browser write into
77 // a temporary value associated with the callback, which is passed to the
78 // plugin code when the callback is issued.
79 typedef T StorageType;
81 // Converts a "storage type" to a value that can be passed to the browser as
82 // an output parameter. This just takes the address to convert the value to
83 // a pointer.
84 static inline APIArgType StorageToAPIArg(StorageType& t) { return &t; }
86 // Converts the "storage type" to the value we pass to the plugin for
87 // callbacks. This doesn't actually need to do anything in this case,
88 // it's needed for some of more complex template specializations below.
89 static inline T& StorageToPluginArg(StorageType& t) { return t; }
92 // Output traits for all resource types. It is implemented to pass a
93 // PP_Resource* as an output parameter to the browser, and convert to the
94 // given resource object type T when passing to the plugin.
96 // Note that this class is parameterized by the resource object, for example
97 // ResourceCallbackOutputTraits<pp::FileRef>. This is used as a base class for
98 // CallbackOutputTraits below for the case where T is a derived class of
99 // pp::Resource.
100 template<typename T>
101 struct ResourceCallbackOutputTraits {
102 // To call the browser, we just pass a PP_Resource pointer as the out param.
103 typedef PP_Resource* APIArgType;
104 typedef PP_Resource StorageType;
106 static inline APIArgType StorageToAPIArg(StorageType& t) {
107 return &t;
110 // Converts the PP_Resource to a pp::* object, passing any reference counted
111 // object along with it. This must only be called once since there will only
112 // be one reference that the browser has assigned to us for the out param!
113 // When calling into the plugin, convert the PP_Resource into the requested
114 // resource object type.
115 static inline T StorageToPluginArg(StorageType& t) {
116 return T(PASS_REF, t);
120 // The general templatized base class for all CallbackOutputTraits. This class
121 // covers resources, extensions API output objects and POD (ints, structs, etc.)
122 // by inheriting from the appropriate base class depending on whether the given
123 // type derives from pp::Resource or ext::internal::OutputObjectBase. This trick
124 // allows us to do this once rather than writing specializations for every
125 // object type.
126 template<typename T>
127 struct CallbackOutputTraits
128 : public InheritIf<ResourceCallbackOutputTraits<T>,
129 IsBaseOf<Resource, T>::value>,
130 public InheritIf<ext::internal::ExtensionsCallbackOutputTraits<T>,
131 IsBaseOf<ext::internal::OutputObjectBase, T>::value>,
132 public InheritIf<GenericCallbackOutputTraits<T>,
133 !IsBaseOf<Resource, T>::value &&
134 !IsBaseOf<ext::internal::OutputObjectBase, T>::value> {
137 // A specialization of CallbackOutputTraits for pp::Var output parameters.
138 // It passes a PP_Var* to the browser and converts to a pp::Var when passing
139 // to the plugin.
140 template<>
141 struct CallbackOutputTraits<Var> {
142 // To call the browser, we just pass a PP_Var* as an output param.
143 typedef PP_Var* APIArgType;
144 typedef PP_Var StorageType;
146 static inline APIArgType StorageToAPIArg(StorageType& t) {
147 return &t;
150 // Converts the PP_Var to a pp::Var object, passing any reference counted
151 // object along with it. This must only be called once since there will only
152 // be one reference that the browser has assigned to us for the out param!
153 static inline pp::Var StorageToPluginArg(StorageType& t) {
154 return Var(PASS_REF, t);
158 // Array output parameters -----------------------------------------------------
160 // Output traits for vectors of all "plain old data" (POD) types. It is
161 // implemented to pass a pointer to the browser as an output parameter.
163 // This is used as a base class for the general vector CallbackOutputTraits
164 // below in the case where T is not a resource.
165 template<typename T>
166 struct GenericVectorCallbackOutputTraits {
167 // All arrays are output via a PP_ArrayOutput type.
168 typedef PP_ArrayOutput APIArgType;
170 // We store the array as this adapter which combines the PP_ArrayOutput
171 // structure with the underlying std::vector that it will write into.
172 typedef ArrayOutputAdapterWithStorage<T> StorageType;
174 // Retrieves the PP_ArrayOutput interface for our vector object that the
175 // browser will use to write into our code.
176 static inline APIArgType StorageToAPIArg(StorageType& t) {
177 return t.pp_array_output();
180 // Retrieves the underlying vector that can be passed to the plugin.
181 static inline std::vector<T>& StorageToPluginArg(StorageType& t) {
182 return t.output();
186 // Output traits for all vectors of resource types. It is implemented to pass
187 // a PP_ArrayOutput parameter to the browser, and convert the returned resources
188 // to a vector of the given resource object type T when passing to the plugin.
190 // Note that this class is parameterized by the resource object, for example
191 // ResourceVectorCallbackOutputTraits<pp::FileRef>. This is used as a base
192 // class for CallbackOutputTraits below for the case where T is a derived
193 // class of pp::Resource.
194 template<typename T>
195 struct ResourceVectorCallbackOutputTraits {
196 typedef PP_ArrayOutput APIArgType;
197 typedef ResourceArrayOutputAdapterWithStorage<T> StorageType;
199 static inline APIArgType StorageToAPIArg(StorageType& t) {
200 return t.pp_array_output();
202 static inline std::vector<T>& StorageToPluginArg(StorageType& t) {
203 return t.output();
207 // Specialization of CallbackOutputTraits for vectors. This struct covers arrays
208 // of resources, extensions API output objects and POD (ints, structs, etc.) by
209 // inheriting from the appropriate base class depending on whether the given
210 // type derives from pp::Resource or ext::internal::OutputObjectBase. This trick
211 // allows us to do this once rather than writing specializations for every
212 // object type.
213 template<typename T>
214 struct CallbackOutputTraits< std::vector<T> >
215 : public InheritIf<ResourceVectorCallbackOutputTraits<T>,
216 IsBaseOf<Resource, T>::value>,
217 public InheritIf<ext::internal::ExtensionsVectorCallbackOutputTraits<T>,
218 IsBaseOf<ext::internal::OutputObjectBase, T>::value>,
219 public InheritIf<GenericVectorCallbackOutputTraits<T>,
220 !IsBaseOf<Resource, T>::value &&
221 !IsBaseOf<ext::internal::OutputObjectBase, T>::value> {
224 // A specialization of CallbackOutputTraits to provide the callback system
225 // the information on how to handle vectors of pp::Var. Vectors of resources
226 // and plain data are handled separately. See the above definition for more.
227 template<>
228 struct CallbackOutputTraits< std::vector<pp::Var> > {
229 // All arrays are output via a PP_ArrayOutput type.
230 typedef PP_ArrayOutput APIArgType;
232 // We store the array as this adapter which combines the PP_ArrayOutput
233 // structure with the underlying std::vector that it will write into.
234 typedef VarArrayOutputAdapterWithStorage StorageType;
236 // Retrieves the PP_ArrayOutput interface for our vector object that the
237 // browser will use to write into our code.
238 static inline APIArgType StorageToAPIArg(StorageType& t) {
239 return t.pp_array_output();
242 // Retrieves the underlying vector that can be passed to the plugin.
243 static inline std::vector<pp::Var>& StorageToPluginArg(StorageType& t) {
244 return t.output();
248 // A specialization of CallbackOutputTraits to provide the callback system the
249 // information on how to handle vectors of pp::DirectoryEntry_Dev. This converts
250 // PP_DirectoryEntry_Dev to pp::DirectoryEntry_Dev when passing to the plugin.
251 template<>
252 struct CallbackOutputTraits< std::vector<pp::DirectoryEntry_Dev> > {
253 typedef PP_ArrayOutput APIArgType;
254 typedef DirectoryEntryArrayOutputAdapterWithStorage StorageType;
256 static inline APIArgType StorageToAPIArg(StorageType& t) {
257 return t.pp_array_output();
260 static inline std::vector<pp::DirectoryEntry_Dev>& StorageToPluginArg(
261 StorageType& t) {
262 return t.output();
266 } // namespace internal
267 } // namespace pp
269 #endif // PPAPI_CPP_OUTPUT_TRAITS_H_