| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 {{"_".join(config.protocol.namespace)}}_ValueConversions_h | |
| 6 #define {{"_".join(config.protocol.namespace)}}_ValueConversions_h | |
| 7 | |
| 8 //#include "ErrorSupport.h" | |
| 9 //#include "Forward.h" | |
| 10 //#include "Values.h" | |
| 11 | |
| 12 {% for namespace in config.protocol.namespace %} | |
| 13 namespace {{namespace}} { | |
| 14 {% endfor %} | |
| 15 | |
| 16 template<typename T> | |
| 17 struct ValueConversions { | |
| 18 static std::unique_ptr<T> parse(protocol::Value* value, ErrorSupport* errors
) | |
| 19 { | |
| 20 return T::parse(value, errors); | |
| 21 } | |
| 22 | |
| 23 static std::unique_ptr<protocol::Value> serialize(T* value) | |
| 24 { | |
| 25 return value->serialize(); | |
| 26 } | |
| 27 | |
| 28 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<T>&
value) | |
| 29 { | |
| 30 return value->serialize(); | |
| 31 } | |
| 32 }; | |
| 33 | |
| 34 template<> | |
| 35 struct ValueConversions<bool> { | |
| 36 static bool parse(protocol::Value* value, ErrorSupport* errors) | |
| 37 { | |
| 38 bool result = false; | |
| 39 bool success = value ? value->asBoolean(&result) : false; | |
| 40 if (!success) | |
| 41 errors->addError("boolean value expected"); | |
| 42 return result; | |
| 43 } | |
| 44 | |
| 45 static std::unique_ptr<protocol::Value> serialize(bool value) | |
| 46 { | |
| 47 return FundamentalValue::create(value); | |
| 48 } | |
| 49 }; | |
| 50 | |
| 51 template<> | |
| 52 struct ValueConversions<int> { | |
| 53 static int parse(protocol::Value* value, ErrorSupport* errors) | |
| 54 { | |
| 55 int result = 0; | |
| 56 bool success = value ? value->asInteger(&result) : false; | |
| 57 if (!success) | |
| 58 errors->addError("integer value expected"); | |
| 59 return result; | |
| 60 } | |
| 61 | |
| 62 static std::unique_ptr<protocol::Value> serialize(int value) | |
| 63 { | |
| 64 return FundamentalValue::create(value); | |
| 65 } | |
| 66 }; | |
| 67 | |
| 68 template<> | |
| 69 struct ValueConversions<double> { | |
| 70 static double parse(protocol::Value* value, ErrorSupport* errors) | |
| 71 { | |
| 72 double result = 0; | |
| 73 bool success = value ? value->asDouble(&result) : false; | |
| 74 if (!success) | |
| 75 errors->addError("double value expected"); | |
| 76 return result; | |
| 77 } | |
| 78 | |
| 79 static std::unique_ptr<protocol::Value> serialize(double value) | |
| 80 { | |
| 81 return FundamentalValue::create(value); | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 template<> | |
| 86 struct ValueConversions<String> { | |
| 87 static String parse(protocol::Value* value, ErrorSupport* errors) | |
| 88 { | |
| 89 String result; | |
| 90 bool success = value ? value->asString(&result) : false; | |
| 91 if (!success) | |
| 92 errors->addError("string value expected"); | |
| 93 return result; | |
| 94 } | |
| 95 | |
| 96 static std::unique_ptr<protocol::Value> serialize(const String& value) | |
| 97 { | |
| 98 return StringValue::create(value); | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 template<> | |
| 103 struct ValueConversions<Value> { | |
| 104 static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* er
rors) | |
| 105 { | |
| 106 bool success = !!value; | |
| 107 if (!success) { | |
| 108 errors->addError("value expected"); | |
| 109 return nullptr; | |
| 110 } | |
| 111 return value->clone(); | |
| 112 } | |
| 113 | |
| 114 static std::unique_ptr<protocol::Value> serialize(Value* value) | |
| 115 { | |
| 116 return value->clone(); | |
| 117 } | |
| 118 | |
| 119 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Valu
e>& value) | |
| 120 { | |
| 121 return value->clone(); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 template<> | |
| 126 struct ValueConversions<DictionaryValue> { | |
| 127 static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorS
upport* errors) | |
| 128 { | |
| 129 bool success = value && value->type() == protocol::Value::TypeObject; | |
| 130 if (!success) | |
| 131 errors->addError("object expected"); | |
| 132 return DictionaryValue::cast(value->clone()); | |
| 133 } | |
| 134 | |
| 135 static std::unique_ptr<protocol::Value> serialize(DictionaryValue* value) | |
| 136 { | |
| 137 return value->clone(); | |
| 138 } | |
| 139 | |
| 140 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Dict
ionaryValue>& value) | |
| 141 { | |
| 142 return value->clone(); | |
| 143 } | |
| 144 }; | |
| 145 | |
| 146 template<> | |
| 147 struct ValueConversions<ListValue> { | |
| 148 static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport
* errors) | |
| 149 { | |
| 150 bool success = value && value->type() == protocol::Value::TypeArray; | |
| 151 if (!success) | |
| 152 errors->addError("list expected"); | |
| 153 return ListValue::cast(value->clone()); | |
| 154 } | |
| 155 | |
| 156 static std::unique_ptr<protocol::Value> serialize(ListValue* value) | |
| 157 { | |
| 158 return value->clone(); | |
| 159 } | |
| 160 | |
| 161 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<List
Value>& value) | |
| 162 { | |
| 163 return value->clone(); | |
| 164 } | |
| 165 }; | |
| 166 | |
| 167 {% for namespace in config.protocol.namespace %} | |
| 168 } // namespace {{namespace}} | |
| 169 {% endfor %} | |
| 170 | |
| 171 #endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h) | |
| OLD | NEW |