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_FROM_VAR_CONVERTOR_H_
6 #define PPAPI_CPP_EXTENSIONS_FROM_VAR_CONVERTOR_H_
10 #include "ppapi/c/pp_var.h"
11 #include "ppapi/cpp/extensions/optional.h"
12 #include "ppapi/cpp/logging.h"
13 #include "ppapi/cpp/var.h"
14 #include "ppapi/cpp/var_array.h"
15 #include "ppapi/cpp/var_array_buffer.h"
16 #include "ppapi/cpp/var_dictionary.h"
23 class FromVarConverterBase
{
25 T
& value() { return value_
; }
28 FromVarConverterBase() : value_() {
31 explicit FromVarConverterBase(const T
& value
) : value_(value
) {
34 ~FromVarConverterBase() {
41 class FromVarConverter
: public FromVarConverterBase
<T
> {
46 explicit FromVarConverter(const PP_Var
& var
) {
53 void Set(const PP_Var
& var
) {
54 bool succeeded
= FromVarConverterBase
<T
>::value_
.Populate(var
);
55 // Suppress unused variable warnings.
56 static_cast<void>(succeeded
);
62 class FromVarConverter
<Optional
<T
> >
63 : public FromVarConverterBase
<Optional
<T
> > {
68 explicit FromVarConverter(const PP_Var
& var
) {
75 void Set(const PP_Var
& var
) {
76 if (var
.type
== PP_VARTYPE_UNDEFINED
) {
77 FromVarConverterBase
<Optional
<T
> >::value_
.Reset();
79 FromVarConverter
<T
> converter(var
);
80 FromVarConverterBase
<Optional
<T
> >::value_
= converter
.value();
86 class FromVarConverter
<bool> : public FromVarConverterBase
<bool> {
91 explicit FromVarConverter(const PP_Var
& var
) {
98 void Set(const PP_Var
& var
) {
99 FromVarConverterBase
<bool>::value_
= Var(var
).AsBool();
104 class FromVarConverter
<int32_t> : public FromVarConverterBase
<int32_t> {
109 explicit FromVarConverter(const PP_Var
& var
) {
113 ~FromVarConverter() {
116 void Set(const PP_Var
& var
) {
117 FromVarConverterBase
<int32_t>::value_
= Var(var
).AsInt();
122 class FromVarConverter
<double> : public FromVarConverterBase
<double> {
127 explicit FromVarConverter(const PP_Var
& var
) {
131 ~FromVarConverter() {
134 void Set(const PP_Var
& var
) {
135 FromVarConverterBase
<double>::value_
= Var(var
).AsDouble();
140 class FromVarConverter
<std::string
> : public FromVarConverterBase
<std::string
> {
145 explicit FromVarConverter(const PP_Var
& var
) {
149 ~FromVarConverter() {
152 void Set(const PP_Var
& var
) {
153 FromVarConverterBase
<std::string
>::value_
= Var(var
).AsString();
158 class FromVarConverter
<Var
> : public FromVarConverterBase
<Var
> {
163 explicit FromVarConverter(const PP_Var
& var
) {
167 ~FromVarConverter() {
170 void Set(const PP_Var
& var
) {
171 FromVarConverterBase
<Var
>::value_
= Var(var
);
176 class FromVarConverter
<VarArray
>
177 : public FromVarConverterBase
<VarArray
> {
182 explicit FromVarConverter(const PP_Var
& var
) {
186 ~FromVarConverter() {
189 void Set(const PP_Var
& var
) {
190 FromVarConverterBase
<VarArray
>::value_
= Var(var
);
195 class FromVarConverter
<VarDictionary
>
196 : public FromVarConverterBase
<VarDictionary
> {
201 explicit FromVarConverter(const PP_Var
& var
) {
205 ~FromVarConverter() {
208 void Set(const PP_Var
& var
) {
209 FromVarConverterBase
<VarDictionary
>::value_
= Var(var
);
214 class FromVarConverter
<VarArrayBuffer
>
215 : public FromVarConverterBase
<VarArrayBuffer
> {
220 explicit FromVarConverter(const PP_Var
& var
) {
224 ~FromVarConverter() {
227 void Set(const PP_Var
& var
) {
228 FromVarConverterBase
<VarArrayBuffer
>::value_
= Var(var
);
232 } // namespace internal
236 #endif // PPAPI_CPP_EXTENSIONS_FROM_VAR_CONVERTOR_H_