blob: cd68b10944688ce7283f468a540603e09a91e7df [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5// This file specifies a recursive data storage class called Value
6// intended for storing setting and other persistable data.
7// It includes the ability to specify (recursive) lists and dictionaries, so
8// it's fairly expressive. However, the API is optimized for the common case,
9// namely storing a hierarchical tree of simple values. Given a
10// DictionaryValue root, you can easily do things like:
11//
12// root->SetString(L"global.pages.homepage", L"http://goateleporter.com");
13// std::wstring homepage = L"http://google.com"; // default/fallback value
14// root->GetString(L"global.pages.homepage", &homepage);
15//
16// where "global" and "pages" are also DictionaryValues, and "homepage"
17// is a string setting. If some elements of the path didn't exist yet,
18// the SetString() method would create the missing elements and attach them
19// to root before attaching the homepage value.
20
darin@chromium.org01f10702008-09-27 05:22:42 +090021#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commit3f4a7322008-07-27 06:49:38 +090023
24#include <iterator>
25#include <map>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090026#include <string>
initial.commit3f4a7322008-07-27 06:49:38 +090027#include <vector>
28
29#include "base/basictypes.h"
30
31class Value;
32class FundamentalValue;
33class StringValue;
34class BinaryValue;
35class DictionaryValue;
36class ListValue;
37
38typedef std::vector<Value*> ValueVector;
nsylvain@chromium.org12426672009-03-04 07:59:43 +090039typedef std::map<std::wstring, Value*> ValueMap;
initial.commit3f4a7322008-07-27 06:49:38 +090040
41// The Value class is the base class for Values. A Value can be
42// instantiated via the Create*Value() factory methods, or by directly
43// creating instances of the subclasses.
44class Value {
45 public:
46 virtual ~Value();
47
48 // Convenience methods for creating Value objects for various
49 // kinds of values without thinking about which class implements them.
50 // These can always be expected to return a valid Value*.
51 static Value* CreateNullValue();
52 static Value* CreateBooleanValue(bool in_value);
53 static Value* CreateIntegerValue(int in_value);
54 static Value* CreateRealValue(double in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090055 static Value* CreateStringValue(const std::string& in_value);
initial.commit3f4a7322008-07-27 06:49:38 +090056 static Value* CreateStringValue(const std::wstring& in_value);
57
58 // This one can return NULL if the input isn't valid. If the return value
59 // is non-null, the new object has taken ownership of the buffer pointer.
60 static BinaryValue* CreateBinaryValue(char* buffer, size_t size);
61
62 typedef enum {
63 TYPE_NULL = 0,
64 TYPE_BOOLEAN,
65 TYPE_INTEGER,
66 TYPE_REAL,
67 TYPE_STRING,
68 TYPE_BINARY,
69 TYPE_DICTIONARY,
70 TYPE_LIST
71 } ValueType;
72
73 // Returns the type of the value stored by the current Value object.
74 // Each type will be implemented by only one subclass of Value, so it's
75 // safe to use the ValueType to determine whether you can cast from
76 // Value* to (Implementing Class)*. Also, a Value object never changes
77 // its type after construction.
78 ValueType GetType() const { return type_; }
79
80 // Returns true if the current object represents a given type.
81 bool IsType(ValueType type) const { return type == type_; }
82
83 // These methods allow the convenient retrieval of settings.
84 // If the current setting object can be converted into the given type,
85 // the value is returned through the "value" parameter and true is returned;
86 // otherwise, false is returned and "value" is unchanged.
87 virtual bool GetAsBoolean(bool* out_value) const;
88 virtual bool GetAsInteger(int* out_value) const;
89 virtual bool GetAsReal(double* out_value) const;
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090090 virtual bool GetAsString(std::string* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +090091 virtual bool GetAsString(std::wstring* out_value) const;
92
93 // This creates a deep copy of the entire Value tree, and returns a pointer
94 // to the copy. The caller gets ownership of the copy, of course.
95 virtual Value* DeepCopy() const;
96
97 // Compares if two Value objects have equal contents.
98 virtual bool Equals(const Value* other) const;
99
100 protected:
101 // This isn't safe for end-users (they should use the Create*Value()
102 // static methods above), but it's useful for subclasses.
103 Value(ValueType type) : type_(type) {}
104
105 private:
106 DISALLOW_EVIL_CONSTRUCTORS(Value);
107 Value();
108
109 ValueType type_;
110};
111
112// FundamentalValue represents the simple fundamental types of values.
113class FundamentalValue : public Value {
114 public:
115 FundamentalValue(bool in_value)
116 : Value(TYPE_BOOLEAN), boolean_value_(in_value) {}
117 FundamentalValue(int in_value)
118 : Value(TYPE_INTEGER), integer_value_(in_value) {}
119 FundamentalValue(double in_value)
120 : Value(TYPE_REAL), real_value_(in_value) {}
121 ~FundamentalValue();
122
123 // Subclassed methods
124 virtual bool GetAsBoolean(bool* out_value) const;
125 virtual bool GetAsInteger(int* out_value) const;
126 virtual bool GetAsReal(double* out_value) const;
127 virtual Value* DeepCopy() const;
128 virtual bool Equals(const Value* other) const;
129
130 private:
131 DISALLOW_EVIL_CONSTRUCTORS(FundamentalValue);
132
133 union {
134 bool boolean_value_;
135 int integer_value_;
136 double real_value_;
137 };
138};
139
140class StringValue : public Value {
141 public:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900142 // Initializes a StringValue with a UTF-8 narrow character string.
143 StringValue(const std::string& in_value);
144
145 // Initializes a StringValue with a wide character string.
146 StringValue(const std::wstring& in_value);
147
initial.commit3f4a7322008-07-27 06:49:38 +0900148 ~StringValue();
149
150 // Subclassed methods
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900151 bool GetAsString(std::string* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900152 bool GetAsString(std::wstring* out_value) const;
153 Value* DeepCopy() const;
154 virtual bool Equals(const Value* other) const;
155
156 private:
157 DISALLOW_EVIL_CONSTRUCTORS(StringValue);
158
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900159 std::string value_;
initial.commit3f4a7322008-07-27 06:49:38 +0900160};
161
162class BinaryValue: public Value {
163public:
164 // Creates a Value to represent a binary buffer. The new object takes
165 // ownership of the pointer passed in, if successful.
166 // Returns NULL if buffer is NULL.
167 static BinaryValue* Create(char* buffer, size_t size);
168
169 // For situations where you want to keep ownership of your buffer, this
170 // factory method creates a new BinaryValue by copying the contents of the
171 // buffer that's passed in.
172 // Returns NULL if buffer is NULL.
173 static BinaryValue* CreateWithCopiedBuffer(char* buffer, size_t size);
174
deanm@google.com522c3832008-08-04 22:56:25 +0900175 ~BinaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900176
177 // Subclassed methods
178 Value* DeepCopy() const;
179 virtual bool Equals(const Value* other) const;
180
181 size_t GetSize() const { return size_; }
182 char* GetBuffer() { return buffer_; }
183
184private:
185 DISALLOW_EVIL_CONSTRUCTORS(BinaryValue);
186
187 // Constructor is private so that only objects with valid buffer pointers
188 // and size values can be created.
deanm@google.com522c3832008-08-04 22:56:25 +0900189 BinaryValue(char* buffer, size_t size);
initial.commit3f4a7322008-07-27 06:49:38 +0900190
191 char* buffer_;
192 size_t size_;
193};
194
195class DictionaryValue : public Value {
196 public:
197 DictionaryValue() : Value(TYPE_DICTIONARY) {}
198 ~DictionaryValue();
199
200 // Subclassed methods
201 Value* DeepCopy() const;
202 virtual bool Equals(const Value* other) const;
203
204 // Returns true if the current dictionary has a value for the given key.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900205 bool HasKey(const std::wstring& key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900206
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900207 // Returns the number of Values in this dictionary.
208 size_t GetSize() const { return dictionary_.size(); }
209
initial.commit3f4a7322008-07-27 06:49:38 +0900210 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900211 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900212
213 // Sets the Value associated with the given path starting from this object.
214 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
215 // into the next DictionaryValue down. Obviously, "." can't be used
216 // within a key, but there are no other restrictions on keys.
217 // If the key at any step of the way doesn't exist, or exists but isn't
218 // a DictionaryValue, a new DictionaryValue will be created and attached
219 // to the path in that location.
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900220 // Note that the dictionary takes ownership of the value referenced by
221 // |in_value|.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900222 bool Set(const std::wstring& path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900223
224 // Convenience forms of Set(). These methods will replace any existing
225 // value at that path, even if it has a different type.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900226 bool SetBoolean(const std::wstring& path, bool in_value);
227 bool SetInteger(const std::wstring& path, int in_value);
228 bool SetReal(const std::wstring& path, double in_value);
229 bool SetString(const std::wstring& path, const std::string& in_value);
230 bool SetString(const std::wstring& path, const std::wstring& in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900231
232 // Gets the Value associated with the given path starting from this object.
233 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
234 // into the next DictionaryValue down. If the path can be resolved
235 // successfully, the value for the last key in the path will be returned
236 // through the "value" parameter, and the function will return true.
237 // Otherwise, it will return false and "value" will be untouched.
238 // Note that the dictionary always owns the value that's returned.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900239 bool Get(const std::wstring& path, Value** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900240
241 // These are convenience forms of Get(). The value will be retrieved
242 // and the return value will be true if the path is valid and the value at
243 // the end of the path can be returned in the form specified.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900244 bool GetBoolean(const std::wstring& path, bool* out_value) const;
245 bool GetInteger(const std::wstring& path, int* out_value) const;
246 bool GetReal(const std::wstring& path, double* out_value) const;
247 bool GetString(const std::wstring& path, std::string* out_value) const;
248 bool GetString(const std::wstring& path, std::wstring* out_value) const;
249 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const;
250 bool GetDictionary(const std::wstring& path,
initial.commit3f4a7322008-07-27 06:49:38 +0900251 DictionaryValue** out_value) const;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900252 bool GetList(const std::wstring& path, ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900253
254 // Removes the Value with the specified path from this dictionary (or one
255 // of its child dictionaries, if the path is more than just a local key).
256 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
257 // passed out via out_value. If |out_value| is NULL, the removed value will
258 // be deleted. This method returns true if |path| is a valid path; otherwise
259 // it will return false and the DictionaryValue object will be unchanged.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900260 bool Remove(const std::wstring& path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900261
262 // This class provides an iterator for the keys in the dictionary.
263 // It can't be used to modify the dictionary.
264 class key_iterator
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900265 : private std::iterator<std::input_iterator_tag, const std::wstring> {
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900266 public:
initial.commit3f4a7322008-07-27 06:49:38 +0900267 key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
268 key_iterator operator++() { ++itr_; return *this; }
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900269 const std::wstring& operator*() { return itr_->first; }
initial.commit3f4a7322008-07-27 06:49:38 +0900270 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
271 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
272
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900273 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900274 ValueMap::const_iterator itr_;
275 };
276
277 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
278 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
279
280 private:
281 DISALLOW_EVIL_CONSTRUCTORS(DictionaryValue);
282
283 // Associates the value |in_value| with the |key|. This method should be
284 // used instead of "dictionary_[key] = foo" so that any previous value can
285 // be properly deleted.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900286 void SetInCurrentNode(const std::wstring& key, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900287
288 ValueMap dictionary_;
289};
290
291// This type of Value represents a list of other Value values.
initial.commit3f4a7322008-07-27 06:49:38 +0900292class ListValue : public Value {
293 public:
294 ListValue() : Value(TYPE_LIST) {}
295 ~ListValue();
296
297 // Subclassed methods
298 Value* DeepCopy() const;
299 virtual bool Equals(const Value* other) const;
300
301 // Clears the contents of this ListValue
302 void Clear();
303
304 // Returns the number of Values in this list.
305 size_t GetSize() const { return list_.size(); }
306
307 // Sets the list item at the given index to be the Value specified by
308 // the value given. If the index beyond the current end of the list, null
309 // Values will be used to pad out the list.
310 // Returns true if successful, or false if the index was negative or
311 // the value is a null pointer.
312 bool Set(size_t index, Value* in_value);
313
314 // Gets the Value at the given index. Modifies value (and returns true)
315 // only if the index falls within the current list range.
316 // Note that the list always owns the Value passed out via out_value.
317 bool Get(size_t index, Value** out_value) const;
318
319 // Convenience forms of Get(). Modifies value (and returns true) only if
320 // the index is valid and the Value at that index can be returned in
321 // the specified form.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900322 bool GetBoolean(size_t index, bool* out_value) const;
323 bool GetInteger(size_t index, int* out_value) const;
324 bool GetReal(size_t index, double* out_value) const;
325 bool GetString(size_t index, std::string* out_value) const;
326 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900327 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900328 bool GetList(size_t index, ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900329
330 // Removes the Value with the specified index from this list.
331 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
ericroman@google.com3d19ee52009-01-06 03:37:51 +0900332 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commit3f4a7322008-07-27 06:49:38 +0900333 // be deleted. This method returns true if |index| is valid; otherwise
334 // it will return false and the ListValue object will be unchanged.
335 bool Remove(size_t index, Value** out_value);
336
pkasting@chromium.org727139c2009-05-09 09:33:04 +0900337 // Removes the first instance of |value| found in the list, if any.
338 void Remove(const Value& value);
339
initial.commit3f4a7322008-07-27 06:49:38 +0900340 // Appends a Value to the end of the list.
341 void Append(Value* in_value);
342
343 // Iteration
344 typedef ValueVector::iterator iterator;
345 typedef ValueVector::const_iterator const_iterator;
346
347 ListValue::iterator begin() { return list_.begin(); }
348 ListValue::iterator end() { return list_.end(); }
349
350 ListValue::const_iterator begin() const { return list_.begin(); }
351 ListValue::const_iterator end() const { return list_.end(); }
352
initial.commit3f4a7322008-07-27 06:49:38 +0900353 private:
354 DISALLOW_EVIL_CONSTRUCTORS(ListValue);
355
356 ValueVector list_;
357};
358
359// This interface is implemented by classes that know how to serialize and
360// deserialize Value objects.
361class ValueSerializer {
362 public:
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900363 virtual ~ValueSerializer() {}
364
initial.commit3f4a7322008-07-27 06:49:38 +0900365 virtual bool Serialize(const Value& root) = 0;
366
367 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900368 // If the return value is non-NULL, the caller takes ownership of returned
369 // Value. If the return value is NULL, and if error_message is non-NULL,
370 // error_message should be filled with a message describing the error.
371 virtual Value* Deserialize(std::string* error_message) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900372};
373
darin@chromium.org01f10702008-09-27 05:22:42 +0900374#endif // BASE_VALUES_H_