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