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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 DISALLOW_COPY_AND_ASSIGN(Value); | 118 DISALLOW_COPY_AND_ASSIGN(Value); |
119 }; | 119 }; |
120 | 120 |
121 // FundamentalValue represents the simple fundamental types of values. | 121 // FundamentalValue represents the simple fundamental types of values. |
122 class FundamentalValue : public Value { | 122 class FundamentalValue : public Value { |
123 public: | 123 public: |
124 explicit FundamentalValue(bool in_value); | 124 explicit FundamentalValue(bool in_value); |
125 explicit FundamentalValue(int in_value); | 125 explicit FundamentalValue(int in_value); |
126 explicit FundamentalValue(double in_value); | 126 explicit FundamentalValue(double in_value); |
127 ~FundamentalValue(); | 127 virtual ~FundamentalValue(); |
128 | 128 |
129 // Subclassed methods | 129 // Subclassed methods |
130 virtual bool GetAsBoolean(bool* out_value) const; | 130 virtual bool GetAsBoolean(bool* out_value) const; |
131 virtual bool GetAsInteger(int* out_value) const; | 131 virtual bool GetAsInteger(int* out_value) const; |
132 virtual bool GetAsReal(double* out_value) const; | 132 virtual bool GetAsReal(double* out_value) const; |
133 virtual Value* DeepCopy() const; | 133 virtual Value* DeepCopy() const; |
134 virtual bool Equals(const Value* other) const; | 134 virtual bool Equals(const Value* other) const; |
135 | 135 |
136 private: | 136 private: |
137 union { | 137 union { |
138 bool boolean_value_; | 138 bool boolean_value_; |
139 int integer_value_; | 139 int integer_value_; |
140 double real_value_; | 140 double real_value_; |
141 }; | 141 }; |
142 | 142 |
143 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); | 143 DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
144 }; | 144 }; |
145 | 145 |
146 class StringValue : public Value { | 146 class StringValue : public Value { |
147 public: | 147 public: |
148 // Initializes a StringValue with a UTF-8 narrow character string. | 148 // Initializes a StringValue with a UTF-8 narrow character string. |
149 explicit StringValue(const std::string& in_value); | 149 explicit StringValue(const std::string& in_value); |
150 | 150 |
151 // Initializes a StringValue with a string16. | 151 // Initializes a StringValue with a string16. |
152 explicit StringValue(const string16& in_value); | 152 explicit StringValue(const string16& in_value); |
153 | 153 |
154 ~StringValue(); | 154 virtual ~StringValue(); |
155 | 155 |
156 // Subclassed methods | 156 // Subclassed methods |
157 bool GetAsString(std::string* out_value) const; | 157 virtual bool GetAsString(std::string* out_value) const; |
158 bool GetAsString(string16* out_value) const; | 158 virtual bool GetAsString(string16* out_value) const; |
159 Value* DeepCopy() const; | 159 virtual Value* DeepCopy() const; |
160 virtual bool Equals(const Value* other) const; | 160 virtual bool Equals(const Value* other) const; |
161 | 161 |
162 private: | 162 private: |
163 std::string value_; | 163 std::string value_; |
164 | 164 |
165 DISALLOW_COPY_AND_ASSIGN(StringValue); | 165 DISALLOW_COPY_AND_ASSIGN(StringValue); |
166 }; | 166 }; |
167 | 167 |
168 class BinaryValue: public Value { | 168 class BinaryValue: public Value { |
169 public: | 169 public: |
170 // Creates a Value to represent a binary buffer. The new object takes | 170 // Creates a Value to represent a binary buffer. The new object takes |
171 // ownership of the pointer passed in, if successful. | 171 // ownership of the pointer passed in, if successful. |
172 // Returns NULL if buffer is NULL. | 172 // Returns NULL if buffer is NULL. |
173 static BinaryValue* Create(char* buffer, size_t size); | 173 static BinaryValue* Create(char* buffer, size_t size); |
174 | 174 |
175 // For situations where you want to keep ownership of your buffer, this | 175 // For situations where you want to keep ownership of your buffer, this |
176 // factory method creates a new BinaryValue by copying the contents of the | 176 // factory method creates a new BinaryValue by copying the contents of the |
177 // buffer that's passed in. | 177 // buffer that's passed in. |
178 // Returns NULL if buffer is NULL. | 178 // Returns NULL if buffer is NULL. |
179 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); | 179 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
180 | 180 |
181 ~BinaryValue(); | 181 virtual ~BinaryValue(); |
182 | 182 |
183 // Subclassed methods | 183 // Subclassed methods |
184 Value* DeepCopy() const; | 184 virtual Value* DeepCopy() const; |
185 virtual bool Equals(const Value* other) const; | 185 virtual bool Equals(const Value* other) const; |
186 | 186 |
187 size_t GetSize() const { return size_; } | 187 size_t GetSize() const { return size_; } |
188 char* GetBuffer() { return buffer_; } | 188 char* GetBuffer() { return buffer_; } |
189 const char* GetBuffer() const { return buffer_; } | 189 const char* GetBuffer() const { return buffer_; } |
190 | 190 |
191 private: | 191 private: |
192 // Constructor is private so that only objects with valid buffer pointers | 192 // Constructor is private so that only objects with valid buffer pointers |
193 // and size values can be created. | 193 // and size values can be created. |
194 BinaryValue(char* buffer, size_t size); | 194 BinaryValue(char* buffer, size_t size); |
195 | 195 |
196 char* buffer_; | 196 char* buffer_; |
197 size_t size_; | 197 size_t size_; |
198 | 198 |
199 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 199 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
200 }; | 200 }; |
201 | 201 |
202 // DictionaryValue provides a key-value dictionary with (optional) "path" | 202 // DictionaryValue provides a key-value dictionary with (optional) "path" |
203 // parsing for recursive access; see the comment at the top of the file. Keys | 203 // parsing for recursive access; see the comment at the top of the file. Keys |
204 // are |std::string|s and should be UTF-8 encoded. | 204 // are |std::string|s and should be UTF-8 encoded. |
205 class DictionaryValue : public Value { | 205 class DictionaryValue : public Value { |
206 public: | 206 public: |
207 DictionaryValue(); | 207 DictionaryValue(); |
208 ~DictionaryValue(); | 208 virtual ~DictionaryValue(); |
209 | 209 |
210 // Subclassed methods | 210 // Subclassed methods |
211 Value* DeepCopy() const; | 211 virtual Value* DeepCopy() const; |
212 virtual bool Equals(const Value* other) const; | 212 virtual bool Equals(const Value* other) const; |
213 | 213 |
214 // Returns true if the current dictionary has a value for the given key. | 214 // Returns true if the current dictionary has a value for the given key. |
215 bool HasKey(const std::string& key) const; | 215 bool HasKey(const std::string& key) const; |
216 | 216 |
217 // Returns the number of Values in this dictionary. | 217 // Returns the number of Values in this dictionary. |
218 size_t size() const { return dictionary_.size(); } | 218 size_t size() const { return dictionary_.size(); } |
219 | 219 |
220 // Returns whether the dictionary is empty. | 220 // Returns whether the dictionary is empty. |
221 bool empty() const { return dictionary_.empty(); } | 221 bool empty() const { return dictionary_.empty(); } |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 }; | 361 }; |
362 | 362 |
363 // This type of Value represents a list of other Value values. | 363 // This type of Value represents a list of other Value values. |
364 class ListValue : public Value { | 364 class ListValue : public Value { |
365 public: | 365 public: |
366 ListValue(); | 366 ListValue(); |
367 ~ListValue(); | 367 ~ListValue(); |
368 | 368 |
369 // Subclassed methods | 369 // Subclassed methods |
370 virtual bool GetAsList(ListValue** out_value); | 370 virtual bool GetAsList(ListValue** out_value); |
371 Value* DeepCopy() const; | 371 virtual Value* DeepCopy() const; |
372 virtual bool Equals(const Value* other) const; | 372 virtual bool Equals(const Value* other) const; |
373 | 373 |
374 // Clears the contents of this ListValue | 374 // Clears the contents of this ListValue |
375 void Clear(); | 375 void Clear(); |
376 | 376 |
377 // Returns the number of Values in this list. | 377 // Returns the number of Values in this list. |
378 size_t GetSize() const { return list_.size(); } | 378 size_t GetSize() const { return list_.size(); } |
379 | 379 |
380 // Returns whether the list is empty. | 380 // Returns whether the list is empty. |
381 bool empty() const { return list_.empty(); } | 381 bool empty() const { return list_.empty(); } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 // This method deserializes the subclass-specific format into a Value object. | 458 // This method deserializes the subclass-specific format into a Value object. |
459 // If the return value is non-NULL, the caller takes ownership of returned | 459 // If the return value is non-NULL, the caller takes ownership of returned |
460 // Value. If the return value is NULL, and if error_code is non-NULL, | 460 // Value. If the return value is NULL, and if error_code is non-NULL, |
461 // error_code will be set with the underlying error. | 461 // error_code will be set with the underlying error. |
462 // If |error_message| is non-null, it will be filled in with a formatted | 462 // If |error_message| is non-null, it will be filled in with a formatted |
463 // error message including the location of the error if appropriate. | 463 // error message including the location of the error if appropriate. |
464 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; | 464 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
465 }; | 465 }; |
466 | 466 |
467 #endif // BASE_VALUES_H_ | 467 #endif // BASE_VALUES_H_ |
OLD | NEW |