2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef InspectorValues_h
32 #define InspectorValues_h
36 #include "PlatformString.h"
38 #include <wtf/Forward.h>
39 #include <wtf/HashMap.h>
40 #include <wtf/RefCounted.h>
41 #include <wtf/Vector.h>
42 #include <wtf/text/StringHash.h>
47 class InspectorObject;
49 class InspectorValue : public RefCounted<InspectorValue> {
51 static const int maxDepth = 1000;
53 InspectorValue() : m_type(TypeNull) { }
54 virtual ~InspectorValue() { }
56 static PassRefPtr<InspectorValue> null()
58 return adoptRef(new InspectorValue());
70 Type type() const { return m_type; }
72 bool isNull() const { return m_type == TypeNull; }
74 virtual bool asBoolean(bool* output) const;
75 virtual bool asNumber(double* output) const;
76 virtual bool asNumber(long* output) const;
77 virtual bool asNumber(int* output) const;
78 virtual bool asNumber(unsigned long* output) const;
79 virtual bool asNumber(unsigned int* output) const;
80 virtual bool asString(String* output) const;
81 virtual bool asValue(RefPtr<InspectorValue>* output);
82 virtual bool asObject(RefPtr<InspectorObject>* output);
83 virtual bool asArray(RefPtr<InspectorArray>* output);
84 virtual PassRefPtr<InspectorObject> asObject();
85 virtual PassRefPtr<InspectorArray> asArray();
87 static PassRefPtr<InspectorValue> parseJSON(const String& json);
89 String toJSONString() const;
90 virtual void writeJSON(StringBuilder* output) const;
93 explicit InspectorValue(Type type) : m_type(type) { }
99 class InspectorBasicValue : public InspectorValue {
102 static PassRefPtr<InspectorBasicValue> create(bool value)
104 return adoptRef(new InspectorBasicValue(value));
107 static PassRefPtr<InspectorBasicValue> create(int value)
109 return adoptRef(new InspectorBasicValue(value));
112 static PassRefPtr<InspectorBasicValue> create(double value)
114 return adoptRef(new InspectorBasicValue(value));
117 virtual bool asBoolean(bool* output) const;
118 virtual bool asNumber(double* output) const;
119 virtual bool asNumber(long* output) const;
120 virtual bool asNumber(int* output) const;
121 virtual bool asNumber(unsigned long* output) const;
122 virtual bool asNumber(unsigned int* output) const;
124 virtual void writeJSON(StringBuilder* output) const;
127 explicit InspectorBasicValue(bool value) : InspectorValue(TypeBoolean), m_boolValue(value) { }
128 explicit InspectorBasicValue(int value) : InspectorValue(TypeNumber), m_doubleValue((double)value) { }
129 explicit InspectorBasicValue(double value) : InspectorValue(TypeNumber), m_doubleValue(value) { }
133 double m_doubleValue;
137 class InspectorString : public InspectorValue {
139 static PassRefPtr<InspectorString> create(const String& value)
141 return adoptRef(new InspectorString(value));
144 static PassRefPtr<InspectorString> create(const char* value)
146 return adoptRef(new InspectorString(value));
149 virtual bool asString(String* output) const;
151 virtual void writeJSON(StringBuilder* output) const;
154 explicit InspectorString(const String& value) : InspectorValue(TypeString), m_stringValue(value) { }
155 explicit InspectorString(const char* value) : InspectorValue(TypeString), m_stringValue(value) { }
157 String m_stringValue;
160 class InspectorObject : public InspectorValue {
162 typedef HashMap<String, RefPtr<InspectorValue> > Dictionary;
165 typedef Dictionary::iterator iterator;
166 typedef Dictionary::const_iterator const_iterator;
169 static PassRefPtr<InspectorObject> create()
171 return adoptRef(new InspectorObject());
175 virtual bool asObject(RefPtr<InspectorObject>* output);
176 virtual PassRefPtr<InspectorObject> asObject();
178 void setBoolean(const String& name, bool);
179 void setNumber(const String& name, double);
180 void setString(const String& name, const String&);
181 void setValue(const String& name, PassRefPtr<InspectorValue>);
182 void setObject(const String& name, PassRefPtr<InspectorObject>);
183 void setArray(const String& name, PassRefPtr<InspectorArray>);
185 iterator find(const String& name);
186 const_iterator find(const String& name) const;
187 bool getBoolean(const String& name, bool* output) const;
188 template<class T> bool getNumber(const String& name, T* output) const
190 RefPtr<InspectorValue> value = get(name);
193 return value->asNumber(output);
195 bool getString(const String& name, String* output) const;
196 PassRefPtr<InspectorObject> getObject(const String& name) const;
197 PassRefPtr<InspectorArray> getArray(const String& name) const;
198 PassRefPtr<InspectorValue> get(const String& name) const;
200 void remove(const String& name);
202 virtual void writeJSON(StringBuilder* output) const;
204 iterator begin() { return m_data.begin(); }
205 iterator end() { return m_data.end(); }
206 const_iterator begin() const { return m_data.begin(); }
207 const_iterator end() const { return m_data.end(); }
212 Vector<String> m_order;
215 class InspectorArray : public InspectorValue {
217 static PassRefPtr<InspectorArray> create()
219 return adoptRef(new InspectorArray());
223 virtual bool asArray(RefPtr<InspectorArray>* output);
224 virtual PassRefPtr<InspectorArray> asArray();
226 void pushBoolean(bool);
227 void pushNumber(double);
228 void pushString(const String&);
229 void pushValue(PassRefPtr<InspectorValue>);
230 void pushObject(PassRefPtr<InspectorObject>);
231 void pushArray(PassRefPtr<InspectorArray>);
232 unsigned length() const { return m_data.size(); }
234 PassRefPtr<InspectorValue> get(size_t index);
236 virtual void writeJSON(StringBuilder* output) const;
240 Vector<RefPtr<InspectorValue> > m_data;
243 inline InspectorObject::iterator InspectorObject::find(const String& name)
245 return m_data.find(name);
248 inline InspectorObject::const_iterator InspectorObject::find(const String& name) const
250 return m_data.find(name);
253 inline void InspectorObject::setBoolean(const String& name, bool value)
255 setValue(name, InspectorBasicValue::create(value));
258 inline void InspectorObject::setNumber(const String& name, double value)
260 setValue(name, InspectorBasicValue::create(value));
263 inline void InspectorObject::setString(const String& name, const String& value)
265 setValue(name, InspectorString::create(value));
268 inline void InspectorObject::setValue(const String& name, PassRefPtr<InspectorValue> value)
270 if (m_data.set(name, value).second)
271 m_order.append(name);
274 inline void InspectorObject::setObject(const String& name, PassRefPtr<InspectorObject> value)
276 if (m_data.set(name, value).second)
277 m_order.append(name);
280 inline void InspectorObject::setArray(const String& name, PassRefPtr<InspectorArray> value)
282 if (m_data.set(name, value).second)
283 m_order.append(name);
286 inline void InspectorArray::pushBoolean(bool value)
288 m_data.append(InspectorBasicValue::create(value));
291 inline void InspectorArray::pushNumber(double value)
293 m_data.append(InspectorBasicValue::create(value));
296 inline void InspectorArray::pushString(const String& value)
298 m_data.append(InspectorString::create(value));
301 inline void InspectorArray::pushValue(PassRefPtr<InspectorValue> value)
303 m_data.append(value);
306 inline void InspectorArray::pushObject(PassRefPtr<InspectorObject> value)
308 m_data.append(value);
311 inline void InspectorArray::pushArray(PassRefPtr<InspectorArray> value)
313 m_data.append(value);
316 } // namespace WebCore
318 #endif // ENABLE(INSPECTOR)
319 #endif // !defined(InspectorValues_h)