| 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // These methods allow the convenient retrieval of the contents of the Value. | 85 // These methods allow the convenient retrieval of the contents of the Value. |
| 86 // If the current object can be converted into the given type, the value is | 86 // If the current object can be converted into the given type, the value is |
| 87 // returned through the |out_value| parameter and true is returned; | 87 // returned through the |out_value| parameter and true is returned; |
| 88 // otherwise, false is returned and |out_value| is unchanged. | 88 // otherwise, false is returned and |out_value| is unchanged. |
| 89 virtual bool GetAsBoolean(bool* out_value) const; | 89 virtual bool GetAsBoolean(bool* out_value) const; |
| 90 virtual bool GetAsInteger(int* out_value) const; | 90 virtual bool GetAsInteger(int* out_value) const; |
| 91 virtual bool GetAsDouble(double* out_value) const; | 91 virtual bool GetAsDouble(double* out_value) const; |
| 92 virtual bool GetAsString(std::string* out_value) const; | 92 virtual bool GetAsString(std::string* out_value) const; |
| 93 virtual bool GetAsString(string16* out_value) const; | 93 virtual bool GetAsString(string16* out_value) const; |
| 94 virtual bool GetAsString(const StringValue** out_value) const; | 94 virtual bool GetAsString(const StringValue** out_value) const; |
| 95 virtual bool GetAsInt64(int64* out_value) const; |
| 96 virtual bool GetAsUint64(uint64* out_value) const; |
| 95 virtual bool GetAsList(ListValue** out_value); | 97 virtual bool GetAsList(ListValue** out_value); |
| 96 virtual bool GetAsList(const ListValue** out_value) const; | 98 virtual bool GetAsList(const ListValue** out_value) const; |
| 97 virtual bool GetAsDictionary(DictionaryValue** out_value); | 99 virtual bool GetAsDictionary(DictionaryValue** out_value); |
| 98 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; | 100 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
| 99 // Note: Do not add more types. See the file-level comment above for why. | 101 // Note: Do not add more types. See the file-level comment above for why. |
| 100 | 102 |
| 101 // This creates a deep copy of the entire Value tree, and returns a pointer | 103 // This creates a deep copy of the entire Value tree, and returns a pointer |
| 102 // to the copy. The caller gets ownership of the copy, of course. | 104 // to the copy. The caller gets ownership of the copy, of course. |
| 103 // | 105 // |
| 104 // Subclasses return their own type directly in their overrides; | 106 // Subclasses return their own type directly in their overrides; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 virtual bool Equals(const Value* other) const OVERRIDE; | 142 virtual bool Equals(const Value* other) const OVERRIDE; |
| 141 | 143 |
| 142 private: | 144 private: |
| 143 union { | 145 union { |
| 144 bool boolean_value_; | 146 bool boolean_value_; |
| 145 int integer_value_; | 147 int integer_value_; |
| 146 double double_value_; | 148 double double_value_; |
| 147 }; | 149 }; |
| 148 }; | 150 }; |
| 149 | 151 |
| 152 // A Value to store strings or (u)int64s internally represented as strings. |
| 153 // Note that while a primitive value could be used for (u)int64s, they would |
| 154 // still have to be serialized as strings (for legacy reasons) and the parser |
| 155 // would need to make a decision as to which type it is on read (which it can't |
| 156 // do deterministically in the current format if we also have legitimate reasons |
| 157 // to want to explicitly store strings representing integers). In order to avoid |
| 158 // extra complexity, simply store (u)int64s as strings and let callers |
| 159 // explicitly state whether they want the value as (u)int64 or as string. |
| 150 class BASE_EXPORT StringValue : public Value { | 160 class BASE_EXPORT StringValue : public Value { |
| 151 public: | 161 public: |
| 152 // Initializes a StringValue with a UTF-8 narrow character string. | 162 // Initializes a StringValue with a UTF-8 narrow character string. |
| 153 explicit StringValue(const std::string& in_value); | 163 explicit StringValue(const std::string& in_value); |
| 154 | 164 |
| 155 // Initializes a StringValue with a string16. | 165 // Initializes a StringValue with a string16. |
| 156 explicit StringValue(const string16& in_value); | 166 explicit StringValue(const string16& in_value); |
| 157 | 167 |
| 168 // Initializes a StringValue with an int64. |
| 169 explicit StringValue(int64 in_value); |
| 170 |
| 171 // Initializes a StringValue with a uint64. |
| 172 explicit StringValue(uint64 in_value); |
| 173 |
| 158 virtual ~StringValue(); | 174 virtual ~StringValue(); |
| 159 | 175 |
| 160 // Returns |value_| as a pointer or reference. | 176 // Returns |value_| as a pointer or reference. |
| 161 std::string* GetString(); | 177 std::string* GetString(); |
| 162 const std::string& GetString() const; | 178 const std::string& GetString() const; |
| 163 | 179 |
| 164 // Overridden from Value: | 180 // Overridden from Value: |
| 165 virtual bool GetAsString(std::string* out_value) const OVERRIDE; | 181 virtual bool GetAsString(std::string* out_value) const OVERRIDE; |
| 166 virtual bool GetAsString(string16* out_value) const OVERRIDE; | 182 virtual bool GetAsString(string16* out_value) const OVERRIDE; |
| 167 virtual bool GetAsString(const StringValue** out_value) const OVERRIDE; | 183 virtual bool GetAsString(const StringValue** out_value) const OVERRIDE; |
| 184 virtual bool GetAsInt64(int64* out_value) const OVERRIDE; |
| 185 virtual bool GetAsUint64(uint64* out_value) const OVERRIDE; |
| 168 virtual StringValue* DeepCopy() const OVERRIDE; | 186 virtual StringValue* DeepCopy() const OVERRIDE; |
| 169 virtual bool Equals(const Value* other) const OVERRIDE; | 187 virtual bool Equals(const Value* other) const OVERRIDE; |
| 170 | 188 |
| 171 private: | 189 private: |
| 172 std::string value_; | 190 std::string value_; |
| 173 }; | 191 }; |
| 174 | 192 |
| 175 class BASE_EXPORT BinaryValue: public Value { | 193 class BASE_EXPORT BinaryValue: public Value { |
| 176 public: | 194 public: |
| 177 // Creates a BinaryValue with a null buffer and size of 0. | 195 // Creates a BinaryValue with a null buffer and size of 0. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // |in_value|, and therefore |in_value| must be non-NULL. | 259 // |in_value|, and therefore |in_value| must be non-NULL. |
| 242 void Set(const std::string& path, Value* in_value); | 260 void Set(const std::string& path, Value* in_value); |
| 243 | 261 |
| 244 // Convenience forms of Set(). These methods will replace any existing | 262 // Convenience forms of Set(). These methods will replace any existing |
| 245 // value at that path, even if it has a different type. | 263 // value at that path, even if it has a different type. |
| 246 void SetBoolean(const std::string& path, bool in_value); | 264 void SetBoolean(const std::string& path, bool in_value); |
| 247 void SetInteger(const std::string& path, int in_value); | 265 void SetInteger(const std::string& path, int in_value); |
| 248 void SetDouble(const std::string& path, double in_value); | 266 void SetDouble(const std::string& path, double in_value); |
| 249 void SetString(const std::string& path, const std::string& in_value); | 267 void SetString(const std::string& path, const std::string& in_value); |
| 250 void SetString(const std::string& path, const string16& in_value); | 268 void SetString(const std::string& path, const string16& in_value); |
| 269 // Int64 helper methods that actually store the given value as a string. |
| 270 // Note that if obtaining the named value via Get, the Value type will be |
| 271 // TYPE_STRING. |
| 272 void SetInt64(const std::string& path, int64 value); |
| 273 // As above, but for unsigned values. |
| 274 void SetUint64(const std::string& path, uint64 value); |
| 251 | 275 |
| 252 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | 276 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 253 // be used as paths. | 277 // be used as paths. |
| 254 void SetWithoutPathExpansion(const std::string& key, Value* in_value); | 278 void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
| 255 | 279 |
| 256 // Convenience forms of SetWithoutPathExpansion(). | 280 // Convenience forms of SetWithoutPathExpansion(). |
| 257 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); | 281 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); |
| 258 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); | 282 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); |
| 259 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); | 283 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); |
| 260 void SetStringWithoutPathExpansion(const std::string& path, | 284 void SetStringWithoutPathExpansion(const std::string& path, |
| 261 const std::string& in_value); | 285 const std::string& in_value); |
| 262 void SetStringWithoutPathExpansion(const std::string& path, | 286 void SetStringWithoutPathExpansion(const std::string& path, |
| 263 const string16& in_value); | 287 const string16& in_value); |
| 288 void SetInt64WithoutPathExpansion(const std::string& path, int64 value); |
| 289 void SetUint64WithoutPathExpansion(const std::string& path, uint64 value); |
| 264 | 290 |
| 265 // Gets the Value associated with the given path starting from this object. | 291 // Gets the Value associated with the given path starting from this object. |
| 266 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 292 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 267 // into the next DictionaryValue down. If the path can be resolved | 293 // into the next DictionaryValue down. If the path can be resolved |
| 268 // successfully, the value for the last key in the path will be returned | 294 // successfully, the value for the last key in the path will be returned |
| 269 // through the |out_value| parameter, and the function will return true. | 295 // through the |out_value| parameter, and the function will return true. |
| 270 // Otherwise, it will return false and |out_value| will be untouched. | 296 // Otherwise, it will return false and |out_value| will be untouched. |
| 271 // Note that the dictionary always owns the value that's returned. | 297 // Note that the dictionary always owns the value that's returned. |
| 272 // |out_value| is optional and will only be set if non-NULL. | 298 // |out_value| is optional and will only be set if non-NULL. |
| 273 bool Get(const std::string& path, const Value** out_value) const; | 299 bool Get(const std::string& path, const Value** out_value) const; |
| 274 bool Get(const std::string& path, Value** out_value); | 300 bool Get(const std::string& path, Value** out_value); |
| 275 | 301 |
| 276 // These are convenience forms of Get(). The value will be retrieved | 302 // These are convenience forms of Get(). The value will be retrieved |
| 277 // and the return value will be true if the path is valid and the value at | 303 // and the return value will be true if the path is valid and the value at |
| 278 // the end of the path can be returned in the form specified. | 304 // the end of the path can be returned in the form specified. |
| 279 // |out_value| is optional and will only be set if non-NULL. | 305 // |out_value| is optional and will only be set if non-NULL. |
| 280 bool GetBoolean(const std::string& path, bool* out_value) const; | 306 bool GetBoolean(const std::string& path, bool* out_value) const; |
| 281 bool GetInteger(const std::string& path, int* out_value) const; | 307 bool GetInteger(const std::string& path, int* out_value) const; |
| 282 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 308 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 283 // doubles. | 309 // doubles. |
| 284 bool GetDouble(const std::string& path, double* out_value) const; | 310 bool GetDouble(const std::string& path, double* out_value) const; |
| 285 bool GetString(const std::string& path, std::string* out_value) const; | 311 bool GetString(const std::string& path, std::string* out_value) const; |
| 286 bool GetString(const std::string& path, string16* out_value) const; | 312 bool GetString(const std::string& path, string16* out_value) const; |
| 287 bool GetStringASCII(const std::string& path, std::string* out_value) const; | 313 bool GetStringASCII(const std::string& path, std::string* out_value) const; |
| 314 bool GetInt64(const std::string& path, int64* out_value) const; |
| 315 bool GetUint64(const std::string& path, uint64* out_value) const; |
| 288 bool GetBinary(const std::string& path, const BinaryValue** out_value) const; | 316 bool GetBinary(const std::string& path, const BinaryValue** out_value) const; |
| 289 bool GetBinary(const std::string& path, BinaryValue** out_value); | 317 bool GetBinary(const std::string& path, BinaryValue** out_value); |
| 290 bool GetDictionary(const std::string& path, | 318 bool GetDictionary(const std::string& path, |
| 291 const DictionaryValue** out_value) const; | 319 const DictionaryValue** out_value) const; |
| 292 bool GetDictionary(const std::string& path, DictionaryValue** out_value); | 320 bool GetDictionary(const std::string& path, DictionaryValue** out_value); |
| 293 bool GetList(const std::string& path, const ListValue** out_value) const; | 321 bool GetList(const std::string& path, const ListValue** out_value) const; |
| 294 bool GetList(const std::string& path, ListValue** out_value); | 322 bool GetList(const std::string& path, ListValue** out_value); |
| 295 | 323 |
| 296 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 324 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 297 // be used as paths. | 325 // be used as paths. |
| 298 bool GetWithoutPathExpansion(const std::string& key, | 326 bool GetWithoutPathExpansion(const std::string& key, |
| 299 const Value** out_value) const; | 327 const Value** out_value) const; |
| 300 bool GetWithoutPathExpansion(const std::string& key, Value** out_value); | 328 bool GetWithoutPathExpansion(const std::string& key, Value** out_value); |
| 301 bool GetBooleanWithoutPathExpansion(const std::string& key, | 329 bool GetBooleanWithoutPathExpansion(const std::string& key, |
| 302 bool* out_value) const; | 330 bool* out_value) const; |
| 303 bool GetIntegerWithoutPathExpansion(const std::string& key, | 331 bool GetIntegerWithoutPathExpansion(const std::string& key, |
| 304 int* out_value) const; | 332 int* out_value) const; |
| 305 bool GetDoubleWithoutPathExpansion(const std::string& key, | 333 bool GetDoubleWithoutPathExpansion(const std::string& key, |
| 306 double* out_value) const; | 334 double* out_value) const; |
| 307 bool GetStringWithoutPathExpansion(const std::string& key, | 335 bool GetStringWithoutPathExpansion(const std::string& key, |
| 308 std::string* out_value) const; | 336 std::string* out_value) const; |
| 309 bool GetStringWithoutPathExpansion(const std::string& key, | 337 bool GetStringWithoutPathExpansion(const std::string& key, |
| 310 string16* out_value) const; | 338 string16* out_value) const; |
| 339 bool GetInt64WithoutPathExpansion(const std::string& path, |
| 340 int64* out_value) const; |
| 341 bool GetUint64WithoutPathExpansion(const std::string& path, |
| 342 uint64* out_value) const; |
| 311 bool GetDictionaryWithoutPathExpansion( | 343 bool GetDictionaryWithoutPathExpansion( |
| 312 const std::string& key, | 344 const std::string& key, |
| 313 const DictionaryValue** out_value) const; | 345 const DictionaryValue** out_value) const; |
| 314 bool GetDictionaryWithoutPathExpansion(const std::string& key, | 346 bool GetDictionaryWithoutPathExpansion(const std::string& key, |
| 315 DictionaryValue** out_value); | 347 DictionaryValue** out_value); |
| 316 bool GetListWithoutPathExpansion(const std::string& key, | 348 bool GetListWithoutPathExpansion(const std::string& key, |
| 317 const ListValue** out_value) const; | 349 const ListValue** out_value) const; |
| 318 bool GetListWithoutPathExpansion(const std::string& key, | 350 bool GetListWithoutPathExpansion(const std::string& key, |
| 319 ListValue** out_value); | 351 ListValue** out_value); |
| 320 | 352 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // only if the index is valid and the Value at that index can be returned | 446 // only if the index is valid and the Value at that index can be returned |
| 415 // in the specified form. | 447 // in the specified form. |
| 416 // |out_value| is optional and will only be set if non-NULL. | 448 // |out_value| is optional and will only be set if non-NULL. |
| 417 bool GetBoolean(size_t index, bool* out_value) const; | 449 bool GetBoolean(size_t index, bool* out_value) const; |
| 418 bool GetInteger(size_t index, int* out_value) const; | 450 bool GetInteger(size_t index, int* out_value) const; |
| 419 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as | 451 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 420 // doubles. | 452 // doubles. |
| 421 bool GetDouble(size_t index, double* out_value) const; | 453 bool GetDouble(size_t index, double* out_value) const; |
| 422 bool GetString(size_t index, std::string* out_value) const; | 454 bool GetString(size_t index, std::string* out_value) const; |
| 423 bool GetString(size_t index, string16* out_value) const; | 455 bool GetString(size_t index, string16* out_value) const; |
| 456 bool GetInt64(size_t index, int64* out_value) const; |
| 457 bool GetUint64(size_t index, uint64* out_value) const; |
| 424 bool GetBinary(size_t index, const BinaryValue** out_value) const; | 458 bool GetBinary(size_t index, const BinaryValue** out_value) const; |
| 425 bool GetBinary(size_t index, BinaryValue** out_value); | 459 bool GetBinary(size_t index, BinaryValue** out_value); |
| 426 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; | 460 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 427 bool GetDictionary(size_t index, DictionaryValue** out_value); | 461 bool GetDictionary(size_t index, DictionaryValue** out_value); |
| 428 bool GetList(size_t index, const ListValue** out_value) const; | 462 bool GetList(size_t index, const ListValue** out_value) const; |
| 429 bool GetList(size_t index, ListValue** out_value); | 463 bool GetList(size_t index, ListValue** out_value); |
| 430 | 464 |
| 431 // Removes the Value with the specified index from this list. | 465 // Removes the Value with the specified index from this list. |
| 432 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 466 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 433 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 467 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
| (...skipping 16 matching lines...) Expand all Loading... |
| 450 void Append(Value* in_value); | 484 void Append(Value* in_value); |
| 451 | 485 |
| 452 // Convenience forms of Append. | 486 // Convenience forms of Append. |
| 453 void AppendBoolean(bool in_value); | 487 void AppendBoolean(bool in_value); |
| 454 void AppendInteger(int in_value); | 488 void AppendInteger(int in_value); |
| 455 void AppendDouble(double in_value); | 489 void AppendDouble(double in_value); |
| 456 void AppendString(const std::string& in_value); | 490 void AppendString(const std::string& in_value); |
| 457 void AppendString(const string16& in_value); | 491 void AppendString(const string16& in_value); |
| 458 void AppendStrings(const std::vector<std::string>& in_values); | 492 void AppendStrings(const std::vector<std::string>& in_values); |
| 459 void AppendStrings(const std::vector<string16>& in_values); | 493 void AppendStrings(const std::vector<string16>& in_values); |
| 494 // Int64 helper methods that actually store the given value as a string. |
| 495 // Note that if obtaining the named value via Get, the Value type will be |
| 496 // TYPE_STRING. |
| 497 void AppendInt64(int64 value); |
| 498 // As above, but for unsigned values. |
| 499 void AppendUint64(uint64 value); |
| 460 | 500 |
| 461 // Appends a Value if it's not already present. Takes ownership of the | 501 // Appends a Value if it's not already present. Takes ownership of the |
| 462 // |in_value|. Returns true if successful, or false if the value was already | 502 // |in_value|. Returns true if successful, or false if the value was already |
| 463 // present. If the value was already present the |in_value| is deleted. | 503 // present. If the value was already present the |in_value| is deleted. |
| 464 bool AppendIfNotPresent(Value* in_value); | 504 bool AppendIfNotPresent(Value* in_value); |
| 465 | 505 |
| 466 // Insert a Value at index. | 506 // Insert a Value at index. |
| 467 // Returns true if successful, or false if the index was out of range. | 507 // Returns true if successful, or false if the index was out of range. |
| 468 bool Insert(size_t index, Value* in_value); | 508 bool Insert(size_t index, Value* in_value); |
| 469 | 509 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 } | 573 } |
| 534 | 574 |
| 535 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, | 575 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 536 const ListValue& value) { | 576 const ListValue& value) { |
| 537 return out << static_cast<const Value&>(value); | 577 return out << static_cast<const Value&>(value); |
| 538 } | 578 } |
| 539 | 579 |
| 540 } // namespace base | 580 } // namespace base |
| 541 | 581 |
| 542 #endif // BASE_VALUES_H_ | 582 #endif // BASE_VALUES_H_ |
| OLD | NEW |