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