blob: 90cc40ccc07bffe6093419be40a19d209ee5b618 [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 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
[email protected]9e4cda7332010-07-31 04:56:145// 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.commitd7cae122008-07-26 21:49:3811//
[email protected]9e4cda7332010-07-31 04:56:1412// 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.commitd7cae122008-07-26 21:49:3815//
[email protected]9e4cda7332010-07-31 04:56:1416// 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.commitd7cae122008-07-26 21:49:3820
[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
[email protected]0bea7252011-08-05 15:34:0030#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3831#include "base/basictypes.h"
[email protected]e8789192011-08-11 20:56:3232#include "base/compiler_specific.h"
[email protected]9101ef1e2010-01-15 20:09:0333#include "base/string16.h"
initial.commitd7cae122008-07-26 21:49:3834
[email protected]f3a1c642011-07-12 19:15:0335// This file declares "using base::Value", etc. at the bottom, so that
36// current code can use these classes without the base namespace. In
37// new code, please always use base::Value, etc. or add your own
38// "using" declaration.
39// http://crbug.com/88666
40namespace base {
41
initial.commitd7cae122008-07-26 21:49:3842class BinaryValue;
43class DictionaryValue;
[email protected]68d9d352011-02-21 16:35:0444class FundamentalValue;
initial.commitd7cae122008-07-26 21:49:3845class ListValue;
[email protected]68d9d352011-02-21 16:35:0446class StringValue;
47class Value;
initial.commitd7cae122008-07-26 21:49:3848
49typedef std::vector<Value*> ValueVector;
[email protected]e7b418b2010-07-30 19:47:4750typedef std::map<std::string, Value*> ValueMap;
initial.commitd7cae122008-07-26 21:49:3851
[email protected]b59ea312011-08-05 18:20:0552// The Value class is the base class for Values. A Value can be instantiated
53// via the Create*Value() factory methods, or by directly creating instances of
54// the subclasses.
[email protected]0bea7252011-08-05 15:34:0055class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3856 public:
[email protected]bab1c13f2011-08-12 20:59:0257 enum Type {
[email protected]a502bbe72011-01-07 18:06:4558 TYPE_NULL = 0,
59 TYPE_BOOLEAN,
60 TYPE_INTEGER,
[email protected]fb534c92011-02-01 01:02:0761 TYPE_DOUBLE,
[email protected]a502bbe72011-01-07 18:06:4562 TYPE_STRING,
63 TYPE_BINARY,
64 TYPE_DICTIONARY,
65 TYPE_LIST
66 };
67
initial.commitd7cae122008-07-26 21:49:3868 virtual ~Value();
69
70 // Convenience methods for creating Value objects for various
71 // kinds of values without thinking about which class implements them.
72 // These can always be expected to return a valid Value*.
73 static Value* CreateNullValue();
[email protected]16f47e082011-01-18 02:16:5974 static FundamentalValue* CreateBooleanValue(bool in_value);
75 static FundamentalValue* CreateIntegerValue(int in_value);
[email protected]fb534c92011-02-01 01:02:0776 static FundamentalValue* CreateDoubleValue(double in_value);
[email protected]16f47e082011-01-18 02:16:5977 static StringValue* CreateStringValue(const std::string& in_value);
78 static StringValue* CreateStringValue(const string16& in_value);
initial.commitd7cae122008-07-26 21:49:3879
initial.commitd7cae122008-07-26 21:49:3880 // Returns the type of the value stored by the current Value object.
81 // Each type will be implemented by only one subclass of Value, so it's
[email protected]bab1c13f2011-08-12 20:59:0282 // safe to use the Type to determine whether you can cast from
initial.commitd7cae122008-07-26 21:49:3883 // Value* to (Implementing Class)*. Also, a Value object never changes
84 // its type after construction.
[email protected]bab1c13f2011-08-12 20:59:0285 Type GetType() const { return type_; }
initial.commitd7cae122008-07-26 21:49:3886
87 // Returns true if the current object represents a given type.
[email protected]bab1c13f2011-08-12 20:59:0288 bool IsType(Type type) const { return type == type_; }
initial.commitd7cae122008-07-26 21:49:3889
90 // These methods allow the convenient retrieval of settings.
91 // If the current setting object can be converted into the given type,
[email protected]35213ce92010-04-08 19:06:1592 // the value is returned through the |out_value| parameter and true is
93 // returned; otherwise, false is returned and |out_value| is unchanged.
initial.commitd7cae122008-07-26 21:49:3894 virtual bool GetAsBoolean(bool* out_value) const;
95 virtual bool GetAsInteger(int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0796 virtual bool GetAsDouble(double* out_value) const;
[email protected]4cd5f6a2008-12-11 01:23:1797 virtual bool GetAsString(std::string* out_value) const;
[email protected]e2e593d2010-08-03 15:42:5898 virtual bool GetAsString(string16* out_value) const;
[email protected]81f9fe0b2010-12-07 00:35:2999 virtual bool GetAsList(ListValue** out_value);
[email protected]35552dc52011-07-12 09:04:38100 virtual bool GetAsList(const ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38101
102 // This creates a deep copy of the entire Value tree, and returns a pointer
103 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59104 //
105 // Subclasses return their own type directly in their overrides;
106 // this works because C++ supports covariant return types.
initial.commitd7cae122008-07-26 21:49:38107 virtual Value* DeepCopy() const;
108
109 // Compares if two Value objects have equal contents.
110 virtual bool Equals(const Value* other) const;
111
[email protected]73c47932010-12-06 18:13:43112 // Compares if two Value objects have equal contents. Can handle NULLs.
113 // NULLs are considered equal but different from Value::CreateNullValue().
114 static bool Equals(const Value* a, const Value* b);
115
initial.commitd7cae122008-07-26 21:49:38116 protected:
117 // This isn't safe for end-users (they should use the Create*Value()
118 // static methods above), but it's useful for subclasses.
[email protected]bab1c13f2011-08-12 20:59:02119 explicit Value(Type type);
initial.commitd7cae122008-07-26 21:49:38120
121 private:
[email protected]9b5f56b42011-08-24 21:17:59122 Value();
[email protected]7867cd02009-09-14 16:56:12123
[email protected]9b5f56b42011-08-24 21:17:59124 Type type_;
[email protected]2f0a6472011-08-23 03:40:57125
[email protected]7867cd02009-09-14 16:56:12126 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commitd7cae122008-07-26 21:49:38127};
128
129// FundamentalValue represents the simple fundamental types of values.
[email protected]0bea7252011-08-05 15:34:00130class BASE_EXPORT FundamentalValue : public Value {
initial.commitd7cae122008-07-26 21:49:38131 public:
[email protected]3a3d47472010-07-15 21:03:54132 explicit FundamentalValue(bool in_value);
133 explicit FundamentalValue(int in_value);
134 explicit FundamentalValue(double in_value);
[email protected]78994ab02010-12-08 18:06:44135 virtual ~FundamentalValue();
initial.commitd7cae122008-07-26 21:49:38136
[email protected]e8789192011-08-11 20:56:32137 // Overridden from Value:
138 virtual bool GetAsBoolean(bool* out_value) const OVERRIDE;
139 virtual bool GetAsInteger(int* out_value) const OVERRIDE;
140 virtual bool GetAsDouble(double* out_value) const OVERRIDE;
141 virtual FundamentalValue* DeepCopy() const OVERRIDE;
142 virtual bool Equals(const Value* other) const OVERRIDE;
initial.commitd7cae122008-07-26 21:49:38143
144 private:
initial.commitd7cae122008-07-26 21:49:38145 union {
146 bool boolean_value_;
147 int integer_value_;
[email protected]fb534c92011-02-01 01:02:07148 double double_value_;
initial.commitd7cae122008-07-26 21:49:38149 };
[email protected]7867cd02009-09-14 16:56:12150
151 DISALLOW_COPY_AND_ASSIGN(FundamentalValue);
initial.commitd7cae122008-07-26 21:49:38152};
153
[email protected]0bea7252011-08-05 15:34:00154class BASE_EXPORT StringValue : public Value {
initial.commitd7cae122008-07-26 21:49:38155 public:
[email protected]4cd5f6a2008-12-11 01:23:17156 // Initializes a StringValue with a UTF-8 narrow character string.
[email protected]7867cd02009-09-14 16:56:12157 explicit StringValue(const std::string& in_value);
[email protected]4cd5f6a2008-12-11 01:23:17158
[email protected]9101ef1e2010-01-15 20:09:03159 // Initializes a StringValue with a string16.
160 explicit StringValue(const string16& in_value);
[email protected]e2e593d2010-08-03 15:42:58161
[email protected]78994ab02010-12-08 18:06:44162 virtual ~StringValue();
initial.commitd7cae122008-07-26 21:49:38163
[email protected]e8789192011-08-11 20:56:32164 // Overridden from Value:
165 virtual bool GetAsString(std::string* out_value) const OVERRIDE;
166 virtual bool GetAsString(string16* out_value) const OVERRIDE;
167 virtual StringValue* DeepCopy() const OVERRIDE;
168 virtual bool Equals(const Value* other) const OVERRIDE;
initial.commitd7cae122008-07-26 21:49:38169
170 private:
[email protected]4cd5f6a2008-12-11 01:23:17171 std::string value_;
[email protected]7867cd02009-09-14 16:56:12172
173 DISALLOW_COPY_AND_ASSIGN(StringValue);
initial.commitd7cae122008-07-26 21:49:38174};
175
[email protected]0bea7252011-08-05 15:34:00176class BASE_EXPORT BinaryValue: public Value {
[email protected]7867cd02009-09-14 16:56:12177 public:
[email protected]a502bbe72011-01-07 18:06:45178 virtual ~BinaryValue();
179
initial.commitd7cae122008-07-26 21:49:38180 // Creates a Value to represent a binary buffer. The new object takes
181 // ownership of the pointer passed in, if successful.
182 // Returns NULL if buffer is NULL.
183 static BinaryValue* Create(char* buffer, size_t size);
184
185 // For situations where you want to keep ownership of your buffer, this
186 // factory method creates a new BinaryValue by copying the contents of the
187 // buffer that's passed in.
188 // Returns NULL if buffer is NULL.
[email protected]e4dad9fb2009-10-06 18:15:58189 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38190
initial.commitd7cae122008-07-26 21:49:38191 size_t GetSize() const { return size_; }
192 char* GetBuffer() { return buffer_; }
[email protected]e4dad9fb2009-10-06 18:15:58193 const char* GetBuffer() const { return buffer_; }
initial.commitd7cae122008-07-26 21:49:38194
[email protected]a502bbe72011-01-07 18:06:45195 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32196 virtual BinaryValue* DeepCopy() const OVERRIDE;
197 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45198
[email protected]7867cd02009-09-14 16:56:12199 private:
initial.commitd7cae122008-07-26 21:49:38200 // Constructor is private so that only objects with valid buffer pointers
201 // and size values can be created.
[email protected]af5ed4a2008-08-04 13:56:25202 BinaryValue(char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38203
204 char* buffer_;
205 size_t size_;
[email protected]7867cd02009-09-14 16:56:12206
207 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commitd7cae122008-07-26 21:49:38208};
209
[email protected]9e4cda7332010-07-31 04:56:14210// DictionaryValue provides a key-value dictionary with (optional) "path"
211// parsing for recursive access; see the comment at the top of the file. Keys
212// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00213class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38214 public:
[email protected]3a3d47472010-07-15 21:03:54215 DictionaryValue();
[email protected]78994ab02010-12-08 18:06:44216 virtual ~DictionaryValue();
initial.commitd7cae122008-07-26 21:49:38217
initial.commitd7cae122008-07-26 21:49:38218 // Returns true if the current dictionary has a value for the given key.
[email protected]e7b418b2010-07-30 19:47:47219 bool HasKey(const std::string& key) const;
initial.commitd7cae122008-07-26 21:49:38220
[email protected]fb804132c2009-04-15 00:17:53221 // Returns the number of Values in this dictionary.
[email protected]4dad9ad82009-11-25 20:47:52222 size_t size() const { return dictionary_.size(); }
223
224 // Returns whether the dictionary is empty.
225 bool empty() const { return dictionary_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53226
initial.commitd7cae122008-07-26 21:49:38227 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25228 void Clear();
initial.commitd7cae122008-07-26 21:49:38229
230 // Sets the Value associated with the given path starting from this object.
231 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
232 // into the next DictionaryValue down. Obviously, "." can't be used
233 // within a key, but there are no other restrictions on keys.
234 // If the key at any step of the way doesn't exist, or exists but isn't
235 // a DictionaryValue, a new DictionaryValue will be created and attached
236 // to the path in that location.
[email protected]fb804132c2009-04-15 00:17:53237 // Note that the dictionary takes ownership of the value referenced by
[email protected]4dad9ad82009-11-25 20:47:52238 // |in_value|, and therefore |in_value| must be non-NULL.
[email protected]e7b418b2010-07-30 19:47:47239 void Set(const std::string& 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);
[email protected]fb534c92011-02-01 01:02:07245 void SetDouble(const std::string& path, double in_value);
[email protected]e7b418b2010-07-30 19:47:47246 void SetString(const std::string& path, const std::string& in_value);
[email protected]ff4c1d82010-08-04 16:58:12247 void SetString(const std::string& path, const string16& in_value);
[email protected]4dad9ad82009-11-25 20:47:52248
249 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
250 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47251 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38252
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
[email protected]35213ce92010-04-08 19:06:15257 // through the |out_value| parameter, and the function will return true.
258 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38259 // Note that the dictionary always owns the value that's returned.
[email protected]e7b418b2010-07-30 19:47:47260 bool Get(const std::string& path, Value** out_value) const;
initial.commitd7cae122008-07-26 21:49:38261
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.
[email protected]e7b418b2010-07-30 19:47:47265 bool GetBoolean(const std::string& path, bool* out_value) const;
266 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07267 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47268 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33269 bool GetString(const std::string& path, string16* out_value) const;
[email protected]9430e4b2010-02-19 13:32:16270 bool GetStringASCII(const std::string& path, std::string* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47271 bool GetBinary(const std::string& path, BinaryValue** out_value) const;
272 bool GetDictionary(const std::string& path,
initial.commitd7cae122008-07-26 21:49:38273 DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47274 bool GetList(const std::string& path, ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38275
[email protected]4dad9ad82009-11-25 20:47:52276 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
277 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47278 bool GetWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52279 Value** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47280 bool GetIntegerWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52281 int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07282 bool GetDoubleWithoutPathExpansion(const std::string& key,
[email protected]b75b8292010-10-01 07:28:25283 double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47284 bool GetStringWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52285 std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33286 bool GetStringWithoutPathExpansion(const std::string& key,
287 string16* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47288 bool GetDictionaryWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52289 DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47290 bool GetListWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52291 ListValue** out_value) const;
292
initial.commitd7cae122008-07-26 21:49:38293 // Removes the Value with the specified path from this dictionary (or one
294 // of its child dictionaries, if the path is more than just a local key).
295 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
296 // passed out via out_value. If |out_value| is NULL, the removed value will
297 // be deleted. This method returns true if |path| is a valid path; otherwise
298 // it will return false and the DictionaryValue object will be unchanged.
[email protected]e7b418b2010-07-30 19:47:47299 bool Remove(const std::string& path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38300
[email protected]4dad9ad82009-11-25 20:47:52301 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
302 // to be used as paths.
[email protected]e7b418b2010-07-30 19:47:47303 bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value);
[email protected]4dad9ad82009-11-25 20:47:52304
[email protected]ec330b52009-12-02 00:20:32305 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
306 // the copy. This never returns NULL, even if |this| itself is empty.
307 DictionaryValue* DeepCopyWithoutEmptyChildren();
308
[email protected]c378cca2010-05-14 13:17:40309 // Merge a given dictionary into this dictionary. This is done recursively,
310 // i.e. any subdictionaries will be merged as well. In case of key collisions,
311 // the passed in dictionary takes precedence and data already present will be
312 // replaced.
313 void MergeDictionary(const DictionaryValue* dictionary);
314
[email protected]ec5263a2011-05-10 09:23:39315 // Swaps contents with the |other| dictionary.
316 void Swap(DictionaryValue* other) {
317 dictionary_.swap(other->dictionary_);
318 }
319
initial.commitd7cae122008-07-26 21:49:38320 // This class provides an iterator for the keys in the dictionary.
321 // It can't be used to modify the dictionary.
[email protected]4dad9ad82009-11-25 20:47:52322 //
323 // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT
324 // THE NORMAL XXX() APIs. This makes sure things will work correctly if any
325 // keys have '.'s in them.
[email protected]2cdf8cda2011-04-29 18:08:21326 class key_iterator
[email protected]e7b418b2010-07-30 19:47:47327 : private std::iterator<std::input_iterator_tag, const std::string> {
[email protected]03c5e862009-02-17 22:50:14328 public:
[email protected]7867cd02009-09-14 16:56:12329 explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; }
[email protected]2fdc86a2010-01-26 23:08:02330 key_iterator operator++() {
331 ++itr_;
332 return *this;
333 }
[email protected]e7b418b2010-07-30 19:47:47334 const std::string& operator*() { return itr_->first; }
initial.commitd7cae122008-07-26 21:49:38335 bool operator!=(const key_iterator& other) { return itr_ != other.itr_; }
336 bool operator==(const key_iterator& other) { return itr_ == other.itr_; }
337
[email protected]03c5e862009-02-17 22:50:14338 private:
initial.commitd7cae122008-07-26 21:49:38339 ValueMap::const_iterator itr_;
340 };
341
342 key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); }
343 key_iterator end_keys() const { return key_iterator(dictionary_.end()); }
344
[email protected]a502bbe72011-01-07 18:06:45345 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32346 virtual DictionaryValue* DeepCopy() const OVERRIDE;
347 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45348
initial.commitd7cae122008-07-26 21:49:38349 private:
initial.commitd7cae122008-07-26 21:49:38350 ValueMap dictionary_;
[email protected]7867cd02009-09-14 16:56:12351
352 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commitd7cae122008-07-26 21:49:38353};
354
355// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00356class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38357 public:
[email protected]a502bbe72011-01-07 18:06:45358 typedef ValueVector::iterator iterator;
359 typedef ValueVector::const_iterator const_iterator;
360
[email protected]3a3d47472010-07-15 21:03:54361 ListValue();
[email protected]3690ebe02011-05-25 09:08:19362 virtual ~ListValue();
initial.commitd7cae122008-07-26 21:49:38363
initial.commitd7cae122008-07-26 21:49:38364 // Clears the contents of this ListValue
365 void Clear();
366
367 // Returns the number of Values in this list.
368 size_t GetSize() const { return list_.size(); }
369
[email protected]ec330b52009-12-02 00:20:32370 // Returns whether the list is empty.
371 bool empty() const { return list_.empty(); }
372
initial.commitd7cae122008-07-26 21:49:38373 // Sets the list item at the given index to be the Value specified by
374 // the value given. If the index beyond the current end of the list, null
375 // Values will be used to pad out the list.
376 // Returns true if successful, or false if the index was negative or
377 // the value is a null pointer.
378 bool Set(size_t index, Value* in_value);
379
[email protected]35213ce92010-04-08 19:06:15380 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38381 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15382 // Note that the list always owns the Value passed out via |out_value|.
initial.commitd7cae122008-07-26 21:49:38383 bool Get(size_t index, Value** out_value) const;
384
[email protected]35213ce92010-04-08 19:06:15385 // Convenience forms of Get(). Modifies |out_value| (and returns true)
386 // only if the index is valid and the Value at that index can be returned
387 // in the specified form.
[email protected]f82fb4952009-01-20 21:05:32388 bool GetBoolean(size_t index, bool* out_value) const;
389 bool GetInteger(size_t index, int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07390 bool GetDouble(size_t index, double* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32391 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33392 bool GetString(size_t index, string16* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32393 bool GetBinary(size_t index, BinaryValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38394 bool GetDictionary(size_t index, DictionaryValue** out_value) const;
[email protected]f82fb4952009-01-20 21:05:32395 bool GetList(size_t index, ListValue** out_value) const;
initial.commitd7cae122008-07-26 21:49:38396
397 // Removes the Value with the specified index from this list.
398 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51399 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38400 // be deleted. This method returns true if |index| is valid; otherwise
401 // it will return false and the ListValue object will be unchanged.
402 bool Remove(size_t index, Value** out_value);
403
[email protected]6832cbe2009-11-30 19:59:11404 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31405 // deletes it. |index| is the location where |value| was found. Returns false
406 // if not found.
407 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04408
initial.commitd7cae122008-07-26 21:49:38409 // Appends a Value to the end of the list.
410 void Append(Value* in_value);
411
[email protected]e36eaac2011-03-18 13:56:38412 // Appends a Value if it's not already present. Takes ownership of the
413 // |in_value|. Returns true if successful, or false if the value was already
414 // present. If the value was already present the |in_value| is deleted.
[email protected]840642202010-04-12 21:48:10415 bool AppendIfNotPresent(Value* in_value);
416
[email protected]86c008e82009-08-28 20:26:05417 // Insert a Value at index.
418 // Returns true if successful, or false if the index was out of range.
419 bool Insert(size_t index, Value* in_value);
420
[email protected]5fb35372011-09-19 15:23:10421 // Searches for the first instance of |value| in the list using the Equals
422 // method of the Value type.
423 // Returns a const_iterator to the found item or to end() if none exists.
424 const_iterator Find(const Value& value) const;
425
[email protected]8b8e7c92010-08-19 18:05:56426 // Swaps contents with the |other| list.
427 void Swap(ListValue* other) {
428 list_.swap(other->list_);
429 }
430
[email protected]e8789192011-08-11 20:56:32431 // Iteration.
432 iterator begin() { return list_.begin(); }
433 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38434
[email protected]e8789192011-08-11 20:56:32435 const_iterator begin() const { return list_.begin(); }
436 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38437
[email protected]a502bbe72011-01-07 18:06:45438 // Overridden from Value:
[email protected]e8789192011-08-11 20:56:32439 virtual bool GetAsList(ListValue** out_value) OVERRIDE;
440 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
441 virtual ListValue* DeepCopy() const OVERRIDE;
442 virtual bool Equals(const Value* other) const OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45443
initial.commitd7cae122008-07-26 21:49:38444 private:
initial.commitd7cae122008-07-26 21:49:38445 ValueVector list_;
[email protected]7867cd02009-09-14 16:56:12446
447 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commitd7cae122008-07-26 21:49:38448};
449
450// This interface is implemented by classes that know how to serialize and
451// deserialize Value objects.
[email protected]0bea7252011-08-05 15:34:00452class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38453 public:
[email protected]3a3d47472010-07-15 21:03:54454 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59455
initial.commitd7cae122008-07-26 21:49:38456 virtual bool Serialize(const Value& root) = 0;
457
458 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08459 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39460 // Value. If the return value is NULL, and if error_code is non-NULL,
461 // error_code will be set with the underlying error.
462 // If |error_message| is non-null, it will be filled in with a formatted
463 // error message including the location of the error if appropriate.
464 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38465};
466
[email protected]f3a1c642011-07-12 19:15:03467} // namespace base
468
469// http://crbug.com/88666
[email protected]f3a1c642011-07-12 19:15:03470using base::DictionaryValue;
[email protected]f3a1c642011-07-12 19:15:03471using base::ListValue;
472using base::StringValue;
473using base::Value;
[email protected]f3a1c642011-07-12 19:15:03474
[email protected]101d5422008-09-26 20:22:42475#endif // BASE_VALUES_H_