blob: 32d8a18df076f0261270413f2b120a9473f8cc5c [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
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_
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;
nsylvain@chromium.org12426672009-03-04 07:59:43 +090042typedef std::map<std::wstring, 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
205class DictionaryValue : public Value {
206 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900207 DictionaryValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900208 ~DictionaryValue();
209
210 // Subclassed methods
211 Value* DeepCopy() const;
212 virtual bool Equals(const Value* other) const;
213
214 // Returns true if the current dictionary has a value for the given key.
evan@chromium.orgd9ab8de2010-02-19 22:32:16 +0900215 bool HasKeyASCII(const std::string& key) const;
216 // Deprecated version of the above. TODO: add a string16 version for Unicode.
217 // http://code.google.com/p/chromium/issues/detail?id=23581
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900218 bool HasKey(const std::wstring& key) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900219
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900220 // Returns the number of Values in this dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900221 size_t size() const { return dictionary_.size(); }
222
223 // Returns whether the dictionary is empty.
224 bool empty() const { return dictionary_.empty(); }
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900225
initial.commit3f4a7322008-07-27 06:49:38 +0900226 // Clears any current contents of this dictionary.
deanm@google.com522c3832008-08-04 22:56:25 +0900227 void Clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900228
229 // Sets the Value associated with the given path starting from this object.
230 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
231 // into the next DictionaryValue down. Obviously, "." can't be used
232 // within a key, but there are no other restrictions on keys.
233 // If the key at any step of the way doesn't exist, or exists but isn't
234 // a DictionaryValue, a new DictionaryValue will be created and attached
235 // to the path in that location.
jcampan@chromium.org9b0f7be2009-04-15 09:17:53 +0900236 // Note that the dictionary takes ownership of the value referenced by
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900237 // |in_value|, and therefore |in_value| must be non-NULL.
238 void Set(const std::wstring& path, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900239
240 // Convenience forms of Set(). These methods will replace any existing
241 // value at that path, even if it has a different type.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900242 void SetBoolean(const std::wstring& path, bool in_value);
243 void SetInteger(const std::wstring& path, int in_value);
244 void SetReal(const std::wstring& path, double in_value);
245 void SetString(const std::wstring& path, const std::string& in_value);
246 void SetString(const std::wstring& path, const std::wstring& in_value);
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900247 void SetStringFromUTF16(const std::wstring& path, const string16& in_value);
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900248
249 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
250 // be used as paths.
251 void SetWithoutPathExpansion(const std::wstring& key, Value* in_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900252
253 // Gets the Value associated with the given path starting from this object.
254 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
255 // into the next DictionaryValue down. If the path can be resolved
256 // successfully, the value for the last key in the path will be returned
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900257 // through the |out_value| parameter, and the function will return true.
258 // Otherwise, it will return false and |out_value| will be untouched.
initial.commit3f4a7322008-07-27 06:49:38 +0900259 // Note that the dictionary always owns the value that's returned.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900260 bool Get(const std::wstring& path, Value** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900261
262 // These are convenience forms of Get(). The value will be retrieved
263 // and the return value will be true if the path is valid and the value at
264 // the end of the path can be returned in the form specified.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900265 bool GetBoolean(const std::wstring& path, bool* out_value) const;
266 bool GetInteger(const std::wstring& path, int* out_value) const;
267 bool GetReal(const std::wstring& path, double* out_value) const;
evan@chromium.orgd9ab8de2010-02-19 22:32:16 +0900268 bool GetString(const std::string& path, string16* out_value) const;
269 bool GetStringASCII(const std::string& path, std::string* out_value) const;
270 // TODO: deprecate wstring accessors.
271 // http://code.google.com/p/chromium/issues/detail?id=23581
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900272 bool GetString(const std::wstring& path, std::string* out_value) const;
273 bool GetString(const std::wstring& path, std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900274 bool GetStringAsUTF16(const std::wstring& path, string16* out_value) const;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900275 bool GetBinary(const std::wstring& path, BinaryValue** out_value) const;
276 bool GetDictionary(const std::wstring& path,
initial.commit3f4a7322008-07-27 06:49:38 +0900277 DictionaryValue** out_value) const;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900278 bool GetList(const std::wstring& path, ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900279
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900280 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
281 // be used as paths.
282 bool GetWithoutPathExpansion(const std::wstring& key,
283 Value** out_value) const;
284 bool GetIntegerWithoutPathExpansion(const std::wstring& path,
285 int* out_value) const;
286 bool GetStringWithoutPathExpansion(const std::wstring& path,
287 std::string* out_value) const;
288 bool GetStringWithoutPathExpansion(const std::wstring& path,
289 std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900290 bool GetStringAsUTF16WithoutPathExpansion(const std::wstring& path,
291 string16* out_value) const;
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900292 bool GetDictionaryWithoutPathExpansion(const std::wstring& path,
293 DictionaryValue** out_value) const;
294 bool GetListWithoutPathExpansion(const std::wstring& path,
295 ListValue** out_value) const;
296
initial.commit3f4a7322008-07-27 06:49:38 +0900297 // Removes the Value with the specified path from this dictionary (or one
298 // of its child dictionaries, if the path is more than just a local key).
299 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
300 // passed out via out_value. If |out_value| is NULL, the removed value will
301 // be deleted. This method returns true if |path| is a valid path; otherwise
302 // it will return false and the DictionaryValue object will be unchanged.
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900303 bool Remove(const std::wstring& path, Value** out_value);
initial.commit3f4a7322008-07-27 06:49:38 +0900304
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900305 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
306 // to be used as paths.
307 bool RemoveWithoutPathExpansion(const std::wstring& key, Value** out_value);
308
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900309 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
310 // the copy. This never returns NULL, even if |this| itself is empty.
311 DictionaryValue* DeepCopyWithoutEmptyChildren();
312
mnissler@chromium.org17cee0e2010-05-14 22:17:40 +0900313 // Merge a given dictionary into this dictionary. This is done recursively,
314 // i.e. any subdictionaries will be merged as well. In case of key collisions,
315 // the passed in dictionary takes precedence and data already present will be
316 // replaced.
317 void MergeDictionary(const DictionaryValue* dictionary);
318
initial.commit3f4a7322008-07-27 06:49:38 +0900319 // This class provides an iterator for the keys in the dictionary.
320 // It can't be used to modify the dictionary.
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900321 //
322 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
323 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
324 // keys have '.'s in them.
initial.commit3f4a7322008-07-27 06:49:38 +0900325 class key_iterator
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900326 : private std::iterator<std::input_iterator_tag, const std::wstring> {
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900327 public:
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900328 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
erg@google.combf6ce9f2010-01-27 08:08:02 +0900329 key_iterator operator++() {
330 ++itr_;
331 return *this;
332 }
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900333 const std::wstring& operator*() { return itr_->first; }
initial.commit3f4a7322008-07-27 06:49:38 +0900334 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
335 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
336
jar@chromium.orgaf39a372009-02-18 07:50:14 +0900337 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900338 ValueMap::const_iterator itr_;
339 };
340
341 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
342 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
343
344 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900345 ValueMap dictionary_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900346
347 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900348};
349
350// This type of Value represents a list of other Value values.
initial.commit3f4a7322008-07-27 06:49:38 +0900351class ListValue : public Value {
352 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900353 ListValue();
initial.commit3f4a7322008-07-27 06:49:38 +0900354 ~ListValue();
355
356 // Subclassed methods
357 Value* DeepCopy() const;
358 virtual bool Equals(const Value* other) const;
359
360 // Clears the contents of this ListValue
361 void Clear();
362
363 // Returns the number of Values in this list.
364 size_t GetSize() const { return list_.size(); }
365
tony@chromium.orge4948ab2009-12-02 09:20:32 +0900366 // Returns whether the list is empty.
367 bool empty() const { return list_.empty(); }
368
initial.commit3f4a7322008-07-27 06:49:38 +0900369 // Sets the list item at the given index to be the Value specified by
370 // the value given. If the index beyond the current end of the list, null
371 // Values will be used to pad out the list.
372 // Returns true if successful, or false if the index was negative or
373 // the value is a null pointer.
374 bool Set(size_t index, Value* in_value);
375
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900376 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commit3f4a7322008-07-27 06:49:38 +0900377 // only if the index falls within the current list range.
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900378 // Note that the list always owns the Value passed out via |out_value|.
initial.commit3f4a7322008-07-27 06:49:38 +0900379 bool Get(size_t index, Value** out_value) const;
380
skerner@chromium.org53f71f02010-04-09 04:06:15 +0900381 // Convenience forms of Get(). Modifies |out_value| (and returns true)
382 // only if the index is valid and the Value at that index can be returned
383 // in the specified form.
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900384 bool GetBoolean(size_t index, bool* out_value) const;
385 bool GetInteger(size_t index, int* out_value) const;
386 bool GetReal(size_t index, double* out_value) const;
387 bool GetString(size_t index, std::string* out_value) const;
estade@chromium.org8f158c32009-12-18 11:39:16 +0900388 bool GetString(size_t index, std::wstring* out_value) const;
munjal@chromium.org3b2d3a42010-01-16 05:09:03 +0900389 bool GetStringAsUTF16(size_t index, string16* out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900390 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900391 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
aa@chromium.orgc93b02f2009-01-21 06:05:32 +0900392 bool GetList(size_t index, ListValue** out_value) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900393
394 // Removes the Value with the specified index from this list.
395 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
ericroman@google.com3d19ee52009-01-06 03:37:51 +0900396 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commit3f4a7322008-07-27 06:49:38 +0900397 // be deleted. This method returns true if |index| is valid; otherwise
398 // it will return false and the ListValue object will be unchanged.
399 bool Remove(size_t index, Value** out_value);
400
pkasting@chromium.orgdfe6a692009-12-01 04:59:11 +0900401 // Removes the first instance of |value| found in the list, if any, and
402 // deletes it. Returns the index that it was located at (-1 for not present).
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900403 int Remove(const Value& value);
pkasting@chromium.org727139c2009-05-09 09:33:04 +0900404
initial.commit3f4a7322008-07-27 06:49:38 +0900405 // Appends a Value to the end of the list.
406 void Append(Value* in_value);
407
zork@chromium.orgb5f742b2010-04-13 06:48:10 +0900408 // Appends a Value if it's not already present.
409 // Returns true if successful, or false if the value was already present.
410 bool AppendIfNotPresent(Value* in_value);
411
erikkay@chromium.org9034a232009-08-29 05:26:05 +0900412 // Insert a Value at index.
413 // Returns true if successful, or false if the index was out of range.
414 bool Insert(size_t index, Value* in_value);
415
initial.commit3f4a7322008-07-27 06:49:38 +0900416 // Iteration
417 typedef ValueVector::iterator iterator;
418 typedef ValueVector::const_iterator const_iterator;
419
420 ListValue::iterator begin() { return list_.begin(); }
421 ListValue::iterator end() { return list_.end(); }
422
423 ListValue::const_iterator begin() const { return list_.begin(); }
424 ListValue::const_iterator end() const { return list_.end(); }
425
initial.commit3f4a7322008-07-27 06:49:38 +0900426 private:
initial.commit3f4a7322008-07-27 06:49:38 +0900427 ValueVector list_;
jcampan@chromium.orgd1105672009-09-15 01:56:12 +0900428
429 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commit3f4a7322008-07-27 06:49:38 +0900430};
431
432// This interface is implemented by classes that know how to serialize and
433// deserialize Value objects.
434class ValueSerializer {
435 public:
erg@chromium.org493f5f62010-07-16 06:03:54 +0900436 virtual ~ValueSerializer();
mmentovai@google.com4a5b6272008-08-07 00:46:59 +0900437
initial.commit3f4a7322008-07-27 06:49:38 +0900438 virtual bool Serialize(const Value& root) = 0;
439
440 // This method deserializes the subclass-specific format into a Value object.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900441 // If the return value is non-NULL, the caller takes ownership of returned
erikkay@chromium.org64b2cf42010-04-07 00:42:39 +0900442 // Value. If the return value is NULL, and if error_code is non-NULL,
443 // error_code will be set with the underlying error.
444 // If |error_message| is non-null, it will be filled in with a formatted
445 // error message including the location of the error if appropriate.
446 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900447};
448
darin@chromium.org01f10702008-09-27 05:22:42 +0900449#endif // BASE_VALUES_H_