| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 instantiated | 52 // The Value class is the base class for Values. A Value can be instantiated |
| 53 // via the Create*Value() factory methods, or by directly creating instances of | 53 // via the Create*Value() factory methods, or by directly creating instances of |
| 54 // the subclasses. | 54 // the subclasses. |
| 55 class BASE_EXPORT Value { | 55 class BASE_EXPORT Value { |
| 56 public: | 56 public: |
| 57 enum ValueType { | 57 enum Type { |
| 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 |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 virtual ~Value(); | 68 virtual ~Value(); |
| 69 | 69 |
| 70 // Convenience methods for creating Value objects for various | 70 // Convenience methods for creating Value objects for various |
| 71 // kinds of values without thinking about which class implements them. | 71 // kinds of values without thinking about which class implements them. |
| 72 // These can always be expected to return a valid Value*. | 72 // These can always be expected to return a valid Value*. |
| 73 static Value* CreateNullValue(); | 73 static Value* CreateNullValue(); |
| 74 static FundamentalValue* CreateBooleanValue(bool in_value); | 74 static FundamentalValue* CreateBooleanValue(bool in_value); |
| 75 static FundamentalValue* CreateIntegerValue(int in_value); | 75 static FundamentalValue* CreateIntegerValue(int in_value); |
| 76 static FundamentalValue* CreateDoubleValue(double in_value); | 76 static FundamentalValue* CreateDoubleValue(double in_value); |
| 77 static StringValue* CreateStringValue(const std::string& in_value); | 77 static StringValue* CreateStringValue(const std::string& in_value); |
| 78 static StringValue* CreateStringValue(const string16& in_value); | 78 static StringValue* CreateStringValue(const string16& in_value); |
| 79 | 79 |
| 80 // Returns the type of the value stored by the current Value object. | 80 // Returns the type of the value stored by the current Value object. |
| 81 // Each type will be implemented by only one subclass of Value, so it's | 81 // Each type will be implemented by only one subclass of Value, so it's |
| 82 // safe to use the ValueType to determine whether you can cast from | 82 // safe to use the Type to determine whether you can cast from |
| 83 // Value* to (Implementing Class)*. Also, a Value object never changes | 83 // Value* to (Implementing Class)*. Also, a Value object never changes |
| 84 // its type after construction. | 84 // its type after construction. |
| 85 ValueType GetType() const { return type_; } | 85 Type GetType() const { return type_; } |
| 86 | 86 |
| 87 // Returns true if the current object represents a given type. | 87 // Returns true if the current object represents a given type. |
| 88 bool IsType(ValueType type) const { return type == type_; } | 88 bool IsType(Type type) const { return type == type_; } |
| 89 | 89 |
| 90 // These methods allow the convenient retrieval of settings. | 90 // These methods allow the convenient retrieval of settings. |
| 91 // If the current setting object can be converted into the given type, | 91 // If the current setting object can be converted into the given type, |
| 92 // the value is returned through the |out_value| parameter and true is | 92 // the value is returned through the |out_value| parameter and true is |
| 93 // returned; otherwise, false is returned and |out_value| is unchanged. | 93 // returned; otherwise, false is returned and |out_value| is unchanged. |
| 94 virtual bool GetAsBoolean(bool* out_value) const; | 94 virtual bool GetAsBoolean(bool* out_value) const; |
| 95 virtual bool GetAsInteger(int* out_value) const; | 95 virtual bool GetAsInteger(int* out_value) const; |
| 96 virtual bool GetAsDouble(double* out_value) const; | 96 virtual bool GetAsDouble(double* out_value) const; |
| 97 virtual bool GetAsString(std::string* out_value) const; | 97 virtual bool GetAsString(std::string* out_value) const; |
| 98 virtual bool GetAsString(string16* out_value) const; | 98 virtual bool GetAsString(string16* out_value) const; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 // Compares if two Value objects have equal contents. | 109 // Compares if two Value objects have equal contents. |
| 110 virtual bool Equals(const Value* other) const; | 110 virtual bool Equals(const Value* other) const; |
| 111 | 111 |
| 112 // Compares if two Value objects have equal contents. Can handle NULLs. | 112 // Compares if two Value objects have equal contents. Can handle NULLs. |
| 113 // NULLs are considered equal but different from Value::CreateNullValue(). | 113 // NULLs are considered equal but different from Value::CreateNullValue(). |
| 114 static bool Equals(const Value* a, const Value* b); | 114 static bool Equals(const Value* a, const Value* b); |
| 115 | 115 |
| 116 protected: | 116 protected: |
| 117 // This isn't safe for end-users (they should use the Create*Value() | 117 // This isn't safe for end-users (they should use the Create*Value() |
| 118 // static methods above), but it's useful for subclasses. | 118 // static methods above), but it's useful for subclasses. |
| 119 explicit Value(ValueType type); | 119 explicit Value(Type type); |
| 120 | 120 |
| 121 private: | 121 private: |
| 122 Value(); | 122 Value(); |
| 123 | 123 |
| 124 ValueType type_; | 124 Type type_; |
| 125 | 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(Value); | 126 DISALLOW_COPY_AND_ASSIGN(Value); |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 // FundamentalValue represents the simple fundamental types of values. | 129 // FundamentalValue represents the simple fundamental types of values. |
| 130 class BASE_EXPORT FundamentalValue : public Value { | 130 class BASE_EXPORT FundamentalValue : public Value { |
| 131 public: | 131 public: |
| 132 explicit FundamentalValue(bool in_value); | 132 explicit FundamentalValue(bool in_value); |
| 133 explicit FundamentalValue(int in_value); | 133 explicit FundamentalValue(int in_value); |
| 134 explicit FundamentalValue(double in_value); | 134 explicit FundamentalValue(double in_value); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 | 460 |
| 461 } // namespace base | 461 } // namespace base |
| 462 | 462 |
| 463 // http://crbug.com/88666 | 463 // http://crbug.com/88666 |
| 464 using base::DictionaryValue; | 464 using base::DictionaryValue; |
| 465 using base::ListValue; | 465 using base::ListValue; |
| 466 using base::StringValue; | 466 using base::StringValue; |
| 467 using base::Value; | 467 using base::Value; |
| 468 | 468 |
| 469 #endif // BASE_VALUES_H_ | 469 #endif // BASE_VALUES_H_ |
| OLD | NEW |