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_ARRAY_OUTPUT_H_ | |
6 #define PPAPI_CPP_ARRAY_OUTPUT_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "ppapi/c/pp_array_output.h" | |
11 #include "ppapi/c/pp_resource.h" | |
12 #include "ppapi/cpp/logging.h" | |
13 #include "ppapi/cpp/pass_ref.h" | |
14 #include "ppapi/cpp/var.h" | |
15 | |
16 namespace pp { | |
17 | |
18 // Converts the given array of PP_Resources into an array of the requested | |
19 // C++ resource types, passing ownership of a reference in the process. | |
20 // | |
21 // This is used to convert output arrays of resources that the browser has | |
22 // generated into the more convenient C++ wrappers for those resources. The | |
23 // initial "PassRef" parameter is there to emphasize what happens to the | |
24 // reference count of the input resource and to match the resource constructors | |
25 // that look the same. | |
26 template<typename ResourceObjectType> | |
27 inline void ConvertPPResourceArrayToObjects( | |
28 PassRef, | |
29 const std::vector<PP_Resource>& input, | |
30 std::vector<ResourceObjectType>* output) { | |
31 output->resize(0); | |
32 output->reserve(input.size()); | |
33 for (size_t i = 0; i < input.size(); i++) | |
34 output->push_back(ResourceObjectType(PASS_REF, input[i])); | |
35 } | |
36 | |
37 // Non-templatized base class for the array output conversion. It provides the | |
38 // C implementation of a PP_ArrayOutput whose callback function is implemented | |
39 // as a virtual call on a derived class. Do not use directly, use one of the | |
40 // derived classes below. | |
41 class ArrayOutputAdapterBase { | |
42 public: | |
43 ArrayOutputAdapterBase() { | |
44 pp_array_output_.GetDataBuffer = | |
45 &ArrayOutputAdapterBase::GetDataBufferThunk; | |
46 pp_array_output_.user_data = this; | |
47 } | |
48 virtual ~ArrayOutputAdapterBase() {} | |
49 | |
50 PP_ArrayOutput* pp_array_output() { return &pp_array_output_; } | |
51 | |
52 protected: | |
53 virtual void* GetDataBuffer(uint32_t element_count, | |
54 uint32_t element_size) = 0; | |
55 | |
56 private: | |
57 static void* GetDataBufferThunk(void* user_data, | |
58 uint32_t element_count, | |
59 uint32_t element_size); | |
60 | |
61 PP_ArrayOutput pp_array_output_; | |
62 }; | |
63 | |
64 // This adapter provides functionality for implementing a PP_ArrayOutput | |
65 // structure as writing to a given vector object. | |
66 // | |
67 // This is generally used internally in the C++ wrapper objects to | |
68 // write into an output parameter supplied by the plugin. If the element size | |
69 // that the browser is writing does not match the size of the type we're using | |
70 // this will assert and return NULL (which will cause the browser to fail the | |
71 // call). | |
72 // | |
73 // Example that allows the browser to write into a given vector: | |
74 // void DoFoo(std::vector<int>* results) { | |
75 // ArrayOutputAdapter<int> adapter(results); | |
76 // ppb_foo->DoFoo(adapter.pp_array_output()); | |
77 // } | |
78 template<typename T> | |
79 class ArrayOutputAdapter : public ArrayOutputAdapterBase { | |
80 public: | |
81 ArrayOutputAdapter(std::vector<T>* output) : output_(output) {} | |
82 | |
83 protected: | |
84 // Two-step init for the "with storage" version below. | |
85 ArrayOutputAdapter() : output_(NULL) {} | |
86 void set_output(std::vector<T>* output) { output_ = output; } | |
87 | |
88 // ArrayOutputAdapterBase implementation. | |
89 virtual void* GetDataBuffer(uint32_t element_count, uint32_t element_size) { | |
90 PP_DCHECK(element_size == sizeof(T)); | |
91 if (element_size != sizeof(T)) | |
92 return NULL; | |
93 output_->resize(element_count); | |
94 return &(*output_)[0]; | |
95 } | |
96 | |
97 private: | |
98 std::vector<T>* output_; | |
dmichael (off chromium)
2012/03/14 18:47:08
you should probably disallow copy & assign (at lea
| |
99 }; | |
100 | |
101 // This adapter provides functionality for implementing a PP_ArrayOutput | |
102 // structure as writing resources to a given vector object. | |
103 // | |
104 // When returning an array of resources, the browser will write PP_Resources | |
105 // via a PP_ArrayOutput. This code will automatically convert the PP_Resources | |
106 // to the given wrapper type, (as long as that wrapper type supports the | |
107 // correct constructor). The ownership of the resources that the browser passed | |
108 // to us will be transferred to the C++ wrapper object. | |
109 // | |
110 // Conversion of the PP_Resources to the C++ wrapper object occurs in the | |
111 // destructor. This object is intended to be used on the stack in a C++ wrapper | |
112 // object for a call. | |
113 // | |
114 // Example: | |
115 // void GetFiles(std::vector<pp::FileRef>* results) { | |
116 // ResourceArrayOutputAdapter<pp::FileRef> adapter(results); | |
117 // ppb_foo->DoFoo(adapter.pp_array_output()); | |
118 // } | |
119 template<typename T> | |
120 class ResourceArrayOutputAdapter : public ArrayOutputAdapterBase { | |
121 public: | |
122 explicit ResourceArrayOutputAdapter(std::vector<T>* output) | |
123 : output_(output) { | |
124 output_->resize(0); | |
125 } | |
126 virtual ~ResourceArrayOutputAdapter() { | |
127 ConvertPPResourceArrayToObjects(PASS_REF, intermediate_output_, output_); | |
128 } | |
129 | |
130 protected: | |
131 // Two-step init for the "with storage" version below. | |
132 ResourceArrayOutputAdapter() : output_(NULL) {} | |
133 void set_output(T* output) { output_ = output; } | |
134 | |
135 // ArrayOutputAdapterBase implementation. | |
136 virtual void* GetDataBuffer(uint32_t element_count, | |
137 uint32_t element_size) { | |
138 PP_DCHECK(element_size == sizeof(PP_Resource)); | |
139 if (element_size != sizeof(PP_Resource)) | |
140 return NULL; | |
141 intermediate_output_.resize(element_count); | |
142 return &intermediate_output_[0]; | |
143 } | |
144 | |
145 private: | |
146 std::vector<PP_Resource> intermediate_output_; | |
147 std::vector<T>* output_; | |
148 }; | |
149 | |
150 // This adapter is like the ArrayOutputAdapter except that it also contains | |
151 // the underlying std::vector that will be populated (rather than writing it to | |
152 // an object passed into the constructor). | |
153 // | |
154 // This is used by the CompletionCallbackFactory system to collect the output | |
155 // parameters from an async function call. The collected data is then passed to | |
156 // the plugins callback function. | |
157 // | |
158 // You can also use it directly if you want to have an array output and aren't | |
159 // using the CompletionCallbackFactory. For example, if you're calling a | |
160 // PPAPI function DoFoo that takes a PP_OutputArray that is supposed to be | |
161 // writing integers, do this: | |
162 // | |
163 // ArrayOutputAdapterWithStorage<int> adapter; | |
164 // ppb_foo->DoFoo(adapter.pp_output_array()); | |
165 // const std::vector<int>& result = adapter.output(); | |
166 template<typename T> | |
167 class ArrayOutputAdapterWithStorage : public ArrayOutputAdapter<T> { | |
168 public: | |
169 ArrayOutputAdapterWithStorage() { | |
170 set_output(&output_storage_); | |
171 } | |
172 | |
173 std::vector<T>& output() { return output_storage_; } | |
174 | |
175 private: | |
176 std::vector<T> output_storage_; | |
177 }; | |
178 | |
179 // This adapter is like the ArrayOutputAdapterWithStorage except this | |
180 // additionally converts PP_Var structs to pp::Var objects. | |
181 // | |
182 // You can also use it directly if you want to have an array output and aren't | |
183 // using the CompletionCallbackFactory. For example, if you're calling a | |
184 // PPAPI function GetVars that takes a PP_OutputArray that is supposed to be | |
185 // writing PP_Vars, do this: | |
186 // | |
187 // VarArrayOutputAdapterWithStorage adapter; | |
188 // ppb_foo->GetVars(adapter.pp_output_array()); | |
189 // const std::vector<pp::Var>& result = adapter.output(). | |
190 // | |
191 // This one is non-inline since it's not templatized. | |
192 class VarArrayOutputAdapterWithStorage : public ArrayOutputAdapter<PP_Var> { | |
193 public: | |
194 VarArrayOutputAdapterWithStorage(); | |
195 | |
196 // Returns the final array of resource objects, converting the PP_Vars | |
197 // written by the browser to pp::Var objects. | |
198 // | |
199 // This function should only be called once or we would end up converting | |
200 // the array more than once, which would mess up the refcounting. | |
201 std::vector<Var>& output(); | |
202 | |
203 private: | |
204 // The browser will write the PP_Vars into this array. | |
205 std::vector<PP_Var> temp_storage_; | |
206 | |
207 // When asked for the output, the resources above will be converted to the | |
208 // C++ resource objects in this array for passing to the calling code. | |
209 std::vector<Var> output_storage_; | |
210 }; | |
211 | |
212 // This adapter is like the ArrayOutputAdapterWithStorage except this | |
213 // additionally converts PP_Resources to C++ wrapper objects of the given type. | |
214 // | |
215 // You can also use it directly if you want to have an array output and aren't | |
216 // using the CompletionCallbackFactory. For example, if you're calling a | |
217 // PPAPI function GetFiles that takes a PP_OutputArray that is supposed to be | |
218 // writing PP_Resources cooresponding to FileRefs, do this: | |
219 // | |
220 // ResourceArrayOutputAdapterWithStorage<FileRef> adapter; | |
221 // ppb_foo->GetFiles(adapter.pp_output_array()); | |
222 // std::vector<FileRef> result = adapter.output(). | |
223 template<typename T> | |
224 class ResourceArrayOutputAdapterWithStorage | |
225 : public ArrayOutputAdapter<PP_Resource> { | |
226 public: | |
227 ResourceArrayOutputAdapterWithStorage() { | |
228 set_output(&temp_storage_); | |
229 } | |
230 | |
231 // Returns the final array of resource objects, converting the PP_Resources | |
232 // written by the browser to resource objects. | |
233 // | |
234 // This function should only be called once or we would end up converting | |
235 // the array more than once, which would mess up the refcounting. | |
236 std::vector<T>& output() { | |
237 PP_DCHECK(output_storage_.empty()); | |
238 | |
239 ConvertPPResourceArrayToObjects(PASS_REF, temp_storage_, &output_storage_); | |
240 temp_storage_.clear(); | |
241 return output_storage_; | |
242 } | |
243 | |
244 private: | |
245 // The browser will write the PP_Resources into this array. | |
246 std::vector<PP_Resource> temp_storage_; | |
247 | |
248 // When asked for the output, the resources above will be converted to the | |
249 // C++ resource objects in this array for passing to the calling code. | |
250 std::vector<T> output_storage_; | |
251 }; | |
252 | |
253 } // namespace pp | |
254 | |
255 #endif // PPAPI_CPP_ARRAY_OUTPUT_H_ | |
OLD | NEW |