OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 virtual ~Value(); | 49 virtual ~Value(); |
50 | 50 |
51 // Convenience methods for creating Value objects for various | 51 // Convenience methods for creating Value objects for various |
52 // kinds of values without thinking about which class implements them. | 52 // kinds of values without thinking about which class implements them. |
53 // These can always be expected to return a valid Value*. | 53 // These can always be expected to return a valid Value*. |
54 static Value* CreateNullValue(); | 54 static Value* CreateNullValue(); |
55 static Value* CreateBooleanValue(bool in_value); | 55 static Value* CreateBooleanValue(bool in_value); |
56 static Value* CreateIntegerValue(int in_value); | 56 static Value* CreateIntegerValue(int in_value); |
57 static Value* CreateRealValue(double in_value); | 57 static Value* CreateRealValue(double in_value); |
58 static Value* CreateStringValue(const std::string& in_value); | 58 static Value* CreateStringValue(const std::string& in_value); |
59 static Value* CreateStringValue(const std::wstring& in_value); | 59 static Value* CreateStringValue(const string16& in_value); |
60 static Value* CreateStringValueFromUTF16(const string16& in_value); | 60 #if !defined(WCHAR_T_IS_UTF16) |
| 61 /*DEPRECATED*/static Value* CreateStringValue(const std::wstring& in_value); |
| 62 #endif |
61 | 63 |
62 // This one can return NULL if the input isn't valid. If the return value | 64 // This one can return NULL if the input isn't valid. If the return value |
63 // is non-null, the new object has taken ownership of the buffer pointer. | 65 // is non-null, the new object has taken ownership of the buffer pointer. |
64 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); | 66 static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
65 | 67 |
66 typedef enum { | 68 typedef enum { |
67 TYPE_NULL = 0, | 69 TYPE_NULL = 0, |
68 TYPE_BOOLEAN, | 70 TYPE_BOOLEAN, |
69 TYPE_INTEGER, | 71 TYPE_INTEGER, |
70 TYPE_REAL, | 72 TYPE_REAL, |
(...skipping 14 matching lines...) Expand all Loading... |
85 bool IsType(ValueType type) const { return type == type_; } | 87 bool IsType(ValueType type) const { return type == type_; } |
86 | 88 |
87 // These methods allow the convenient retrieval of settings. | 89 // These methods allow the convenient retrieval of settings. |
88 // If the current setting object can be converted into the given type, | 90 // If the current setting object can be converted into the given type, |
89 // the value is returned through the |out_value| parameter and true is | 91 // the value is returned through the |out_value| parameter and true is |
90 // returned; otherwise, false is returned and |out_value| is unchanged. | 92 // returned; otherwise, false is returned and |out_value| is unchanged. |
91 virtual bool GetAsBoolean(bool* out_value) const; | 93 virtual bool GetAsBoolean(bool* out_value) const; |
92 virtual bool GetAsInteger(int* out_value) const; | 94 virtual bool GetAsInteger(int* out_value) const; |
93 virtual bool GetAsReal(double* out_value) const; | 95 virtual bool GetAsReal(double* out_value) const; |
94 virtual bool GetAsString(std::string* out_value) const; | 96 virtual bool GetAsString(std::string* out_value) const; |
95 virtual bool GetAsString(std::wstring* out_value) const; | 97 virtual bool GetAsString(string16* out_value) const; |
96 virtual bool GetAsUTF16(string16* out_value) const; | 98 #if !defined(WCHAR_T_IS_UTF16) |
| 99 /*DEPRECATED*/virtual bool GetAsString(std::wstring* out_value) const; |
| 100 #endif |
97 | 101 |
98 // This creates a deep copy of the entire Value tree, and returns a pointer | 102 // This creates a deep copy of the entire Value tree, and returns a pointer |
99 // to the copy. The caller gets ownership of the copy, of course. | 103 // to the copy. The caller gets ownership of the copy, of course. |
100 virtual Value* DeepCopy() const; | 104 virtual Value* DeepCopy() const; |
101 | 105 |
102 // Compares if two Value objects have equal contents. | 106 // Compares if two Value objects have equal contents. |
103 virtual bool Equals(const Value* other) const; | 107 virtual bool Equals(const Value* other) const; |
104 | 108 |
105 protected: | 109 protected: |
106 // This isn't safe for end-users (they should use the Create*Value() | 110 // This isn't safe for end-users (they should use the Create*Value() |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 }; | 142 }; |
139 | 143 |
140 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 144 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
141 }; | 145 }; |
142 | 146 |
143 class StringValue : public Value { | 147 class StringValue : public Value { |
144 public: | 148 public: |
145 // Initializes a StringValue with a UTF-8 narrow character string. | 149 // Initializes a StringValue with a UTF-8 narrow character string. |
146 explicit StringValue(const std::string& in_value); | 150 explicit StringValue(const std::string& in_value); |
147 | 151 |
148 // Initializes a StringValue with a wide character string. | 152 // Initializes a StringValue with a string16. |
149 explicit StringValue(const std::wstring& in_value); | 153 explicit StringValue(const string16& in_value); |
150 | 154 |
151 #if !defined(WCHAR_T_IS_UTF16) | 155 #if !defined(WCHAR_T_IS_UTF16) |
152 // Initializes a StringValue with a string16. | 156 // Initializes a StringValue with a wide character string. |
153 explicit StringValue(const string16& in_value); | 157 /*DEPRECATED*/explicit StringValue(const std::wstring& in_value); |
154 #endif | 158 #endif |
155 | 159 |
156 ~StringValue(); | 160 ~StringValue(); |
157 | 161 |
158 // Subclassed methods | 162 // Subclassed methods |
159 bool GetAsString(std::string* out_value) const; | 163 bool GetAsString(std::string* out_value) const; |
160 bool GetAsString(std::wstring* out_value) const; | 164 bool GetAsString(string16* out_value) const; |
161 bool GetAsUTF16(string16* out_value) const; | 165 #if !defined(WCHAR_T_IS_UTF16) |
| 166 /*DEPRECATED*/bool GetAsString(std::wstring* out_value) const; |
| 167 #endif |
162 Value* DeepCopy() const; | 168 Value* DeepCopy() const; |
163 virtual bool Equals(const Value* other) const; | 169 virtual bool Equals(const Value* other) const; |
164 | 170 |
165 private: | 171 private: |
166 std::string value_; | 172 std::string value_; |
167 | 173 |
168 DISALLOW_COPY_AND_ASSIGN(StringValue); | 174 DISALLOW_COPY_AND_ASSIGN(StringValue); |
169 }; | 175 }; |
170 | 176 |
171 class BinaryValue: public Value { | 177 class BinaryValue: public Value { |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // This method deserializes the subclass-specific format into a Value object. | 491 // This method deserializes the subclass-specific format into a Value object. |
486 // If the return value is non-NULL, the caller takes ownership of returned | 492 // If the return value is non-NULL, the caller takes ownership of returned |
487 // Value. If the return value is NULL, and if error_code is non-NULL, | 493 // Value. If the return value is NULL, and if error_code is non-NULL, |
488 // error_code will be set with the underlying error. | 494 // error_code will be set with the underlying error. |
489 // If |error_message| is non-null, it will be filled in with a formatted | 495 // If |error_message| is non-null, it will be filled in with a formatted |
490 // error message including the location of the error if appropriate. | 496 // error message including the location of the error if appropriate. |
491 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 497 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
492 }; | 498 }; |
493 | 499 |
494 #endif // BASE_VALUES_H_ | 500 #endif // BASE_VALUES_H_ |
OLD | NEW |