| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 setting and other persistable data. It includes the ability to | 6 // storing setting and other persistable data. It includes the ability to |
| 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. | 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. |
| 8 // However, the API is optimized for the common case, namely storing a | 8 // However, the API is optimized for the common case, namely storing a |
| 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can | 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can |
| 10 // easily do things like: | 10 // easily do things like: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 // The Value class is the base class for Values. A Value can be instantiated | 52 // The Value class is the base class for Values. A Value can be instantiated |
| 53 // via the Create*Value() factory methods, or by directly creating instances of | 53 // via the Create*Value() factory methods, or by directly creating instances of |
| 54 // the subclasses. | 54 // the subclasses. |
| 55 class BASE_EXPORT Value { | 55 class BASE_EXPORT Value { |
| 56 public: | 56 public: |
| 57 enum Type { | 57 enum Type { |
| 58 TYPE_NULL = 0, | 58 TYPE_NULL = 0, |
| 59 TYPE_BOOLEAN, | 59 TYPE_BOOLEAN, |
| 60 TYPE_INTEGER, | 60 TYPE_INTEGER, |
| 61 TYPE_INTEGER64, |
| 61 TYPE_DOUBLE, | 62 TYPE_DOUBLE, |
| 62 TYPE_STRING, | 63 TYPE_STRING, |
| 63 TYPE_BINARY, | 64 TYPE_BINARY, |
| 64 TYPE_DICTIONARY, | 65 TYPE_DICTIONARY, |
| 65 TYPE_LIST | 66 TYPE_LIST |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 virtual ~Value(); | 69 virtual ~Value(); |
| 69 | 70 |
| 70 // Convenience methods for creating Value objects for various | 71 // Convenience methods for creating Value objects for various |
| 71 // kinds of values without thinking about which class implements them. | 72 // kinds of values without thinking about which class implements them. |
| 72 // These can always be expected to return a valid Value*. | 73 // These can always be expected to return a valid Value*. |
| 73 static Value* CreateNullValue(); | 74 static Value* CreateNullValue(); |
| 74 static FundamentalValue* CreateBooleanValue(bool in_value); | 75 static FundamentalValue* CreateBooleanValue(bool in_value); |
| 75 static FundamentalValue* CreateIntegerValue(int in_value); | 76 static FundamentalValue* CreateIntegerValue(int in_value); |
| 77 static FundamentalValue* CreateInteger64Value(int64 in_value); |
| 76 static FundamentalValue* CreateDoubleValue(double in_value); | 78 static FundamentalValue* CreateDoubleValue(double in_value); |
| 77 static StringValue* CreateStringValue(const std::string& in_value); | 79 static StringValue* CreateStringValue(const std::string& in_value); |
| 78 static StringValue* CreateStringValue(const string16& in_value); | 80 static StringValue* CreateStringValue(const string16& in_value); |
| 79 | 81 |
| 80 // Returns the type of the value stored by the current Value object. | 82 // Returns the type of the value stored by the current Value object. |
| 81 // Each type will be implemented by only one subclass of Value, so it's | 83 // Each type will be implemented by only one subclass of Value, so it's |
| 82 // safe to use the Type to determine whether you can cast from | 84 // safe to use the Type to determine whether you can cast from |
| 83 // Value* to (Implementing Class)*. Also, a Value object never changes | 85 // Value* to (Implementing Class)*. Also, a Value object never changes |
| 84 // its type after construction. | 86 // its type after construction. |
| 85 Type GetType() const { return type_; } | 87 Type GetType() const { return type_; } |
| 86 | 88 |
| 87 // Returns true if the current object represents a given type. | 89 // Returns true if the current object represents a given type. |
| 88 bool IsType(Type type) const { return type == type_; } | 90 bool IsType(Type type) const { return type == type_; } |
| 89 | 91 |
| 90 // These methods allow the convenient retrieval of settings. | 92 // These methods allow the convenient retrieval of settings. |
| 91 // If the current setting object can be converted into the given type, | 93 // If the current setting object can be converted into the given type, |
| 92 // the value is returned through the |out_value| parameter and true is | 94 // the value is returned through the |out_value| parameter and true is |
| 93 // returned; otherwise, false is returned and |out_value| is unchanged. | 95 // returned; otherwise, false is returned and |out_value| is unchanged. |
| 96 // An integer type can be converted to a double, but the 64-bit integer |
| 97 // cannot. |
| 94 virtual bool GetAsBoolean(bool* out_value) const; | 98 virtual bool GetAsBoolean(bool* out_value) const; |
| 95 virtual bool GetAsInteger(int* out_value) const; | 99 virtual bool GetAsInteger(int* out_value) const; |
| 100 virtual bool GetAsInteger64(int64* out_value) const; |
| 96 virtual bool GetAsDouble(double* out_value) const; | 101 virtual bool GetAsDouble(double* out_value) const; |
| 97 virtual bool GetAsString(std::string* out_value) const; | 102 virtual bool GetAsString(std::string* out_value) const; |
| 98 virtual bool GetAsString(string16* out_value) const; | 103 virtual bool GetAsString(string16* out_value) const; |
| 99 virtual bool GetAsList(ListValue** out_value); | 104 virtual bool GetAsList(ListValue** out_value); |
| 100 virtual bool GetAsList(const ListValue** out_value) const; | 105 virtual bool GetAsList(const ListValue** out_value) const; |
| 101 virtual bool GetAsDictionary(DictionaryValue** out_value); | 106 virtual bool GetAsDictionary(DictionaryValue** out_value); |
| 102 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; | 107 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
| 103 | 108 |
| 104 // This creates a deep copy of the entire Value tree, and returns a pointer | 109 // This creates a deep copy of the entire Value tree, and returns a pointer |
| 105 // to the copy. The caller gets ownership of the copy, of course. | 110 // to the copy. The caller gets ownership of the copy, of course. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 126 Type type_; | 131 Type type_; |
| 127 | 132 |
| 128 DISALLOW_COPY_AND_ASSIGN(Value); | 133 DISALLOW_COPY_AND_ASSIGN(Value); |
| 129 }; | 134 }; |
| 130 | 135 |
| 131 // FundamentalValue represents the simple fundamental types of values. | 136 // FundamentalValue represents the simple fundamental types of values. |
| 132 class BASE_EXPORT FundamentalValue : public Value { | 137 class BASE_EXPORT FundamentalValue : public Value { |
| 133 public: | 138 public: |
| 134 explicit FundamentalValue(bool in_value); | 139 explicit FundamentalValue(bool in_value); |
| 135 explicit FundamentalValue(int in_value); | 140 explicit FundamentalValue(int in_value); |
| 141 explicit FundamentalValue(int64 in_value); |
| 136 explicit FundamentalValue(double in_value); | 142 explicit FundamentalValue(double in_value); |
| 137 virtual ~FundamentalValue(); | 143 virtual ~FundamentalValue(); |
| 138 | 144 |
| 139 // Overridden from Value: | 145 // Overridden from Value: |
| 140 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; | 146 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
| 141 virtual bool GetAsInteger(int* out_value) const OVERRIDE; | 147 virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
| 148 virtual bool GetAsInteger64(int64* out_value) const OVERRIDE; |
| 142 virtual bool GetAsDouble(double* out_value) const OVERRIDE; | 149 virtual bool GetAsDouble(double* out_value) const OVERRIDE; |
| 143 virtual FundamentalValue* DeepCopy() const OVERRIDE; | 150 virtual FundamentalValue* DeepCopy() const OVERRIDE; |
| 144 virtual bool Equals(const Value* other) const OVERRIDE; | 151 virtual bool Equals(const Value* other) const OVERRIDE; |
| 145 | 152 |
| 146 private: | 153 private: |
| 147 union { | 154 union { |
| 148 bool boolean_value_; | 155 bool boolean_value_; |
| 149 int integer_value_; | 156 int64 integer_value_; |
| 150 double double_value_; | 157 double double_value_; |
| 151 }; | 158 }; |
| 152 | 159 |
| 153 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 160 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
| 154 }; | 161 }; |
| 155 | 162 |
| 156 class BASE_EXPORT StringValue : public Value { | 163 class BASE_EXPORT StringValue : public Value { |
| 157 public: | 164 public: |
| 158 // Initializes a StringValue with a UTF-8 narrow character string. | 165 // Initializes a StringValue with a UTF-8 narrow character string. |
| 159 explicit StringValue(const std::string& in_value); | 166 explicit StringValue(const std::string& in_value); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // a DictionaryValue, a new DictionaryValue will be created and attached | 249 // a DictionaryValue, a new DictionaryValue will be created and attached |
| 243 // to the path in that location. | 250 // to the path in that location. |
| 244 // Note that the dictionary takes ownership of the value referenced by | 251 // Note that the dictionary takes ownership of the value referenced by |
| 245 // |in_value|, and therefore |in_value| must be non-NULL. | 252 // |in_value|, and therefore |in_value| must be non-NULL. |
| 246 void Set(const std::string& path, Value* in_value); | 253 void Set(const std::string& path, Value* in_value); |
| 247 | 254 |
| 248 // Convenience forms of Set(). These methods will replace any existing | 255 // Convenience forms of Set(). These methods will replace any existing |
| 249 // value at that path, even if it has a different type. | 256 // value at that path, even if it has a different type. |
| 250 void SetBoolean(const std::string& path, bool in_value); | 257 void SetBoolean(const std::string& path, bool in_value); |
| 251 void SetInteger(const std::string& path, int in_value); | 258 void SetInteger(const std::string& path, int in_value); |
| 259 void SetInteger64(const std::string& path, int64 in_value); |
| 252 void SetDouble(const std::string& path, double in_value); | 260 void SetDouble(const std::string& path, double in_value); |
| 253 void SetString(const std::string& path, const std::string& in_value); | 261 void SetString(const std::string& path, const std::string& in_value); |
| 254 void SetString(const std::string& path, const string16& in_value); | 262 void SetString(const std::string& path, const string16& in_value); |
| 255 | 263 |
| 256 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | 264 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 257 // be used as paths. | 265 // be used as paths. |
| 258 void SetWithoutPathExpansion(const std::string& key, Value* in_value); | 266 void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
| 259 | 267 |
| 260 // Gets the Value associated with the given path starting from this object. | 268 // Gets the Value associated with the given path starting from this object. |
| 261 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 269 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 262 // into the next DictionaryValue down. If the path can be resolved | 270 // into the next DictionaryValue down. If the path can be resolved |
| 263 // successfully, the value for the last key in the path will be returned | 271 // successfully, the value for the last key in the path will be returned |
| 264 // through the |out_value| parameter, and the function will return true. | 272 // through the |out_value| parameter, and the function will return true. |
| 265 // Otherwise, it will return false and |out_value| will be untouched. | 273 // Otherwise, it will return false and |out_value| will be untouched. |
| 266 // Note that the dictionary always owns the value that's returned. | 274 // Note that the dictionary always owns the value that's returned. |
| 267 bool Get(const std::string& path, Value** out_value) const; | 275 bool Get(const std::string& path, Value** out_value) const; |
| 268 | 276 |
| 269 // These are convenience forms of Get(). The value will be retrieved | 277 // These are convenience forms of Get(). The value will be retrieved |
| 270 // and the return value will be true if the path is valid and the value at | 278 // and the return value will be true if the path is valid and the value at |
| 271 // the end of the path can be returned in the form specified. | 279 // the end of the path can be returned in the form specified. |
| 272 bool GetBoolean(const std::string& path, bool* out_value) const; | 280 bool GetBoolean(const std::string& path, bool* out_value) const; |
| 273 bool GetInteger(const std::string& path, int* out_value) const; | 281 bool GetInteger(const std::string& path, int* out_value) const; |
| 282 bool GetInteger64(const std::string& path, int64* out_value) const; |
| 274 bool GetDouble(const std::string& path, double* out_value) const; | 283 bool GetDouble(const std::string& path, double* out_value) const; |
| 275 bool GetString(const std::string& path, std::string* out_value) const; | 284 bool GetString(const std::string& path, std::string* out_value) const; |
| 276 bool GetString(const std::string& path, string16* out_value) const; | 285 bool GetString(const std::string& path, string16* out_value) const; |
| 277 bool GetStringASCII(const std::string& path, std::string* out_value) const; | 286 bool GetStringASCII(const std::string& path, std::string* out_value) const; |
| 278 bool GetBinary(const std::string& path, BinaryValue** out_value) const; | 287 bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
| 279 bool GetDictionary(const std::string& path, | 288 bool GetDictionary(const std::string& path, |
| 280 DictionaryValue** out_value) const; | 289 DictionaryValue** out_value) const; |
| 281 bool GetList(const std::string& path, ListValue** out_value) const; | 290 bool GetList(const std::string& path, ListValue** out_value) const; |
| 282 | 291 |
| 283 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | 292 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 284 // be used as paths. | 293 // be used as paths. |
| 285 bool GetWithoutPathExpansion(const std::string& key, | 294 bool GetWithoutPathExpansion(const std::string& key, |
| 286 Value** out_value) const; | 295 Value** out_value) const; |
| 287 bool GetIntegerWithoutPathExpansion(const std::string& key, | 296 bool GetIntegerWithoutPathExpansion(const std::string& key, |
| 288 int* out_value) const; | 297 int* out_value) const; |
| 298 bool GetInteger64WithoutPathExpansion(const std::string& key, |
| 299 int64* out_value) const; |
| 289 bool GetDoubleWithoutPathExpansion(const std::string& key, | 300 bool GetDoubleWithoutPathExpansion(const std::string& key, |
| 290 double* out_value) const; | 301 double* out_value) const; |
| 291 bool GetStringWithoutPathExpansion(const std::string& key, | 302 bool GetStringWithoutPathExpansion(const std::string& key, |
| 292 std::string* out_value) const; | 303 std::string* out_value) const; |
| 293 bool GetStringWithoutPathExpansion(const std::string& key, | 304 bool GetStringWithoutPathExpansion(const std::string& key, |
| 294 string16* out_value) const; | 305 string16* out_value) const; |
| 295 bool GetDictionaryWithoutPathExpansion(const std::string& key, | 306 bool GetDictionaryWithoutPathExpansion(const std::string& key, |
| 296 DictionaryValue** out_value) const; | 307 DictionaryValue** out_value) const; |
| 297 bool GetListWithoutPathExpansion(const std::string& key, | 308 bool GetListWithoutPathExpansion(const std::string& key, |
| 298 ListValue** out_value) const; | 309 ListValue** out_value) const; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 // Gets the Value at the given index. Modifies |out_value| (and returns true) | 416 // Gets the Value at the given index. Modifies |out_value| (and returns true) |
| 406 // only if the index falls within the current list range. | 417 // only if the index falls within the current list range. |
| 407 // Note that the list always owns the Value passed out via |out_value|. | 418 // Note that the list always owns the Value passed out via |out_value|. |
| 408 bool Get(size_t index, Value** out_value) const; | 419 bool Get(size_t index, Value** out_value) const; |
| 409 | 420 |
| 410 // Convenience forms of Get(). Modifies |out_value| (and returns true) | 421 // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 411 // only if the index is valid and the Value at that index can be returned | 422 // only if the index is valid and the Value at that index can be returned |
| 412 // in the specified form. | 423 // in the specified form. |
| 413 bool GetBoolean(size_t index, bool* out_value) const; | 424 bool GetBoolean(size_t index, bool* out_value) const; |
| 414 bool GetInteger(size_t index, int* out_value) const; | 425 bool GetInteger(size_t index, int* out_value) const; |
| 426 bool GetInteger64(size_t index, int64* out_value) const; |
| 415 bool GetDouble(size_t index, double* out_value) const; | 427 bool GetDouble(size_t index, double* out_value) const; |
| 416 bool GetString(size_t index, std::string* out_value) const; | 428 bool GetString(size_t index, std::string* out_value) const; |
| 417 bool GetString(size_t index, string16* out_value) const; | 429 bool GetString(size_t index, string16* out_value) const; |
| 418 bool GetBinary(size_t index, BinaryValue** out_value) const; | 430 bool GetBinary(size_t index, BinaryValue** out_value) const; |
| 419 bool GetDictionary(size_t index, DictionaryValue** out_value) const; | 431 bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
| 420 bool GetList(size_t index, ListValue** out_value) const; | 432 bool GetList(size_t index, ListValue** out_value) const; |
| 421 | 433 |
| 422 // Removes the Value with the specified index from this list. | 434 // Removes the Value with the specified index from this list. |
| 423 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 435 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 424 // passed out via |out_value|. If |out_value| is NULL, the removed value will | 436 // passed out via |out_value|. If |out_value| is NULL, the removed value will |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 | 503 |
| 492 } // namespace base | 504 } // namespace base |
| 493 | 505 |
| 494 // http://crbug.com/88666 | 506 // http://crbug.com/88666 |
| 495 using base::DictionaryValue; | 507 using base::DictionaryValue; |
| 496 using base::ListValue; | 508 using base::ListValue; |
| 497 using base::StringValue; | 509 using base::StringValue; |
| 498 using base::Value; | 510 using base::Value; |
| 499 | 511 |
| 500 #endif // BASE_VALUES_H_ | 512 #endif // BASE_VALUES_H_ |
| OLD | NEW |