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 {{"_".join(config.protocol.namespace)}}_Values_h | 5 #ifndef {{"_".join(config.protocol.namespace)}}_Values_h |
6 #define {{"_".join(config.protocol.namespace)}}_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 "Forward.h" | 10 //#include "Forward.h" |
11 | 11 |
12 {% for namespace in config.protocol.namespace %} | 12 {% for namespace in config.protocol.namespace %} |
13 namespace {{namespace}} { | 13 namespace {{namespace}} { |
14 {% endfor %} | 14 {% endfor %} |
15 | 15 |
16 class ListValue; | 16 class ListValue; |
17 class DictionaryValue; | 17 class DictionaryValue; |
18 class Value; | 18 class Value; |
19 | 19 |
20 class {{config.lib.export_macro}} Value { | 20 class {{config.lib.export_macro}} Value { |
21 PROTOCOL_DISALLOW_COPY(Value); | 21 PROTOCOL_DISALLOW_COPY(Value); |
22 public: | 22 public: |
23 virtual ~Value() { } | 23 virtual ~Value() { } |
24 | 24 |
25 static std::unique_ptr<Value> null() | 25 static std::unique_ptr<Value> null() |
26 { | 26 { |
27 return wrapUnique(new Value()); | 27 return std::unique_ptr<Value>(new Value()); |
28 } | 28 } |
29 | 29 |
30 enum ValueType { | 30 enum ValueType { |
31 TypeNull = 0, | 31 TypeNull = 0, |
32 TypeBoolean, | 32 TypeBoolean, |
33 TypeInteger, | 33 TypeInteger, |
34 TypeDouble, | 34 TypeDouble, |
35 TypeString, | 35 TypeString, |
36 TypeObject, | 36 TypeObject, |
37 TypeArray, | 37 TypeArray, |
(...skipping 22 matching lines...) Expand all Loading... |
60 friend class DictionaryValue; | 60 friend class DictionaryValue; |
61 friend class ListValue; | 61 friend class ListValue; |
62 | 62 |
63 ValueType m_type; | 63 ValueType m_type; |
64 }; | 64 }; |
65 | 65 |
66 class {{config.lib.export_macro}} FundamentalValue : public Value { | 66 class {{config.lib.export_macro}} FundamentalValue : public Value { |
67 public: | 67 public: |
68 static std::unique_ptr<FundamentalValue> create(bool value) | 68 static std::unique_ptr<FundamentalValue> create(bool value) |
69 { | 69 { |
70 return wrapUnique(new FundamentalValue(value)); | 70 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); |
71 } | 71 } |
72 | 72 |
73 static std::unique_ptr<FundamentalValue> create(int value) | 73 static std::unique_ptr<FundamentalValue> create(int value) |
74 { | 74 { |
75 return wrapUnique(new FundamentalValue(value)); | 75 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); |
76 } | 76 } |
77 | 77 |
78 static std::unique_ptr<FundamentalValue> create(double value) | 78 static std::unique_ptr<FundamentalValue> create(double value) |
79 { | 79 { |
80 return wrapUnique(new FundamentalValue(value)); | 80 return std::unique_ptr<FundamentalValue>(new FundamentalValue(value)); |
81 } | 81 } |
82 | 82 |
83 bool asBoolean(bool* output) const override; | 83 bool asBoolean(bool* output) const override; |
84 bool asDouble(double* output) const override; | 84 bool asDouble(double* output) const override; |
85 bool asInteger(int* output) const override; | 85 bool asInteger(int* output) const override; |
86 void writeJSON(StringBuilder* output) const override; | 86 void writeJSON(StringBuilder* output) const override; |
87 std::unique_ptr<Value> clone() const override; | 87 std::unique_ptr<Value> clone() const override; |
88 | 88 |
89 private: | 89 private: |
90 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } | 90 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } |
91 explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(va
lue) { } | 91 explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(va
lue) { } |
92 explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(v
alue) { } | 92 explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(v
alue) { } |
93 | 93 |
94 union { | 94 union { |
95 bool m_boolValue; | 95 bool m_boolValue; |
96 double m_doubleValue; | 96 double m_doubleValue; |
97 int m_integerValue; | 97 int m_integerValue; |
98 }; | 98 }; |
99 }; | 99 }; |
100 | 100 |
101 class {{config.lib.export_macro}} StringValue : public Value { | 101 class {{config.lib.export_macro}} StringValue : public Value { |
102 public: | 102 public: |
103 static std::unique_ptr<StringValue> create(const String& value) | 103 static std::unique_ptr<StringValue> create(const String& value) |
104 { | 104 { |
105 return wrapUnique(new StringValue(value)); | 105 return std::unique_ptr<StringValue>(new StringValue(value)); |
106 } | 106 } |
107 | 107 |
108 static std::unique_ptr<StringValue> create(const char* value) | 108 static std::unique_ptr<StringValue> create(const char* value) |
109 { | 109 { |
110 return wrapUnique(new StringValue(value)); | 110 return std::unique_ptr<StringValue>(new StringValue(value)); |
111 } | 111 } |
112 | 112 |
113 bool asString(String* output) const override; | 113 bool asString(String* output) const override; |
114 void writeJSON(StringBuilder* output) const override; | 114 void writeJSON(StringBuilder* output) const override; |
115 std::unique_ptr<Value> clone() const override; | 115 std::unique_ptr<Value> clone() const override; |
116 | 116 |
117 private: | 117 private: |
118 explicit StringValue(const String& value) : Value(TypeString), m_stringValue
(value) { } | 118 explicit StringValue(const String& value) : Value(TypeString), m_stringValue
(value) { } |
119 explicit StringValue(const char* value) : Value(TypeString), m_stringValue(v
alue) { } | 119 explicit StringValue(const char* value) : Value(TypeString), m_stringValue(v
alue) { } |
120 | 120 |
121 String m_stringValue; | 121 String m_stringValue; |
122 }; | 122 }; |
123 | 123 |
124 class {{config.lib.export_macro}} SerializedValue : public Value { | 124 class {{config.lib.export_macro}} SerializedValue : public Value { |
125 public: | 125 public: |
126 static std::unique_ptr<SerializedValue> create(const String& value) | 126 static std::unique_ptr<SerializedValue> create(const String& value) |
127 { | 127 { |
128 return wrapUnique(new SerializedValue(value)); | 128 return std::unique_ptr<SerializedValue>(new SerializedValue(value)); |
129 } | 129 } |
130 | 130 |
131 bool asSerialized(String* output) const override; | 131 bool asSerialized(String* output) const override; |
132 void writeJSON(StringBuilder* output) const override; | 132 void writeJSON(StringBuilder* output) const override; |
133 std::unique_ptr<Value> clone() const override; | 133 std::unique_ptr<Value> clone() const override; |
134 | 134 |
135 private: | 135 private: |
136 explicit SerializedValue(const String& value) : Value(TypeSerialized), m_ser
ializedValue(value) { } | 136 explicit SerializedValue(const String& value) : Value(TypeSerialized), m_ser
ializedValue(value) { } |
137 | 137 |
138 String m_serializedValue; | 138 String m_serializedValue; |
139 }; | 139 }; |
140 | 140 |
141 class {{config.lib.export_macro}} DictionaryValue : public Value { | 141 class {{config.lib.export_macro}} DictionaryValue : public Value { |
142 public: | 142 public: |
143 using Entry = std::pair<String, Value*>; | 143 using Entry = std::pair<String, Value*>; |
144 static std::unique_ptr<DictionaryValue> create() | 144 static std::unique_ptr<DictionaryValue> create() |
145 { | 145 { |
146 return wrapUnique(new DictionaryValue()); | 146 return std::unique_ptr<DictionaryValue>(new DictionaryValue()); |
147 } | 147 } |
148 | 148 |
149 static DictionaryValue* cast(Value* value) | 149 static DictionaryValue* cast(Value* value) |
150 { | 150 { |
151 if (!value || value->type() != TypeObject) | 151 if (!value || value->type() != TypeObject) |
152 return nullptr; | 152 return nullptr; |
153 return static_cast<DictionaryValue*>(value); | 153 return static_cast<DictionaryValue*>(value); |
154 } | 154 } |
155 | 155 |
156 static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) | 156 static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value) |
157 { | 157 { |
158 return wrapUnique(DictionaryValue::cast(value.release())); | 158 return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.rele
ase())); |
159 } | 159 } |
160 | 160 |
161 void writeJSON(StringBuilder* output) const override; | 161 void writeJSON(StringBuilder* output) const override; |
162 std::unique_ptr<Value> clone() const override; | 162 std::unique_ptr<Value> clone() const override; |
163 | 163 |
164 size_t size() const { return m_data.size(); } | 164 size_t size() const { return m_data.size(); } |
165 | 165 |
166 void setBoolean(const String& name, bool); | 166 void setBoolean(const String& name, bool); |
167 void setInteger(const String& name, int); | 167 void setInteger(const String& name, int); |
168 void setDouble(const String& name, double); | 168 void setDouble(const String& name, double); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 202 |
203 using Dictionary = protocol::HashMap<String, std::unique_ptr<Value>>; | 203 using Dictionary = protocol::HashMap<String, std::unique_ptr<Value>>; |
204 Dictionary m_data; | 204 Dictionary m_data; |
205 std::vector<String> m_order; | 205 std::vector<String> m_order; |
206 }; | 206 }; |
207 | 207 |
208 class {{config.lib.export_macro}} ListValue : public Value { | 208 class {{config.lib.export_macro}} ListValue : public Value { |
209 public: | 209 public: |
210 static std::unique_ptr<ListValue> create() | 210 static std::unique_ptr<ListValue> create() |
211 { | 211 { |
212 return wrapUnique(new ListValue()); | 212 return std::unique_ptr<ListValue>(new ListValue()); |
213 } | 213 } |
214 | 214 |
215 static ListValue* cast(Value* value) | 215 static ListValue* cast(Value* value) |
216 { | 216 { |
217 if (!value || value->type() != TypeArray) | 217 if (!value || value->type() != TypeArray) |
218 return nullptr; | 218 return nullptr; |
219 return static_cast<ListValue*>(value); | 219 return static_cast<ListValue*>(value); |
220 } | 220 } |
221 | 221 |
222 static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) | 222 static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value) |
223 { | 223 { |
224 return wrapUnique(ListValue::cast(value.release())); | 224 return std::unique_ptr<ListValue>(ListValue::cast(value.release())); |
225 } | 225 } |
226 | 226 |
227 ~ListValue() override; | 227 ~ListValue() override; |
228 | 228 |
229 void writeJSON(StringBuilder* output) const override; | 229 void writeJSON(StringBuilder* output) const override; |
230 std::unique_ptr<Value> clone() const override; | 230 std::unique_ptr<Value> clone() const override; |
231 | 231 |
232 void pushValue(std::unique_ptr<Value>); | 232 void pushValue(std::unique_ptr<Value>); |
233 | 233 |
234 Value* at(size_t index); | 234 Value* at(size_t index); |
235 size_t size() const { return m_data.size(); } | 235 size_t size() const { return m_data.size(); } |
236 | 236 |
237 private: | 237 private: |
238 ListValue(); | 238 ListValue(); |
239 std::vector<std::unique_ptr<Value>> m_data; | 239 std::vector<std::unique_ptr<Value>> m_data; |
240 }; | 240 }; |
241 | 241 |
242 {% for namespace in config.protocol.namespace %} | 242 {% for namespace in config.protocol.namespace %} |
243 } // namespace {{namespace}} | 243 } // namespace {{namespace}} |
244 {% endfor %} | 244 {% endfor %} |
245 | 245 |
246 #endif // {{"_".join(config.protocol.namespace)}}_Values_h | 246 #endif // {{"_".join(config.protocol.namespace)}}_Values_h |
OLD | NEW |