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