blob: 2fc2044a8b62e935663780edfa811b5b28b4bdcd [file] [log] [blame]
skerner@chromium.org53f71f02010-04-09 04:06:15 +09001// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// 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
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +09005// This file specifies a recursive data storage class called Value intended for
6// storing setting and other persistable data. It includes the ability to
7// specify (recursive) lists and dictionaries, so it's fairly expressive.
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
10// easily do things like:
initial.commit3f4a7322008-07-27 06:49:38 +090011//
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +090012// root->SetString("global.pages.homepage", "http://goateleporter.com");
13// std::string homepage = "http://google.com"; // default/fallback value
14// root->GetString("global.pages.homepage", &homepage);
initial.commit3f4a7322008-07-27 06:49:38 +090015//
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +090016// where "global" and "pages" are also DictionaryValues, and "homepage" is a
17// string setting. If some elements of the path didn't exist yet, the
18// SetString() method would create the missing elements and attach them to root
19// before attaching the homepage value.
initial.commit3f4a7322008-07-27 06:49:38 +090020
darin@chromium.org01f10702008-09-27 05:22:42 +090021#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
thakis@chromium.org01d14522010-07-27 08:08:24 +090023#pragma once
initial.commit3f4a7322008-07-27 06:49:38 +090024
25#include <iterator>
26#include <map>
nsylvain@chromium.org12426672009-03-04 07:59:43 +090027#include <string>
initial.commit3f4a7322008-07-27 06:49:38 +090028#include <vector>
29
30#include "base/basictypes.h"
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +090031#include "base/string16.h"
32#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090033
34class Value;
35class FundamentalValue;
36class StringValue;
37class BinaryValue;
38class DictionaryValue;
39class ListValue;
40
41typedef std::vector<Value*> ValueVector;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +090042typedef std::map<std::string, Value*> ValueMap;
initial.commit3f4a7322008-07-27 06:49:38 +090043
44// The Value class is the base class for Values. A Value can be
45// instantiated via the Create*Value() factory methods, or by directly
46// creating instances of the subclasses.
47class Value {
48 public:
49 virtual ~Value();
50
51 // Convenience methods for creating Value objects for various
52 // kinds of values without thinking about which class implements them.
53 // These can always be expected to return a valid Value*.
54 static Value* CreateNullValue();
55 static Value* CreateBooleanValue(bool in_value);
56 static Value* CreateIntegerValue(int in_value);
57 static Value* CreateRealValue(double in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090058 static Value* CreateStringValue(const std::string& in_value);
initial.commit3f4a7322008-07-27 06:49:38 +090059 static Value* CreateStringValue(const std::wstring& in_value);
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +090060 static Value* CreateStringValueFromUTF16(const string16& in_value);
initial.commit3f4a7322008-07-27 06:49:38 +090061
62 // 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.
64 static BinaryValue* CreateBinaryValue(char* buffer, size_t size);
65
66 typedef enum {
67 TYPE_NULL = 0,
68 TYPE_BOOLEAN,
69 TYPE_INTEGER,
70 TYPE_REAL,
71 TYPE_STRING,
72 TYPE_BINARY,
73 TYPE_DICTIONARY,
74 TYPE_LIST
75 } ValueType;
76
77 // Returns the type of the value stored by the current Value object.
78 // Each type will be implemented by only one subclass of Value, so it's
79 // safe to use the ValueType to determine whether you can cast from
80 // Value* to (Implementing Class)*. Also, a Value object never changes
81 // its type after construction.
82 ValueType GetType() const { return type_; }
83
84 // Returns true if the current object represents a given type.
85 bool IsType(ValueType type) const { return type == type_; }
86
87 // These methods allow the convenient retrieval of settings.
88 // If the current setting object can be converted into the given type,
skerner@chromium.org53f71f02010-04-09 04:06:15 +090089 // the value is returned through the |out_value| parameter and true is
90 // returned; otherwise, false is returned and |out_value| is unchanged.
initial.commit3f4a7322008-07-27 06:49:38 +090091 virtual bool GetAsBoolean(bool* out_value) const;
92 virtual bool GetAsInteger(int* out_value) const;
93 virtual bool GetAsReal(double* out_value) const;
scherkus@chromium.org2b923e72008-12-11 10:23:17 +090094 virtual bool GetAsString(std::string* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +090095 virtual bool GetAsString(std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +090096 virtual bool GetAsUTF16(string16* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +090097
98 // 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.
100 virtual Value* DeepCopy() const;
101
102 // Compares if two Value objects have equal contents.
103 virtual bool Equals(const Value* other) const;
104
105 protected:
106 // This isn't safe for end-users (they should use the Create*Value()
107 // static methods above), but it's useful for subclasses.
erg@chromium.org493f5f62010-07-16 06:03:54 +0900108 explicit Value(ValueType type);
initial.commit3f4a7322008-07-27 06:49:38 +0900109
110 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900111 Value();
112
113 ValueType type_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900114
115 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commit3f4a7322008-07-27 06:49:38 +0900116};
117
118// FundamentalValue represents the simple fundamental types of values.
119class FundamentalValue : public Value {
120 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900121 explicit FundamentalValue(bool in_value);
122 explicit FundamentalValue(int in_value);
123 explicit FundamentalValue(double in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900124 ~FundamentalValue();
125
126 // Subclassed methods
127 virtual bool GetAsBoolean(bool* out_value) const;
128 virtual bool GetAsInteger(int* out_value) const;
129 virtual bool GetAsReal(double* out_value) const;
130 virtual Value* DeepCopy() const;
131 virtual bool Equals(const Value* other) const;
132
133 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900134 union {
135 bool boolean_value_;
136 int integer_value_;
137 double real_value_;
138 };
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900139
140 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900141};
142
143class StringValue : public Value {
144 public:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900145 // Initializes a StringValue with a UTF-8 narrow character string.
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900146 explicit StringValue(const std::string& in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900147
148 // Initializes a StringValue with a wide character string.
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900149 explicit StringValue(const std::wstring& in_value);
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900150
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900151#if !defined(WCHAR_T_IS_UTF16)
152 // Initializes a StringValue with a string16.
153 explicit StringValue(const string16& in_value);
154#endif
155
initial.commit3f4a7322008-07-27 06:49:38 +0900156 ~StringValue();
157
158 // Subclassed methods
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900159 bool GetAsString(std::string* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900160 bool GetAsString(std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900161 bool GetAsUTF16(string16* out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900162 Value* DeepCopy() const;
163 virtual bool Equals(const Value* other) const;
164
165 private:
scherkus@chromium.org2b923e72008-12-11 10:23:17 +0900166 std::string value_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900167
168 DISALLOW_COPY_AND_ASSIGN(StringValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900169};
170
171class BinaryValue: public Value {
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900172 public:
initial.commit3f4a7322008-07-27 06:49:38 +0900173 // Creates a Value to represent a binary buffer. The new object takes
174 // ownership of the pointer passed in, if successful.
175 // Returns NULL if buffer is NULL.
176 static BinaryValue* Create(char* buffer, size_t size);
177
178 // For situations where you want to keep ownership of your buffer, this
179 // factory method creates a new BinaryValue by copying the contents of the
180 // buffer that's passed in.
181 // Returns NULL if buffer is NULL.
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900182 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commit3f4a7322008-07-27 06:49:38 +0900183
deanm@google.com522c3832008-08-04 22:56:25 +0900184 ~BinaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900185
186 // Subclassed methods
187 Value* DeepCopy() const;
188 virtual bool Equals(const Value* other) const;
189
190 size_t GetSize() const { return size_; }
191 char* GetBuffer() { return buffer_; }
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900192 const char* GetBuffer() const { return buffer_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900193
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900194 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900195 // Constructor is private so that only objects with valid buffer pointers
196 // and size values can be created.
deanm@google.com522c3832008-08-04 22:56:25 +0900197 BinaryValue(char* buffer, size_t size);
initial.commit3f4a7322008-07-27 06:49:38 +0900198
199 char* buffer_;
200 size_t size_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900201
202 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900203};
204
viettrungluu@chromium.org496dec22010-07-31 13:56:14 +0900205// DictionaryValue provides a key-value dictionary with (optional) "path"
206// parsing for recursive access; see the comment at the top of the file. Keys
207// are |std::string|s and should be UTF-8 encoded.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900208// TODO(viettrungluu): Things marked DEPRECATED will be removed. crbug.com/23581
initial.commit3f4a7322008-07-27 06:49:38 +0900209class DictionaryValue : public Value {
210 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900211 DictionaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900212 ~DictionaryValue();
213
214 // Subclassed methods
215 Value* DeepCopy() const;
216 virtual bool Equals(const Value* other) const;
217
218 // Returns true if the current dictionary has a value for the given key.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900219 bool HasKey(const std::string& key) const;
220 /*DEPRECATED*/bool HasKeyASCII(const std::string& key) const;
221 /*DEPRECATED*/bool HasKey(const std::wstring& key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900222
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900223 // Returns the number of Values in this dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900224 size_t size() const { return dictionary_.size(); }
225
226 // Returns whether the dictionary is empty.
227 bool empty() const { return dictionary_.empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900228
initial.commit3f4a7322008-07-27 06:49:38 +0900229 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900230 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900231
232 // Sets 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. Obviously, "." can't be used
235 // within a key, but there are no other restrictions on keys.
236 // If the key at any step of the way doesn't exist, or exists but isn't
237 // a DictionaryValue, a new DictionaryValue will be created and attached
238 // to the path in that location.
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900239 // Note that the dictionary takes ownership of the value referenced by
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900240 // |in_value|, and therefore |in_value| must be non-NULL.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900241 void Set(const std::string& path, Value* in_value);
242 /*DEPRECATED*/void Set(const std::wstring& path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900243
244 // Convenience forms of Set(). These methods will replace any existing
245 // value at that path, even if it has a different type.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900246 void SetBoolean(const std::string& path, bool in_value);
247 void SetInteger(const std::string& path, int in_value);
248 void SetReal(const std::string& path, double in_value);
249 void SetString(const std::string& path, const std::string& in_value);
250 void SetStringFromUTF16(const std::string& path, const string16& in_value);
251 /*DEPRECATED*/void SetBoolean(const std::wstring& path, bool in_value);
252 /*DEPRECATED*/void SetInteger(const std::wstring& path, int in_value);
253 /*DEPRECATED*/void SetReal(const std::wstring& path, double in_value);
254 /*DEPRECATED*/void SetString(const std::wstring& path,
255 const std::string& in_value);
256 /*DEPRECATED*/void SetString(const std::wstring& path,
257 const std::wstring& in_value);
258 /*DEPRECATED*/void SetStringFromUTF16(const std::wstring& path,
259 const string16& in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900260
261 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
262 // be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900263 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
264 /*DEPRECATED*/void SetWithoutPathExpansion(const std::wstring& key,
265 Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900266
267 // Gets the Value associated with the given path starting from this object.
268 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
269 // into the next DictionaryValue down. If the path can be resolved
270 // successfully, the value for the last key in the path will be returned
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900271 // through the |out_value| parameter, and the function will return true.
272 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900273 // Note that the dictionary always owns the value that's returned.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900274 bool Get(const std::string& path, Value** out_value) const;
275 /*DEPRECATED*/bool Get(const std::wstring& path, Value** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900276
277 // These are convenience forms of Get(). The value will be retrieved
278 // and the return value will be true if the path is valid and the value at
279 // the end of the path can be returned in the form specified.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900280 bool GetBoolean(const std::string& path, bool* out_value) const;
281 bool GetInteger(const std::string& path, int* out_value) const;
282 bool GetReal(const std::string& path, double* out_value) const;
283 bool GetString(const std::string& path, std::string* out_value) const;
284 bool GetStringAsUTF16(const std::string& path, string16* out_value) const;
evan@chromium.orgd9ab8de2010-02-19 22:32:16 +0900285 bool GetStringASCII(const std::string& path, std::string* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900286 bool GetBinary(const std::string& path, BinaryValue** out_value) const;
287 bool GetDictionary(const std::string& path,
initial.commit3f4a7322008-07-27 06:49:38 +0900288 DictionaryValue** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900289 bool GetList(const std::string& path, ListValue** out_value) const;
290 /*DEPRECATED*/bool GetBoolean(const std::wstring& path,
291 bool* out_value) const;
292 /*DEPRECATED*/bool GetInteger(const std::wstring& path, int* out_value) const;
293 /*DEPRECATED*/bool GetReal(const std::wstring& path, double* out_value) const;
294 // Use |GetStringAsUTF16()| instead:
295 /*DEPRECATED*/bool GetString(const std::string& path,
296 string16* out_value) const;
297 /*DEPRECATED*/bool GetString(const std::wstring& path,
298 std::string* out_value) const;
299 /*DEPRECATED*/bool GetString(const std::wstring& path,
300 std::wstring* out_value) const;
301 /*DEPRECATED*/bool GetStringAsUTF16(const std::wstring& path,
302 string16* out_value) const;
303 /*DEPRECATED*/bool GetBinary(const std::wstring& path,
304 BinaryValue** out_value) const;
305 /*DEPRECATED*/bool GetDictionary(const std::wstring& path,
306 DictionaryValue** out_value) const;
307 /*DEPRECATED*/bool GetList(const std::wstring& path,
308 ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900309
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900310 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
311 // be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900312 bool GetWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900313 Value** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900314 bool GetIntegerWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900315 int* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900316 bool GetStringWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900317 std::string* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900318 bool GetStringAsUTF16WithoutPathExpansion(const std::string& key,
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900319 string16* out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900320 bool GetDictionaryWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900321 DictionaryValue** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900322 bool GetListWithoutPathExpansion(const std::string& key,
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900323 ListValue** out_value) const;
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900324 /*DEPRECATED*/bool GetWithoutPathExpansion(const std::wstring& key,
325 Value** out_value) const;
326 /*DEPRECATED*/bool GetIntegerWithoutPathExpansion(const std::wstring& key,
327 int* out_value) const;
328 /*DEPRECATED*/bool GetStringWithoutPathExpansion(
329 const std::wstring& key, std::string* out_value) const;
330 /*DEPRECATED*/bool GetStringWithoutPathExpansion(
331 const std::wstring& key, std::wstring* out_value) const;
332 /*DEPRECATED*/bool GetStringAsUTF16WithoutPathExpansion(
333 const std::wstring& key, string16* out_value) const;
334 /*DEPRECATED*/bool GetDictionaryWithoutPathExpansion(
335 const std::wstring& key, DictionaryValue** out_value) const;
336 /*DEPRECATED*/bool GetListWithoutPathExpansion(const std::wstring& key,
337 ListValue** out_value) const;
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900338
initial.commit3f4a7322008-07-27 06:49:38 +0900339 // Removes the Value with the specified path from this dictionary (or one
340 // of its child dictionaries, if the path is more than just a local key).
341 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
342 // passed out via out_value. If |out_value| is NULL, the removed value will
343 // be deleted. This method returns true if |path| is a valid path; otherwise
344 // it will return false and the DictionaryValue object will be unchanged.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900345 bool Remove(const std::string& path, Value** out_value);
346 /*DEPRECATED*/bool Remove(const std::wstring& path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900347
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900348 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
349 // to be used as paths.
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900350 bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value);
351 /*DEPRECATED*/bool RemoveWithoutPathExpansion(const std::wstring& key,
352 Value** out_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900353
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900354 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
355 // the copy. This never returns NULL, even if |this| itself is empty.
356 DictionaryValue* DeepCopyWithoutEmptyChildren();
357
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900358 // Merge a given dictionary into this dictionary. This is done recursively,
359 // i.e. any subdictionaries will be merged as well. In case of key collisions,
360 // the passed in dictionary takes precedence and data already present will be
361 // replaced.
362 void MergeDictionary(const DictionaryValue* dictionary);
363
initial.commit3f4a7322008-07-27 06:49:38 +0900364 // This class provides an iterator for the keys in the dictionary.
365 // It can't be used to modify the dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900366 //
367 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
368 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
369 // keys have '.'s in them.
initial.commit3f4a7322008-07-27 06:49:38 +0900370 class key_iterator
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900371 : private std::iterator<std::input_iterator_tag, const std::string> {
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900372 public:
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900373 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
erg@google.combf6ce9f2010-01-27 08:08:02 +0900374 key_iterator operator++() {
375 ++itr_;
376 return *this;
377 }
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900378 const std::string& operator*() { return itr_->first; }
initial.commit3f4a7322008-07-27 06:49:38 +0900379 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
380 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
381
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900382 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900383 ValueMap::const_iterator itr_;
384 };
385
386 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
387 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
388
389 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900390 ValueMap dictionary_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900391
392 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900393};
394
395// This type of Value represents a list of other Value values.
initial.commit3f4a7322008-07-27 06:49:38 +0900396class ListValue : public Value {
397 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900398 ListValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900399 ~ListValue();
400
401 // Subclassed methods
402 Value* DeepCopy() const;
403 virtual bool Equals(const Value* other) const;
404
405 // Clears the contents of this ListValue
406 void Clear();
407
408 // Returns the number of Values in this list.
409 size_t GetSize() const { return list_.size(); }
410
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900411 // Returns whether the list is empty.
412 bool empty() const { return list_.empty(); }
413
initial.commit3f4a7322008-07-27 06:49:38 +0900414 // Sets the list item at the given index to be the Value specified by
415 // the value given. If the index beyond the current end of the list, null
416 // Values will be used to pad out the list.
417 // Returns true if successful, or false if the index was negative or
418 // the value is a null pointer.
419 bool Set(size_t index, Value* in_value);
420
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900421 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commit3f4a7322008-07-27 06:49:38 +0900422 // only if the index falls within the current list range.
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900423 // Note that the list always owns the Value passed out via |out_value|.
initial.commit3f4a7322008-07-27 06:49:38 +0900424 bool Get(size_t index, Value** out_value) const;
425
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900426 // Convenience forms of Get(). Modifies |out_value| (and returns true)
427 // only if the index is valid and the Value at that index can be returned
428 // in the specified form.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900429 bool GetBoolean(size_t index, bool* out_value) const;
430 bool GetInteger(size_t index, int* out_value) const;
431 bool GetReal(size_t index, double* out_value) const;
432 bool GetString(size_t index, std::string* out_value) const;
estade@chromium.org8f158c32009-12-18 11:39:16 +0900433 bool GetString(size_t index, std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900434 bool GetStringAsUTF16(size_t index, string16* out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900435 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900436 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900437 bool GetList(size_t index, ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900438
439 // Removes the Value with the specified index from this list.
440 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
ericroman@google.com3d19ee52009-01-06 03:37:51 +0900441 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commit3f4a7322008-07-27 06:49:38 +0900442 // be deleted. This method returns true if |index| is valid; otherwise
443 // it will return false and the ListValue object will be unchanged.
444 bool Remove(size_t index, Value** out_value);
445
pkasting@chromium.orgdfe6a692009-12-01 04:59:11 +0900446 // Removes the first instance of |value| found in the list, if any, and
447 // deletes it. Returns the index that it was located at (-1 for not present).
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900448 int Remove(const Value& value);
pkasting@chromium.org727139c2009-05-09 09:33:04 +0900449
initial.commit3f4a7322008-07-27 06:49:38 +0900450 // Appends a Value to the end of the list.
451 void Append(Value* in_value);
452
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900453 // Appends a Value if it's not already present.
454 // Returns true if successful, or false if the value was already present.
455 bool AppendIfNotPresent(Value* in_value);
456
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900457 // Insert a Value at index.
458 // Returns true if successful, or false if the index was out of range.
459 bool Insert(size_t index, Value* in_value);
460
initial.commit3f4a7322008-07-27 06:49:38 +0900461 // Iteration
462 typedef ValueVector::iterator iterator;
463 typedef ValueVector::const_iterator const_iterator;
464
465 ListValue::iterator begin() { return list_.begin(); }
466 ListValue::iterator end() { return list_.end(); }
467
468 ListValue::const_iterator begin() const { return list_.begin(); }
469 ListValue::const_iterator end() const { return list_.end(); }
470
initial.commit3f4a7322008-07-27 06:49:38 +0900471 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900472 ValueVector list_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900473
474 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900475};
476
477// This interface is implemented by classes that know how to serialize and
478// deserialize Value objects.
479class ValueSerializer {
480 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900481 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900482
initial.commit3f4a7322008-07-27 06:49:38 +0900483 virtual bool Serialize(const Value& root) = 0;
484
485 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900486 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900487 // Value. If the return value is NULL, and if error_code is non-NULL,
488 // error_code will be set with the underlying error.
489 // 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.
491 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900492};
493
darin@chromium.org01f10702008-09-27 05:22:42 +0900494#endif // BASE_VALUES_H_