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 InspectorValue() : m_type(TypeNull) { }
52 virtual ~InspectorValue() { }
54 static PassRefPtr<InspectorValue> null()
56 return adoptRef(new InspectorValue());
68 Type type() const { return m_type; }
70 bool isNull() const { return m_type == TypeNull; }
72 virtual bool asBoolean(bool* output) const;
73 virtual bool asNumber(double* output) const;
74 virtual bool asNumber(long* output) const;
75 virtual bool asNumber(int* output) const;
76 virtual bool asNumber(unsigned long* output) const;
77 virtual bool asNumber(unsigned int* output) const;
78 virtual bool asString(String* output) const;
79 virtual bool asValue(RefPtr<InspectorValue>* output);
80 virtual bool asObject(RefPtr<InspectorObject>* output);
81 virtual bool asArray(RefPtr<InspectorArray>* output);
82 virtual PassRefPtr<InspectorObject> asObject();
83 virtual PassRefPtr<InspectorArray> asArray();
85 static PassRefPtr<InspectorValue> parseJSON(const String& json);
87 String toJSONString() const;
88 virtual void writeJSON(Vector<UChar>* output) const;
91 explicit InspectorValue(Type type) : m_type(type) { }
97 class InspectorBasicValue : public InspectorValue {
100 static PassRefPtr<InspectorBasicValue> create(bool value)
102 return adoptRef(new InspectorBasicValue(value));
105 static PassRefPtr<InspectorBasicValue> create(int value)
107 return adoptRef(new InspectorBasicValue(value));
110 static PassRefPtr<InspectorBasicValue> create(double value)
112 return adoptRef(new InspectorBasicValue(value));
115 virtual bool asBoolean(bool* output) const;
116 virtual bool asNumber(double* output) const;
117 virtual bool asNumber(long* output) const;
118 virtual bool asNumber(int* output) const;
119 virtual bool asNumber(unsigned long* output) const;
120 virtual bool asNumber(unsigned int* output) const;
122 virtual void writeJSON(Vector<UChar>* output) const;
125 explicit InspectorBasicValue(bool value) : InspectorValue(TypeBoolean), m_boolValue(value) { }
126 explicit InspectorBasicValue(int value) : InspectorValue(TypeNumber), m_doubleValue((double)value) { }
127 explicit InspectorBasicValue(double value) : InspectorValue(TypeNumber), m_doubleValue(value) { }
131 double m_doubleValue;
135 class InspectorString : public InspectorValue {
137 static PassRefPtr<InspectorString> create(const String& value)
139 return adoptRef(new InspectorString(value));
142 static PassRefPtr<InspectorString> create(const char* value)
144 return adoptRef(new InspectorString(value));
147 virtual bool asString(String* output) const;
149 virtual void writeJSON(Vector<UChar>* output) const;
152 explicit InspectorString(const String& value) : InspectorValue(TypeString), m_stringValue(value) { }
153 explicit InspectorString(const char* value) : InspectorValue(TypeString), m_stringValue(value) { }
155 String m_stringValue;
158 class InspectorObject : public InspectorValue {
160 typedef HashMap<String, RefPtr<InspectorValue> > Dictionary;
163 typedef Dictionary::iterator iterator;
164 typedef Dictionary::const_iterator const_iterator;
167 static PassRefPtr<InspectorObject> create()
169 return adoptRef(new InspectorObject());
173 virtual bool asObject(RefPtr<InspectorObject>* output);
174 virtual PassRefPtr<InspectorObject> asObject();
176 void setBoolean(const String& name, bool);
177 void setNumber(const String& name, double);
178 void setString(const String& name, const String&);
179 void setValue(const String& name, PassRefPtr<InspectorValue>);
180 void setObject(const String& name, PassRefPtr<InspectorObject>);
181 void setArray(const String& name, PassRefPtr<InspectorArray>);
183 iterator find(const String& name);
184 const_iterator find(const String& name) const;
185 bool getBoolean(const String& name, bool* output) const;
186 template<class T> bool getNumber(const String& name, T* output) const
188 RefPtr<InspectorValue> value = get(name);
191 return value->asNumber(output);
193 bool getString(const String& name, String* output) const;
194 PassRefPtr<InspectorObject> getObject(const String& name) const;
195 PassRefPtr<InspectorArray> getArray(const String& name) const;
196 PassRefPtr<InspectorValue> get(const String& name) const;
198 void remove(const String& name);
200 virtual void writeJSON(Vector<UChar>* output) const;
202 iterator begin() { return m_data.begin(); }
203 iterator end() { return m_data.end(); }
204 const_iterator begin() const { return m_data.begin(); }
205 const_iterator end() const { return m_data.end(); }
210 Vector<String> m_order;
213 class InspectorArray : public InspectorValue {
215 static PassRefPtr<InspectorArray> create()
217 return adoptRef(new InspectorArray());
221 virtual bool asArray(RefPtr<InspectorArray>* output);
222 virtual PassRefPtr<InspectorArray> asArray();
224 void pushBoolean(bool);
225 void pushNumber(double);
226 void pushString(const String&);
227 void pushValue(PassRefPtr<InspectorValue>);
228 void pushObject(PassRefPtr<InspectorObject>);
229 void pushArray(PassRefPtr<InspectorArray>);
230 unsigned length() const { return m_data.size(); }
232 PassRefPtr<InspectorValue> get(size_t index);
234 virtual void writeJSON(Vector<UChar>* output) const;
238 Vector<RefPtr<InspectorValue> > m_data;
241 inline InspectorObject::iterator InspectorObject::find(const String& name)
243 return m_data.find(name);
246 inline InspectorObject::const_iterator InspectorObject::find(const String& name) const
248 return m_data.find(name);
251 inline void InspectorObject::setBoolean(const String& name, bool value)
253 setValue(name, InspectorBasicValue::create(value));
256 inline void InspectorObject::setNumber(const String& name, double value)
258 setValue(name, InspectorBasicValue::create(value));
261 inline void InspectorObject::setString(const String& name, const String& value)
263 setValue(name, InspectorString::create(value));
266 inline void InspectorObject::setValue(const String& name, PassRefPtr<InspectorValue> value)
268 if (m_data.set(name, value).second)
269 m_order.append(name);
272 inline void InspectorObject::setObject(const String& name, PassRefPtr<InspectorObject> value)
274 if (m_data.set(name, value).second)
275 m_order.append(name);
278 inline void InspectorObject::setArray(const String& name, PassRefPtr<InspectorArray> value)
280 if (m_data.set(name, value).second)
281 m_order.append(name);
284 inline void InspectorArray::pushBoolean(bool value)
286 m_data.append(InspectorBasicValue::create(value));
289 inline void InspectorArray::pushNumber(double value)
291 m_data.append(InspectorBasicValue::create(value));
294 inline void InspectorArray::pushString(const String& value)
296 m_data.append(InspectorString::create(value));
299 inline void InspectorArray::pushValue(PassRefPtr<InspectorValue> value)
301 m_data.append(value);
304 inline void InspectorArray::pushObject(PassRefPtr<InspectorObject> value)
306 m_data.append(value);
309 inline void InspectorArray::pushArray(PassRefPtr<InspectorArray> value)
311 m_data.append(value);
314 } // namespace WebCore
316 #endif // ENABLE(INSPECTOR)
317 #endif // !defined(InspectorValues_h)