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