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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 TYPE_BINARY, | 63 TYPE_BINARY, |
64 TYPE_DICTIONARY, | 64 TYPE_DICTIONARY, |
65 TYPE_LIST | 65 TYPE_LIST |
66 }; | 66 }; |
67 | 67 |
68 virtual ~Value(); | 68 virtual ~Value(); |
69 | 69 |
70 // Convenience methods for creating Value objects for various | 70 // Convenience methods for creating Value objects for various |
71 // kinds of values without thinking about which class implements them. | 71 // kinds of values without thinking about which class implements them. |
72 // These can always be expected to return a valid Value*. | 72 // These can always be expected to return a valid Value*. |
73 static Value* CreateNullValue(); | |
74 static FundamentalValue* CreateBooleanValue(bool in_value); | 73 static FundamentalValue* CreateBooleanValue(bool in_value); |
75 static FundamentalValue* CreateIntegerValue(int in_value); | 74 static FundamentalValue* CreateIntegerValue(int in_value); |
76 static FundamentalValue* CreateDoubleValue(double in_value); | 75 static FundamentalValue* CreateDoubleValue(double in_value); |
77 static StringValue* CreateStringValue(const std::string& in_value); | 76 static StringValue* CreateStringValue(const std::string& in_value); |
78 static StringValue* CreateStringValue(const string16& in_value); | 77 static StringValue* CreateStringValue(const string16& in_value); |
79 | 78 |
80 // Returns the type of the value stored by the current Value object. | 79 // 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 | 80 // 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 | 81 // safe to use the Type to determine whether you can cast from |
83 // Value* to (Implementing Class)*. Also, a Value object never changes | 82 // Value* to (Implementing Class)*. Also, a Value object never changes |
(...skipping 19 matching lines...) Expand all Loading... |
103 // to the copy. The caller gets ownership of the copy, of course. | 102 // to the copy. The caller gets ownership of the copy, of course. |
104 // | 103 // |
105 // Subclasses return their own type directly in their overrides; | 104 // Subclasses return their own type directly in their overrides; |
106 // this works because C++ supports covariant return types. | 105 // this works because C++ supports covariant return types. |
107 virtual Value* DeepCopy() const; | 106 virtual Value* DeepCopy() const; |
108 | 107 |
109 // Compares if two Value objects have equal contents. | 108 // Compares if two Value objects have equal contents. |
110 virtual bool Equals(const Value* other) const; | 109 virtual bool Equals(const Value* other) const; |
111 | 110 |
112 // Compares if two Value objects have equal contents. Can handle NULLs. | 111 // Compares if two Value objects have equal contents. Can handle NULLs. |
113 // NULLs are considered equal but different from Value::CreateNullValue(). | 112 // NULLs are considered equal but different from NullValue(). |
114 static bool Equals(const Value* a, const Value* b); | 113 static bool Equals(const Value* a, const Value* b); |
115 | 114 |
116 protected: | 115 protected: |
117 // This isn't safe for end-users (they should use the Create*Value() | 116 // This isn't safe for end-users (they should use the Create*Value() |
118 // static methods above), but it's useful for subclasses. | 117 // static methods above), but it's useful for subclasses. |
119 explicit Value(Type type); | 118 explicit Value(Type type); |
120 | 119 |
121 private: | 120 private: |
122 Value(); | 121 Value(); |
123 | 122 |
124 Type type_; | 123 Type type_; |
125 | 124 |
126 DISALLOW_COPY_AND_ASSIGN(Value); | 125 DISALLOW_COPY_AND_ASSIGN(Value); |
127 }; | 126 }; |
128 | 127 |
129 // FundamentalValue represents the simple fundamental types of values. | 128 // Helper functions. Use TrueValue() and FalseValue() instead of: |
| 129 // new FundamentalValue() and CreateBooleanValue(). |
| 130 BASE_EXPORT Value* NullValue(); |
| 131 BASE_EXPORT FundamentalValue* TrueValue(); |
| 132 BASE_EXPORT FundamentalValue* FalseValue(); |
| 133 |
| 134 // FundamentalValue represents one of the fundamental values: |
| 135 // bool, int or double. |
130 class BASE_EXPORT FundamentalValue : public Value { | 136 class BASE_EXPORT FundamentalValue : public Value { |
131 public: | 137 public: |
132 explicit FundamentalValue(bool in_value); | 138 explicit FundamentalValue(bool in_value); |
133 explicit FundamentalValue(int in_value); | 139 explicit FundamentalValue(int in_value); |
134 explicit FundamentalValue(double in_value); | 140 explicit FundamentalValue(double in_value); |
135 virtual ~FundamentalValue(); | 141 virtual ~FundamentalValue(); |
136 | 142 |
137 // Overridden from Value: | 143 // Overridden from Value: |
138 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; | 144 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
139 virtual bool GetAsInteger(int* out_value) const OVERRIDE; | 145 virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 private: | 445 private: |
440 ValueVector list_; | 446 ValueVector list_; |
441 | 447 |
442 DISALLOW_COPY_AND_ASSIGN(ListValue); | 448 DISALLOW_COPY_AND_ASSIGN(ListValue); |
443 }; | 449 }; |
444 | 450 |
445 // This interface is implemented by classes that know how to serialize and | 451 // This interface is implemented by classes that know how to serialize and |
446 // deserialize Value objects. | 452 // deserialize Value objects. |
447 class BASE_EXPORT ValueSerializer { | 453 class BASE_EXPORT ValueSerializer { |
448 public: | 454 public: |
449 virtual ~ValueSerializer(); | 455 virtual ~ValueSerializer() {} |
450 | 456 |
451 virtual bool Serialize(const Value& root) = 0; | 457 virtual bool Serialize(const Value& root) = 0; |
452 | 458 |
453 // This method deserializes the subclass-specific format into a Value object. | 459 // This method deserializes the subclass-specific format into a Value object. |
454 // If the return value is non-NULL, the caller takes ownership of returned | 460 // If the return value is non-NULL, the caller takes ownership of returned |
455 // Value. If the return value is NULL, and if error_code is non-NULL, | 461 // Value. If the return value is NULL, and if error_code is non-NULL, |
456 // error_code will be set with the underlying error. | 462 // error_code will be set with the underlying error. |
457 // If |error_message| is non-null, it will be filled in with a formatted | 463 // If |error_message| is non-null, it will be filled in with a formatted |
458 // error message including the location of the error if appropriate. | 464 // error message including the location of the error if appropriate. |
459 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 465 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
460 }; | 466 }; |
461 | 467 |
462 } // namespace base | 468 } // namespace base |
463 | 469 |
464 // http://crbug.com/88666 | 470 // http://crbug.com/88666 |
465 using base::DictionaryValue; | 471 using base::DictionaryValue; |
466 using base::ListValue; | 472 using base::ListValue; |
467 using base::StringValue; | 473 using base::StringValue; |
468 using base::Value; | 474 using base::Value; |
469 | 475 |
470 #endif // BASE_VALUES_H_ | 476 #endif // BASE_VALUES_H_ |
OLD | NEW |