Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file specifies a recursive data storage class called Value intended for | 5 // This file specifies a recursive data storage class called Value intended for |
| 6 // storing settings and other persistable data. | 6 // storing settings and other persistable data. |
| 7 // | 7 // |
| 8 // A Value represents something that can be stored in JSON or passed to/from | 8 // A Value represents something that can be stored in JSON or passed to/from |
| 9 // JavaScript. As such, it is NOT a generalized variant type, since only the | 9 // JavaScript. As such, it is NOT a generalized variant type, since only the |
| 10 // types supported by JavaScript/JSON are supported. | 10 // types supported by JavaScript/JSON are supported. |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 union { | 142 union { |
| 143 bool boolean_value_; | 143 bool boolean_value_; |
| 144 int integer_value_; | 144 int integer_value_; |
| 145 double double_value_; | 145 double double_value_; |
| 146 }; | 146 }; |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 class BASE_EXPORT StringValue : public Value { | 149 class BASE_EXPORT StringValue : public Value { |
| 150 public: | 150 public: |
| 151 // Initializes a StringValue with a UTF-8 narrow character string. | 151 // Initializes a StringValue with a UTF-8 narrow character string. |
| 152 explicit StringValue(const std::string& in_value); | 152 explicit StringValue(StringPiece in_value); |
| 153 | 153 |
| 154 // Initializes a StringValue with a string16. | 154 // Initializes a StringValue with a string16. |
| 155 explicit StringValue(const string16& in_value); | 155 explicit StringValue(const string16& in_value); |
|
dcheng
2016/08/25 07:39:34
I didn't bother with string16, since calling this
| |
| 156 | 156 |
| 157 ~StringValue() override; | 157 ~StringValue() override; |
| 158 | 158 |
| 159 // Returns |value_| as a pointer or reference. | 159 // Returns |value_| as a pointer or reference. |
| 160 std::string* GetString(); | 160 std::string* GetString(); |
| 161 const std::string& GetString() const; | 161 const std::string& GetString() const; |
| 162 | 162 |
| 163 // Overridden from Value: | 163 // Overridden from Value: |
| 164 bool GetAsString(std::string* out_value) const override; | 164 bool GetAsString(std::string* out_value) const override; |
| 165 bool GetAsString(string16* out_value) const override; | 165 bool GetAsString(string16* out_value) const override; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); | 216 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
| 217 | 217 |
| 218 DictionaryValue(); | 218 DictionaryValue(); |
| 219 ~DictionaryValue() override; | 219 ~DictionaryValue() override; |
| 220 | 220 |
| 221 // Overridden from Value: | 221 // Overridden from Value: |
| 222 bool GetAsDictionary(DictionaryValue** out_value) override; | 222 bool GetAsDictionary(DictionaryValue** out_value) override; |
| 223 bool GetAsDictionary(const DictionaryValue** out_value) const override; | 223 bool GetAsDictionary(const DictionaryValue** out_value) const override; |
| 224 | 224 |
| 225 // Returns true if the current dictionary has a value for the given key. | 225 // Returns true if the current dictionary has a value for the given key. |
| 226 bool HasKey(const std::string& key) const; | 226 bool HasKey(StringPiece key) const; |
| 227 | 227 |
| 228 // Returns the number of Values in this dictionary. | 228 // Returns the number of Values in this dictionary. |
| 229 size_t size() const { return dictionary_.size(); } | 229 size_t size() const { return dictionary_.size(); } |
| 230 | 230 |
| 231 // Returns whether the dictionary is empty. | 231 // Returns whether the dictionary is empty. |
| 232 bool empty() const { return dictionary_.empty(); } | 232 bool empty() const { return dictionary_.empty(); } |
| 233 | 233 |
| 234 // Clears any current contents of this dictionary. | 234 // Clears any current contents of this dictionary. |
| 235 void Clear(); | 235 void Clear(); |
| 236 | 236 |
| 237 // Sets the Value associated with the given path starting from this object. | 237 // Sets the Value associated with the given path starting from this object. |
| 238 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 238 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 239 // into the next DictionaryValue down. Obviously, "." can't be used | 239 // into the next DictionaryValue down. Obviously, "." can't be used |
| 240 // within a key, but there are no other restrictions on keys. | 240 // within a key, but there are no other restrictions on keys. |
| 241 // If the key at any step of the way doesn't exist, or exists but isn't | 241 // If the key at any step of the way doesn't exist, or exists but isn't |
| 242 // a DictionaryValue, a new DictionaryValue will be created and attached | 242 // a DictionaryValue, a new DictionaryValue will be created and attached |
| 243 // to the path in that location. |in_value| must be non-null. | 243 // to the path in that location. |in_value| must be non-null. |
| 244 void Set(const std::string& path, std::unique_ptr<Value> in_value); | 244 void Set(StringPiece path, std::unique_ptr<Value> in_value); |
| 245 // Deprecated version of the above. TODO(estade): remove. | 245 // Deprecated version of the above. TODO(estade): remove. |
| 246 void Set(const std::string& path, Value* in_value); | 246 void Set(StringPiece path, Value* in_value); |
| 247 | 247 |
| 248 // Convenience forms of Set(). These methods will replace any existing | 248 // Convenience forms of Set(). These methods will replace any existing |
| 249 // value at that path, even if it has a different type. | 249 // value at that path, even if it has a different type. |
| 250 void SetBoolean(const std::string& path, bool in_value); | 250 void SetBoolean(StringPiece path, bool in_value); |
| 251 void SetInteger(const std::string& path, int in_value); | 251 void SetInteger(StringPiece path, int in_value); |
| 252 void SetDouble(const std::string& path, double in_value); | 252 void SetDouble(StringPiece path, double in_value); |
| 253 void SetString(const std::string& path, const std::string& in_value); | 253 void SetString(StringPiece path, StringPiece in_value); |
| 254 void SetString(const std::string& path, const string16& in_value); | 254 void SetString(StringPiece path, const string16& in_value); |
| 255 | 255 |
| 256 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | 256 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 257 // be used as paths. | 257 // be used as paths. |
| 258 void SetWithoutPathExpansion(const std::string& key, | 258 void SetWithoutPathExpansion(StringPiece key, |
| 259 std::unique_ptr<Value> in_value); | 259 std::unique_ptr<Value> in_value); |
| 260 // Deprecated version of the above. TODO(estade): remove. | 260 // Deprecated version of the above. TODO(estade): remove. |
| 261 void SetWithoutPathExpansion(const std::string& key, Value* in_value); | 261 void SetWithoutPathExpansion(StringPiece key, Value* in_value); |
| 262 | 262 |
| 263 // Convenience forms of SetWithoutPathExpansion(). | 263 // Convenience forms of SetWithoutPathExpansion(). |
| 264 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); | 264 void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value); |
| 265 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); | 265 void SetIntegerWithoutPathExpansion(StringPiece path, int in_value); |
| 266 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); | 266 void SetDoubleWithoutPathExpansion(StringPiece path, double in_value); |
| 267 void SetStringWithoutPathExpansion(const std::string& path, | 267 void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value); |
| 268 const std::string& in_value); | 268 void SetStringWithoutPathExpansion(StringPiece path, |
| 269 void SetStringWithoutPathExpansion(const std::string& path, | |
| 270 const string16& in_value); | 269 const string16& in_value); |
| 271 | 270 |
| 272 // Gets the Value associated with the given path starting from this object. | 271 // Gets the Value associated with the given path starting from this object. |
| 273 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 272 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 274 // into the next DictionaryValue down. If the path can be resolved | 273 // into the next DictionaryValue down. If the path can be resolved |
| 275 // successfully, the value for the last key in the path will be returned | 274 // successfully, the value for the last key in the path will be returned |
| 276 // through the |out_value| parameter, and the function will return true. | 275 // through the |out_value| parameter, and the function will return true. |
| 277 // Otherwise, it will return false and |out_value| will be untouched. | 276 // Otherwise, it will return false and |out_value| will be untouched. |
| 278 // Note that the dictionary always owns the value that's returned. | 277 // Note that the dictionary always owns the value that's returned. |
| 279 // |out_value| is optional and will only be set if non-NULL. | 278 // |out_value| is optional and will only be set if non-NULL. |
| 280 bool Get(StringPiece path, const Value** out_value) const; | 279 bool Get(StringPiece path, const Value** out_value) const; |
| 281 bool Get(StringPiece path, Value** out_value); | 280 bool Get(StringPiece path, Value** out_value); |
| 282 | 281 |
| 283 // These are convenience forms of Get(). The value will be retrieved | 282 // These are convenience forms of Get(). The value will be retrieved |
| 284 // and the return value will be true if the path is valid and the value at | 283 // and the return value will be true if the path is valid and the value at |
| 285 // the end of the path can be returned in the form specified. | 284 // the end of the path can be returned in the form specified. |
| 286 // |out_value| is optional and will only be set if non-NULL. | 285 // |out_value| is optional and will only be set if non-NULL. |
| 287 bool GetBoolean(const std::string& path, bool* out_value) const; | 286 bool GetBoolean(StringPiece path, bool* out_value) const; |
| 288 bool GetInteger(const std::string& path, int* out_value) const; | 287 bool GetInteger(StringPiece path, int* out_value) const; |
| 289 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 288 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 290 // doubles. | 289 // doubles. |
| 291 bool GetDouble(const std::string& path, double* out_value) const; | 290 bool GetDouble(StringPiece path, double* out_value) const; |
| 292 bool GetString(const std::string& path, std::string* out_value) const; | 291 bool GetString(StringPiece path, std::string* out_value) const; |
| 293 bool GetString(const std::string& path, string16* out_value) const; | 292 bool GetString(StringPiece path, string16* out_value) const; |
| 294 bool GetStringASCII(const std::string& path, std::string* out_value) const; | 293 bool GetStringASCII(StringPiece path, std::string* out_value) const; |
| 295 bool GetBinary(const std::string& path, const BinaryValue** out_value) const; | 294 bool GetBinary(StringPiece path, const BinaryValue** out_value) const; |
| 296 bool GetBinary(const std::string& path, BinaryValue** out_value); | 295 bool GetBinary(StringPiece path, BinaryValue** out_value); |
| 297 bool GetDictionary(StringPiece path, | 296 bool GetDictionary(StringPiece path, |
| 298 const DictionaryValue** out_value) const; | 297 const DictionaryValue** out_value) const; |
| 299 bool GetDictionary(StringPiece path, DictionaryValue** out_value); | 298 bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
| 300 bool GetList(const std::string& path, const ListValue** out_value) const; | 299 bool GetList(StringPiece path, const ListValue** out_value) const; |
| 301 bool GetList(const std::string& path, ListValue** out_value); | 300 bool GetList(StringPiece path, ListValue** out_value); |
| 302 | 301 |
| 303 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 302 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 304 // be used as paths. | 303 // be used as paths. |
| 305 bool GetWithoutPathExpansion(const std::string& key, | 304 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
| 306 const Value** out_value) const; | 305 bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
| 307 bool GetWithoutPathExpansion(const std::string& key, Value** out_value); | 306 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
| 308 bool GetBooleanWithoutPathExpansion(const std::string& key, | 307 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
| 309 bool* out_value) const; | 308 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
| 310 bool GetIntegerWithoutPathExpansion(const std::string& key, | 309 bool GetStringWithoutPathExpansion(StringPiece key, |
| 311 int* out_value) const; | |
| 312 bool GetDoubleWithoutPathExpansion(const std::string& key, | |
| 313 double* out_value) const; | |
| 314 bool GetStringWithoutPathExpansion(const std::string& key, | |
| 315 std::string* out_value) const; | 310 std::string* out_value) const; |
| 316 bool GetStringWithoutPathExpansion(const std::string& key, | 311 bool GetStringWithoutPathExpansion(StringPiece key, |
| 317 string16* out_value) const; | 312 string16* out_value) const; |
| 318 bool GetDictionaryWithoutPathExpansion( | 313 bool GetDictionaryWithoutPathExpansion( |
| 319 const std::string& key, | 314 StringPiece key, |
| 320 const DictionaryValue** out_value) const; | 315 const DictionaryValue** out_value) const; |
| 321 bool GetDictionaryWithoutPathExpansion(const std::string& key, | 316 bool GetDictionaryWithoutPathExpansion(StringPiece key, |
| 322 DictionaryValue** out_value); | 317 DictionaryValue** out_value); |
| 323 bool GetListWithoutPathExpansion(const std::string& key, | 318 bool GetListWithoutPathExpansion(StringPiece key, |
| 324 const ListValue** out_value) const; | 319 const ListValue** out_value) const; |
| 325 bool GetListWithoutPathExpansion(const std::string& key, | 320 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
| 326 ListValue** out_value); | |
| 327 | 321 |
| 328 // Removes the Value with the specified path from this dictionary (or one | 322 // Removes the Value with the specified path from this dictionary (or one |
| 329 // of its child dictionaries, if the path is more than just a local key). | 323 // of its child dictionaries, if the path is more than just a local key). |
| 330 // If |out_value| is non-NULL, the removed Value will be passed out via | 324 // If |out_value| is non-NULL, the removed Value will be passed out via |
| 331 // |out_value|. If |out_value| is NULL, the removed value will be deleted. | 325 // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 332 // This method returns true if |path| is a valid path; otherwise it will | 326 // This method returns true if |path| is a valid path; otherwise it will |
| 333 // return false and the DictionaryValue object will be unchanged. | 327 // return false and the DictionaryValue object will be unchanged. |
| 334 virtual bool Remove(const std::string& path, | 328 virtual bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
| 335 std::unique_ptr<Value>* out_value); | |
| 336 | 329 |
| 337 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs | 330 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 338 // to be used as paths. | 331 // to be used as paths. |
| 339 virtual bool RemoveWithoutPathExpansion(const std::string& key, | 332 virtual bool RemoveWithoutPathExpansion(StringPiece key, |
| 340 std::unique_ptr<Value>* out_value); | 333 std::unique_ptr<Value>* out_value); |
| 341 | 334 |
| 342 // Removes a path, clearing out all dictionaries on |path| that remain empty | 335 // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 343 // after removing the value at |path|. | 336 // after removing the value at |path|. |
| 344 virtual bool RemovePath(const std::string& path, | 337 virtual bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
| 345 std::unique_ptr<Value>* out_value); | |
| 346 | 338 |
| 347 // Makes a copy of |this| but doesn't include empty dictionaries and lists in | 339 // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 348 // the copy. This never returns NULL, even if |this| itself is empty. | 340 // the copy. This never returns NULL, even if |this| itself is empty. |
| 349 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; | 341 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
| 350 | 342 |
| 351 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any | 343 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 352 // sub-dictionaries will be merged as well. In case of key collisions, the | 344 // sub-dictionaries will be merged as well. In case of key collisions, the |
| 353 // passed in dictionary takes precedence and data already present will be | 345 // passed in dictionary takes precedence and data already present will be |
| 354 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may | 346 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 355 // be freed any time after this call. | 347 // be freed any time after this call. |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 | 457 |
| 466 // Appends a Value to the end of the list. | 458 // Appends a Value to the end of the list. |
| 467 void Append(std::unique_ptr<Value> in_value); | 459 void Append(std::unique_ptr<Value> in_value); |
| 468 // Deprecated version of the above. TODO(estade): remove. | 460 // Deprecated version of the above. TODO(estade): remove. |
| 469 void Append(Value* in_value); | 461 void Append(Value* in_value); |
| 470 | 462 |
| 471 // Convenience forms of Append. | 463 // Convenience forms of Append. |
| 472 void AppendBoolean(bool in_value); | 464 void AppendBoolean(bool in_value); |
| 473 void AppendInteger(int in_value); | 465 void AppendInteger(int in_value); |
| 474 void AppendDouble(double in_value); | 466 void AppendDouble(double in_value); |
| 475 void AppendString(const std::string& in_value); | 467 void AppendString(StringPiece in_value); |
| 476 void AppendString(const string16& in_value); | 468 void AppendString(const string16& in_value); |
| 477 void AppendStrings(const std::vector<std::string>& in_values); | 469 void AppendStrings(const std::vector<std::string>& in_values); |
| 478 void AppendStrings(const std::vector<string16>& in_values); | 470 void AppendStrings(const std::vector<string16>& in_values); |
| 479 | 471 |
| 480 // Appends a Value if it's not already present. Takes ownership of the | 472 // Appends a Value if it's not already present. Takes ownership of the |
| 481 // |in_value|. Returns true if successful, or false if the value was already | 473 // |in_value|. Returns true if successful, or false if the value was already |
| 482 // present. If the value was already present the |in_value| is deleted. | 474 // present. If the value was already present the |in_value| is deleted. |
| 483 bool AppendIfNotPresent(Value* in_value); | 475 bool AppendIfNotPresent(Value* in_value); |
| 484 | 476 |
| 485 // Insert a Value at index. | 477 // Insert a Value at index. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 } | 555 } |
| 564 | 556 |
| 565 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 557 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 566 const ListValue& value) { | 558 const ListValue& value) { |
| 567 return out << static_cast<const Value&>(value); | 559 return out << static_cast<const Value&>(value); |
| 568 } | 560 } |
| 569 | 561 |
| 570 } // namespace base | 562 } // namespace base |
| 571 | 563 |
| 572 #endif // BASE_VALUES_H_ | 564 #endif // BASE_VALUES_H_ |
| OLD | NEW |