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_TO_VAR_CONVERTOR_H_ 6 #define PPAPI_CPP_EXTENSIONS_TO_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/var.h" 13 #include "ppapi/cpp/var_array.h" 14 #include "ppapi/cpp/var_array_buffer.h" 15 #include "ppapi/cpp/var_dictionary.h" 16 17 namespace pp { 18 namespace ext { 19 namespace internal { 20 21 class ToVarConverterBase { 22 public: pp_var()23 PP_Var pp_var() const { 24 return var_.pp_var(); 25 } 26 var()27 const Var& var() const { 28 return var_; 29 } 30 31 protected: ToVarConverterBase()32 ToVarConverterBase() { 33 } 34 ToVarConverterBase(const PP_Var & var)35 explicit ToVarConverterBase(const PP_Var& var) : var_(var) { 36 } 37 ToVarConverterBase(const Var & var)38 explicit ToVarConverterBase(const Var& var): var_(var) { 39 } 40 ~ToVarConverterBase()41 ~ToVarConverterBase() { 42 } 43 44 Var var_; 45 }; 46 47 template <class T> 48 class ToVarConverter : public ToVarConverterBase { 49 public: ToVarConverter(const T & object)50 explicit ToVarConverter(const T& object) 51 : ToVarConverterBase(object.CreateVar()) { 52 } 53 ~ToVarConverter()54 ~ToVarConverter() { 55 } 56 }; 57 58 template <class T> 59 class ToVarConverter<Optional<T> > : public ToVarConverterBase { 60 public: ToVarConverter(const Optional<T> & object)61 explicit ToVarConverter(const Optional<T>& object) 62 : ToVarConverterBase( 63 object.IsSet() ? ToVarConverter<T>(*object).pp_var() : 64 PP_MakeUndefined()) { 65 } 66 ~ToVarConverter()67 ~ToVarConverter() { 68 } 69 }; 70 71 template <> 72 class ToVarConverter<bool> : public ToVarConverterBase { 73 public: ToVarConverter(bool object)74 explicit ToVarConverter(bool object) : ToVarConverterBase(Var(object)) { 75 } 76 ~ToVarConverter()77 ~ToVarConverter() { 78 } 79 }; 80 81 template <> 82 class ToVarConverter<int32_t> : public ToVarConverterBase { 83 public: ToVarConverter(int32_t object)84 explicit ToVarConverter(int32_t object) : ToVarConverterBase(Var(object)) { 85 } 86 ~ToVarConverter()87 ~ToVarConverter() { 88 } 89 }; 90 91 template <> 92 class ToVarConverter<double> : public ToVarConverterBase { 93 public: ToVarConverter(double object)94 explicit ToVarConverter(double object) : ToVarConverterBase(Var(object)) { 95 } 96 ~ToVarConverter()97 ~ToVarConverter() { 98 } 99 }; 100 101 template <> 102 class ToVarConverter<std::string> : public ToVarConverterBase { 103 public: ToVarConverter(const std::string & object)104 explicit ToVarConverter(const std::string& object) 105 : ToVarConverterBase(Var(object)) { 106 } 107 ~ToVarConverter()108 ~ToVarConverter() { 109 } 110 }; 111 112 template <> 113 class ToVarConverter<Var> : public ToVarConverterBase { 114 public: ToVarConverter(const Var & object)115 explicit ToVarConverter(const Var& object) : ToVarConverterBase(object) { 116 } 117 ~ToVarConverter()118 ~ToVarConverter() { 119 } 120 }; 121 122 template <> 123 class ToVarConverter<VarArray> : public ToVarConverterBase { 124 public: ToVarConverter(const Var & object)125 explicit ToVarConverter(const Var& object) : ToVarConverterBase(object) { 126 } 127 ~ToVarConverter()128 ~ToVarConverter() { 129 } 130 }; 131 132 template <> 133 class ToVarConverter<VarDictionary> : public ToVarConverterBase { 134 public: ToVarConverter(const Var & object)135 explicit ToVarConverter(const Var& object) : ToVarConverterBase(object) { 136 } 137 ~ToVarConverter()138 ~ToVarConverter() { 139 } 140 }; 141 142 template <> 143 class ToVarConverter<VarArrayBuffer> : public ToVarConverterBase { 144 public: ToVarConverter(const Var & object)145 explicit ToVarConverter(const Var& object) : ToVarConverterBase(object) { 146 } 147 ~ToVarConverter()148 ~ToVarConverter() { 149 } 150 }; 151 152 } // namespace internal 153 } // namespace ext 154 } // namespace pp 155 156 #endif // PPAPI_CPP_EXTENSIONS_TO_VAR_CONVERTOR_H_ 157