Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: ppapi/cpp/array_output.h

Issue 9651002: Add C++ wrappers for output parameters. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: With swapping Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 void swap(ArrayOutputAdapter<T>& other) {
84 std::swap(output_, other.output_);
85 }
86
87 protected:
88 // Two-step init for the "with storage" version below.
89 ArrayOutputAdapter() : output_(NULL) {}
90 void set_output(std::vector<T>* output) { output_ = output; }
91
92 // ArrayOutputAdapterBase implementation.
93 virtual void* GetDataBuffer(uint32_t element_count, uint32_t element_size) {
94 PP_DCHECK(element_size == sizeof(T));
95 if (element_size != sizeof(T))
96 return NULL;
97 output_->resize(element_count);
98 return &(*output_)[0];
99 }
100
101 private:
102 std::vector<T>* output_;
103 };
104
105 // This adapter provides functionality for implementing a PP_ArrayOutput
106 // structure as writing resources to a given vector object.
107 //
108 // When returning an array of resources, the browser will write PP_Resources
109 // via a PP_ArrayOutput. This code will automatically convert the PP_Resources
110 // to the given wrapper type, (as long as that wrapper type supports the
111 // correct constructor). The ownership of the resources that the browser passed
112 // to us will be transferred to the C++ wrapper object.
113 //
114 // Conversion of the PP_Resources to the C++ wrapper object occurs in the
115 // destructor. This object is intended to be used on the stack in a C++ wrapper
116 // object for a call.
117 //
118 // Example:
119 // void GetFiles(std::vector<pp::FileRef>* results) {
120 // ResourceArrayOutputAdapter<pp::FileRef> adapter(results);
121 // ppb_foo->DoFoo(adapter.pp_array_output());
122 // }
123 template<typename T>
124 class ResourceArrayOutputAdapter : public ArrayOutputAdapterBase {
125 public:
126 explicit ResourceArrayOutputAdapter(std::vector<T>* output)
127 : output_(output) {
128 output_->resize(0);
129 }
130 ~ResourceArrayOutputAdapter() {
dmichael (off chromium) 2012/03/13 05:04:04 style nit: virtual
131 ConvertPPResourceArrayToObjects(PASS_REF, intermediate_output_, output_);
132 }
133
134 void swap(ResourceArrayOutputAdapter<T>& other) {
135 intermediate_output_.swap(other.intermediate_output_);
136 std::swap(output_, other.output_);
137 }
138
139 protected:
140 // Two-step init for the "with storage" version below.
141 ResourceArrayOutputAdapter() : output_(NULL) {}
142 void set_output(T* output) { output_ = output; }
143
144 // ArrayOutputAdapterBase implementation.
145 virtual void* GetDataBuffer(uint32_t element_count,
146 uint32_t element_size) {
147 PP_DCHECK(element_size == sizeof(PP_Resource));
148 if (element_size != sizeof(PP_Resource))
149 return NULL;
150 intermediate_output_.resize(element_count);
151 return &intermediate_output_[0];
152 }
153
154 private:
155 std::vector<PP_Resource> intermediate_output_;
156 std::vector<T>* output_;
157 };
158
159 // This adapter is like the ArrayOutputAdapter except that it also contains
160 // the underlying std::vector that will be populated (rather than writing it to
161 // an object passed into the constructor).
162 //
163 // This is used by the CompletionCallbackFactory system to collect the output
164 // parameters from an async function call. The collected data is then passed to
165 // the plugins callback function.
166 //
167 // You can also use it directly if you want to have an array output and aren't
168 // using the CompletionCallbackFactory. For example, if you're calling a
169 // PPAPI function DoFoo that takes a PP_OutputArray that is supposed to be
170 // writing integers, do this:
171 //
172 // ArrayOutputAdapterWithStorage<int> adapter;
173 // ppb_foo->DoFoo(adapter.pp_output_array());
174 // const std::vector<int>& result = adapter.output();
175 template<typename T>
176 class ArrayOutputAdapterWithStorage : public ArrayOutputAdapter<T> {
177 public:
178 ArrayOutputAdapterWithStorage() {
179 set_output(&output_storage_);
180 }
181
182 void swap(ArrayOutputAdapterWithStorage<T>& other) {
183 output_storage_.swap(other.output_storage_);
184 ArrayOutputAdapter<T>::swap(other);
185 }
186
187 std::vector<T>& output() { return output_storage_; }
188
189 private:
190 std::vector<T> output_storage_;
191 };
192
193 // This adapter is like the ArrayOutputAdapterWithStorage except this
194 // additionally converts PP_Var structs to pp::Var objects.
195 //
196 // You can also use it directly if you want to have an array output and aren't
197 // using the CompletionCallbackFactory. For example, if you're calling a
198 // PPAPI function GetVars that takes a PP_OutputArray that is supposed to be
199 // writing PP_Vars, do this:
200 //
201 // VarArrayOutputAdapterWithStorage adapter;
202 // ppb_foo->GetVars(adapter.pp_output_array());
203 // const std::vector<pp::Var>& result = adapter.output().
204 //
205 // This one is non-inline since it's not templatized.
206 class VarArrayOutputAdapterWithStorage : public ArrayOutputAdapter<PP_Var> {
207 public:
208 VarArrayOutputAdapterWithStorage();
209
210 void swap(VarArrayOutputAdapterWithStorage& other);
211
212 // Returns the final array of resource objects, converting the PP_Vars
213 // written by the browser to pp::Var objects.
214 //
215 // This function should only be called once or we would end up converting
216 // the array more than once, which would mess up the refcounting.
217 std::vector<Var>& output();
218
219 private:
220 // The browser will write the PP_Vars into this array.
221 std::vector<PP_Var> temp_storage_;
222
223 // When asked for the output, the resources above will be converted to the
224 // C++ resource objects in this array for passing to the calling code.
225 std::vector<Var> output_storage_;
226 };
227
228 // This adapter is like the ArrayOutputAdapterWithStorage except this
229 // additionally converts PP_Resources to C++ wrapper objects of the given type.
230 //
231 // You can also use it directly if you want to have an array output and aren't
232 // using the CompletionCallbackFactory. For example, if you're calling a
233 // PPAPI function GetFiles that takes a PP_OutputArray that is supposed to be
234 // writing PP_Resources cooresponding to FileRefs, do this:
235 //
236 // ResourceArrayOutputAdapterWithStorage<FileRef> adapter;
237 // ppb_foo->GetFiles(adapter.pp_output_array());
238 // std::vector<FileRef> result = adapter.output().
239 template<typename T>
240 class ResourceArrayOutputAdapterWithStorage
241 : public ArrayOutputAdapter<PP_Resource> {
242 public:
243 ResourceArrayOutputAdapterWithStorage() {
244 set_output(&temp_storage_);
245 }
246
247 void swap(ResourceArrayOutputAdapterWithStorage<T>& other) {
248 temp_storage_.swap(other.temp_storage_);
249 output_storage_.swap(other.output_storage_);
250 ArrayOutputAdapter<PP_Resource>::swap(other);
251 }
252
253 // Returns the final array of resource objects, converting the PP_Resources
254 // written by the browser to resource objects.
255 //
256 // This function should only be called once or we would end up converting
257 // the array more than once, which would mess up the refcounting.
258 std::vector<T>& output() {
259 PP_DCHECK(output_storage_.empty());
260
261 ConvertPPResourceArrayToObjects(PASS_REF, temp_storage_, &output_storage_);
262 temp_storage_.clear();
263 return output_storage_;
264 }
265
266 private:
267 // The browser will write the PP_Resources into this array.
268 std::vector<PP_Resource> temp_storage_;
269
270 // When asked for the output, the resources above will be converted to the
271 // C++ resource objects in this array for passing to the calling code.
272 std::vector<T> output_storage_;
273 };
274
275 } // namespace pp
276
277 namespace std {
278
279 template<typename T>
280 void swap(pp::ArrayOutputAdapter<T>& a, pp::ArrayOutputAdapter<T>& b) {
281 a.swap(b);
282 }
283
284 template<typename T>
285 void swap(pp::ResourceArrayOutputAdapter<T>& a,
286 pp::ResourceArrayOutputAdapter<T>& b) {
287 a.swap(b);
288 }
289
290 template<typename T>
291 void swap(pp::ArrayOutputAdapterWithStorage<T>& a,
292 pp::ArrayOutputAdapterWithStorage<T>& b) {
293 a.swap(b);
294 }
295
296 template<>
297 void swap(pp::VarArrayOutputAdapterWithStorage& a,
298 pp::VarArrayOutputAdapterWithStorage& b) {
299 a.swap(b);
300 }
301
302 template<typename T>
303 void swap(pp::ResourceArrayOutputAdapterWithStorage<T>& a,
304 pp::ResourceArrayOutputAdapterWithStorage<T>& b) {
305 a.swap(b);
306 }
307
308 } // namespace std
309
310 #endif // PPAPI_CPP_ARRAY_OUTPUT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698