| 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/inspector_protocol/Allocator.h" | 8 #include "platform/inspector_protocol/Allocator.h" |
| 9 #include "platform/inspector_protocol/Collections.h" | 9 #include "platform/inspector_protocol/Collections.h" |
| 10 #include "platform/inspector_protocol/Platform.h" | 10 #include "platform/inspector_protocol/Platform.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 virtual ~Value() { } | 27 virtual ~Value() { } |
| 28 | 28 |
| 29 static std::unique_ptr<Value> null() | 29 static std::unique_ptr<Value> null() |
| 30 { | 30 { |
| 31 return wrapUnique(new Value()); | 31 return wrapUnique(new Value()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 enum ValueType { | 34 enum ValueType { |
| 35 TypeNull = 0, | 35 TypeNull = 0, |
| 36 TypeBoolean, | 36 TypeBoolean, |
| 37 TypeNumber, | 37 TypeInteger, |
| 38 TypeDouble, |
| 38 TypeString, | 39 TypeString, |
| 39 TypeObject, | 40 TypeObject, |
| 40 TypeArray | 41 TypeArray |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 ValueType type() const { return m_type; } | 44 ValueType type() const { return m_type; } |
| 44 | 45 |
| 45 bool isNull() const { return m_type == TypeNull; } | 46 bool isNull() const { return m_type == TypeNull; } |
| 46 | 47 |
| 47 virtual bool asBoolean(bool* output) const; | 48 virtual bool asBoolean(bool* output) const; |
| 48 virtual bool asNumber(double* output) const; | 49 virtual bool asDouble(double* output) const; |
| 49 virtual bool asNumber(int* output) const; | 50 virtual bool asInteger(int* output) const; |
| 50 virtual bool asString(String16* output) const; | 51 virtual bool asString(String16* output) const; |
| 51 | 52 |
| 52 String16 toJSONString() const; | 53 String16 toJSONString() const; |
| 53 virtual void writeJSON(String16Builder* output) const; | 54 virtual void writeJSON(String16Builder* output) const; |
| 54 virtual std::unique_ptr<Value> clone() const; | 55 virtual std::unique_ptr<Value> clone() const; |
| 55 | 56 |
| 56 protected: | 57 protected: |
| 57 Value() : m_type(TypeNull) { } | 58 Value() : m_type(TypeNull) { } |
| 58 explicit Value(ValueType type) : m_type(type) { } | 59 explicit Value(ValueType type) : m_type(type) { } |
| 59 | 60 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 75 { | 76 { |
| 76 return wrapUnique(new FundamentalValue(value)); | 77 return wrapUnique(new FundamentalValue(value)); |
| 77 } | 78 } |
| 78 | 79 |
| 79 static std::unique_ptr<FundamentalValue> create(double value) | 80 static std::unique_ptr<FundamentalValue> create(double value) |
| 80 { | 81 { |
| 81 return wrapUnique(new FundamentalValue(value)); | 82 return wrapUnique(new FundamentalValue(value)); |
| 82 } | 83 } |
| 83 | 84 |
| 84 bool asBoolean(bool* output) const override; | 85 bool asBoolean(bool* output) const override; |
| 85 bool asNumber(double* output) const override; | 86 bool asDouble(double* output) const override; |
| 86 bool asNumber(int* output) const override; | 87 bool asInteger(int* output) const override; |
| 87 void writeJSON(String16Builder* output) const override; | 88 void writeJSON(String16Builder* output) const override; |
| 88 std::unique_ptr<Value> clone() const override; | 89 std::unique_ptr<Value> clone() const override; |
| 89 | 90 |
| 90 private: | 91 private: |
| 91 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } | 92 explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(valu
e) { } |
| 92 explicit FundamentalValue(int value) : Value(TypeNumber), m_doubleValue((dou
ble)value) { } | 93 explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(va
lue) { } |
| 93 explicit FundamentalValue(double value) : Value(TypeNumber), m_doubleValue(v
alue) { } | 94 explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(v
alue) { } |
| 94 | 95 |
| 95 union { | 96 union { |
| 96 bool m_boolValue; | 97 bool m_boolValue; |
| 97 double m_doubleValue; | 98 double m_doubleValue; |
| 99 int m_integerValue; |
| 98 }; | 100 }; |
| 99 }; | 101 }; |
| 100 | 102 |
| 101 class PLATFORM_EXPORT StringValue : public Value { | 103 class PLATFORM_EXPORT StringValue : public Value { |
| 102 public: | 104 public: |
| 103 static std::unique_ptr<StringValue> create(const String16& value) | 105 static std::unique_ptr<StringValue> create(const String16& value) |
| 104 { | 106 { |
| 105 return wrapUnique(new StringValue(value)); | 107 return wrapUnique(new StringValue(value)); |
| 106 } | 108 } |
| 107 | 109 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 { | 142 { |
| 141 return wrapUnique(DictionaryValue::cast(value.release())); | 143 return wrapUnique(DictionaryValue::cast(value.release())); |
| 142 } | 144 } |
| 143 | 145 |
| 144 void writeJSON(String16Builder* output) const override; | 146 void writeJSON(String16Builder* output) const override; |
| 145 std::unique_ptr<Value> clone() const override; | 147 std::unique_ptr<Value> clone() const override; |
| 146 | 148 |
| 147 size_t size() const { return m_data.size(); } | 149 size_t size() const { return m_data.size(); } |
| 148 | 150 |
| 149 void setBoolean(const String16& name, bool); | 151 void setBoolean(const String16& name, bool); |
| 150 void setNumber(const String16& name, double); | 152 void setInteger(const String16& name, int); |
| 153 void setDouble(const String16& name, double); |
| 151 void setString(const String16& name, const String16&); | 154 void setString(const String16& name, const String16&); |
| 152 void setValue(const String16& name, std::unique_ptr<Value>); | 155 void setValue(const String16& name, std::unique_ptr<Value>); |
| 153 void setObject(const String16& name, std::unique_ptr<DictionaryValue>); | 156 void setObject(const String16& name, std::unique_ptr<DictionaryValue>); |
| 154 void setArray(const String16& name, std::unique_ptr<ListValue>); | 157 void setArray(const String16& name, std::unique_ptr<ListValue>); |
| 155 | 158 |
| 156 bool getBoolean(const String16& name, bool* output) const; | 159 bool getBoolean(const String16& name, bool* output) const; |
| 157 template<class T> bool getNumber(const String16& name, T* output) const | 160 bool getInteger(const String16& name, int* output) const; |
| 158 { | 161 bool getDouble(const String16& name, double* output) const; |
| 159 Value* value = get(name); | |
| 160 if (!value) | |
| 161 return false; | |
| 162 return value->asNumber(output); | |
| 163 } | |
| 164 bool getString(const String16& name, String16* output) const; | 162 bool getString(const String16& name, String16* output) const; |
| 165 | 163 |
| 166 DictionaryValue* getObject(const String16& name) const; | 164 DictionaryValue* getObject(const String16& name) const; |
| 167 ListValue* getArray(const String16& name) const; | 165 ListValue* getArray(const String16& name) const; |
| 168 Value* get(const String16& name) const; | 166 Value* get(const String16& name) const; |
| 169 Entry at(size_t index) const; | 167 Entry at(size_t index) const; |
| 170 | 168 |
| 171 bool booleanProperty(const String16& name, bool defaultValue) const; | 169 bool booleanProperty(const String16& name, bool defaultValue) const; |
| 172 double numberProperty(const String16& name, double defaultValue) const; | 170 int integerProperty(const String16& name, int defaultValue) const; |
| 171 double doubleProperty(const String16& name, double defaultValue) const; |
| 173 void remove(const String16& name); | 172 void remove(const String16& name); |
| 174 | 173 |
| 175 ~DictionaryValue() override; | 174 ~DictionaryValue() override; |
| 176 | 175 |
| 177 private: | 176 private: |
| 178 DictionaryValue(); | 177 DictionaryValue(); |
| 179 template<typename T> | 178 template<typename T> |
| 180 void set(const String16& key, std::unique_ptr<T>& value) | 179 void set(const String16& key, std::unique_ptr<T>& value) |
| 181 { | 180 { |
| 182 DCHECK(value); | 181 DCHECK(value); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 | 221 |
| 223 private: | 222 private: |
| 224 ListValue(); | 223 ListValue(); |
| 225 std::vector<std::unique_ptr<Value>> m_data; | 224 std::vector<std::unique_ptr<Value>> m_data; |
| 226 }; | 225 }; |
| 227 | 226 |
| 228 } // namespace protocol | 227 } // namespace protocol |
| 229 } // namespace blink | 228 } // namespace blink |
| 230 | 229 |
| 231 #endif // Values_h | 230 #endif // Values_h |
| OLD | NEW |