blob: cc9d0fc0ab75178467db7ad4cd94432d83b601f3 [file] [log] [blame]
[email protected]35213ce92010-04-08 19:06:151// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
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
[email protected]101d5422008-09-26 20:22:4221#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
[email protected]32b76ef2010-07-26 23:08:2423#pragma once
initial.commitd7cae122008-07-26 21:49:3824
25#include <iterator>
26#include <map>
[email protected]8e50b602009-03-03 22:59:4327#include <string>
initial.commitd7cae122008-07-26 21:49:3828#include <vector>
29
30#include "base/basictypes.h"
[email protected]9101ef1e2010-01-15 20:09:0331#include "base/string16.h"
32#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3833
34class Value;
35class FundamentalValue;
36class StringValue;
37class BinaryValue;
38class DictionaryValue;
39class ListValue;
40
41typedef std::vector<Value*> ValueVector;
[email protected]e7b418b2010-07-30 19:47:4742typedef std::map<std::string, Value*> ValueMap;
initial.commitd7cae122008-07-26 21:49:3843
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);
[email protected]4cd5f6a2008-12-11 01:23:1758 static Value* CreateStringValue(const std::string& in_value);
initial.commitd7cae122008-07-26 21:49:3859 static Value* CreateStringValue(const std::wstring& in_value);
[email protected]9101ef1e2010-01-15 20:09:0360 static Value* CreateStringValueFromUTF16(const string16& in_value);
initial.commitd7cae122008-07-26 21:49:3861
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,
[email protected]35213ce92010-04-08 19:06:1589 // the value is returned through the |out_value| parameter and true is
90 // returned; otherwise, false is returned and |out_value| is unchanged.
initial.commitd7cae122008-07-26 21:49:3891 virtual bool GetAsBoolean(bool* out_value) const;
92 virtual bool GetAsInteger(int* out_value) const;
93 virtual bool GetAsReal(double* out_value) const;
[email protected]4cd5f6a2008-12-11 01:23:1794 virtual bool GetAsString(std::string* out_value) const;
initial.commitd7cae122008-07-26 21:49:3895 virtual bool GetAsString(std::wstring* out_value) const;
[email protected]9101ef1e2010-01-15 20:09:0396 virtual bool GetAsUTF16(string16* out_value) const;
initial.commitd7cae122008-07-26 21:49:3897
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.
[email protected]3a3d47472010-07-15 21:03:54108 explicit Value(ValueType type);
initial.commitd7cae122008-07-26 21:49:38109
110 private:
initial.commitd7cae122008-07-26 21:49:38111 Value();
112
113 ValueType type_;
[email protected]7867cd02009-09-14 16:56:12114
115 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commitd7cae122008-07-26 21:49:38116};
117
118// FundamentalValue represents the simple fundamental types of values.
119class FundamentalValue : public Value {
120 public:
[email protected]3a3d47472010-07-15 21:03:54121 explicit FundamentalValue(bool in_value);
122 explicit FundamentalValue(int in_value);
123 explicit FundamentalValue(double in_value);
initial.commitd7cae122008-07-26 21:49:38124 ~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.commitd7cae122008-07-26 21:49:38134 union {
135 bool boolean_value_;
136 int integer_value_;
137 double real_value_;
138 };
[email protected]7867cd02009-09-14 16:56:12139
140 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
initial.commitd7cae122008-07-26 21:49:38141};
142
143class StringValue : public Value {
144 public:
[email protected]4cd5f6a2008-12-11 01:23:17145 // Initializes a StringValue with a UTF-8 narrow character string.
[email protected]7867cd02009-09-14 16:56:12146 explicit StringValue(const std::string& in_value);
[email protected]4cd5f6a2008-12-11 01:23:17147
148 // Initializes a StringValue with a wide character string.
[email protected]7867cd02009-09-14 16:56:12149 explicit StringValue(const std::wstring& in_value);
[email protected]4cd5f6a2008-12-11 01:23:17150
[email protected]9101ef1e2010-01-15 20:09:03151#if !defined(WCHAR_T_IS_UTF16)
152 // Initializes a StringValue with a string16.
153 explicit StringValue(const string16& in_value);
154#endif
155
initial.commitd7cae122008-07-26 21:49:38156 ~StringValue();
157
158 // Subclassed methods
[email protected]4cd5f6a2008-12-11 01:23:17159 bool GetAsString(std::string* out_value) const;
initial.commitd7cae122008-07-26 21:49:38160 bool GetAsString(std::wstring* out_value) const;
[email protected]9101ef1e2010-01-15 20:09:03161 bool GetAsUTF16(string16* out_value) const;
initial.commitd7cae122008-07-26 21:49:38162 Value* DeepCopy() const;
163 virtual bool Equals(const Value* other) const;
164
165 private:
[email protected]4cd5f6a2008-12-11 01:23:17166 std::string value_;
[email protected]7867cd02009-09-14 16:56:12167
168 DISALLOW_COPY_AND_ASSIGN(StringValue);
initial.commitd7cae122008-07-26 21:49:38169};
170
171class BinaryValue: public Value {
[email protected]7867cd02009-09-14 16:56:12172 public:
initial.commitd7cae122008-07-26 21:49:38173 // 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.
[email protected]e4dad9fb2009-10-06 18:15:58182 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38183
[email protected]af5ed4a2008-08-04 13:56:25184 ~BinaryValue();
initial.commitd7cae122008-07-26 21:49:38185
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_; }
[email protected]e4dad9fb2009-10-06 18:15:58192 const char* GetBuffer() const { return buffer_; }
initial.commitd7cae122008-07-26 21:49:38193
[email protected]7867cd02009-09-14 16:56:12194 private:
initial.commitd7cae122008-07-26 21:49:38195 // Constructor is private so that only objects with valid buffer pointers
196 // and size values can be created.
[email protected]af5ed4a2008-08-04 13:56:25197 BinaryValue(char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38198
199 char* buffer_;
200 size_t size_;
[email protected]7867cd02009-09-14 16:56:12201
202 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commitd7cae122008-07-26 21:49:38203};
204
[email protected]e7b418b2010-07-30 19:47:47205// TODO(viettrungluu): Things marked DEPRECATED will be removed. crbug.com/23581
initial.commitd7cae122008-07-26 21:49:38206class DictionaryValue : public Value {
207 public:
[email protected]3a3d47472010-07-15 21:03:54208 DictionaryValue();
initial.commitd7cae122008-07-26 21:49:38209 ~DictionaryValue();
210
211 // Subclassed methods
212 Value* DeepCopy() const;
213 virtual bool Equals(const Value* other) const;
214
215 // Returns true if the current dictionary has a value for the given key.
[email protected]e7b418b2010-07-30 19:47:47216 bool HasKey(const std::string& key) const;
217 /*DEPRECATED*/bool HasKeyASCII(const std::string& key) const;
218 /*DEPRECATED*/bool HasKey(const std::wstring& key) const;
initial.commitd7cae122008-07-26 21:49:38219
[email protected]fb804132c2009-04-15 00:17:53220 // Returns the number of Values in this dictionary.
[email protected]4dad9ad82009-11-25 20:47:52221 size_t size() const { return dictionary_.size(); }
222
223 // Returns whether the dictionary is empty.
224 bool empty() const { return dictionary_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53225
initial.commitd7cae122008-07-26 21:49:38226 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25227 void Clear();
initial.commitd7cae122008-07-26 21:49:38228
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.
[email protected]fb804132c2009-04-15 00:17:53236 // Note that the dictionary takes ownership of the value referenced by
[email protected]4dad9ad82009-11-25 20:47:52237 // |in_value|, and therefore |in_value| must be non-NULL.
[email protected]e7b418b2010-07-30 19:47:47238 void Set(const std::string& path, Value* in_value);
239 /*DEPRECATED*/void Set(const std::wstring& path, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38240
241 // Convenience forms of Set(). These methods will replace any existing
242 // value at that path, even if it has a different type.
[email protected]e7b418b2010-07-30 19:47:47243 void SetBoolean(const std::string& path, bool in_value);
244 void SetInteger(const std::string& path, int in_value);
245 void SetReal(const std::string& path, double in_value);
246 void SetString(const std::string& path, const std::string& in_value);
247 void SetStringFromUTF16(const std::string& path, const string16& in_value);
248 /*DEPRECATED*/void SetBoolean(const std::wstring& path, bool in_value);
249 /*DEPRECATED*/void SetInteger(const std::wstring& path, int in_value);
250 /*DEPRECATED*/void SetReal(const std::wstring& path, double in_value);
251 /*DEPRECATED*/void SetString(const std::wstring& path,
252 const std::string& in_value);
253 /*DEPRECATED*/void SetString(const std::wstring& path,
254 const std::wstring& in_value);
255 /*DEPRECATED*/void SetStringFromUTF16(const std::wstring& path,
256 const string16& in_value);
[email protected]4dad9ad82009-11-25 20:47:52257
258 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
259 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47260 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
261 /*DEPRECATED*/void SetWithoutPathExpansion(const std::wstring& key,
262 Value* in_value);
initial.commitd7cae122008-07-26 21:49:38263
264 // Gets the Value associated with the given path starting from this object.
265 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
266 // into the next DictionaryValue down. If the path can be resolved
267 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15268 // through the |out_value| parameter, and the function will return true.
269 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38270 // Note that the dictionary always owns the value that's returned.
[email protected]e7b418b2010-07-30 19:47:47271 bool Get(const std::string& path, Value** out_value) const;
272 /*DEPRECATED*/bool Get(const std::wstring& path, Value** out_value) const;
initial.commitd7cae122008-07-26 21:49:38273
274 // These are convenience forms of Get(). The value will be retrieved
275 // and the return value will be true if the path is valid and the value at
276 // the end of the path can be returned in the form specified.
[email protected]e7b418b2010-07-30 19:47:47277 bool GetBoolean(const std::string& path, bool* out_value) const;
278 bool GetInteger(const std::string& path, int* out_value) const;
279 bool GetReal(const std::string& path, double* out_value) const;
280 bool GetString(const std::string& path, std::string* out_value) const;
281 bool GetStringAsUTF16(const std::string& path, string16* out_value) const;
[email protected]9430e4b2010-02-19 13:32:16282 bool GetStringASCII(const std::string& path, std::string* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47283 bool GetBinary(const std::string& path, BinaryValue** out_value) const;
284 bool GetDictionary(const std::string& path,
initial.commitd7cae122008-07-26 21:49:38285 DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47286 bool GetList(const std::string& path, ListValue** out_value) const;
287 /*DEPRECATED*/bool GetBoolean(const std::wstring& path,
288 bool* out_value) const;
289 /*DEPRECATED*/bool GetInteger(const std::wstring& path, int* out_value) const;
290 /*DEPRECATED*/bool GetReal(const std::wstring& path, double* out_value) const;
291 // Use |GetStringAsUTF16()| instead:
292 /*DEPRECATED*/bool GetString(const std::string& path,
293 string16* out_value) const;
294 /*DEPRECATED*/bool GetString(const std::wstring& path,
295 std::string* out_value) const;
296 /*DEPRECATED*/bool GetString(const std::wstring& path,
297 std::wstring* out_value) const;
298 /*DEPRECATED*/bool GetStringAsUTF16(const std::wstring& path,
299 string16* out_value) const;
300 /*DEPRECATED*/bool GetBinary(const std::wstring& path,
301 BinaryValue** out_value) const;
302 /*DEPRECATED*/bool GetDictionary(const std::wstring& path,
303 DictionaryValue** out_value) const;
304 /*DEPRECATED*/bool GetList(const std::wstring& path,
305 ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38306
[email protected]4dad9ad82009-11-25 20:47:52307 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
308 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47309 bool GetWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52310 Value** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47311 bool GetIntegerWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52312 int* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47313 bool GetStringWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52314 std::string* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47315 bool GetStringAsUTF16WithoutPathExpansion(const std::string& key,
[email protected]9101ef1e2010-01-15 20:09:03316 string16* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47317 bool GetDictionaryWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52318 DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47319 bool GetListWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52320 ListValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47321 /*DEPRECATED*/bool GetWithoutPathExpansion(const std::wstring& key,
322 Value** out_value) const;
323 /*DEPRECATED*/bool GetIntegerWithoutPathExpansion(const std::wstring& key,
324 int* out_value) const;
325 /*DEPRECATED*/bool GetStringWithoutPathExpansion(
326 const std::wstring& key, std::string* out_value) const;
327 /*DEPRECATED*/bool GetStringWithoutPathExpansion(
328 const std::wstring& key, std::wstring* out_value) const;
329 /*DEPRECATED*/bool GetStringAsUTF16WithoutPathExpansion(
330 const std::wstring& key, string16* out_value) const;
331 /*DEPRECATED*/bool GetDictionaryWithoutPathExpansion(
332 const std::wstring& key, DictionaryValue** out_value) const;
333 /*DEPRECATED*/bool GetListWithoutPathExpansion(const std::wstring& key,
334 ListValue** out_value) const;
[email protected]4dad9ad82009-11-25 20:47:52335
initial.commitd7cae122008-07-26 21:49:38336 // Removes the Value with the specified path from this dictionary (or one
337 // of its child dictionaries, if the path is more than just a local key).
338 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
339 // passed out via out_value. If |out_value| is NULL, the removed value will
340 // be deleted. This method returns true if |path| is a valid path; otherwise
341 // it will return false and the DictionaryValue object will be unchanged.
[email protected]e7b418b2010-07-30 19:47:47342 bool Remove(const std::string& path, Value** out_value);
343 /*DEPRECATED*/bool Remove(const std::wstring& path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38344
[email protected]4dad9ad82009-11-25 20:47:52345 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
346 // to be used as paths.
[email protected]e7b418b2010-07-30 19:47:47347 bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value);
348 /*DEPRECATED*/bool RemoveWithoutPathExpansion(const std::wstring& key,
349 Value** out_value);
[email protected]4dad9ad82009-11-25 20:47:52350
[email protected]ec330b52009-12-02 00:20:32351 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
352 // the copy. This never returns NULL, even if |this| itself is empty.
353 DictionaryValue* DeepCopyWithoutEmptyChildren();
354
[email protected]c378cca2010-05-14 13:17:40355 // Merge a given dictionary into this dictionary. This is done recursively,
356 // i.e. any subdictionaries will be merged as well. In case of key collisions,
357 // the passed in dictionary takes precedence and data already present will be
358 // replaced.
359 void MergeDictionary(const DictionaryValue* dictionary);
360
initial.commitd7cae122008-07-26 21:49:38361 // This class provides an iterator for the keys in the dictionary.
362 // It can't be used to modify the dictionary.
[email protected]4dad9ad82009-11-25 20:47:52363 //
364 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
365 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
366 // keys have '.'s in them.
initial.commitd7cae122008-07-26 21:49:38367 class key_iterator
[email protected]e7b418b2010-07-30 19:47:47368 : private std::iterator<std::input_iterator_tag, const std::string> {
[email protected]03c5e862009-02-17 22:50:14369 public:
[email protected]7867cd02009-09-14 16:56:12370 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
[email protected]2fdc86a2010-01-26 23:08:02371 key_iterator operator++() {
372 ++itr_;
373 return *this;
374 }
[email protected]e7b418b2010-07-30 19:47:47375 const std::string& operator*() { return itr_->first; }
initial.commitd7cae122008-07-26 21:49:38376 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
377 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
378
[email protected]03c5e862009-02-17 22:50:14379 private:
initial.commitd7cae122008-07-26 21:49:38380 ValueMap::const_iterator itr_;
381 };
382
383 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
384 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
385
386 private:
initial.commitd7cae122008-07-26 21:49:38387 ValueMap dictionary_;
[email protected]7867cd02009-09-14 16:56:12388
389 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commitd7cae122008-07-26 21:49:38390};
391
392// This type of Value represents a list of other Value values.
initial.commitd7cae122008-07-26 21:49:38393class ListValue : public Value {
394 public:
[email protected]3a3d47472010-07-15 21:03:54395 ListValue();
initial.commitd7cae122008-07-26 21:49:38396 ~ListValue();
397
398 // Subclassed methods
399 Value* DeepCopy() const;
400 virtual bool Equals(const Value* other) const;
401
402 // Clears the contents of this ListValue
403 void Clear();
404
405 // Returns the number of Values in this list.
406 size_t GetSize() const { return list_.size(); }
407
[email protected]ec330b52009-12-02 00:20:32408 // Returns whether the list is empty.
409 bool empty() const { return list_.empty(); }
410
initial.commitd7cae122008-07-26 21:49:38411 // Sets the list item at the given index to be the Value specified by
412 // the value given. If the index beyond the current end of the list, null
413 // Values will be used to pad out the list.
414 // Returns true if successful, or false if the index was negative or
415 // the value is a null pointer.
416 bool Set(size_t index, Value* in_value);
417
[email protected]35213ce92010-04-08 19:06:15418 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38419 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15420 // Note that the list always owns the Value passed out via |out_value|.
initial.commitd7cae122008-07-26 21:49:38421 bool Get(size_t index, Value** out_value) const;
422
[email protected]35213ce92010-04-08 19:06:15423 // Convenience forms of Get(). Modifies |out_value| (and returns true)
424 // only if the index is valid and the Value at that index can be returned
425 // in the specified form.
[email protected]f82fb4952009-01-20 21:05:32426 bool GetBoolean(size_t index, bool* out_value) const;
427 bool GetInteger(size_t index, int* out_value) const;
428 bool GetReal(size_t index, double* out_value) const;
429 bool GetString(size_t index, std::string* out_value) const;
[email protected]99efb7b12009-12-18 02:39:16430 bool GetString(size_t index, std::wstring* out_value) const;
[email protected]9101ef1e2010-01-15 20:09:03431 bool GetStringAsUTF16(size_t index, string16* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32432 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38433 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
[email protected]f82fb4952009-01-20 21:05:32434 bool GetList(size_t index, ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38435
436 // Removes the Value with the specified index from this list.
437 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51438 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38439 // be deleted. This method returns true if |index| is valid; otherwise
440 // it will return false and the ListValue object will be unchanged.
441 bool Remove(size_t index, Value** out_value);
442
[email protected]6832cbe2009-11-30 19:59:11443 // Removes the first instance of |value| found in the list, if any, and
444 // deletes it. Returns the index that it was located at (-1 for not present).
[email protected]86c008e82009-08-28 20:26:05445 int Remove(const Value& value);
[email protected]e7f5c6f2009-05-09 00:33:04446
initial.commitd7cae122008-07-26 21:49:38447 // Appends a Value to the end of the list.
448 void Append(Value* in_value);
449
[email protected]840642202010-04-12 21:48:10450 // Appends a Value if it's not already present.
451 // Returns true if successful, or false if the value was already present.
452 bool AppendIfNotPresent(Value* in_value);
453
[email protected]86c008e82009-08-28 20:26:05454 // Insert a Value at index.
455 // Returns true if successful, or false if the index was out of range.
456 bool Insert(size_t index, Value* in_value);
457
initial.commitd7cae122008-07-26 21:49:38458 // Iteration
459 typedef ValueVector::iterator iterator;
460 typedef ValueVector::const_iterator const_iterator;
461
462 ListValue::iterator begin() { return list_.begin(); }
463 ListValue::iterator end() { return list_.end(); }
464
465 ListValue::const_iterator begin() const { return list_.begin(); }
466 ListValue::const_iterator end() const { return list_.end(); }
467
initial.commitd7cae122008-07-26 21:49:38468 private:
initial.commitd7cae122008-07-26 21:49:38469 ValueVector list_;
[email protected]7867cd02009-09-14 16:56:12470
471 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commitd7cae122008-07-26 21:49:38472};
473
474// This interface is implemented by classes that know how to serialize and
475// deserialize Value objects.
476class ValueSerializer {
477 public:
[email protected]3a3d47472010-07-15 21:03:54478 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59479
initial.commitd7cae122008-07-26 21:49:38480 virtual bool Serialize(const Value& root) = 0;
481
482 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08483 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39484 // Value. If the return value is NULL, and if error_code is non-NULL,
485 // error_code will be set with the underlying error.
486 // If |error_message| is non-null, it will be filled in with a formatted
487 // error message including the location of the error if appropriate.
488 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38489};
490
[email protected]101d5422008-09-26 20:22:42491#endif // BASE_VALUES_H_