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