OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 5 // This file specifies a recursive data storage class called Value |
6 // intended for storing setting and other persistable data. | 6 // intended for storing setting and other persistable data. |
7 // It includes the ability to specify (recursive) lists and dictionaries, so | 7 // It includes the ability to specify (recursive) lists and dictionaries, so |
8 // it's fairly expressive. However, the API is optimized for the common case, | 8 // it's fairly expressive. However, the API is optimized for the common case, |
9 // namely storing a hierarchical tree of simple values. Given a | 9 // namely storing a hierarchical tree of simple values. Given a |
10 // DictionaryValue root, you can easily do things like: | 10 // DictionaryValue root, you can easily do things like: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 public: | 45 public: |
46 virtual ~Value(); | 46 virtual ~Value(); |
47 | 47 |
48 // Convenience methods for creating Value objects for various | 48 // Convenience methods for creating Value objects for various |
49 // kinds of values without thinking about which class implements them. | 49 // kinds of values without thinking about which class implements them. |
50 // These can always be expected to return a valid Value*. | 50 // These can always be expected to return a valid Value*. |
51 static Value* CreateNullValue(); | 51 static Value* CreateNullValue(); |
52 static Value* CreateBooleanValue(bool in_value); | 52 static Value* CreateBooleanValue(bool in_value); |
53 static Value* CreateIntegerValue(int in_value); | 53 static Value* CreateIntegerValue(int in_value); |
54 static Value* CreateRealValue(double in_value); | 54 static Value* CreateRealValue(double in_value); |
55 static Value* CreateStringValue(const std::string& in_value); | |
56 static Value* CreateStringValue(const std::wstring& in_value); | 55 static Value* CreateStringValue(const std::wstring& in_value); |
57 | 56 |
58 // This one can return NULL if the input isn't valid. If the return value | 57 // This one can return NULL if the input isn't valid. If the return value |
59 // is non-null, the new object has taken ownership of the buffer pointer. | 58 // is non-null, the new object has taken ownership of the buffer pointer. |
60 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); | 59 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
61 | 60 |
62 typedef enum { | 61 typedef enum { |
63 TYPE_NULL = 0, | 62 TYPE_NULL = 0, |
64 TYPE_BOOLEAN, | 63 TYPE_BOOLEAN, |
65 TYPE_INTEGER, | 64 TYPE_INTEGER, |
(...skipping 14 matching lines...) Expand all Loading... |
80 // Returns true if the current object represents a given type. | 79 // Returns true if the current object represents a given type. |
81 bool IsType(ValueType type) const { return type == type_; } | 80 bool IsType(ValueType type) const { return type == type_; } |
82 | 81 |
83 // These methods allow the convenient retrieval of settings. | 82 // These methods allow the convenient retrieval of settings. |
84 // If the current setting object can be converted into the given type, | 83 // If the current setting object can be converted into the given type, |
85 // the value is returned through the "value" parameter and true is returned; | 84 // the value is returned through the "value" parameter and true is returned; |
86 // otherwise, false is returned and "value" is unchanged. | 85 // otherwise, false is returned and "value" is unchanged. |
87 virtual bool GetAsBoolean(bool* out_value) const; | 86 virtual bool GetAsBoolean(bool* out_value) const; |
88 virtual bool GetAsInteger(int* out_value) const; | 87 virtual bool GetAsInteger(int* out_value) const; |
89 virtual bool GetAsReal(double* out_value) const; | 88 virtual bool GetAsReal(double* out_value) const; |
90 virtual bool GetAsString(std::string* out_value) const; | |
91 virtual bool GetAsString(std::wstring* out_value) const; | 89 virtual bool GetAsString(std::wstring* out_value) const; |
92 | 90 |
93 // This creates a deep copy of the entire Value tree, and returns a pointer | 91 // This creates a deep copy of the entire Value tree, and returns a pointer |
94 // to the copy. The caller gets ownership of the copy, of course. | 92 // to the copy. The caller gets ownership of the copy, of course. |
95 virtual Value* DeepCopy() const; | 93 virtual Value* DeepCopy() const; |
96 | 94 |
97 // Compares if two Value objects have equal contents. | 95 // Compares if two Value objects have equal contents. |
98 virtual bool Equals(const Value* other) const; | 96 virtual bool Equals(const Value* other) const; |
99 | 97 |
100 protected: | 98 protected: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 130 |
133 union { | 131 union { |
134 bool boolean_value_; | 132 bool boolean_value_; |
135 int integer_value_; | 133 int integer_value_; |
136 double real_value_; | 134 double real_value_; |
137 }; | 135 }; |
138 }; | 136 }; |
139 | 137 |
140 class StringValue : public Value { | 138 class StringValue : public Value { |
141 public: | 139 public: |
142 // Initializes a StringValue with a UTF-8 narrow character string. | 140 StringValue(const std::wstring& in_value) |
143 StringValue(const std::string& in_value); | 141 : Value(TYPE_STRING), value_(in_value) {} |
144 ~StringValue(); | 142 ~StringValue(); |
145 | 143 |
146 // Subclassed methods | 144 // Subclassed methods |
147 bool GetAsString(std::string* out_value) const; | |
148 bool GetAsString(std::wstring* out_value) const; | 145 bool GetAsString(std::wstring* out_value) const; |
149 Value* DeepCopy() const; | 146 Value* DeepCopy() const; |
150 virtual bool Equals(const Value* other) const; | 147 virtual bool Equals(const Value* other) const; |
151 | 148 |
152 private: | 149 private: |
153 DISALLOW_EVIL_CONSTRUCTORS(StringValue); | 150 DISALLOW_EVIL_CONSTRUCTORS(StringValue); |
154 | 151 |
155 std::string value_; | 152 std::wstring value_; |
156 }; | 153 }; |
157 | 154 |
158 class BinaryValue: public Value { | 155 class BinaryValue: public Value { |
159 public: | 156 public: |
160 // Creates a Value to represent a binary buffer. The new object takes | 157 // Creates a Value to represent a binary buffer. The new object takes |
161 // ownership of the pointer passed in, if successful. | 158 // ownership of the pointer passed in, if successful. |
162 // Returns NULL if buffer is NULL. | 159 // Returns NULL if buffer is NULL. |
163 static BinaryValue* Create(char* buffer, size_t size); | 160 static BinaryValue* Create(char* buffer, size_t size); |
164 | 161 |
165 // For situations where you want to keep ownership of your buffer, this | 162 // For situations where you want to keep ownership of your buffer, this |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 // to the path in that location. | 209 // to the path in that location. |
213 // Note that the dictionary takes ownership of the value | 210 // Note that the dictionary takes ownership of the value |
214 // referenced by in_value. | 211 // referenced by in_value. |
215 bool Set(const std::wstring& path, Value* in_value); | 212 bool Set(const std::wstring& path, Value* in_value); |
216 | 213 |
217 // Convenience forms of Set(). These methods will replace any existing | 214 // Convenience forms of Set(). These methods will replace any existing |
218 // value at that path, even if it has a different type. | 215 // value at that path, even if it has a different type. |
219 bool SetBoolean(const std::wstring& path, bool in_value); | 216 bool SetBoolean(const std::wstring& path, bool in_value); |
220 bool SetInteger(const std::wstring& path, int in_value); | 217 bool SetInteger(const std::wstring& path, int in_value); |
221 bool SetReal(const std::wstring& path, double in_value); | 218 bool SetReal(const std::wstring& path, double in_value); |
222 bool SetString(const std::wstring& path, const std::string& in_value); | |
223 bool SetString(const std::wstring& path, const std::wstring& in_value); | 219 bool SetString(const std::wstring& path, const std::wstring& in_value); |
224 | 220 |
225 // Gets the Value associated with the given path starting from this object. | 221 // Gets the Value associated with the given path starting from this object. |
226 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | 222 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
227 // into the next DictionaryValue down. If the path can be resolved | 223 // into the next DictionaryValue down. If the path can be resolved |
228 // successfully, the value for the last key in the path will be returned | 224 // successfully, the value for the last key in the path will be returned |
229 // through the "value" parameter, and the function will return true. | 225 // through the "value" parameter, and the function will return true. |
230 // Otherwise, it will return false and "value" will be untouched. | 226 // Otherwise, it will return false and "value" will be untouched. |
231 // Note that the dictionary always owns the value that's returned. | 227 // Note that the dictionary always owns the value that's returned. |
232 bool Get(const std::wstring& path, Value** out_value) const; | 228 bool Get(const std::wstring& path, Value** out_value) const; |
233 | 229 |
234 // These are convenience forms of Get(). The value will be retrieved | 230 // These are convenience forms of Get(). The value will be retrieved |
235 // and the return value will be true if the path is valid and the value at | 231 // and the return value will be true if the path is valid and the value at |
236 // the end of the path can be returned in the form specified. | 232 // the end of the path can be returned in the form specified. |
237 bool GetBoolean(const std::wstring& path, bool* out_value) const; | 233 bool GetBoolean(const std::wstring& path, bool* out_value) const; |
238 bool GetInteger(const std::wstring& path, int* out_value) const; | 234 bool GetInteger(const std::wstring& path, int* out_value) const; |
239 bool GetReal(const std::wstring& path, double* out_value) const; | 235 bool GetReal(const std::wstring& path, double* out_value) const; |
240 bool GetString(const std::wstring& path, std::string* out_value) const; | |
241 bool GetString(const std::wstring& path, std::wstring* out_value) const; | 236 bool GetString(const std::wstring& path, std::wstring* out_value) const; |
242 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; | 237 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; |
243 bool GetDictionary(const std::wstring& path, | 238 bool GetDictionary(const std::wstring& path, |
244 DictionaryValue** out_value) const; | 239 DictionaryValue** out_value) const; |
245 bool GetList(const std::wstring& path, ListValue** out_value) const; | 240 bool GetList(const std::wstring& path, ListValue** out_value) const; |
246 | 241 |
247 // Removes the Value with the specified path from this dictionary (or one | 242 // Removes the Value with the specified path from this dictionary (or one |
248 // of its child dictionaries, if the path is more than just a local key). | 243 // of its child dictionaries, if the path is more than just a local key). |
249 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be | 244 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
250 // passed out via out_value. If |out_value| is NULL, the removed value will | 245 // passed out via out_value. If |out_value| is NULL, the removed value will |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 // The method should return true if and only if the root parameter is set | 352 // The method should return true if and only if the root parameter is set |
358 // to a complete Value representation of the serialized form. If the | 353 // to a complete Value representation of the serialized form. If the |
359 // return value is true, the caller takes ownership of the objects pointed | 354 // return value is true, the caller takes ownership of the objects pointed |
360 // to by root. If the return value is false, root should be unchanged and if | 355 // to by root. If the return value is false, root should be unchanged and if |
361 // error_message is non-null, it should be filled with a message describing | 356 // error_message is non-null, it should be filled with a message describing |
362 // the error. | 357 // the error. |
363 virtual bool Deserialize(Value** root, std::string* error_message) = 0; | 358 virtual bool Deserialize(Value** root, std::string* error_message) = 0; |
364 }; | 359 }; |
365 | 360 |
366 #endif // BASE_VALUES_H_ | 361 #endif // BASE_VALUES_H_ |
OLD | NEW |