1 // Copyright (c) 2013 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.
5 #ifndef PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_
6 #define PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_
10 #include "ppapi/c/pp_var.h"
11 #include "ppapi/cpp/extensions/from_var_converter.h"
12 #include "ppapi/cpp/logging.h"
13 #include "ppapi/cpp/pass_ref.h"
14 #include "ppapi/cpp/var.h"
15 #include "ppapi/cpp/var_array.h"
22 class VarOutputAdapterWithStorage
{
24 VarOutputAdapterWithStorage() : pp_var_(PP_MakeUndefined()) {
27 ~VarOutputAdapterWithStorage() {
28 PP_DCHECK(pp_var_
.type
== PP_VARTYPE_UNDEFINED
);
31 PP_Var
& pp_var() { return pp_var_
; }
34 Var
auto_release(PASS_REF
, pp_var_
);
35 converter_
.Set(pp_var_
);
36 pp_var_
= PP_MakeUndefined();
37 return converter_
.value();
42 FromVarConverter
<T
> converter_
;
44 // Disallow copying and assignment.
45 VarOutputAdapterWithStorage(const VarOutputAdapterWithStorage
<T
>&);
46 VarOutputAdapterWithStorage
<T
>& operator=(
47 const VarOutputAdapterWithStorage
<T
>&);
50 // ExtCallbackOutputTraits is used with ExtCompletionCallbackWithOutput. Unlike
51 // pp::internal::CallbackOutputTraits, it always uses PP_Var* as output
52 // parameter type to interact with the browser.
54 // For example, CompletionCallbackWithOutput<double> (using
55 // pp::internal::CallbackOutputTraits) uses double* as the output parameter
56 // type; while ExtCompletionCallbackWithOutput<double> uses PP_Var*.
58 struct ExtCallbackOutputTraits
{
59 typedef PP_Var
* APIArgType
;
60 typedef VarOutputAdapterWithStorage
<T
> StorageType
;
62 static inline APIArgType
StorageToAPIArg(StorageType
& t
) {
66 // This must be called exactly once to consume the one PP_Var reference
67 // assigned to us by the browser.
68 static inline T
& StorageToPluginArg(StorageType
& t
) {
73 // This class provides storage for a PP_Var and a vector of objects which are
74 // of type T. The PP_Var is used as an output parameter to receive an array var
75 // from the browser. Each element in the array var is converted to a T object,
76 // using FromVarConverter, and stores in the vector.
78 class ArrayVarOutputAdapterWithStorage
{
80 ArrayVarOutputAdapterWithStorage() : pp_var_(PP_MakeUndefined()) {
83 ~ArrayVarOutputAdapterWithStorage() {
84 PP_DCHECK(pp_var_
.type
== PP_VARTYPE_UNDEFINED
);
87 PP_Var
& pp_var() { return pp_var_
; }
89 std::vector
<T
>& output() {
90 PP_DCHECK(output_storage_
.empty());
92 Var
var(PASS_REF
, pp_var_
);
93 pp_var_
= PP_MakeUndefined();
97 uint32_t length
= array
.GetLength();
98 output_storage_
.reserve(length
);
99 for (uint32_t i
= 0; i
< length
; ++i
) {
100 FromVarConverter
<T
> converter(array
.Get(i
).pp_var());
101 output_storage_
.push_back(converter
.value());
105 return output_storage_
;
110 std::vector
<T
> output_storage_
;
112 // Disallow copying and assignment.
113 ArrayVarOutputAdapterWithStorage(const ArrayVarOutputAdapterWithStorage
<T
>&);
114 ArrayVarOutputAdapterWithStorage
<T
>& operator=(
115 const ArrayVarOutputAdapterWithStorage
<T
>&);
119 struct ExtCallbackOutputTraits
< std::vector
<T
> > {
120 typedef PP_Var
* APIArgType
;
121 typedef ArrayVarOutputAdapterWithStorage
<T
> StorageType
;
123 static inline APIArgType
StorageToAPIArg(StorageType
& t
) {
127 // This must be called exactly once to consume the one PP_Var reference
128 // assigned to us by the browser.
129 static inline std::vector
<T
>& StorageToPluginArg(StorageType
& t
) {
134 } // namespace internal
138 #endif // PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_