| 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/basictypes.h" | 31 #include "base/basictypes.h" |
| 31 #include "base/string16.h" | 32 #include "base/string16.h" |
| 32 #include "build/build_config.h" | 33 #include "build/build_config.h" |
| 33 | 34 |
| 34 class BinaryValue; | 35 class BinaryValue; |
| 35 class DictionaryValue; | 36 class DictionaryValue; |
| 36 class FundamentalValue; | 37 class FundamentalValue; |
| 37 class ListValue; | 38 class ListValue; |
| 38 class StringValue; | 39 class StringValue; |
| 39 class Value; | 40 class Value; |
| 40 | 41 |
| 41 typedef std::vector<Value*> ValueVector; | 42 typedef std::vector<Value*> ValueVector; |
| 42 typedef std::map<std::string, Value*> ValueMap; | 43 typedef std::map<std::string, Value*> ValueMap; |
| 43 | 44 |
| 44 // The Value class is the base class for Values. A Value can be | 45 // The Value class is the base class for Values. A Value can be |
| 45 // instantiated via the Create*Value() factory methods, or by directly | 46 // instantiated via the Create*Value() factory methods, or by directly |
| 46 // creating instances of the subclasses. | 47 // creating instances of the subclasses. |
| 47 class Value { | 48 class BASE_API Value { |
| 48 public: | 49 public: |
| 49 enum ValueType { | 50 enum ValueType { |
| 50 TYPE_NULL = 0, | 51 TYPE_NULL = 0, |
| 51 TYPE_BOOLEAN, | 52 TYPE_BOOLEAN, |
| 52 TYPE_INTEGER, | 53 TYPE_INTEGER, |
| 53 TYPE_DOUBLE, | 54 TYPE_DOUBLE, |
| 54 TYPE_STRING, | 55 TYPE_STRING, |
| 55 TYPE_BINARY, | 56 TYPE_BINARY, |
| 56 TYPE_DICTIONARY, | 57 TYPE_DICTIONARY, |
| 57 TYPE_LIST | 58 TYPE_LIST |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 116 |
| 116 private: | 117 private: |
| 117 Value(); | 118 Value(); |
| 118 | 119 |
| 119 ValueType type_; | 120 ValueType type_; |
| 120 | 121 |
| 121 DISALLOW_COPY_AND_ASSIGN(Value); | 122 DISALLOW_COPY_AND_ASSIGN(Value); |
| 122 }; | 123 }; |
| 123 | 124 |
| 124 // FundamentalValue represents the simple fundamental types of values. | 125 // FundamentalValue represents the simple fundamental types of values. |
| 125 class FundamentalValue : public Value { | 126 class BASE_API FundamentalValue : public Value { |
| 126 public: | 127 public: |
| 127 explicit FundamentalValue(bool in_value); | 128 explicit FundamentalValue(bool in_value); |
| 128 explicit FundamentalValue(int in_value); | 129 explicit FundamentalValue(int in_value); |
| 129 explicit FundamentalValue(double in_value); | 130 explicit FundamentalValue(double in_value); |
| 130 virtual ~FundamentalValue(); | 131 virtual ~FundamentalValue(); |
| 131 | 132 |
| 132 // Subclassed methods | 133 // Subclassed methods |
| 133 virtual bool GetAsBoolean(bool* out_value) const; | 134 virtual bool GetAsBoolean(bool* out_value) const; |
| 134 virtual bool GetAsInteger(int* out_value) const; | 135 virtual bool GetAsInteger(int* out_value) const; |
| 135 virtual bool GetAsDouble(double* out_value) const; | 136 virtual bool GetAsDouble(double* out_value) const; |
| 136 virtual FundamentalValue* DeepCopy() const; | 137 virtual FundamentalValue* DeepCopy() const; |
| 137 virtual bool Equals(const Value* other) const; | 138 virtual bool Equals(const Value* other) const; |
| 138 | 139 |
| 139 private: | 140 private: |
| 140 union { | 141 union { |
| 141 bool boolean_value_; | 142 bool boolean_value_; |
| 142 int integer_value_; | 143 int integer_value_; |
| 143 double double_value_; | 144 double double_value_; |
| 144 }; | 145 }; |
| 145 | 146 |
| 146 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 147 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
| 147 }; | 148 }; |
| 148 | 149 |
| 149 class StringValue : public Value { | 150 class BASE_API StringValue : public Value { |
| 150 public: | 151 public: |
| 151 // Initializes a StringValue with a UTF-8 narrow character string. | 152 // Initializes a StringValue with a UTF-8 narrow character string. |
| 152 explicit StringValue(const std::string& in_value); | 153 explicit StringValue(const std::string& in_value); |
| 153 | 154 |
| 154 // Initializes a StringValue with a string16. | 155 // Initializes a StringValue with a string16. |
| 155 explicit StringValue(const string16& in_value); | 156 explicit StringValue(const string16& in_value); |
| 156 | 157 |
| 157 virtual ~StringValue(); | 158 virtual ~StringValue(); |
| 158 | 159 |
| 159 // Subclassed methods | 160 // Subclassed methods |
| 160 virtual bool GetAsString(std::string* out_value) const; | 161 virtual bool GetAsString(std::string* out_value) const; |
| 161 virtual bool GetAsString(string16* out_value) const; | 162 virtual bool GetAsString(string16* out_value) const; |
| 162 virtual StringValue* DeepCopy() const; | 163 virtual StringValue* DeepCopy() const; |
| 163 virtual bool Equals(const Value* other) const; | 164 virtual bool Equals(const Value* other) const; |
| 164 | 165 |
| 165 private: | 166 private: |
| 166 std::string value_; | 167 std::string value_; |
| 167 | 168 |
| 168 DISALLOW_COPY_AND_ASSIGN(StringValue); | 169 DISALLOW_COPY_AND_ASSIGN(StringValue); |
| 169 }; | 170 }; |
| 170 | 171 |
| 171 class BinaryValue: public Value { | 172 class BASE_API BinaryValue: public Value { |
| 172 public: | 173 public: |
| 173 virtual ~BinaryValue(); | 174 virtual ~BinaryValue(); |
| 174 | 175 |
| 175 // Creates a Value to represent a binary buffer. The new object takes | 176 // Creates a Value to represent a binary buffer. The new object takes |
| 176 // ownership of the pointer passed in, if successful. | 177 // ownership of the pointer passed in, if successful. |
| 177 // Returns NULL if buffer is NULL. | 178 // Returns NULL if buffer is NULL. |
| 178 static BinaryValue* Create(char* buffer, size_t size); | 179 static BinaryValue* Create(char* buffer, size_t size); |
| 179 | 180 |
| 180 // For situations where you want to keep ownership of your buffer, this | 181 // For situations where you want to keep ownership of your buffer, this |
| 181 // factory method creates a new BinaryValue by copying the contents of the | 182 // factory method creates a new BinaryValue by copying the contents of the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 198 | 199 |
| 199 char* buffer_; | 200 char* buffer_; |
| 200 size_t size_; | 201 size_t size_; |
| 201 | 202 |
| 202 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 203 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
| 203 }; | 204 }; |
| 204 | 205 |
| 205 // DictionaryValue provides a key-value dictionary with (optional) "path" | 206 // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 206 // parsing for recursive access; see the comment at the top of the file. Keys | 207 // parsing for recursive access; see the comment at the top of the file. Keys |
| 207 // are |std::string|s and should be UTF-8 encoded. | 208 // are |std::string|s and should be UTF-8 encoded. |
| 208 class DictionaryValue : public Value { | 209 class BASE_API DictionaryValue : public Value { |
| 209 public: | 210 public: |
| 210 DictionaryValue(); | 211 DictionaryValue(); |
| 211 virtual ~DictionaryValue(); | 212 virtual ~DictionaryValue(); |
| 212 | 213 |
| 213 // Returns true if the current dictionary has a value for the given key. | 214 // Returns true if the current dictionary has a value for the given key. |
| 214 bool HasKey(const std::string& key) const; | 215 bool HasKey(const std::string& key) const; |
| 215 | 216 |
| 216 // Returns the number of Values in this dictionary. | 217 // Returns the number of Values in this dictionary. |
| 217 size_t size() const { return dictionary_.size(); } | 218 size_t size() const { return dictionary_.size(); } |
| 218 | 219 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 // the passed in dictionary takes precedence and data already present will be | 307 // the passed in dictionary takes precedence and data already present will be |
| 307 // replaced. | 308 // replaced. |
| 308 void MergeDictionary(const DictionaryValue* dictionary); | 309 void MergeDictionary(const DictionaryValue* dictionary); |
| 309 | 310 |
| 310 // This class provides an iterator for the keys in the dictionary. | 311 // This class provides an iterator for the keys in the dictionary. |
| 311 // It can't be used to modify the dictionary. | 312 // It can't be used to modify the dictionary. |
| 312 // | 313 // |
| 313 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT | 314 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 314 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any | 315 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 315 // keys have '.'s in them. | 316 // keys have '.'s in them. |
| 316 class key_iterator | 317 class BASE_API key_iterator |
| 317 : private std::iterator<std::input_iterator_tag, const std::string> { | 318 : private std::iterator<std::input_iterator_tag, const std::string> { |
| 318 public: | 319 public: |
| 319 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } | 320 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
| 320 key_iterator operator++() { | 321 key_iterator operator++() { |
| 321 ++itr_; | 322 ++itr_; |
| 322 return *this; | 323 return *this; |
| 323 } | 324 } |
| 324 const std::string& operator*() { return itr_->first; } | 325 const std::string& operator*() { return itr_->first; } |
| 325 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 326 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 326 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 327 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 327 | 328 |
| 328 private: | 329 private: |
| 329 ValueMap::const_iterator itr_; | 330 ValueMap::const_iterator itr_; |
| 330 }; | 331 }; |
| 331 | 332 |
| 332 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 333 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 333 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 334 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 334 | 335 |
| 335 // Overridden from Value: | 336 // Overridden from Value: |
| 336 virtual DictionaryValue* DeepCopy() const; | 337 virtual DictionaryValue* DeepCopy() const; |
| 337 virtual bool Equals(const Value* other) const; | 338 virtual bool Equals(const Value* other) const; |
| 338 | 339 |
| 339 private: | 340 private: |
| 340 ValueMap dictionary_; | 341 ValueMap dictionary_; |
| 341 | 342 |
| 342 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 343 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
| 343 }; | 344 }; |
| 344 | 345 |
| 345 // This type of Value represents a list of other Value values. | 346 // This type of Value represents a list of other Value values. |
| 346 class ListValue : public Value { | 347 class BASE_API ListValue : public Value { |
| 347 public: | 348 public: |
| 348 typedef ValueVector::iterator iterator; | 349 typedef ValueVector::iterator iterator; |
| 349 typedef ValueVector::const_iterator const_iterator; | 350 typedef ValueVector::const_iterator const_iterator; |
| 350 | 351 |
| 351 ListValue(); | 352 ListValue(); |
| 352 ~ListValue(); | 353 ~ListValue(); |
| 353 | 354 |
| 354 // Clears the contents of this ListValue | 355 // Clears the contents of this ListValue |
| 355 void Clear(); | 356 void Clear(); |
| 356 | 357 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 virtual bool Equals(const Value* other) const; | 426 virtual bool Equals(const Value* other) const; |
| 426 | 427 |
| 427 private: | 428 private: |
| 428 ValueVector list_; | 429 ValueVector list_; |
| 429 | 430 |
| 430 DISALLOW_COPY_AND_ASSIGN(ListValue); | 431 DISALLOW_COPY_AND_ASSIGN(ListValue); |
| 431 }; | 432 }; |
| 432 | 433 |
| 433 // This interface is implemented by classes that know how to serialize and | 434 // This interface is implemented by classes that know how to serialize and |
| 434 // deserialize Value objects. | 435 // deserialize Value objects. |
| 435 class ValueSerializer { | 436 class BASE_API ValueSerializer { |
| 436 public: | 437 public: |
| 437 virtual ~ValueSerializer(); | 438 virtual ~ValueSerializer(); |
| 438 | 439 |
| 439 virtual bool Serialize(const Value& root) = 0; | 440 virtual bool Serialize(const Value& root) = 0; |
| 440 | 441 |
| 441 // This method deserializes the subclass-specific format into a Value object. | 442 // This method deserializes the subclass-specific format into a Value object. |
| 442 // If the return value is non-NULL, the caller takes ownership of returned | 443 // If the return value is non-NULL, the caller takes ownership of returned |
| 443 // Value. If the return value is NULL, and if error_code is non-NULL, | 444 // Value. If the return value is NULL, and if error_code is non-NULL, |
| 444 // error_code will be set with the underlying error. | 445 // error_code will be set with the underlying error. |
| 445 // If |error_message| is non-null, it will be filled in with a formatted | 446 // If |error_message| is non-null, it will be filled in with a formatted |
| 446 // error message including the location of the error if appropriate. | 447 // error message including the location of the error if appropriate. |
| 447 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 448 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
| 448 }; | 449 }; |
| 449 | 450 |
| 450 #endif // BASE_VALUES_H_ | 451 #endif // BASE_VALUES_H_ |
| OLD | NEW |