| 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 11 matching lines...) Expand all Loading... |
| 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_export.h" | 30 #include "base/base_export.h" |
| 31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
| 32 #include "base/compiler_specific.h" |
| 32 #include "base/string16.h" | 33 #include "base/string16.h" |
| 33 | 34 |
| 34 // 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 |
| 35 // current code can use these classes without the base namespace. In | 36 // current code can use these classes without the base namespace. In |
| 36 // 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 |
| 37 // "using" declaration. | 38 // "using" declaration. |
| 38 // http://crbug.com/88666 | 39 // http://crbug.com/88666 |
| 39 namespace base { | 40 namespace base { |
| 40 | 41 |
| 41 class BinaryValue; | 42 class BinaryValue; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 }; | 127 }; |
| 127 | 128 |
| 128 // FundamentalValue represents the simple fundamental types of values. | 129 // FundamentalValue represents the simple fundamental types of values. |
| 129 class BASE_EXPORT FundamentalValue : public Value { | 130 class BASE_EXPORT FundamentalValue : public Value { |
| 130 public: | 131 public: |
| 131 explicit FundamentalValue(bool in_value); | 132 explicit FundamentalValue(bool in_value); |
| 132 explicit FundamentalValue(int in_value); | 133 explicit FundamentalValue(int in_value); |
| 133 explicit FundamentalValue(double in_value); | 134 explicit FundamentalValue(double in_value); |
| 134 virtual ~FundamentalValue(); | 135 virtual ~FundamentalValue(); |
| 135 | 136 |
| 136 // Subclassed methods | 137 // Overridden from Value: |
| 137 virtual bool GetAsBoolean(bool* out_value) const; | 138 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
| 138 virtual bool GetAsInteger(int* out_value) const; | 139 virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
| 139 virtual bool GetAsDouble(double* out_value) const; | 140 virtual bool GetAsDouble(double* out_value) const OVERRIDE; |
| 140 virtual FundamentalValue* DeepCopy() const; | 141 virtual FundamentalValue* DeepCopy() const OVERRIDE; |
| 141 virtual bool Equals(const Value* other) const; | 142 virtual bool Equals(const Value* other) const OVERRIDE; |
| 142 | 143 |
| 143 private: | 144 private: |
| 144 union { | 145 union { |
| 145 bool boolean_value_; | 146 bool boolean_value_; |
| 146 int integer_value_; | 147 int integer_value_; |
| 147 double double_value_; | 148 double double_value_; |
| 148 }; | 149 }; |
| 149 | 150 |
| 150 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 151 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
| 151 }; | 152 }; |
| 152 | 153 |
| 153 class BASE_EXPORT StringValue : public Value { | 154 class BASE_EXPORT StringValue : public Value { |
| 154 public: | 155 public: |
| 155 // Initializes a StringValue with a UTF-8 narrow character string. | 156 // Initializes a StringValue with a UTF-8 narrow character string. |
| 156 explicit StringValue(const std::string& in_value); | 157 explicit StringValue(const std::string& in_value); |
| 157 | 158 |
| 158 // Initializes a StringValue with a string16. | 159 // Initializes a StringValue with a string16. |
| 159 explicit StringValue(const string16& in_value); | 160 explicit StringValue(const string16& in_value); |
| 160 | 161 |
| 161 virtual ~StringValue(); | 162 virtual ~StringValue(); |
| 162 | 163 |
| 163 // Subclassed methods | 164 // Overridden from Value: |
| 164 virtual bool GetAsString(std::string* out_value) const; | 165 virtual bool GetAsString(std::string* out_value) const OVERRIDE; |
| 165 virtual bool GetAsString(string16* out_value) const; | 166 virtual bool GetAsString(string16* out_value) const OVERRIDE; |
| 166 virtual StringValue* DeepCopy() const; | 167 virtual StringValue* DeepCopy() const OVERRIDE; |
| 167 virtual bool Equals(const Value* other) const; | 168 virtual bool Equals(const Value* other) const OVERRIDE; |
| 168 | 169 |
| 169 private: | 170 private: |
| 170 std::string value_; | 171 std::string value_; |
| 171 | 172 |
| 172 DISALLOW_COPY_AND_ASSIGN(StringValue); | 173 DISALLOW_COPY_AND_ASSIGN(StringValue); |
| 173 }; | 174 }; |
| 174 | 175 |
| 175 class BASE_EXPORT BinaryValue: public Value { | 176 class BASE_EXPORT BinaryValue: public Value { |
| 176 public: | 177 public: |
| 177 virtual ~BinaryValue(); | 178 virtual ~BinaryValue(); |
| 178 | 179 |
| 179 // Creates a Value to represent a binary buffer. The new object takes | 180 // Creates a Value to represent a binary buffer. The new object takes |
| 180 // ownership of the pointer passed in, if successful. | 181 // ownership of the pointer passed in, if successful. |
| 181 // Returns NULL if buffer is NULL. | 182 // Returns NULL if buffer is NULL. |
| 182 static BinaryValue* Create(char* buffer, size_t size); | 183 static BinaryValue* Create(char* buffer, size_t size); |
| 183 | 184 |
| 184 // For situations where you want to keep ownership of your buffer, this | 185 // For situations where you want to keep ownership of your buffer, this |
| 185 // factory method creates a new BinaryValue by copying the contents of the | 186 // factory method creates a new BinaryValue by copying the contents of the |
| 186 // buffer that's passed in. | 187 // buffer that's passed in. |
| 187 // Returns NULL if buffer is NULL. | 188 // Returns NULL if buffer is NULL. |
| 188 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); | 189 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
| 189 | 190 |
| 190 size_t GetSize() const { return size_; } | 191 size_t GetSize() const { return size_; } |
| 191 char* GetBuffer() { return buffer_; } | 192 char* GetBuffer() { return buffer_; } |
| 192 const char* GetBuffer() const { return buffer_; } | 193 const char* GetBuffer() const { return buffer_; } |
| 193 | 194 |
| 194 // Overridden from Value: | 195 // Overridden from Value: |
| 195 virtual BinaryValue* DeepCopy() const; | 196 virtual BinaryValue* DeepCopy() const OVERRIDE; |
| 196 virtual bool Equals(const Value* other) const; | 197 virtual bool Equals(const Value* other) const OVERRIDE; |
| 197 | 198 |
| 198 private: | 199 private: |
| 199 // Constructor is private so that only objects with valid buffer pointers | 200 // Constructor is private so that only objects with valid buffer pointers |
| 200 // and size values can be created. | 201 // and size values can be created. |
| 201 BinaryValue(char* buffer, size_t size); | 202 BinaryValue(char* buffer, size_t size); |
| 202 | 203 |
| 203 char* buffer_; | 204 char* buffer_; |
| 204 size_t size_; | 205 size_t size_; |
| 205 | 206 |
| 206 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 207 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } | 336 bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 336 | 337 |
| 337 private: | 338 private: |
| 338 ValueMap::const_iterator itr_; | 339 ValueMap::const_iterator itr_; |
| 339 }; | 340 }; |
| 340 | 341 |
| 341 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } | 342 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 342 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } | 343 key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 343 | 344 |
| 344 // Overridden from Value: | 345 // Overridden from Value: |
| 345 virtual DictionaryValue* DeepCopy() const; | 346 virtual DictionaryValue* DeepCopy() const OVERRIDE; |
| 346 virtual bool Equals(const Value* other) const; | 347 virtual bool Equals(const Value* other) const OVERRIDE; |
| 347 | 348 |
| 348 private: | 349 private: |
| 349 ValueMap dictionary_; | 350 ValueMap dictionary_; |
| 350 | 351 |
| 351 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); | 352 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
| 352 }; | 353 }; |
| 353 | 354 |
| 354 // This type of Value represents a list of other Value values. | 355 // This type of Value represents a list of other Value values. |
| 355 class BASE_EXPORT ListValue : public Value { | 356 class BASE_EXPORT ListValue : public Value { |
| 356 public: | 357 public: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 | 415 |
| 415 // Insert a Value at index. | 416 // Insert a Value at index. |
| 416 // Returns true if successful, or false if the index was out of range. | 417 // Returns true if successful, or false if the index was out of range. |
| 417 bool Insert(size_t index, Value* in_value); | 418 bool Insert(size_t index, Value* in_value); |
| 418 | 419 |
| 419 // Swaps contents with the |other| list. | 420 // Swaps contents with the |other| list. |
| 420 void Swap(ListValue* other) { | 421 void Swap(ListValue* other) { |
| 421 list_.swap(other->list_); | 422 list_.swap(other->list_); |
| 422 } | 423 } |
| 423 | 424 |
| 424 // Iteration | 425 // Iteration. |
| 425 ListValue::iterator begin() { return list_.begin(); } | 426 iterator begin() { return list_.begin(); } |
| 426 ListValue::iterator end() { return list_.end(); } | 427 iterator end() { return list_.end(); } |
| 427 | 428 |
| 428 ListValue::const_iterator begin() const { return list_.begin(); } | 429 const_iterator begin() const { return list_.begin(); } |
| 429 ListValue::const_iterator end() const { return list_.end(); } | 430 const_iterator end() const { return list_.end(); } |
| 430 | 431 |
| 431 // Overridden from Value: | 432 // Overridden from Value: |
| 432 virtual bool GetAsList(ListValue** out_value); | 433 virtual bool GetAsList(ListValue** out_value) OVERRIDE; |
| 433 virtual bool GetAsList(const ListValue** out_value) const; | 434 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE; |
| 434 virtual ListValue* DeepCopy() const; | 435 virtual ListValue* DeepCopy() const OVERRIDE; |
| 435 virtual bool Equals(const Value* other) const; | 436 virtual bool Equals(const Value* other) const OVERRIDE; |
| 436 | 437 |
| 437 private: | 438 private: |
| 438 ValueVector list_; | 439 ValueVector list_; |
| 439 | 440 |
| 440 DISALLOW_COPY_AND_ASSIGN(ListValue); | 441 DISALLOW_COPY_AND_ASSIGN(ListValue); |
| 441 }; | 442 }; |
| 442 | 443 |
| 443 // This interface is implemented by classes that know how to serialize and | 444 // This interface is implemented by classes that know how to serialize and |
| 444 // deserialize Value objects. | 445 // deserialize Value objects. |
| 445 class BASE_EXPORT ValueSerializer { | 446 class BASE_EXPORT ValueSerializer { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 459 | 460 |
| 460 } // namespace base | 461 } // namespace base |
| 461 | 462 |
| 462 // http://crbug.com/88666 | 463 // http://crbug.com/88666 |
| 463 using base::DictionaryValue; | 464 using base::DictionaryValue; |
| 464 using base::ListValue; | 465 using base::ListValue; |
| 465 using base::StringValue; | 466 using base::StringValue; |
| 466 using base::Value; | 467 using base::Value; |
| 467 | 468 |
| 468 #endif // BASE_VALUES_H_ | 469 #endif // BASE_VALUES_H_ |
| OLD | NEW |