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

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: 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
« no previous file with comments | « no previous file | ppapi/cpp/array_output.cc » ('j') | ppapi/cpp/array_output.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
49 PP_ArrayOutput* pp_array_output() { return &pp_array_output_; }
50
51 protected:
52 virtual void* GetDataBuffer(uint32_t element_count,
53 uint32_t element_size) = 0;
54
55 private:
56 static void* GetDataBufferThunk(void* user_data,
57 uint32_t element_count,
58 uint32_t element_size);
59
60 PP_ArrayOutput pp_array_output_;
61 };
62
63 // This adapter provides functionality for implementing a PP_ArrayOutput
64 // structure as writing to a given vector object.
65 //
66 // This is generally used internally in the C++ wrapper objects to
67 // write into an output parameter supplied by the plugin. If the element size
68 // that the browser is writing does not match the size of the type we're using
69 // this will assert and return NULL (which will cause the browser to fail the
70 // call).
71 //
72 // Example that allows the browser to write into a given vector:
73 // void DoFoo(std::vector<int>* results) {
74 // ArrayOutputAdapter<int> adapter(results);
75 // ppb_foo->DoFoo(adapter.pp_array_output());
76 // }
77 template<typename T>
78 class ArrayOutputAdapter : public ArrayOutputAdapterBase {
79 public:
80 ArrayOutputAdapter(std::vector<T>* output) : output_(output) {}
81
82 protected:
83 // Two-step init for the "with storage" version below.
84 ArrayOutputAdapter() : output_(NULL) {}
85 void set_output(std::vector<T>* output) { output_ = output; }
86
87 // ArrayOutputAdapterBase implementation.
88 virtual void* GetDataBuffer(uint32_t element_count,
89 uint32_t element_size) {
90 PP_DCHECK(element_count > 0);
dmichael (off chromium) 2012/03/09 18:23:27 can't you be called with 0 elements? (here and els
91 PP_DCHECK(element_size == sizeof(T));
92 if (element_size != sizeof(T))
93 return NULL;
94 output_->resize(element_count);
95 return &(*output_)[0];
96 }
97
98 private:
99 std::vector<T>* output_;
100 };
101
102 // This adapter provides functionality for implementing a PP_ArrayOutput
103 // structure as writing resources to a given vector object.
104 //
105 // When returning an array of resources, the browser will write PP_Resources
106 // via a PP_ArrayOutput. This code will automatically convert the PP_Resources
107 // to the given wrapper type, (as long as that wrapper type supports the
108 // correct constructor). The ownership of the resources that the browser passed
109 // to us will be transferred to the C++ wrapper object.
110 //
111 // Conversion of the PP_Resources to the C++ wrapper object occurs in the
112 // destructor. This object is intended to be used on the stack in a C++ wrapper
113 // object for a call.
114 //
115 // Example:
116 // void GetFiles(std::vector<pp::FileRef>* results) {
117 // ResourceArrayOutputAdapter<pp::FileRef> adapter(results);
118 // ppb_foo->DoFoo(adapter.pp_array_output());
119 // }
120 template<typename T>
121 class ResourceArrayOutputAdapter : public ArrayOutputAdapterBase {
122 public:
123 ResourceArrayOutputAdapter(std::vector<T>* output) : output_(output) {
dmichael (off chromium) 2012/03/09 18:23:27 explicit (here and other 1-arg constructors)
124 output_->resize(0);
125 }
126 ~ResourceArrayOutputAdapter() {
dmichael (off chromium) 2012/03/09 18:23:27 Considering you are using virtual methods here, I
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_count > 0);
139 PP_DCHECK(element_size == sizeof(PP_Resource));
140 if (element_size != sizeof(PP_Resource))
141 return NULL;
142 intermediate_output_.resize(element_count);
143 return &intermediate_output_[0];
144 }
145
146 private:
147 std::vector<PP_Resource> intermediate_output_;
148 std::vector<T>* output_;
149 };
150
151 // This adapter is like the ArrayOutputAdapter except that it also contains
152 // the underlying std::vector that will be populated (rather than writing it to
153 // an object passed into the constructor).
154 //
155 // This is used by the CompletionCallbackFactory system to collect the output
156 // parameters from an async function call. The collected data is then passed to
157 // the plugins callback function.
158 //
159 // You can also use it directly if you want to have an array output and aren't
160 // using the CompletionCallbackFactory. For example, if you're calling a
161 // PPAPI function DoFoo that takes a PP_OutputArray that is supposed to be
162 // writing integers, do this:
163 //
164 // ArrayOutputAdapterWithStorage<int> adapter;
165 // ppb_foo->DoFoo(adapter.pp_output_array());
166 // std::vector<int> result = adapter.output().
dmichael (off chromium) 2012/03/09 18:23:27 Looks like an unfinished line? I still think some
167 template<typename T>
168 class ArrayOutputAdapterWithStorage : public ArrayOutputAdapter<T> {
169 public:
170 ArrayOutputAdapterWithStorage() {
171 set_output(&output_storage_);
172 }
173 ArrayOutputAdapterWithStorage(const ArrayOutputAdapterWithStorage<T>& other) {
dmichael (off chromium) 2012/03/09 18:23:27 Do you really need a copy constructor (and assignm
174 set_output(&output_storage_);
175 }
176
177 ArrayOutputAdapterWithStorage<T>& operator=(
178 const ArrayOutputAdapterWithStorage<T>& other) {
179 set_output(&output_storage_);
180 }
181
182 std::vector<T>& output() { return output_storage_; }
183
184 private:
185 std::vector<T> output_storage_;
186 };
187
188 // This adapter is like the ArrayOutputAdapterWithStorage except this
189 // additionally converts PP_Var structs to pp::Var objects.
190 //
191 // You can also use it directly if you want to have an array output and aren't
192 // using the CompletionCallbackFactory. For example, if you're calling a
193 // PPAPI function GetVars that takes a PP_OutputArray that is supposed to be
194 // writing PP_Vars, do this:
195 //
196 // VarArrayOutputAdapterWithStorage adapter;
197 // ppb_foo->GetVars(adapter.pp_output_array());
198 // std::vector<pp::Var> result = adapter.output().
dmichael (off chromium) 2012/03/09 18:23:27 ditto on the example
brettw 2012/03/11 05:15:42 I just changed these to const refs.
dmichael (off chromium) 2012/03/13 05:04:04 That's fine. I think non-const ref is also fine he
199 //
200 // Thie one is non-inline since it's not templatized.
dmichael (off chromium) 2012/03/09 18:23:27 nit: Thie->This
201 class VarArrayOutputAdapterWithStorage : public ArrayOutputAdapter<PP_Var> {
202 public:
203 VarArrayOutputAdapterWithStorage();
204 VarArrayOutputAdapterWithStorage(
205 const VarArrayOutputAdapterWithStorage& other);
206 VarArrayOutputAdapterWithStorage& operator=(
207 const VarArrayOutputAdapterWithStorage& other);
208
209 // Returns the final array of resource objects, converting the PP_Vars
210 // written by the browser to pp::Var objects.
211 //
212 // This function should only be called once or we would end up converting
213 // the array more than once, which would mess up the refcounting.
214 std::vector<Var>& output();
215
216 private:
217 // The browser will write the PP_Vars into this array.
218 std::vector<PP_Var> temp_storage_;
219
220 // When asked for the output, the resources above will be converted to the
221 // C++ resource objects in this array for passing to the calling code.
222 std::vector<Var> output_storage_;
223 };
224
225 // This adapter is like the ArrayOutputAdapterWithStorage except this
226 // additionally converts PP_Resources to C++ wrapper objects of the given type.
227 //
228 // You can also use it directly if you want to have an array output and aren't
229 // using the CompletionCallbackFactory. For example, if you're calling a
230 // PPAPI function GetFiles that takes a PP_OutputArray that is supposed to be
231 // writing PP_Resources cooresponding to FileRefs, do this:
232 //
233 // ResourceArrayOutputAdapterWithStorage<FileRef> adapter;
234 // ppb_foo->GetFiles(adapter.pp_output_array());
235 // std::vector<FileRef> result = adapter.output().
236 template<typename T>
237 class ResourceArrayOutputAdapterWithStorage
238 : public ArrayOutputAdapter<PP_Resource> {
239 public:
240 ResourceArrayOutputAdapterWithStorage() {
241 set_output(&temp_storage_);
242 }
243 ResourceArrayOutputAdapterWithStorage(
244 const ResourceArrayOutputAdapterWithStorage<T>& other) {
245 set_output(&temp_storage_);
246 }
247
248 ResourceArrayOutputAdapterWithStorage<T>& operator=(
249 const ResourceArrayOutputAdapterWithStorage<T>& other) {
250 set_output(&temp_storage_);
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 #endif // PPAPI_CPP_ARRAY_OUTPUT_H_
OLDNEW
« no previous file with comments | « no previous file | ppapi/cpp/array_output.cc » ('j') | ppapi/cpp/array_output.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698