OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef Values_h |
| 6 #define Values_h |
| 7 |
| 8 #include "platform/PlatformExport.h" |
| 9 #include "wtf/Allocator.h" |
| 10 #include "wtf/Forward.h" |
| 11 #include "wtf/HashMap.h" |
| 12 #include "wtf/RefCounted.h" |
| 13 #include "wtf/TypeTraits.h" |
| 14 #include "wtf/Vector.h" |
| 15 #include "wtf/text/StringHash.h" |
| 16 #include "wtf/text/WTFString.h" |
| 17 |
| 18 namespace blink { |
| 19 namespace protocol { |
| 20 |
| 21 class ListValue; |
| 22 class DictionaryValue; |
| 23 class Value; |
| 24 |
| 25 class PLATFORM_EXPORT Value : public RefCounted<Value> { |
| 26 public: |
| 27 static const int maxDepth = 1000; |
| 28 |
| 29 virtual ~Value() { } |
| 30 |
| 31 static PassRefPtr<Value> null() |
| 32 { |
| 33 return adoptRef(new Value()); |
| 34 } |
| 35 |
| 36 typedef enum { |
| 37 TypeNull = 0, |
| 38 TypeBoolean, |
| 39 TypeNumber, |
| 40 TypeString, |
| 41 TypeObject, |
| 42 TypeArray |
| 43 } Type; |
| 44 |
| 45 Type type() const { return m_type; } |
| 46 |
| 47 bool isNull() const { return m_type == TypeNull; } |
| 48 |
| 49 virtual bool asBoolean(bool* output) const; |
| 50 virtual bool asNumber(double* output) const; |
| 51 virtual bool asNumber(int* output) const; |
| 52 virtual bool asString(String* output) const; |
| 53 |
| 54 String toJSONString() const; |
| 55 virtual void writeJSON(StringBuilder* output) const; |
| 56 |
| 57 protected: |
| 58 Value() : m_type(TypeNull) { } |
| 59 explicit Value(Type type) : m_type(type) { } |
| 60 |
| 61 private: |
| 62 friend class DictionaryValue; |
| 63 friend class ListValue; |
| 64 |
| 65 Type m_type; |
| 66 }; |
| 67 |
| 68 class PLATFORM_EXPORT FundamentalValue : public Value { |
| 69 public: |
| 70 |
| 71 static PassRefPtr<FundamentalValue> create(bool value) |
| 72 { |
| 73 return adoptRef(new FundamentalValue(value)); |
| 74 } |
| 75 |
| 76 static PassRefPtr<FundamentalValue> create(int value) |
| 77 { |
| 78 return adoptRef(new FundamentalValue(value)); |
| 79 } |
| 80 |
| 81 static PassRefPtr<FundamentalValue> create(double value) |
| 82 { |
| 83 return adoptRef(new FundamentalValue(value)); |
| 84 } |
| 85 |
| 86 bool asBoolean(bool* output) const override; |
| 87 bool asNumber(double* output) const override; |
| 88 bool asNumber(int* output) const override; |
| 89 |
| 90 void writeJSON(StringBuilder* output) const override; |
| 91 |
| 92 private: |
| 93 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } |
| 94 explicit FundamentalValue(int value) : Value(TypeNumber), m_doubleValue((dou
ble)value) { } |
| 95 explicit FundamentalValue(double value) : Value(TypeNumber), m_doubleValue(v
alue) { } |
| 96 |
| 97 union { |
| 98 bool m_boolValue; |
| 99 double m_doubleValue; |
| 100 }; |
| 101 }; |
| 102 |
| 103 class PLATFORM_EXPORT StringValue : public Value { |
| 104 public: |
| 105 static PassRefPtr<StringValue> create(const String& value) |
| 106 { |
| 107 return adoptRef(new StringValue(value)); |
| 108 } |
| 109 |
| 110 static PassRefPtr<StringValue> create(const char* value) |
| 111 { |
| 112 return adoptRef(new StringValue(value)); |
| 113 } |
| 114 |
| 115 bool asString(String* output) const override; |
| 116 |
| 117 void writeJSON(StringBuilder* output) const override; |
| 118 |
| 119 private: |
| 120 explicit StringValue(const String& value) : Value(TypeString), m_stringValue
(value) { } |
| 121 explicit StringValue(const char* value) : Value(TypeString), m_stringValue(v
alue) { } |
| 122 |
| 123 String m_stringValue; |
| 124 }; |
| 125 |
| 126 class PLATFORM_EXPORT DictionaryValue : public Value { |
| 127 private: |
| 128 typedef HashMap<String, RefPtr<Value>> Dictionary; |
| 129 |
| 130 public: |
| 131 typedef Dictionary::iterator iterator; |
| 132 typedef Dictionary::const_iterator const_iterator; |
| 133 |
| 134 static PassRefPtr<DictionaryValue> create() |
| 135 { |
| 136 return adoptRef(new DictionaryValue()); |
| 137 } |
| 138 |
| 139 static PassRefPtr<DictionaryValue> cast(PassRefPtr<Value> value) |
| 140 { |
| 141 if (!value || value->type() != TypeObject) |
| 142 return nullptr; |
| 143 return adoptRef(static_cast<DictionaryValue*>(value.leakRef())); |
| 144 } |
| 145 |
| 146 void writeJSON(StringBuilder* output) const override; |
| 147 |
| 148 int size() const { return m_data.size(); } |
| 149 |
| 150 void setBoolean(const String& name, bool); |
| 151 void setNumber(const String& name, double); |
| 152 void setString(const String& name, const String&); |
| 153 void setValue(const String& name, PassRefPtr<Value>); |
| 154 void setObject(const String& name, PassRefPtr<DictionaryValue>); |
| 155 void setArray(const String& name, PassRefPtr<ListValue>); |
| 156 |
| 157 iterator find(const String& name); |
| 158 const_iterator find(const String& name) const; |
| 159 bool getBoolean(const String& name, bool* output) const; |
| 160 template<class T> bool getNumber(const String& name, T* output) const |
| 161 { |
| 162 RefPtr<Value> value = get(name); |
| 163 if (!value) |
| 164 return false; |
| 165 return value->asNumber(output); |
| 166 } |
| 167 bool getString(const String& name, String* output) const; |
| 168 PassRefPtr<DictionaryValue> getObject(const String& name) const; |
| 169 PassRefPtr<ListValue> getArray(const String& name) const; |
| 170 PassRefPtr<Value> get(const String& name) const; |
| 171 |
| 172 bool booleanProperty(const String& name, bool defaultValue) const; |
| 173 |
| 174 void remove(const String& name); |
| 175 |
| 176 iterator begin() { return m_data.begin(); } |
| 177 iterator end() { return m_data.end(); } |
| 178 const_iterator begin() const { return m_data.begin(); } |
| 179 const_iterator end() const { return m_data.end(); } |
| 180 ~DictionaryValue() override; |
| 181 |
| 182 private: |
| 183 DictionaryValue(); |
| 184 |
| 185 Dictionary m_data; |
| 186 Vector<String> m_order; |
| 187 }; |
| 188 |
| 189 class PLATFORM_EXPORT ListValue : public Value { |
| 190 public: |
| 191 typedef Vector<RefPtr<Value>>::iterator iterator; |
| 192 typedef Vector<RefPtr<Value>>::const_iterator const_iterator; |
| 193 |
| 194 static PassRefPtr<ListValue> create() |
| 195 { |
| 196 return adoptRef(new ListValue()); |
| 197 } |
| 198 |
| 199 static PassRefPtr<ListValue> cast(PassRefPtr<Value> value) |
| 200 { |
| 201 if (!value || value->type() != TypeArray) |
| 202 return nullptr; |
| 203 return adoptRef(static_cast<ListValue*>(value.leakRef())); |
| 204 } |
| 205 |
| 206 ~ListValue() override; |
| 207 |
| 208 void writeJSON(StringBuilder* output) const override; |
| 209 |
| 210 void pushValue(PassRefPtr<Value>); |
| 211 |
| 212 PassRefPtr<Value> get(size_t index); |
| 213 unsigned length() const { return m_data.size(); } |
| 214 |
| 215 iterator begin() { return m_data.begin(); } |
| 216 iterator end() { return m_data.end(); } |
| 217 const_iterator begin() const { return m_data.begin(); } |
| 218 const_iterator end() const { return m_data.end(); } |
| 219 |
| 220 private: |
| 221 ListValue(); |
| 222 Vector<RefPtr<Value>> m_data; |
| 223 }; |
| 224 |
| 225 } // namespace protocol |
| 226 } // namespace blink |
| 227 |
| 228 #endif // Values_h |
OLD | NEW |