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. 4 5 #ifndef PPAPI_CPP_EXTENSIONS_FROM_VAR_CONVERTOR_H_ 6 #define PPAPI_CPP_EXTENSIONS_FROM_VAR_CONVERTOR_H_ 7 8 #include <string> 9 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" 17 18 namespace pp { 19 namespace ext { 20 namespace internal { 21 22 template <class T> 23 class FromVarConverterBase { 24 public: 25 T& value() { return value_; } 26 27 protected: 28 FromVarConverterBase() : value_() { 29 } 30 31 explicit FromVarConverterBase(const T& value) : value_(value) { 32 } 33 34 ~FromVarConverterBase() { 35 } 36 37 T value_; 38 }; 39 40 template <class T> 41 class FromVarConverter : public FromVarConverterBase<T> { 42 public: 43 FromVarConverter() { 44 } 45 46 explicit FromVarConverter(const PP_Var& var) { 47 Set(var); 48 } 49 50 ~FromVarConverter() { 51 } 52 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); 57 PP_DCHECK(succeeded); 58 } 59 }; 60 61 template <class T> 62 class FromVarConverter<Optional<T> > 63 : public FromVarConverterBase<Optional<T> > { 64 public: 65 FromVarConverter() { 66 } 67 68 explicit FromVarConverter(const PP_Var& var) { 69 Set(var); 70 } 71 72 ~FromVarConverter() { 73 } 74 75 void Set(const PP_Var& var) { 76 if (var.type == PP_VARTYPE_UNDEFINED) { 77 FromVarConverterBase<Optional<T> >::value_.Reset(); 78 } else { 79 FromVarConverter<T> converter(var); 80 FromVarConverterBase<Optional<T> >::value_ = converter.value(); 81 } 82 } 83 }; 84 85 template <> 86 class FromVarConverter<bool> : public FromVarConverterBase<bool> { 87 public: 88 FromVarConverter() { 89 } 90 91 explicit FromVarConverter(const PP_Var& var) { 92 Set(var); 93 } 94 95 ~FromVarConverter() { 96 } 97 98 void Set(const PP_Var& var) { 99 FromVarConverterBase<bool>::value_ = Var(var).AsBool(); 100 } 101 }; 102 103 template <> 104 class FromVarConverter<int32_t> : public FromVarConverterBase<int32_t> { 105 public: 106 FromVarConverter() { 107 } 108 109 explicit FromVarConverter(const PP_Var& var) { 110 Set(var); 111 } 112 113 ~FromVarConverter() { 114 } 115 116 void Set(const PP_Var& var) { 117 FromVarConverterBase<int32_t>::value_ = Var(var).AsInt(); 118 } 119 }; 120 121 template <> 122 class FromVarConverter<double> : public FromVarConverterBase<double> { 123 public: 124 FromVarConverter() { 125 } 126 127 explicit FromVarConverter(const PP_Var& var) { 128 Set(var); 129 } 130 131 ~FromVarConverter() { 132 } 133 134 void Set(const PP_Var& var) { 135 FromVarConverterBase<double>::value_ = Var(var).AsDouble(); 136 } 137 }; 138 139 template <> 140 class FromVarConverter<std::string> : public FromVarConverterBase<std::string> { 141 public: 142 FromVarConverter() { 143 } 144 145 explicit FromVarConverter(const PP_Var& var) { 146 Set(var); 147 } 148 149 ~FromVarConverter() { 150 } 151 152 void Set(const PP_Var& var) { 153 FromVarConverterBase<std::string>::value_ = Var(var).AsString(); 154 } 155 }; 156 157 template <> 158 class FromVarConverter<Var> : public FromVarConverterBase<Var> { 159 public: 160 FromVarConverter() { 161 } 162 163 explicit FromVarConverter(const PP_Var& var) { 164 Set(var); 165 } 166 167 ~FromVarConverter() { 168 } 169 170 void Set(const PP_Var& var) { 171 FromVarConverterBase<Var>::value_ = Var(var); 172 } 173 }; 174 175 template <> 176 class FromVarConverter<VarArray> 177 : public FromVarConverterBase<VarArray> { 178 public: 179 FromVarConverter() { 180 } 181 182 explicit FromVarConverter(const PP_Var& var) { 183 Set(var); 184 } 185 186 ~FromVarConverter() { 187 } 188 189 void Set(const PP_Var& var) { 190 FromVarConverterBase<VarArray>::value_ = Var(var); 191 } 192 }; 193 194 template <> 195 class FromVarConverter<VarDictionary> 196 : public FromVarConverterBase<VarDictionary> { 197 public: 198 FromVarConverter() { 199 } 200 201 explicit FromVarConverter(const PP_Var& var) { 202 Set(var); 203 } 204 205 ~FromVarConverter() { 206 } 207 208 void Set(const PP_Var& var) { 209 FromVarConverterBase<VarDictionary>::value_ = Var(var); 210 } 211 }; 212 213 template <> 214 class FromVarConverter<VarArrayBuffer> 215 : public FromVarConverterBase<VarArrayBuffer> { 216 public: 217 FromVarConverter() { 218 } 219 220 explicit FromVarConverter(const PP_Var& var) { 221 Set(var); 222 } 223 224 ~FromVarConverter() { 225 } 226 227 void Set(const PP_Var& var) { 228 FromVarConverterBase<VarArrayBuffer>::value_ = Var(var); 229 } 230 }; 231 232 } // namespace internal 233 } // namespace ext 234 } // namespace pp 235 236 #endif // PPAPI_CPP_EXTENSIONS_FROM_VAR_CONVERTOR_H_ 237