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_SHARED_IMPL_ARRAY_WRITER_H_ | |
6 #define PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "ppapi/c/pp_array_output.h" | |
12 #include "ppapi/c/pp_resource.h" | |
13 #include "ppapi/c/pp_var.h" | |
14 #include "ppapi/shared_impl/ppapi_shared_export.h" | |
15 | |
16 namespace ppapi { | |
17 | |
18 class Resource; | |
19 | |
20 // Holds a PP_ArrayWriter and provides helper functions for writing arrays | |
21 // to it. It also handles 0-initialization of the raw C struct and attempts | |
22 // to prevent you from writing the array twice. | |
23 class PPAPI_SHARED_EXPORT ArrayWriter { | |
24 public: | |
25 ArrayWriter(); // Creates an is_null() object | |
26 ArrayWriter(const PP_ArrayOutput& output); | |
27 ~ArrayWriter(); | |
28 | |
29 bool is_valid() const { return !!pp_array_output_.GetDataBuffer; } | |
30 bool is_null() const { return !is_valid(); } | |
31 | |
32 void set_pp_array_output(const PP_ArrayOutput& output) { | |
33 pp_array_output_ = output; | |
34 } | |
35 | |
36 // Sets the array output back to its is_null() state. | |
37 void Reset(); | |
38 | |
39 // Copies the given vector of data to the plugin output array. | |
40 // | |
41 // Returns true on success, false if the plugin reported allocation failure. | |
42 // In either case, the object will become is_null() immediately after the | |
43 // call since one output function should only be issued once. | |
44 // | |
45 // THIS IS DESIGNED FOR POD ONLY. For the case of resources, for example, we | |
46 // want to transfer a reference only on success. Likewise, if you have a | |
47 // structure of PP_Vars or a struct that contains a PP_Resource, we need to | |
48 // make sure that the right thing happens with the ref on success and failure. | |
49 template<typename T> | |
50 bool StoreVector(const std::vector<T>& input) { | |
51 // Always call the alloc function, even on 0 array size. | |
52 void* dest = pp_array_output_.GetDataBuffer( | |
53 output.user_data, | |
54 static_cast<uint32_t>(input.size()), | |
55 sizeof(PP_Resource)); | |
viettrungluu
2012/03/26 16:55:19
sizeof(T)
| |
56 | |
57 // Regardless of success, we clear the output to prevent future calls on | |
58 // this same output object. | |
59 Reset(); | |
60 | |
61 if (input.empty()) | |
62 return true; // Allow plugin to return NULL on 0 elements. | |
63 if (!dest) | |
64 return false; | |
65 | |
66 memcpy(dest, &input[0], sizeof(T) * input.size()); | |
viettrungluu
2012/03/26 16:55:19
include <string.h>
| |
67 return true; | |
68 } | |
69 | |
70 // Stores the given vector of resources as PP_Resources to the output vector, | |
71 // adding one reference to each. | |
72 // | |
73 // On failure this returns false, nothing will be copied, and the resource | |
74 // refcounts will be unchanged. In either case, the object will become | |
75 // is_null() immediately after the call since one output function should only | |
76 // be issued once. | |
77 // | |
78 // Note: potentially this could be a template in case you have a vector of | |
79 // FileRef objects, for example. However, this saves code since there's only | |
80 // one instantiation and is sufficient for now. | |
81 bool StoreResourceVector( | |
82 const std::vector< scoped_refptr<Resource> >& input); | |
83 | |
84 // Like the above version but takes an array of AddRed'ed PP_Resources. On | |
85 // storage failure, this will release each resource. | |
86 bool StoreResourceVector(const std::vector<PP_Resource>& input); | |
87 | |
88 private: | |
89 PP_ArrayOutput pp_array_output_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(ArrayWriter); | |
92 }; | |
93 | |
94 } // namespace ppapi | |
95 | |
96 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ | |
OLD | NEW |