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