| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ | 5 #ifndef PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ |
| 6 #define PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ | 6 #define PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // call since one output function should only be issued once. | 47 // call since one output function should only be issued once. |
| 48 // | 48 // |
| 49 // THIS IS DESIGNED FOR POD ONLY. For the case of resources, for example, we | 49 // THIS IS DESIGNED FOR POD ONLY. For the case of resources, for example, we |
| 50 // want to transfer a reference only on success. Likewise, if you have a | 50 // want to transfer a reference only on success. Likewise, if you have a |
| 51 // structure of PP_Vars or a struct that contains a PP_Resource, we need to | 51 // structure of PP_Vars or a struct that contains a PP_Resource, we need to |
| 52 // make sure that the right thing happens with the ref on success and failure. | 52 // make sure that the right thing happens with the ref on success and failure. |
| 53 template <typename T> | 53 template <typename T> |
| 54 bool StoreArray(const T* input, uint32_t count) { | 54 bool StoreArray(const T* input, uint32_t count) { |
| 55 // Always call the alloc function, even on 0 array size. | 55 // Always call the alloc function, even on 0 array size. |
| 56 void* dest = pp_array_output_.GetDataBuffer( | 56 void* dest = pp_array_output_.GetDataBuffer( |
| 57 pp_array_output_.user_data, | 57 pp_array_output_.user_data, count, sizeof(T)); |
| 58 count, | |
| 59 sizeof(T)); | |
| 60 | 58 |
| 61 // Regardless of success, we clear the output to prevent future calls on | 59 // Regardless of success, we clear the output to prevent future calls on |
| 62 // this same output object. | 60 // this same output object. |
| 63 Reset(); | 61 Reset(); |
| 64 | 62 |
| 65 if (count == 0) | 63 if (count == 0) |
| 66 return true; // Allow plugin to return NULL on 0 elements. | 64 return true; // Allow plugin to return NULL on 0 elements. |
| 67 if (!dest) | 65 if (!dest) |
| 68 return false; | 66 return false; |
| 69 | 67 |
| 70 if (input) | 68 if (input) |
| 71 memcpy(dest, input, sizeof(T) * count); | 69 memcpy(dest, input, sizeof(T) * count); |
| 72 return true; | 70 return true; |
| 73 } | 71 } |
| 74 | 72 |
| 75 // Copies the given array/vector of data to the plugin output array. See | 73 // Copies the given array/vector of data to the plugin output array. See |
| 76 // comment of StoreArray() for detail. | 74 // comment of StoreArray() for detail. |
| 77 template<typename T> | 75 template <typename T> |
| 78 bool StoreVector(const std::vector<T>& input) { | 76 bool StoreVector(const std::vector<T>& input) { |
| 79 return StoreArray(input.size() ? &input[0] : NULL, input.size()); | 77 return StoreArray(input.size() ? &input[0] : NULL, input.size()); |
| 80 } | 78 } |
| 81 | 79 |
| 82 // Stores the given vector of resources as PP_Resources to the output vector, | 80 // Stores the given vector of resources as PP_Resources to the output vector, |
| 83 // adding one reference to each. | 81 // adding one reference to each. |
| 84 // | 82 // |
| 85 // On failure this returns false, nothing will be copied, and the resource | 83 // On failure this returns false, nothing will be copied, and the resource |
| 86 // refcounts will be unchanged. In either case, the object will become | 84 // refcounts will be unchanged. In either case, the object will become |
| 87 // is_null() immediately after the call since one output function should only | 85 // is_null() immediately after the call since one output function should only |
| 88 // be issued once. | 86 // be issued once. |
| 89 // | 87 // |
| 90 // Note: potentially this could be a template in case you have a vector of | 88 // Note: potentially this could be a template in case you have a vector of |
| 91 // FileRef objects, for example. However, this saves code since there's only | 89 // FileRef objects, for example. However, this saves code since there's only |
| 92 // one instantiation and is sufficient for now. | 90 // one instantiation and is sufficient for now. |
| 93 bool StoreResourceVector(const std::vector< scoped_refptr<Resource> >& input); | 91 bool StoreResourceVector(const std::vector<scoped_refptr<Resource> >& input); |
| 94 | 92 |
| 95 // Like the above version but takes an array of AddRef'ed PP_Resources. On | 93 // Like the above version but takes an array of AddRef'ed PP_Resources. On |
| 96 // storage failure, this will release each resource. | 94 // storage failure, this will release each resource. |
| 97 bool StoreResourceVector(const std::vector<PP_Resource>& input); | 95 bool StoreResourceVector(const std::vector<PP_Resource>& input); |
| 98 | 96 |
| 99 // Stores the given vector of vars as PP_Vars to the output vector, | 97 // Stores the given vector of vars as PP_Vars to the output vector, |
| 100 // adding one reference to each. | 98 // adding one reference to each. |
| 101 // | 99 // |
| 102 // On failure this returns false, nothing will be copied, and the var | 100 // On failure this returns false, nothing will be copied, and the var |
| 103 // refcounts will be unchanged. In either case, the object will become | 101 // refcounts will be unchanged. In either case, the object will become |
| 104 // is_null() immediately after the call since one output function should only | 102 // is_null() immediately after the call since one output function should only |
| 105 // be issued once. | 103 // be issued once. |
| 106 bool StoreVarVector(const std::vector< scoped_refptr<Var> >& input); | 104 bool StoreVarVector(const std::vector<scoped_refptr<Var> >& input); |
| 107 | 105 |
| 108 // Like the above version but takes an array of AddRef'ed PP_Vars. On | 106 // Like the above version but takes an array of AddRef'ed PP_Vars. On |
| 109 // storage failure, this will release each var. | 107 // storage failure, this will release each var. |
| 110 bool StoreVarVector(const std::vector<PP_Var>& input); | 108 bool StoreVarVector(const std::vector<PP_Var>& input); |
| 111 | 109 |
| 112 private: | 110 private: |
| 113 PP_ArrayOutput pp_array_output_; | 111 PP_ArrayOutput pp_array_output_; |
| 114 | 112 |
| 115 DISALLOW_COPY_AND_ASSIGN(ArrayWriter); | 113 DISALLOW_COPY_AND_ASSIGN(ArrayWriter); |
| 116 }; | 114 }; |
| 117 | 115 |
| 118 } // namespace ppapi | 116 } // namespace ppapi |
| 119 | 117 |
| 120 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ | 118 #endif // PPAPI_SHARED_IMPL_ARRAY_WRITER_H_ |
| OLD | NEW |