1// This file is generated by ValueConversions_h.template. 2 3// Copyright 2016 The Chromium Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style license that can be 5// found in the LICENSE file. 6 7#ifndef {{"_".join(config.protocol.namespace)}}_ValueConversions_h 8#define {{"_".join(config.protocol.namespace)}}_ValueConversions_h 9 10//#include "ErrorSupport.h" 11//#include "Forward.h" 12//#include "Values.h" 13 14{% for namespace in config.protocol.namespace %} 15namespace {{namespace}} { 16{% endfor %} 17 18template<typename T> 19struct ValueConversions { 20 static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors) 21 { 22 return T::fromValue(value, errors); 23 } 24 25 static std::unique_ptr<protocol::Value> toValue(T* value) 26 { 27 return value->toValue(); 28 } 29 30 static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value) 31 { 32 return value->toValue(); 33 } 34}; 35 36template<> 37struct ValueConversions<bool> { 38 static bool fromValue(protocol::Value* value, ErrorSupport* errors) 39 { 40 bool result = false; 41 bool success = value ? value->asBoolean(&result) : false; 42 if (!success) 43 errors->addError("boolean value expected"); 44 return result; 45 } 46 47 static std::unique_ptr<protocol::Value> toValue(bool value) 48 { 49 return FundamentalValue::create(value); 50 } 51}; 52 53template<> 54struct ValueConversions<int> { 55 static int fromValue(protocol::Value* value, ErrorSupport* errors) 56 { 57 int result = 0; 58 bool success = value ? value->asInteger(&result) : false; 59 if (!success) 60 errors->addError("integer value expected"); 61 return result; 62 } 63 64 static std::unique_ptr<protocol::Value> toValue(int value) 65 { 66 return FundamentalValue::create(value); 67 } 68}; 69 70template<> 71struct ValueConversions<double> { 72 static double fromValue(protocol::Value* value, ErrorSupport* errors) 73 { 74 double result = 0; 75 bool success = value ? value->asDouble(&result) : false; 76 if (!success) 77 errors->addError("double value expected"); 78 return result; 79 } 80 81 static std::unique_ptr<protocol::Value> toValue(double value) 82 { 83 return FundamentalValue::create(value); 84 } 85}; 86 87template<> 88struct ValueConversions<String> { 89 static String fromValue(protocol::Value* value, ErrorSupport* errors) 90 { 91 String result; 92 bool success = value ? value->asString(&result) : false; 93 if (!success) 94 errors->addError("string value expected"); 95 return result; 96 } 97 98 static std::unique_ptr<protocol::Value> toValue(const String& value) 99 { 100 return StringValue::create(value); 101 } 102}; 103 104template<> 105struct ValueConversions<Binary> { 106 static Binary fromValue(protocol::Value* value, ErrorSupport* errors) 107 { 108 if (!value || 109 (value->type() != Value::TypeBinary && value->type() != Value::TypeString)) { 110 errors->addError("Either string base64 or binary value expected"); 111 return Binary(); 112 } 113 Binary binary; 114 if (value->asBinary(&binary)) 115 return binary; 116 String result; 117 value->asString(&result); 118 bool success; 119 Binary out = Binary::fromBase64(result, &success); 120 if (!success) 121 errors->addError("base64 decoding error"); 122 return out; 123 } 124 125 static std::unique_ptr<protocol::Value> toValue(const Binary& value) 126 { 127 return BinaryValue::create(value); 128 } 129}; 130 131template<> 132struct ValueConversions<Value> { 133 static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors) 134 { 135 bool success = !!value; 136 if (!success) { 137 errors->addError("value expected"); 138 return nullptr; 139 } 140 return value->clone(); 141 } 142 143 static std::unique_ptr<protocol::Value> toValue(Value* value) 144 { 145 return value->clone(); 146 } 147 148 static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value) 149 { 150 return value->clone(); 151 } 152}; 153 154template<> 155struct ValueConversions<DictionaryValue> { 156 static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors) 157 { 158 bool success = value && value->type() == protocol::Value::TypeObject; 159 if (!success) 160 errors->addError("object expected"); 161 return DictionaryValue::cast(value->clone()); 162 } 163 164 static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value) 165 { 166 return value->clone(); 167 } 168 169 static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value) 170 { 171 return value->clone(); 172 } 173}; 174 175template<> 176struct ValueConversions<ListValue> { 177 static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors) 178 { 179 bool success = value && value->type() == protocol::Value::TypeArray; 180 if (!success) 181 errors->addError("list expected"); 182 return ListValue::cast(value->clone()); 183 } 184 185 static std::unique_ptr<protocol::Value> toValue(ListValue* value) 186 { 187 return value->clone(); 188 } 189 190 static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value) 191 { 192 return value->clone(); 193 } 194}; 195 196{% for namespace in config.protocol.namespace %} 197} // namespace {{namespace}} 198{% endfor %} 199 200#endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h) 201