| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 TYPE_INTEGER, | 59 TYPE_INTEGER, |
| 60 TYPE_DOUBLE, | 60 TYPE_DOUBLE, |
| 61 TYPE_STRING, | 61 TYPE_STRING, |
| 62 TYPE_BINARY, | 62 TYPE_BINARY, |
| 63 TYPE_DICTIONARY, | 63 TYPE_DICTIONARY, |
| 64 TYPE_LIST | 64 TYPE_LIST |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 virtual ~Value(); | 67 virtual ~Value(); |
| 68 | 68 |
| 69 // Convenience methods for creating Value objects for various | |
| 70 // kinds of values without thinking about which class implements them. | |
| 71 // These can always be expected to return a valid Value*. | |
| 72 static Value* CreateNullValue(); | 69 static Value* CreateNullValue(); |
| 70 // DEPRECATED: Do not use the following 5 functions. Instead, use |
| 71 // new FundamentalValue or new StringValue. |
| 73 static FundamentalValue* CreateBooleanValue(bool in_value); | 72 static FundamentalValue* CreateBooleanValue(bool in_value); |
| 74 static FundamentalValue* CreateIntegerValue(int in_value); | 73 static FundamentalValue* CreateIntegerValue(int in_value); |
| 75 static FundamentalValue* CreateDoubleValue(double in_value); | 74 static FundamentalValue* CreateDoubleValue(double in_value); |
| 76 static StringValue* CreateStringValue(const std::string& in_value); | 75 static StringValue* CreateStringValue(const std::string& in_value); |
| 77 static StringValue* CreateStringValue(const string16& in_value); | 76 static StringValue* CreateStringValue(const string16& in_value); |
| 78 | 77 |
| 79 // Returns the type of the value stored by the current Value object. | 78 // Returns the type of the value stored by the current Value object. |
| 80 // Each type will be implemented by only one subclass of Value, so it's | 79 // Each type will be implemented by only one subclass of Value, so it's |
| 81 // safe to use the Type to determine whether you can cast from | 80 // safe to use the Type to determine whether you can cast from |
| 82 // Value* to (Implementing Class)*. Also, a Value object never changes | 81 // Value* to (Implementing Class)*. Also, a Value object never changes |
| (...skipping 25 matching lines...) Expand all Loading... |
| 108 virtual Value* DeepCopy() const; | 107 virtual Value* DeepCopy() const; |
| 109 | 108 |
| 110 // Compares if two Value objects have equal contents. | 109 // Compares if two Value objects have equal contents. |
| 111 virtual bool Equals(const Value* other) const; | 110 virtual bool Equals(const Value* other) const; |
| 112 | 111 |
| 113 // Compares if two Value objects have equal contents. Can handle NULLs. | 112 // Compares if two Value objects have equal contents. Can handle NULLs. |
| 114 // NULLs are considered equal but different from Value::CreateNullValue(). | 113 // NULLs are considered equal but different from Value::CreateNullValue(). |
| 115 static bool Equals(const Value* a, const Value* b); | 114 static bool Equals(const Value* a, const Value* b); |
| 116 | 115 |
| 117 protected: | 116 protected: |
| 118 // This isn't safe for end-users (they should use the Create*Value() | 117 // These aren't safe for end-users, but they are useful for subclasses. |
| 119 // static methods above), but it's useful for subclasses. | |
| 120 explicit Value(Type type); | 118 explicit Value(Type type); |
| 119 Value(const Value& that); |
| 120 Value& operator=(const Value& that); |
| 121 | 121 |
| 122 private: | 122 private: |
| 123 Value(); | |
| 124 | |
| 125 Type type_; | 123 Type type_; |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(Value); | |
| 128 }; | 124 }; |
| 129 | 125 |
| 130 // FundamentalValue represents the simple fundamental types of values. | 126 // FundamentalValue represents the simple fundamental types of values. |
| 131 class BASE_EXPORT FundamentalValue : public Value { | 127 class BASE_EXPORT FundamentalValue : public Value { |
| 132 public: | 128 public: |
| 133 explicit FundamentalValue(bool in_value); | 129 explicit FundamentalValue(bool in_value); |
| 134 explicit FundamentalValue(int in_value); | 130 explicit FundamentalValue(int in_value); |
| 135 explicit FundamentalValue(double in_value); | 131 explicit FundamentalValue(double in_value); |
| 136 virtual ~FundamentalValue(); | 132 virtual ~FundamentalValue(); |
| 137 | 133 |
| 138 // Overridden from Value: | 134 // Overridden from Value: |
| 139 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; | 135 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
| 140 virtual bool GetAsInteger(int* out_value) const OVERRIDE; | 136 virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
| 141 virtual bool GetAsDouble(double* out_value) const OVERRIDE; | 137 virtual bool GetAsDouble(double* out_value) const OVERRIDE; |
| 142 virtual FundamentalValue* DeepCopy() const OVERRIDE; | 138 virtual FundamentalValue* DeepCopy() const OVERRIDE; |
| 143 virtual bool Equals(const Value* other) const OVERRIDE; | 139 virtual bool Equals(const Value* other) const OVERRIDE; |
| 144 | 140 |
| 145 private: | 141 private: |
| 146 union { | 142 union { |
| 147 bool boolean_value_; | 143 bool boolean_value_; |
| 148 int integer_value_; | 144 int integer_value_; |
| 149 double double_value_; | 145 double double_value_; |
| 150 }; | 146 }; |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | |
| 153 }; | 147 }; |
| 154 | 148 |
| 155 class BASE_EXPORT StringValue : public Value { | 149 class BASE_EXPORT StringValue : public Value { |
| 156 public: | 150 public: |
| 157 // Initializes a StringValue with a UTF-8 narrow character string. | 151 // Initializes a StringValue with a UTF-8 narrow character string. |
| 158 explicit StringValue(const std::string& in_value); | 152 explicit StringValue(const std::string& in_value); |
| 159 | 153 |
| 160 // Initializes a StringValue with a string16. | 154 // Initializes a StringValue with a string16. |
| 161 explicit StringValue(const string16& in_value); | 155 explicit StringValue(const string16& in_value); |
| 162 | 156 |
| 163 virtual ~StringValue(); | 157 virtual ~StringValue(); |
| 164 | 158 |
| 165 // Overridden from Value: | 159 // Overridden from Value: |
| 166 virtual bool GetAsString(std::string* out_value) const OVERRIDE; | 160 virtual bool GetAsString(std::string* out_value) const OVERRIDE; |
| 167 virtual bool GetAsString(string16* out_value) const OVERRIDE; | 161 virtual bool GetAsString(string16* out_value) const OVERRIDE; |
| 168 virtual StringValue* DeepCopy() const OVERRIDE; | 162 virtual StringValue* DeepCopy() const OVERRIDE; |
| 169 virtual bool Equals(const Value* other) const OVERRIDE; | 163 virtual bool Equals(const Value* other) const OVERRIDE; |
| 170 | 164 |
| 171 private: | 165 private: |
| 172 std::string value_; | 166 std::string value_; |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(StringValue); | |
| 175 }; | 167 }; |
| 176 | 168 |
| 177 class BASE_EXPORT BinaryValue: public Value { | 169 class BASE_EXPORT BinaryValue: public Value { |
| 178 public: | 170 public: |
| 179 virtual ~BinaryValue(); | 171 virtual ~BinaryValue(); |
| 180 | 172 |
| 181 // Creates a Value to represent a binary buffer. The new object takes | 173 // Creates a Value to represent a binary buffer. The new object takes |
| 182 // ownership of the pointer passed in, if successful. | 174 // ownership of the pointer passed in, if successful. |
| 183 // Returns NULL if buffer is NULL. | 175 // Returns NULL if buffer is NULL. |
| 184 static BinaryValue* Create(char* buffer, size_t size); | 176 static BinaryValue* Create(char* buffer, size_t size); |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 522 |
| 531 } // namespace base | 523 } // namespace base |
| 532 | 524 |
| 533 // http://crbug.com/88666 | 525 // http://crbug.com/88666 |
| 534 using base::DictionaryValue; | 526 using base::DictionaryValue; |
| 535 using base::ListValue; | 527 using base::ListValue; |
| 536 using base::StringValue; | 528 using base::StringValue; |
| 537 using base::Value; | 529 using base::Value; |
| 538 | 530 |
| 539 #endif // BASE_VALUES_H_ | 531 #endif // BASE_VALUES_H_ |
| OLD | NEW |