initial import
[vuplus_webkit] / Source / WebCore / inspector / InspectorValues.h
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef InspectorValues_h
32 #define InspectorValues_h
33
34 #if ENABLE(INSPECTOR)
35
36 #include "PlatformString.h"
37
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>
43
44 namespace WebCore {
45
46 class InspectorArray;
47 class InspectorObject;
48
49 class InspectorValue : public RefCounted<InspectorValue> {
50 public:
51     static const int maxDepth = 1000;
52
53     InspectorValue() : m_type(TypeNull) { }
54     virtual ~InspectorValue() { }
55
56     static PassRefPtr<InspectorValue> null()
57     {
58         return adoptRef(new InspectorValue());
59     }
60
61     typedef enum {
62         TypeNull = 0,
63         TypeBoolean,
64         TypeNumber,
65         TypeString,
66         TypeObject,
67         TypeArray
68     } Type;
69
70     Type type() const { return m_type; }
71
72     bool isNull() const { return m_type == TypeNull; }
73
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();
86
87     static PassRefPtr<InspectorValue> parseJSON(const String& json);
88
89     String toJSONString() const;
90     virtual void writeJSON(StringBuilder* output) const;
91
92 protected:
93     explicit InspectorValue(Type type) : m_type(type) { }
94
95 private:
96     Type m_type;
97 };
98
99 class InspectorBasicValue : public InspectorValue {
100 public:
101
102     static PassRefPtr<InspectorBasicValue> create(bool value)
103     {
104         return adoptRef(new InspectorBasicValue(value));
105     }
106
107     static PassRefPtr<InspectorBasicValue> create(int value)
108     {
109         return adoptRef(new InspectorBasicValue(value));
110     }
111
112     static PassRefPtr<InspectorBasicValue> create(double value)
113     {
114         return adoptRef(new InspectorBasicValue(value));
115     }
116
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;
123
124     virtual void writeJSON(StringBuilder* output) const;
125
126 private:
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) { }
130
131     union {
132         bool m_boolValue;
133         double m_doubleValue;
134     };
135 };
136
137 class InspectorString : public InspectorValue {
138 public:
139     static PassRefPtr<InspectorString> create(const String& value)
140     {
141         return adoptRef(new InspectorString(value));
142     }
143
144     static PassRefPtr<InspectorString> create(const char* value)
145     {
146         return adoptRef(new InspectorString(value));
147     }
148
149     virtual bool asString(String* output) const;    
150
151     virtual void writeJSON(StringBuilder* output) const;
152
153 private:
154     explicit InspectorString(const String& value) : InspectorValue(TypeString), m_stringValue(value) { }
155     explicit InspectorString(const char* value) : InspectorValue(TypeString), m_stringValue(value) { }
156
157     String m_stringValue;
158 };
159
160 class InspectorObject : public InspectorValue {
161 private:
162     typedef HashMap<String, RefPtr<InspectorValue> > Dictionary;
163
164 public:
165     typedef Dictionary::iterator iterator;
166     typedef Dictionary::const_iterator const_iterator;
167
168 public:
169     static PassRefPtr<InspectorObject> create()
170     {
171         return adoptRef(new InspectorObject());
172     }
173     ~InspectorObject();
174
175     virtual bool asObject(RefPtr<InspectorObject>* output);
176     virtual PassRefPtr<InspectorObject> asObject();
177
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>);
184
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
189     {
190         RefPtr<InspectorValue> value = get(name);
191         if (!value)
192             return false;
193         return value->asNumber(output);
194     }
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;
199
200     void remove(const String& name);
201
202     virtual void writeJSON(StringBuilder* output) const;
203
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(); }
208
209 private:
210     InspectorObject();
211     Dictionary m_data;
212     Vector<String> m_order;
213 };
214
215 class InspectorArray : public InspectorValue {
216 public:
217     static PassRefPtr<InspectorArray> create()
218     {
219         return adoptRef(new InspectorArray());
220     }
221     ~InspectorArray();
222
223     virtual bool asArray(RefPtr<InspectorArray>* output);
224     virtual PassRefPtr<InspectorArray> asArray();
225
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(); }
233
234     PassRefPtr<InspectorValue> get(size_t index);
235
236     virtual void writeJSON(StringBuilder* output) const;
237
238 private:
239     InspectorArray();
240     Vector<RefPtr<InspectorValue> > m_data;
241 };
242
243 inline InspectorObject::iterator InspectorObject::find(const String& name)
244 {
245     return m_data.find(name);
246 }
247
248 inline InspectorObject::const_iterator InspectorObject::find(const String& name) const
249 {
250     return m_data.find(name);
251 }
252
253 inline void InspectorObject::setBoolean(const String& name, bool value)
254 {
255     setValue(name, InspectorBasicValue::create(value));
256 }
257
258 inline void InspectorObject::setNumber(const String& name, double value)
259 {
260     setValue(name, InspectorBasicValue::create(value));
261 }
262
263 inline void InspectorObject::setString(const String& name, const String& value)
264 {
265     setValue(name, InspectorString::create(value));
266 }
267
268 inline void InspectorObject::setValue(const String& name, PassRefPtr<InspectorValue> value)
269 {
270     if (m_data.set(name, value).second)
271         m_order.append(name);
272 }
273
274 inline void InspectorObject::setObject(const String& name, PassRefPtr<InspectorObject> value)
275 {
276     if (m_data.set(name, value).second)
277         m_order.append(name);
278 }
279
280 inline void InspectorObject::setArray(const String& name, PassRefPtr<InspectorArray> value)
281 {
282     if (m_data.set(name, value).second)
283         m_order.append(name);
284 }
285
286 inline void InspectorArray::pushBoolean(bool value)
287 {
288     m_data.append(InspectorBasicValue::create(value));
289 }
290
291 inline void InspectorArray::pushNumber(double value)
292 {
293     m_data.append(InspectorBasicValue::create(value));
294 }
295
296 inline void InspectorArray::pushString(const String& value)
297 {
298     m_data.append(InspectorString::create(value));
299 }
300
301 inline void InspectorArray::pushValue(PassRefPtr<InspectorValue> value)
302 {
303     m_data.append(value);
304 }
305
306 inline void InspectorArray::pushObject(PassRefPtr<InspectorObject> value)
307 {
308     m_data.append(value);
309 }
310
311 inline void InspectorArray::pushArray(PassRefPtr<InspectorArray> value)
312 {
313     m_data.append(value);
314 }
315
316 } // namespace WebCore
317
318 #endif // ENABLE(INSPECTOR)
319 #endif // !defined(InspectorValues_h)