Android WebView: build debug/release based on variant.
[chromium-blink-merge.git] / ppapi / cpp / extensions / ext_output_traits.h
blob08dc744c47d87e2f4ab53c50b2c0c2da33686df9
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_
8 #include <vector>
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"
17 namespace pp {
18 namespace ext {
19 namespace internal {
21 template <class T>
22 class VarOutputAdapterWithStorage {
23 public:
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_; }
33 T& output() {
34 Var auto_release(PASS_REF, pp_var_);
35 converter_.Set(pp_var_);
36 pp_var_ = PP_MakeUndefined();
37 return converter_.value();
40 private:
41 PP_Var pp_var_;
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*.
57 template <class T>
58 struct ExtCallbackOutputTraits {
59 typedef PP_Var* APIArgType;
60 typedef VarOutputAdapterWithStorage<T> StorageType;
62 static inline APIArgType StorageToAPIArg(StorageType& t) {
63 return &t.pp_var();
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) {
69 return t.output();
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.
77 template <class T>
78 class ArrayVarOutputAdapterWithStorage {
79 public:
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();
94 if (var.is_array()) {
95 VarArray array(var);
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_;
108 private:
109 PP_Var pp_var_;
110 std::vector<T> output_storage_;
112 // Disallow copying and assignment.
113 ArrayVarOutputAdapterWithStorage(const ArrayVarOutputAdapterWithStorage<T>&);
114 ArrayVarOutputAdapterWithStorage<T>& operator=(
115 const ArrayVarOutputAdapterWithStorage<T>&);
118 template <class T>
119 struct ExtCallbackOutputTraits< std::vector<T> > {
120 typedef PP_Var* APIArgType;
121 typedef ArrayVarOutputAdapterWithStorage<T> StorageType;
123 static inline APIArgType StorageToAPIArg(StorageType& t) {
124 return &t.pp_var();
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) {
130 return t.output();
134 } // namespace internal
135 } // namespace ext
136 } // namespace pp
138 #endif // PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_