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 Values_h | |
6 #define Values_h | |
7 | |
8 #include "platform/inspector_protocol/Allocator.h" | |
9 #include "platform/inspector_protocol/Collections.h" | |
10 #include "platform/inspector_protocol/Platform.h" | |
11 #include "platform/inspector_protocol/String16.h" | |
12 | |
13 #include <vector> | |
14 | |
15 namespace blink { | |
16 namespace protocol { | |
17 | |
18 class ListValue; | |
19 class DictionaryValue; | |
20 class Value; | |
21 | |
22 class PLATFORM_EXPORT Value { | |
23 PROTOCOL_DISALLOW_COPY(Value); | |
24 public: | |
25 static const int maxDepth = 1000; | |
26 | |
27 virtual ~Value() { } | |
28 | |
29 static std::unique_ptr<Value> null() | |
30 { | |
31 return wrapUnique(new Value()); | |
32 } | |
33 | |
34 enum ValueType { | |
35 TypeNull = 0, | |
36 TypeBoolean, | |
37 TypeInteger, | |
38 TypeDouble, | |
39 TypeString, | |
40 TypeObject, | |
41 TypeArray, | |
42 TypeSerialized | |
43 }; | |
44 | |
45 ValueType type() const { return m_type; } | |
46 | |
47 bool isNull() const { return m_type == TypeNull; } | |
48 | |
49 virtual bool asBoolean(bool* output) const; | |
50 virtual bool asDouble(double* output) const; | |
51 virtual bool asInteger(int* output) const; | |
52 virtual bool asString(String16* output) const; | |
53 virtual bool asSerialized(String16* output) const; | |
54 | |
55 String16 toJSONString() const; | |
56 virtual void writeJSON(String16Builder* output) const; | |
57 virtual std::unique_ptr<Value> clone() const; | |
58 | |
59 protected: | |
60 Value() : m_type(TypeNull) { } | |
61 explicit Value(ValueType type) : m_type(type) { } | |
62 | |
63 private: | |
64 friend class DictionaryValue; | |
65 friend class ListValue; | |
66 | |
67 ValueType m_type; | |
68 }; | |
69 | |
70 class PLATFORM_EXPORT FundamentalValue : public Value { | |
71 public: | |
72 static std::unique_ptr<FundamentalValue> create(bool value) | |
73 { | |
74 return wrapUnique(new FundamentalValue(value)); | |
75 } | |
76 | |
77 static std::unique_ptr<FundamentalValue> create(int value) | |
78 { | |
79 return wrapUnique(new FundamentalValue(value)); | |
80 } | |
81 | |
82 static std::unique_ptr<FundamentalValue> create(double value) | |
83 { | |
84 return wrapUnique(new FundamentalValue(value)); | |
85 } | |
86 | |
87 bool asBoolean(bool* output) const override; | |
88 bool asDouble(double* output) const override; | |
89 bool asInteger(int* output) const override; | |
90 void writeJSON(String16Builder* output) const override; | |
91 std::unique_ptr<Value> clone() const override; | |
92 | |
93 private: | |
94 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } | |
95 explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(va
lue) { } | |
96 explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(v
alue) { } | |
97 | |
98 union { | |
99 bool m_boolValue; | |
100 double m_doubleValue; | |
101 int m_integerValue; | |
102 }; | |
103 }; | |
104 | |
105 class PLATFORM_EXPORT StringValue : public Value { | |
106 public: | |
107 static std::unique_ptr<StringValue> create(const String16& value) | |
108 { | |
109 return wrapUnique(new StringValue(value)); | |
110 } | |
111 | |
112 static std::unique_ptr<StringValue> create(const char* value) | |
113 { | |
114 return wrapUnique(new StringValue(value)); | |
115 } | |
116 | |
117 bool asString(String16* output) const override; | |
118 void writeJSON(String16Builder* output) const override; | |
119 std::unique_ptr<Value> clone() const override; | |
120 | |
121 private: | |
122 explicit StringValue(const String16& value) : Value(TypeString), m_stringVal
ue(value) { } | |
123 explicit StringValue(const char* value) : Value(TypeString), m_stringValue(v
alue) { } | |
124 | |
125 String16 m_stringValue; | |
126 }; | |
127 | |
128 class PLATFORM_EXPORT SerializedValue : public Value { | |
129 public: | |
130 static std::unique_ptr<SerializedValue> create(const String16& value) | |
131 { | |
132 return wrapUnique(new SerializedValue(value)); | |
133 } | |
134 | |
135 bool asSerialized(String16* output) const override; | |
136 void writeJSON(String16Builder* output) const override; | |
137 std::unique_ptr<Value> clone() const override; | |
138 | |
139 private: | |
140 explicit SerializedValue(const String16& value) : Value(TypeSerialized), m_s
erializedValue(value) { } | |
141 explicit SerializedValue(const char* value) : Value(TypeSerialized), m_seria
lizedValue(value) { } | |
142 | |
143 String16 m_serializedValue; | |
144 }; | |
145 | |
146 class PLATFORM_EXPORT DictionaryValue : public Value { | |
147 public: | |
148 using Entry = std::pair<String16, Value*>; | |
149 static std::unique_ptr<DictionaryValue> create() | |
150 { | |
151 return wrapUnique(new DictionaryValue()); | |
152 } | |
153 | |
154 static DictionaryValue* cast(Value* value) | |
155 { | |
156 if (!value || value->type() != TypeObject) | |
157 return nullptr; | |
158 return static_cast<DictionaryValue*>(value); | |
159 } | |
160 | |
161 static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) | |
162 { | |
163 return wrapUnique(DictionaryValue::cast(value.release())); | |
164 } | |
165 | |
166 void writeJSON(String16Builder* output) const override; | |
167 std::unique_ptr<Value> clone() const override; | |
168 | |
169 size_t size() const { return m_data.size(); } | |
170 | |
171 void setBoolean(const String16& name, bool); | |
172 void setInteger(const String16& name, int); | |
173 void setDouble(const String16& name, double); | |
174 void setString(const String16& name, const String16&); | |
175 void setValue(const String16& name, std::unique_ptr<Value>); | |
176 void setObject(const String16& name, std::unique_ptr<DictionaryValue>); | |
177 void setArray(const String16& name, std::unique_ptr<ListValue>); | |
178 | |
179 bool getBoolean(const String16& name, bool* output) const; | |
180 bool getInteger(const String16& name, int* output) const; | |
181 bool getDouble(const String16& name, double* output) const; | |
182 bool getString(const String16& name, String16* output) const; | |
183 | |
184 DictionaryValue* getObject(const String16& name) const; | |
185 ListValue* getArray(const String16& name) const; | |
186 Value* get(const String16& name) const; | |
187 Entry at(size_t index) const; | |
188 | |
189 bool booleanProperty(const String16& name, bool defaultValue) const; | |
190 int integerProperty(const String16& name, int defaultValue) const; | |
191 double doubleProperty(const String16& name, double defaultValue) const; | |
192 void remove(const String16& name); | |
193 | |
194 ~DictionaryValue() override; | |
195 | |
196 private: | |
197 DictionaryValue(); | |
198 template<typename T> | |
199 void set(const String16& key, std::unique_ptr<T>& value) | |
200 { | |
201 DCHECK(value); | |
202 bool isNew = m_data.find(key) == m_data.end(); | |
203 m_data[key] = std::move(value); | |
204 if (isNew) | |
205 m_order.push_back(key); | |
206 } | |
207 | |
208 using Dictionary = protocol::HashMap<String16, std::unique_ptr<Value>>; | |
209 Dictionary m_data; | |
210 std::vector<String16> m_order; | |
211 }; | |
212 | |
213 class PLATFORM_EXPORT ListValue : public Value { | |
214 public: | |
215 static std::unique_ptr<ListValue> create() | |
216 { | |
217 return wrapUnique(new ListValue()); | |
218 } | |
219 | |
220 static ListValue* cast(Value* value) | |
221 { | |
222 if (!value || value->type() != TypeArray) | |
223 return nullptr; | |
224 return static_cast<ListValue*>(value); | |
225 } | |
226 | |
227 static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) | |
228 { | |
229 return wrapUnique(ListValue::cast(value.release())); | |
230 } | |
231 | |
232 ~ListValue() override; | |
233 | |
234 void writeJSON(String16Builder* output) const override; | |
235 std::unique_ptr<Value> clone() const override; | |
236 | |
237 void pushValue(std::unique_ptr<Value>); | |
238 | |
239 Value* at(size_t index); | |
240 size_t size() const { return m_data.size(); } | |
241 | |
242 private: | |
243 ListValue(); | |
244 std::vector<std::unique_ptr<Value>> m_data; | |
245 }; | |
246 | |
247 } // namespace protocol | |
248 } // namespace blink | |
249 | |
250 #endif // Values_h | |
OLD | NEW |