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