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 ValueConversions_h | |
6 #define ValueConversions_h | |
7 | |
8 #include "platform/inspector_protocol/ErrorSupport.h" | |
9 #include "platform/inspector_protocol/Platform.h" | |
10 #include "platform/inspector_protocol/String16.h" | |
11 #include "platform/inspector_protocol/Values.h" | |
12 | |
13 namespace blink { | |
14 namespace protocol { | |
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<InspectorProtocolConvenienceStringType> { | |
87 static InspectorProtocolConvenienceStringType parse(protocol::Value* value,
ErrorSupport* errors) | |
88 { | |
89 String16 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 InspectorProtocolCon
venienceStringType& value) | |
97 { | |
98 return StringValue::create(value); | |
99 } | |
100 }; | |
101 | |
102 template<> | |
103 struct ValueConversions<String16> { | |
104 static String16 parse(protocol::Value* value, ErrorSupport* errors) | |
105 { | |
106 String16 result; | |
107 bool success = value ? value->asString(&result) : false; | |
108 if (!success) | |
109 errors->addError("string value expected"); | |
110 return result; | |
111 } | |
112 | |
113 static std::unique_ptr<protocol::Value> serialize(const String16& value) | |
114 { | |
115 return StringValue::create(value); | |
116 } | |
117 }; | |
118 | |
119 template<> | |
120 struct ValueConversions<Value> { | |
121 static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* er
rors) | |
122 { | |
123 bool success = !!value; | |
124 if (!success) { | |
125 errors->addError("value expected"); | |
126 return nullptr; | |
127 } | |
128 return value->clone(); | |
129 } | |
130 | |
131 static std::unique_ptr<protocol::Value> serialize(Value* value) | |
132 { | |
133 return value->clone(); | |
134 } | |
135 | |
136 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Valu
e>& value) | |
137 { | |
138 return value->clone(); | |
139 } | |
140 }; | |
141 | |
142 template<> | |
143 struct ValueConversions<DictionaryValue> { | |
144 static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorS
upport* errors) | |
145 { | |
146 bool success = value && value->type() == protocol::Value::TypeObject; | |
147 if (!success) | |
148 errors->addError("object expected"); | |
149 return DictionaryValue::cast(value->clone()); | |
150 } | |
151 | |
152 static std::unique_ptr<protocol::Value> serialize(DictionaryValue* value) | |
153 { | |
154 return value->clone(); | |
155 } | |
156 | |
157 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Dict
ionaryValue>& value) | |
158 { | |
159 return value->clone(); | |
160 } | |
161 }; | |
162 | |
163 template<> | |
164 struct ValueConversions<ListValue> { | |
165 static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport
* errors) | |
166 { | |
167 bool success = value && value->type() == protocol::Value::TypeArray; | |
168 if (!success) | |
169 errors->addError("list expected"); | |
170 return ListValue::cast(value->clone()); | |
171 } | |
172 | |
173 static std::unique_ptr<protocol::Value> serialize(ListValue* value) | |
174 { | |
175 return value->clone(); | |
176 } | |
177 | |
178 static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<List
Value>& value) | |
179 { | |
180 return value->clone(); | |
181 } | |
182 }; | |
183 | |
184 } // namespace platform | |
185 } // namespace blink | |
186 | |
187 #endif // !defined(ValueConversions_h) | |
OLD | NEW |