| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // This creates a deep copy of the entire Value tree, and returns a pointer | 93 // 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. | 94 // to the copy. The caller gets ownership of the copy, of course. |
| 95 virtual Value* DeepCopy() const; | 95 virtual Value* DeepCopy() const; |
| 96 | 96 |
| 97 // Compares if two Value objects have equal contents. | 97 // Compares if two Value objects have equal contents. |
| 98 virtual bool Equals(const Value* other) const; | 98 virtual bool Equals(const Value* other) const; |
| 99 | 99 |
| 100 protected: | 100 protected: |
| 101 // This isn't safe for end-users (they should use the Create*Value() | 101 // This isn't safe for end-users (they should use the Create*Value() |
| 102 // static methods above), but it's useful for subclasses. | 102 // static methods above), but it's useful for subclasses. |
| 103 Value(ValueType type) : type_(type) {} | 103 explicit Value(ValueType type) : type_(type) {} |
| 104 | 104 |
| 105 private: | 105 private: |
| 106 DISALLOW_EVIL_CONSTRUCTORS(Value); | |
| 107 Value(); | 106 Value(); |
| 108 | 107 |
| 109 ValueType type_; | 108 ValueType type_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(Value); |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 // FundamentalValue represents the simple fundamental types of values. | 113 // FundamentalValue represents the simple fundamental types of values. |
| 113 class FundamentalValue : public Value { | 114 class FundamentalValue : public Value { |
| 114 public: | 115 public: |
| 115 FundamentalValue(bool in_value) | 116 explicit FundamentalValue(bool in_value) |
| 116 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {} | 117 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {} |
| 117 FundamentalValue(int in_value) | 118 explicit FundamentalValue(int in_value) |
| 118 : Value(TYPE_INTEGER), integer_value_(in_value) {} | 119 : Value(TYPE_INTEGER), integer_value_(in_value) {} |
| 119 FundamentalValue(double in_value) | 120 explicit FundamentalValue(double in_value) |
| 120 : Value(TYPE_REAL), real_value_(in_value) {} | 121 : Value(TYPE_REAL), real_value_(in_value) {} |
| 121 ~FundamentalValue(); | 122 ~FundamentalValue(); |
| 122 | 123 |
| 123 // Subclassed methods | 124 // Subclassed methods |
| 124 virtual bool GetAsBoolean(bool* out_value) const; | 125 virtual bool GetAsBoolean(bool* out_value) const; |
| 125 virtual bool GetAsInteger(int* out_value) const; | 126 virtual bool GetAsInteger(int* out_value) const; |
| 126 virtual bool GetAsReal(double* out_value) const; | 127 virtual bool GetAsReal(double* out_value) const; |
| 127 virtual Value* DeepCopy() const; | 128 virtual Value* DeepCopy() const; |
| 128 virtual bool Equals(const Value* other) const; | 129 virtual bool Equals(const Value* other) const; |
| 129 | 130 |
| 130 private: | 131 private: |
| 131 DISALLOW_EVIL_CONSTRUCTORS(FundamentalValue); | |
| 132 | |
| 133 union { | 132 union { |
| 134 bool boolean_value_; | 133 bool boolean_value_; |
| 135 int integer_value_; | 134 int integer_value_; |
| 136 double real_value_; | 135 double real_value_; |
| 137 }; | 136 }; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
| 138 }; | 139 }; |
| 139 | 140 |
| 140 class StringValue : public Value { | 141 class StringValue : public Value { |
| 141 public: | 142 public: |
| 142 // Initializes a StringValue with a UTF-8 narrow character string. | 143 // Initializes a StringValue with a UTF-8 narrow character string. |
| 143 StringValue(const std::string& in_value); | 144 explicit StringValue(const std::string& in_value); |
| 144 | 145 |
| 145 // Initializes a StringValue with a wide character string. | 146 // Initializes a StringValue with a wide character string. |
| 146 StringValue(const std::wstring& in_value); | 147 explicit StringValue(const std::wstring& in_value); |
| 147 | 148 |
| 148 ~StringValue(); | 149 ~StringValue(); |
| 149 | 150 |
| 150 // Subclassed methods | 151 // Subclassed methods |
| 151 bool GetAsString(std::string* out_value) const; | 152 bool GetAsString(std::string* out_value) const; |
| 152 bool GetAsString(std::wstring* out_value) const; | 153 bool GetAsString(std::wstring* out_value) const; |
| 153 Value* DeepCopy() const; | 154 Value* DeepCopy() const; |
| 154 virtual bool Equals(const Value* other) const; | 155 virtual bool Equals(const Value* other) const; |
| 155 | 156 |
| 156 private: | 157 private: |
| 157 DISALLOW_EVIL_CONSTRUCTORS(StringValue); | 158 std::string value_; |
| 158 | 159 |
| 159 std::string value_; | 160 DISALLOW_COPY_AND_ASSIGN(StringValue); |
| 160 }; | 161 }; |
| 161 | 162 |
| 162 class BinaryValue: public Value { | 163 class BinaryValue: public Value { |
| 163 public: | 164 public: |
| 164 // Creates a Value to represent a binary buffer. The new object takes | 165 // Creates a Value to represent a binary buffer. The new object takes |
| 165 // ownership of the pointer passed in, if successful. | 166 // ownership of the pointer passed in, if successful. |
| 166 // Returns NULL if buffer is NULL. | 167 // Returns NULL if buffer is NULL. |
| 167 static BinaryValue* Create(char* buffer, size_t size); | 168 static BinaryValue* Create(char* buffer, size_t size); |
| 168 | 169 |
| 169 // For situations where you want to keep ownership of your buffer, this | 170 // For situations where you want to keep ownership of your buffer, this |
| 170 // factory method creates a new BinaryValue by copying the contents of the | 171 // factory method creates a new BinaryValue by copying the contents of the |
| 171 // buffer that's passed in. | 172 // buffer that's passed in. |
| 172 // Returns NULL if buffer is NULL. | 173 // Returns NULL if buffer is NULL. |
| 173 static BinaryValue* CreateWithCopiedBuffer(char* buffer, size_t size); | 174 static BinaryValue* CreateWithCopiedBuffer(char* buffer, size_t size); |
| 174 | 175 |
| 175 ~BinaryValue(); | 176 ~BinaryValue(); |
| 176 | 177 |
| 177 // Subclassed methods | 178 // Subclassed methods |
| 178 Value* DeepCopy() const; | 179 Value* DeepCopy() const; |
| 179 virtual bool Equals(const Value* other) const; | 180 virtual bool Equals(const Value* other) const; |
| 180 | 181 |
| 181 size_t GetSize() const { return size_; } | 182 size_t GetSize() const { return size_; } |
| 182 char* GetBuffer() { return buffer_; } | 183 char* GetBuffer() { return buffer_; } |
| 183 | 184 |
| 184 private: | 185 private: |
| 185 DISALLOW_EVIL_CONSTRUCTORS(BinaryValue); | |
| 186 | |
| 187 // Constructor is private so that only objects with valid buffer pointers | 186 // Constructor is private so that only objects with valid buffer pointers |
| 188 // and size values can be created. | 187 // and size values can be created. |
| 189 BinaryValue(char* buffer, size_t size); | 188 BinaryValue(char* buffer, size_t size); |
| 190 | 189 |
| 191 char* buffer_; | 190 char* buffer_; |
| 192 size_t size_; | 191 size_t size_; |
| 192 |
| 193 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
| 193 }; | 194 }; |
| 194 | 195 |
| 195 class DictionaryValue : public Value { | 196 class DictionaryValue : public Value { |
| 196 public: | 197 public: |
| 197 DictionaryValue() : Value(TYPE_DICTIONARY) {} | 198 DictionaryValue() : Value(TYPE_DICTIONARY) {} |
| 198 ~DictionaryValue(); | 199 ~DictionaryValue(); |
| 199 | 200 |
| 200 // Subclassed methods | 201 // Subclassed methods |
| 201 Value* DeepCopy() const; | 202 Value* DeepCopy() const; |
| 202 virtual bool Equals(const Value* other) const; | 203 virtual bool Equals(const Value* other) const; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // passed out via out_value. If |out_value| is NULL, the removed value will | 258 // passed out via out_value. If |out_value| is NULL, the removed value will |
| 258 // be deleted. This method returns true if |path| is a valid path; otherwise | 259 // be deleted. This method returns true if |path| is a valid path; otherwise |
| 259 // it will return false and the DictionaryValue object will be unchanged. | 260 // it will return false and the DictionaryValue object will be unchanged. |
| 260 bool Remove(const std::wstring& path, Value** out_value); | 261 bool Remove(const std::wstring& path, Value** out_value); |
| 261 | 262 |
| 262 // This class provides an iterator for the keys in the dictionary. | 263 // This class provides an iterator for the keys in the dictionary. |
| 263 // It can't be used to modify the dictionary. | 264 // It can't be used to modify the dictionary. |
| 264 class key_iterator | 265 class key_iterator |
| 265 : private std::iterator<std::input_iterator_tag, const std::wstring> { | 266 : private std::iterator<std::input_iterator_tag, const std::wstring> { |
| 266 public: | 267 public: |
| 267 key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } | 268 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
| 268 key_iterator operator++() { ++itr_; return *this; } | 269 key_iterator operator++() { ++itr_; return *this; } |
| 269 const std::wstring& operator*() { return itr_->first; } | 270 const std::wstring& operator*() { return itr_->first; } |
| 270 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } | 271 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 271 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 272 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 272 | 273 |
| 273 private: | 274 private: |
| 274 ValueMap::const_iterator itr_; | 275 ValueMap::const_iterator itr_; |
| 275 }; | 276 }; |
| 276 | 277 |
| 277 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 278 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 278 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 279 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 279 | 280 |
| 280 private: | 281 private: |
| 281 DISALLOW_EVIL_CONSTRUCTORS(DictionaryValue); | |
| 282 | |
| 283 // Associates the value |in_value| with the |key|. This method should be | 282 // Associates the value |in_value| with the |key|. This method should be |
| 284 // used instead of "dictionary_[key] = foo" so that any previous value can | 283 // used instead of "dictionary_[key] = foo" so that any previous value can |
| 285 // be properly deleted. | 284 // be properly deleted. |
| 286 void SetInCurrentNode(const std::wstring& key, Value* in_value); | 285 void SetInCurrentNode(const std::wstring& key, Value* in_value); |
| 287 | 286 |
| 288 ValueMap dictionary_; | 287 ValueMap dictionary_; |
| 288 |
| 289 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
| 289 }; | 290 }; |
| 290 | 291 |
| 291 // This type of Value represents a list of other Value values. | 292 // This type of Value represents a list of other Value values. |
| 292 class ListValue : public Value { | 293 class ListValue : public Value { |
| 293 public: | 294 public: |
| 294 ListValue() : Value(TYPE_LIST) {} | 295 ListValue() : Value(TYPE_LIST) {} |
| 295 ~ListValue(); | 296 ~ListValue(); |
| 296 | 297 |
| 297 // Subclassed methods | 298 // Subclassed methods |
| 298 Value* DeepCopy() const; | 299 Value* DeepCopy() const; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 typedef ValueVector::iterator iterator; | 350 typedef ValueVector::iterator iterator; |
| 350 typedef ValueVector::const_iterator const_iterator; | 351 typedef ValueVector::const_iterator const_iterator; |
| 351 | 352 |
| 352 ListValue::iterator begin() { return list_.begin(); } | 353 ListValue::iterator begin() { return list_.begin(); } |
| 353 ListValue::iterator end() { return list_.end(); } | 354 ListValue::iterator end() { return list_.end(); } |
| 354 | 355 |
| 355 ListValue::const_iterator begin() const { return list_.begin(); } | 356 ListValue::const_iterator begin() const { return list_.begin(); } |
| 356 ListValue::const_iterator end() const { return list_.end(); } | 357 ListValue::const_iterator end() const { return list_.end(); } |
| 357 | 358 |
| 358 private: | 359 private: |
| 359 DISALLOW_EVIL_CONSTRUCTORS(ListValue); | 360 ValueVector list_; |
| 360 | 361 |
| 361 ValueVector list_; | 362 DISALLOW_COPY_AND_ASSIGN(ListValue); |
| 362 }; | 363 }; |
| 363 | 364 |
| 364 // This interface is implemented by classes that know how to serialize and | 365 // This interface is implemented by classes that know how to serialize and |
| 365 // deserialize Value objects. | 366 // deserialize Value objects. |
| 366 class ValueSerializer { | 367 class ValueSerializer { |
| 367 public: | 368 public: |
| 368 virtual ~ValueSerializer() {} | 369 virtual ~ValueSerializer() {} |
| 369 | 370 |
| 370 virtual bool Serialize(const Value& root) = 0; | 371 virtual bool Serialize(const Value& root) = 0; |
| 371 | 372 |
| 372 // This method deserializes the subclass-specific format into a Value object. | 373 // This method deserializes the subclass-specific format into a Value object. |
| 373 // If the return value is non-NULL, the caller takes ownership of returned | 374 // If the return value is non-NULL, the caller takes ownership of returned |
| 374 // Value. If the return value is NULL, and if error_message is non-NULL, | 375 // Value. If the return value is NULL, and if error_message is non-NULL, |
| 375 // error_message should be filled with a message describing the error. | 376 // error_message should be filled with a message describing the error. |
| 376 virtual Value* Deserialize(std::string* error_message) = 0; | 377 virtual Value* Deserialize(std::string* error_message) = 0; |
| 377 }; | 378 }; |
| 378 | 379 |
| 379 #endif // BASE_VALUES_H_ | 380 #endif // BASE_VALUES_H_ |
| OLD | NEW |