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: |
11 // | 11 // |
12 // root->SetString("global.pages.homepage", "http://goateleporter.com"); | 12 // root->SetString("global.pages.homepage", "http://goateleporter.com"); |
13 // std::string homepage = "http://google.com"; // default/fallback value | 13 // std::string homepage = "http://google.com"; // default/fallback value |
14 // root->GetString("global.pages.homepage", &homepage); | 14 // root->GetString("global.pages.homepage", &homepage); |
15 // | 15 // |
16 // where "global" and "pages" are also DictionaryValues, and "homepage" is a | 16 // where "global" and "pages" are also DictionaryValues, and "homepage" is a |
17 // string setting. If some elements of the path didn't exist yet, the | 17 // string setting. If some elements of the path didn't exist yet, the |
18 // SetString() method would create the missing elements and attach them to root | 18 // SetString() method would create the missing elements and attach them to root |
19 // before attaching the homepage value. | 19 // before attaching the homepage value. |
20 | 20 |
21 #ifndef BASE_VALUES_H_ | 21 #ifndef BASE_VALUES_H_ |
22 #define BASE_VALUES_H_ | 22 #define BASE_VALUES_H_ |
23 #pragma once | 23 #pragma once |
24 | 24 |
25 #include <iterator> | 25 #include <iterator> |
26 #include <map> | 26 #include <map> |
27 #include <string> | 27 #include <string> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
30 #include "base/base_api.h" | 30 #include "base/base_export.h" |
31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
32 #include "base/string16.h" | 32 #include "base/string16.h" |
33 #include "build/build_config.h" | 33 #include "build/build_config.h" |
34 | 34 |
35 // This file declares "using base::Value", etc. at the bottom, so that | 35 // This file declares "using base::Value", etc. at the bottom, so that |
36 // current code can use these classes without the base namespace. In | 36 // current code can use these classes without the base namespace. In |
37 // new code, please always use base::Value, etc. or add your own | 37 // new code, please always use base::Value, etc. or add your own |
38 // "using" declaration. | 38 // "using" declaration. |
39 // http://crbug.com/88666 | 39 // http://crbug.com/88666 |
40 namespace base { | 40 namespace base { |
41 | 41 |
42 class BinaryValue; | 42 class BinaryValue; |
43 class DictionaryValue; | 43 class DictionaryValue; |
44 class FundamentalValue; | 44 class FundamentalValue; |
45 class ListValue; | 45 class ListValue; |
46 class StringValue; | 46 class StringValue; |
47 class Value; | 47 class Value; |
48 | 48 |
49 typedef std::vector<Value*> ValueVector; | 49 typedef std::vector<Value*> ValueVector; |
50 typedef std::map<std::string, Value*> ValueMap; | 50 typedef std::map<std::string, Value*> ValueMap; |
51 | 51 |
52 // The Value class is the base class for Values. A Value can be | 52 // The Value class is the base class for Values. A Value can be |
53 // instantiated via the Create*Value() factory methods, or by directly | 53 // instantiated via the Create*Value() factory methods, or by directly |
54 // creating instances of the subclasses. | 54 // creating instances of the subclasses. |
55 class BASE_API Value { | 55 class BASE_EXPORT Value { |
56 public: | 56 public: |
57 enum ValueType { | 57 enum ValueType { |
58 TYPE_NULL = 0, | 58 TYPE_NULL = 0, |
59 TYPE_BOOLEAN, | 59 TYPE_BOOLEAN, |
60 TYPE_INTEGER, | 60 TYPE_INTEGER, |
61 TYPE_DOUBLE, | 61 TYPE_DOUBLE, |
62 TYPE_STRING, | 62 TYPE_STRING, |
63 TYPE_BINARY, | 63 TYPE_BINARY, |
64 TYPE_DICTIONARY, | 64 TYPE_DICTIONARY, |
65 TYPE_LIST | 65 TYPE_LIST |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 private: | 125 private: |
126 Value(); | 126 Value(); |
127 | 127 |
128 ValueType type_; | 128 ValueType type_; |
129 | 129 |
130 DISALLOW_COPY_AND_ASSIGN(Value); | 130 DISALLOW_COPY_AND_ASSIGN(Value); |
131 }; | 131 }; |
132 | 132 |
133 // FundamentalValue represents the simple fundamental types of values. | 133 // FundamentalValue represents the simple fundamental types of values. |
134 class BASE_API FundamentalValue : public Value { | 134 class BASE_EXPORT FundamentalValue : public Value { |
135 public: | 135 public: |
136 explicit FundamentalValue(bool in_value); | 136 explicit FundamentalValue(bool in_value); |
137 explicit FundamentalValue(int in_value); | 137 explicit FundamentalValue(int in_value); |
138 explicit FundamentalValue(double in_value); | 138 explicit FundamentalValue(double in_value); |
139 virtual ~FundamentalValue(); | 139 virtual ~FundamentalValue(); |
140 | 140 |
141 // Subclassed methods | 141 // Subclassed methods |
142 virtual bool GetAsBoolean(bool* out_value) const; | 142 virtual bool GetAsBoolean(bool* out_value) const; |
143 virtual bool GetAsInteger(int* out_value) const; | 143 virtual bool GetAsInteger(int* out_value) const; |
144 virtual bool GetAsDouble(double* out_value) const; | 144 virtual bool GetAsDouble(double* out_value) const; |
145 virtual FundamentalValue* DeepCopy() const; | 145 virtual FundamentalValue* DeepCopy() const; |
146 virtual bool Equals(const Value* other) const; | 146 virtual bool Equals(const Value* other) const; |
147 | 147 |
148 private: | 148 private: |
149 union { | 149 union { |
150 bool boolean_value_; | 150 bool boolean_value_; |
151 int integer_value_; | 151 int integer_value_; |
152 double double_value_; | 152 double double_value_; |
153 }; | 153 }; |
154 | 154 |
155 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 155 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
156 }; | 156 }; |
157 | 157 |
158 class BASE_API StringValue : public Value { | 158 class BASE_EXPORT StringValue : public Value { |
159 public: | 159 public: |
160 // Initializes a StringValue with a UTF-8 narrow character string. | 160 // Initializes a StringValue with a UTF-8 narrow character string. |
161 explicit StringValue(const std::string& in_value); | 161 explicit StringValue(const std::string& in_value); |
162 | 162 |
163 // Initializes a StringValue with a string16. | 163 // Initializes a StringValue with a string16. |
164 explicit StringValue(const string16& in_value); | 164 explicit StringValue(const string16& in_value); |
165 | 165 |
166 virtual ~StringValue(); | 166 virtual ~StringValue(); |
167 | 167 |
168 // Subclassed methods | 168 // Subclassed methods |
169 virtual bool GetAsString(std::string* out_value) const; | 169 virtual bool GetAsString(std::string* out_value) const; |
170 virtual bool GetAsString(string16* out_value) const; | 170 virtual bool GetAsString(string16* out_value) const; |
171 virtual StringValue* DeepCopy() const; | 171 virtual StringValue* DeepCopy() const; |
172 virtual bool Equals(const Value* other) const; | 172 virtual bool Equals(const Value* other) const; |
173 | 173 |
174 private: | 174 private: |
175 std::string value_; | 175 std::string value_; |
176 | 176 |
177 DISALLOW_COPY_AND_ASSIGN(StringValue); | 177 DISALLOW_COPY_AND_ASSIGN(StringValue); |
178 }; | 178 }; |
179 | 179 |
180 class BASE_API BinaryValue: public Value { | 180 class BASE_EXPORT BinaryValue: public Value { |
181 public: | 181 public: |
182 virtual ~BinaryValue(); | 182 virtual ~BinaryValue(); |
183 | 183 |
184 // Creates a Value to represent a binary buffer. The new object takes | 184 // Creates a Value to represent a binary buffer. The new object takes |
185 // ownership of the pointer passed in, if successful. | 185 // ownership of the pointer passed in, if successful. |
186 // Returns NULL if buffer is NULL. | 186 // Returns NULL if buffer is NULL. |
187 static BinaryValue* Create(char* buffer, size_t size); | 187 static BinaryValue* Create(char* buffer, size_t size); |
188 | 188 |
189 // For situations where you want to keep ownership of your buffer, this | 189 // For situations where you want to keep ownership of your buffer, this |
190 // factory method creates a new BinaryValue by copying the contents of the | 190 // factory method creates a new BinaryValue by copying the contents of the |
(...skipping 16 matching lines...) Expand all Loading... |
207 | 207 |
208 char* buffer_; | 208 char* buffer_; |
209 size_t size_; | 209 size_t size_; |
210 | 210 |
211 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 211 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
212 }; | 212 }; |
213 | 213 |
214 // DictionaryValue provides a key-value dictionary with (optional) "path" | 214 // DictionaryValue provides a key-value dictionary with (optional) "path" |
215 // parsing for recursive access; see the comment at the top of the file. Keys | 215 // parsing for recursive access; see the comment at the top of the file. Keys |
216 // are |std::string|s and should be UTF-8 encoded. | 216 // are |std::string|s and should be UTF-8 encoded. |
217 class BASE_API DictionaryValue : public Value { | 217 class BASE_EXPORT DictionaryValue : public Value { |
218 public: | 218 public: |
219 DictionaryValue(); | 219 DictionaryValue(); |
220 virtual ~DictionaryValue(); | 220 virtual ~DictionaryValue(); |
221 | 221 |
222 // Returns true if the current dictionary has a value for the given key. | 222 // Returns true if the current dictionary has a value for the given key. |
223 bool HasKey(const std::string& key) const; | 223 bool HasKey(const std::string& key) const; |
224 | 224 |
225 // Returns the number of Values in this dictionary. | 225 // Returns the number of Values in this dictionary. |
226 size_t size() const { return dictionary_.size(); } | 226 size_t size() const { return dictionary_.size(); } |
227 | 227 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 virtual DictionaryValue* DeepCopy() const; | 350 virtual DictionaryValue* DeepCopy() const; |
351 virtual bool Equals(const Value* other) const; | 351 virtual bool Equals(const Value* other) const; |
352 | 352 |
353 private: | 353 private: |
354 ValueMap dictionary_; | 354 ValueMap dictionary_; |
355 | 355 |
356 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 356 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
357 }; | 357 }; |
358 | 358 |
359 // This type of Value represents a list of other Value values. | 359 // This type of Value represents a list of other Value values. |
360 class BASE_API ListValue : public Value { | 360 class BASE_EXPORT ListValue : public Value { |
361 public: | 361 public: |
362 typedef ValueVector::iterator iterator; | 362 typedef ValueVector::iterator iterator; |
363 typedef ValueVector::const_iterator const_iterator; | 363 typedef ValueVector::const_iterator const_iterator; |
364 | 364 |
365 ListValue(); | 365 ListValue(); |
366 virtual ~ListValue(); | 366 virtual ~ListValue(); |
367 | 367 |
368 // Clears the contents of this ListValue | 368 // Clears the contents of this ListValue |
369 void Clear(); | 369 void Clear(); |
370 | 370 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 virtual bool Equals(const Value* other) const; | 440 virtual bool Equals(const Value* other) const; |
441 | 441 |
442 private: | 442 private: |
443 ValueVector list_; | 443 ValueVector list_; |
444 | 444 |
445 DISALLOW_COPY_AND_ASSIGN(ListValue); | 445 DISALLOW_COPY_AND_ASSIGN(ListValue); |
446 }; | 446 }; |
447 | 447 |
448 // This interface is implemented by classes that know how to serialize and | 448 // This interface is implemented by classes that know how to serialize and |
449 // deserialize Value objects. | 449 // deserialize Value objects. |
450 class BASE_API ValueSerializer { | 450 class BASE_EXPORT ValueSerializer { |
451 public: | 451 public: |
452 virtual ~ValueSerializer(); | 452 virtual ~ValueSerializer(); |
453 | 453 |
454 virtual bool Serialize(const Value& root) = 0; | 454 virtual bool Serialize(const Value& root) = 0; |
455 | 455 |
456 // This method deserializes the subclass-specific format into a Value object. | 456 // This method deserializes the subclass-specific format into a Value object. |
457 // If the return value is non-NULL, the caller takes ownership of returned | 457 // If the return value is non-NULL, the caller takes ownership of returned |
458 // Value. If the return value is NULL, and if error_code is non-NULL, | 458 // Value. If the return value is NULL, and if error_code is non-NULL, |
459 // error_code will be set with the underlying error. | 459 // error_code will be set with the underlying error. |
460 // If |error_message| is non-null, it will be filled in with a formatted | 460 // If |error_message| is non-null, it will be filled in with a formatted |
461 // error message including the location of the error if appropriate. | 461 // error message including the location of the error if appropriate. |
462 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 462 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
463 }; | 463 }; |
464 | 464 |
465 } // namespace base | 465 } // namespace base |
466 | 466 |
467 // http://crbug.com/88666 | 467 // http://crbug.com/88666 |
468 using base::BinaryValue; | 468 using base::BinaryValue; |
469 using base::DictionaryValue; | 469 using base::DictionaryValue; |
470 using base::FundamentalValue; | 470 using base::FundamentalValue; |
471 using base::ListValue; | 471 using base::ListValue; |
472 using base::StringValue; | 472 using base::StringValue; |
473 using base::Value; | 473 using base::Value; |
474 | 474 |
475 #endif // BASE_VALUES_H_ | 475 #endif // BASE_VALUES_H_ |
OLD | NEW |