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