blob: 1e1cae3a9a93efaeaa0cc46feb71a8c6d19b2890 [file] [log] [blame]
[email protected]13502562012-05-09 21:54:271// Copyright (c) 2012 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
[email protected]2f03f532013-07-17 08:43:336// storing settings and other persistable data.
initial.commitd7cae122008-07-26 21:49:387//
[email protected]2f03f532013-07-17 08:43:338// A Value represents something that can be stored in JSON or passed to/from
9// JavaScript. As such, it is NOT a generalized variant type, since only the
10// types supported by JavaScript/JSON are supported.
initial.commitd7cae122008-07-26 21:49:3811//
[email protected]2f03f532013-07-17 08:43:3312// IN PARTICULAR this means that there is no support for int64 or unsigned
13// numbers. Writing JSON with such types would violate the spec. If you need
14// something like this, either use a double or make a string value containing
15// the number you want.
initial.commitd7cae122008-07-26 21:49:3816
[email protected]101d5422008-09-26 20:22:4217#ifndef BASE_VALUES_H_
18#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:3819
[email protected]c014f2b32013-09-03 23:29:1220#include <stddef.h>
21
22#include <iosfwd>
initial.commitd7cae122008-07-26 21:49:3823#include <map>
[email protected]8e50b602009-03-03 22:59:4324#include <string>
[email protected]c014f2b32013-09-03 23:29:1225#include <utility>
initial.commitd7cae122008-07-26 21:49:3826#include <vector>
27
[email protected]0bea7252011-08-05 15:34:0028#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3829#include "base/basictypes.h"
[email protected]e8789192011-08-11 20:56:3230#include "base/compiler_specific.h"
[email protected]2d6504b72013-01-08 03:16:2231#include "base/memory/scoped_ptr.h"
[email protected]c851cfd2013-06-10 20:11:1432#include "base/strings/string16.h"
initial.commitd7cae122008-07-26 21:49:3833
[email protected]f3a1c642011-07-12 19:15:0334namespace base {
35
pneubeck93871252015-01-20 11:26:3636class BinaryValue;
initial.commitd7cae122008-07-26 21:49:3837class DictionaryValue;
[email protected]68d9d352011-02-21 16:35:0438class FundamentalValue;
initial.commitd7cae122008-07-26 21:49:3839class ListValue;
[email protected]68d9d352011-02-21 16:35:0440class StringValue;
41class Value;
initial.commitd7cae122008-07-26 21:49:3842
43typedef std::vector<Value*> ValueVector;
[email protected]e7b418b2010-07-30 19:47:4744typedef std::map<std::string, Value*> ValueMap;
initial.commitd7cae122008-07-26 21:49:3845
[email protected]b59ea312011-08-05 18:20:0546// The Value class is the base class for Values. A Value can be instantiated
47// via the Create*Value() factory methods, or by directly creating instances of
48// the subclasses.
[email protected]2f03f532013-07-17 08:43:3349//
50// See the file-level comment above for more information.
[email protected]0bea7252011-08-05 15:34:0051class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3852 public:
[email protected]bab1c13f2011-08-12 20:59:0253 enum Type {
[email protected]a502bbe72011-01-07 18:06:4554 TYPE_NULL = 0,
55 TYPE_BOOLEAN,
56 TYPE_INTEGER,
[email protected]fb534c92011-02-01 01:02:0757 TYPE_DOUBLE,
[email protected]a502bbe72011-01-07 18:06:4558 TYPE_STRING,
59 TYPE_BINARY,
60 TYPE_DICTIONARY,
61 TYPE_LIST
[email protected]2f03f532013-07-17 08:43:3362 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:4563 };
64
initial.commitd7cae122008-07-26 21:49:3865 virtual ~Value();
66
initial.commitd7cae122008-07-26 21:49:3867 static Value* CreateNullValue();
initial.commitd7cae122008-07-26 21:49:3868
initial.commitd7cae122008-07-26 21:49:3869 // Returns the type of the value stored by the current Value object.
70 // Each type will be implemented by only one subclass of Value, so it's
[email protected]bab1c13f2011-08-12 20:59:0271 // safe to use the Type to determine whether you can cast from
initial.commitd7cae122008-07-26 21:49:3872 // Value* to (Implementing Class)*. Also, a Value object never changes
73 // its type after construction.
[email protected]bab1c13f2011-08-12 20:59:0274 Type GetType() const { return type_; }
initial.commitd7cae122008-07-26 21:49:3875
76 // Returns true if the current object represents a given type.
[email protected]bab1c13f2011-08-12 20:59:0277 bool IsType(Type type) const { return type == type_; }
initial.commitd7cae122008-07-26 21:49:3878
[email protected]2f03f532013-07-17 08:43:3379 // These methods allow the convenient retrieval of the contents of the Value.
80 // If the current object can be converted into the given type, the value is
81 // returned through the |out_value| parameter and true is returned;
82 // otherwise, false is returned and |out_value| is unchanged.
initial.commitd7cae122008-07-26 21:49:3883 virtual bool GetAsBoolean(bool* out_value) const;
84 virtual bool GetAsInteger(int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0785 virtual bool GetAsDouble(double* out_value) const;
[email protected]4cd5f6a2008-12-11 01:23:1786 virtual bool GetAsString(std::string* out_value) const;
[email protected]e2e593d2010-08-03 15:42:5887 virtual bool GetAsString(string16* out_value) const;
[email protected]c0373312014-02-05 20:42:0688 virtual bool GetAsString(const StringValue** out_value) const;
pneubeck93871252015-01-20 11:26:3689 virtual bool GetAsBinary(const BinaryValue** out_value) const;
[email protected]81f9fe0b2010-12-07 00:35:2990 virtual bool GetAsList(ListValue** out_value);
[email protected]35552dc52011-07-12 09:04:3891 virtual bool GetAsList(const ListValue** out_value) const;
[email protected]5cf906f82011-11-26 01:11:4492 virtual bool GetAsDictionary(DictionaryValue** out_value);
93 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:3394 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:3895
96 // This creates a deep copy of the entire Value tree, and returns a pointer
97 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:5998 //
99 // Subclasses return their own type directly in their overrides;
100 // this works because C++ supports covariant return types.
initial.commitd7cae122008-07-26 21:49:38101 virtual Value* DeepCopy() const;
102
103 // Compares if two Value objects have equal contents.
104 virtual bool Equals(const Value* other) const;
105
[email protected]73c47932010-12-06 18:13:43106 // Compares if two Value objects have equal contents. Can handle NULLs.
107 // NULLs are considered equal but different from Value::CreateNullValue().
108 static bool Equals(const Value* a, const Value* b);
109
initial.commitd7cae122008-07-26 21:49:38110 protected:
[email protected]09d7a3a2012-11-20 20:37:55111 // These aren't safe for end-users, but they are useful for subclasses.
[email protected]bab1c13f2011-08-12 20:59:02112 explicit Value(Type type);
[email protected]09d7a3a2012-11-20 20:37:55113 Value(const Value& that);
114 Value& operator=(const Value& that);
initial.commitd7cae122008-07-26 21:49:38115
116 private:
[email protected]9b5f56b42011-08-24 21:17:59117 Type type_;
initial.commitd7cae122008-07-26 21:49:38118};
119
120// FundamentalValue represents the simple fundamental types of values.
[email protected]0bea7252011-08-05 15:34:00121class BASE_EXPORT FundamentalValue : public Value {
initial.commitd7cae122008-07-26 21:49:38122 public:
[email protected]3a3d47472010-07-15 21:03:54123 explicit FundamentalValue(bool in_value);
124 explicit FundamentalValue(int in_value);
125 explicit FundamentalValue(double in_value);
dcheng56488182014-10-21 10:54:51126 ~FundamentalValue() override;
initial.commitd7cae122008-07-26 21:49:38127
[email protected]e8789192011-08-11 20:56:32128 // Overridden from Value:
dcheng56488182014-10-21 10:54:51129 bool GetAsBoolean(bool* out_value) const override;
130 bool GetAsInteger(int* out_value) const override;
[email protected]78c03a42014-03-09 07:13:23131 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
132 // doubles.
dcheng56488182014-10-21 10:54:51133 bool GetAsDouble(double* out_value) const override;
134 FundamentalValue* DeepCopy() const override;
135 bool Equals(const Value* other) const override;
initial.commitd7cae122008-07-26 21:49:38136
137 private:
initial.commitd7cae122008-07-26 21:49:38138 union {
139 bool boolean_value_;
140 int integer_value_;
[email protected]fb534c92011-02-01 01:02:07141 double double_value_;
initial.commitd7cae122008-07-26 21:49:38142 };
143};
144
[email protected]0bea7252011-08-05 15:34:00145class BASE_EXPORT StringValue : public Value {
initial.commitd7cae122008-07-26 21:49:38146 public:
[email protected]4cd5f6a2008-12-11 01:23:17147 // Initializes a StringValue with a UTF-8 narrow character string.
[email protected]7867cd02009-09-14 16:56:12148 explicit StringValue(const std::string& in_value);
[email protected]4cd5f6a2008-12-11 01:23:17149
[email protected]9101ef1e2010-01-15 20:09:03150 // Initializes a StringValue with a string16.
151 explicit StringValue(const string16& in_value);
[email protected]e2e593d2010-08-03 15:42:58152
dcheng56488182014-10-21 10:54:51153 ~StringValue() override;
initial.commitd7cae122008-07-26 21:49:38154
[email protected]c0373312014-02-05 20:42:06155 // Returns |value_| as a pointer or reference.
156 std::string* GetString();
157 const std::string& GetString() const;
158
[email protected]e8789192011-08-11 20:56:32159 // Overridden from Value:
dcheng56488182014-10-21 10:54:51160 bool GetAsString(std::string* out_value) const override;
161 bool GetAsString(string16* out_value) const override;
162 bool GetAsString(const StringValue** out_value) const override;
163 StringValue* DeepCopy() const override;
164 bool Equals(const Value* other) const override;
initial.commitd7cae122008-07-26 21:49:38165
166 private:
[email protected]4cd5f6a2008-12-11 01:23:17167 std::string value_;
initial.commitd7cae122008-07-26 21:49:38168};
169
[email protected]0bea7252011-08-05 15:34:00170class BASE_EXPORT BinaryValue: public Value {
[email protected]7867cd02009-09-14 16:56:12171 public:
[email protected]2d6504b72013-01-08 03:16:22172 // Creates a BinaryValue with a null buffer and size of 0.
173 BinaryValue();
initial.commitd7cae122008-07-26 21:49:38174
[email protected]2d6504b72013-01-08 03:16:22175 // Creates a BinaryValue, taking ownership of the bytes pointed to by
176 // |buffer|.
177 BinaryValue(scoped_ptr<char[]> buffer, size_t size);
178
dcheng56488182014-10-21 10:54:51179 ~BinaryValue() override;
[email protected]0d0ed0c2012-05-20 02:34:57180
initial.commitd7cae122008-07-26 21:49:38181 // For situations where you want to keep ownership of your buffer, this
182 // factory method creates a new BinaryValue by copying the contents of the
183 // buffer that's passed in.
[email protected]e4dad9fb2009-10-06 18:15:58184 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
initial.commitd7cae122008-07-26 21:49:38185
initial.commitd7cae122008-07-26 21:49:38186 size_t GetSize() const { return size_; }
[email protected]2d6504b72013-01-08 03:16:22187
188 // May return NULL.
189 char* GetBuffer() { return buffer_.get(); }
190 const char* GetBuffer() const { return buffer_.get(); }
initial.commitd7cae122008-07-26 21:49:38191
[email protected]a502bbe72011-01-07 18:06:45192 // Overridden from Value:
pneubeck93871252015-01-20 11:26:36193 bool GetAsBinary(const BinaryValue** out_value) const override;
dcheng56488182014-10-21 10:54:51194 BinaryValue* DeepCopy() const override;
195 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45196
[email protected]7867cd02009-09-14 16:56:12197 private:
[email protected]2d6504b72013-01-08 03:16:22198 scoped_ptr<char[]> buffer_;
initial.commitd7cae122008-07-26 21:49:38199 size_t size_;
[email protected]7867cd02009-09-14 16:56:12200
201 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commitd7cae122008-07-26 21:49:38202};
203
[email protected]9e4cda7332010-07-31 04:56:14204// DictionaryValue provides a key-value dictionary with (optional) "path"
205// parsing for recursive access; see the comment at the top of the file. Keys
206// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00207class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38208 public:
[email protected]3a3d47472010-07-15 21:03:54209 DictionaryValue();
dcheng56488182014-10-21 10:54:51210 ~DictionaryValue() override;
initial.commitd7cae122008-07-26 21:49:38211
[email protected]5cf906f82011-11-26 01:11:44212 // Overridden from Value:
dcheng56488182014-10-21 10:54:51213 bool GetAsDictionary(DictionaryValue** out_value) override;
214 bool GetAsDictionary(const DictionaryValue** out_value) const override;
[email protected]5cf906f82011-11-26 01:11:44215
initial.commitd7cae122008-07-26 21:49:38216 // Returns true if the current dictionary has a value for the given key.
[email protected]e7b418b2010-07-30 19:47:47217 bool HasKey(const std::string& key) const;
initial.commitd7cae122008-07-26 21:49:38218
[email protected]fb804132c2009-04-15 00:17:53219 // Returns the number of Values in this dictionary.
[email protected]4dad9ad82009-11-25 20:47:52220 size_t size() const { return dictionary_.size(); }
221
222 // Returns whether the dictionary is empty.
223 bool empty() const { return dictionary_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53224
initial.commitd7cae122008-07-26 21:49:38225 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25226 void Clear();
initial.commitd7cae122008-07-26 21:49:38227
228 // Sets the Value associated with the given path starting from this object.
229 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
230 // into the next DictionaryValue down. Obviously, "." can't be used
231 // within a key, but there are no other restrictions on keys.
232 // If the key at any step of the way doesn't exist, or exists but isn't
233 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50234 // to the path in that location. |in_value| must be non-null.
235 void Set(const std::string& path, scoped_ptr<Value> in_value);
236 // Deprecated version of the above. TODO(estade): remove.
[email protected]e7b418b2010-07-30 19:47:47237 void Set(const std::string& path, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38238
239 // Convenience forms of Set(). These methods will replace any existing
240 // value at that path, even if it has a different type.
[email protected]e7b418b2010-07-30 19:47:47241 void SetBoolean(const std::string& path, bool in_value);
242 void SetInteger(const std::string& path, int in_value);
[email protected]fb534c92011-02-01 01:02:07243 void SetDouble(const std::string& path, double in_value);
[email protected]e7b418b2010-07-30 19:47:47244 void SetString(const std::string& path, const std::string& in_value);
[email protected]ff4c1d82010-08-04 16:58:12245 void SetString(const std::string& path, const string16& in_value);
[email protected]4dad9ad82009-11-25 20:47:52246
247 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
248 // be used as paths.
estadeca798482015-01-06 20:06:50249 void SetWithoutPathExpansion(const std::string& key,
250 scoped_ptr<Value> in_value);
251 // Deprecated version of the above. TODO(estade): remove.
[email protected]e7b418b2010-07-30 19:47:47252 void SetWithoutPathExpansion(const std::string& key, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38253
[email protected]095812b2012-09-14 02:14:01254 // Convenience forms of SetWithoutPathExpansion().
255 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value);
256 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value);
257 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value);
258 void SetStringWithoutPathExpansion(const std::string& path,
259 const std::string& in_value);
260 void SetStringWithoutPathExpansion(const std::string& path,
261 const string16& in_value);
262
initial.commitd7cae122008-07-26 21:49:38263 // Gets the Value associated with the given path starting from this object.
264 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
265 // into the next DictionaryValue down. If the path can be resolved
266 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15267 // through the |out_value| parameter, and the function will return true.
268 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38269 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23270 // |out_value| is optional and will only be set if non-NULL.
[email protected]a61890e2012-07-27 22:27:11271 bool Get(const std::string& path, const Value** out_value) const;
272 bool Get(const std::string& path, Value** out_value);
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]78c03a42014-03-09 07:13:23277 // |out_value| is optional and will only be set if non-NULL.
[email protected]e7b418b2010-07-30 19:47:47278 bool GetBoolean(const std::string& path, bool* out_value) const;
279 bool GetInteger(const std::string& path, int* out_value) const;
[email protected]78c03a42014-03-09 07:13:23280 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
281 // doubles.
[email protected]fb534c92011-02-01 01:02:07282 bool GetDouble(const std::string& path, double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47283 bool GetString(const std::string& path, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33284 bool GetString(const std::string& path, string16* out_value) const;
[email protected]9430e4b2010-02-19 13:32:16285 bool GetStringASCII(const std::string& path, std::string* out_value) const;
[email protected]a61890e2012-07-27 22:27:11286 bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
287 bool GetBinary(const std::string& path, BinaryValue** out_value);
[email protected]e7b418b2010-07-30 19:47:47288 bool GetDictionary(const std::string& path,
[email protected]a61890e2012-07-27 22:27:11289 const DictionaryValue** out_value) const;
290 bool GetDictionary(const std::string& path, DictionaryValue** out_value);
291 bool GetList(const std::string& path, const ListValue** out_value) const;
292 bool GetList(const std::string& path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38293
[email protected]4dad9ad82009-11-25 20:47:52294 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
295 // be used as paths.
[email protected]e7b418b2010-07-30 19:47:47296 bool GetWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11297 const Value** out_value) const;
298 bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
[email protected]a3a34012012-11-06 16:46:55299 bool GetBooleanWithoutPathExpansion(const std::string& key,
300 bool* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47301 bool GetIntegerWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52302 int* out_value) const;
[email protected]fb534c92011-02-01 01:02:07303 bool GetDoubleWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11304 double* out_value) const;
[email protected]e7b418b2010-07-30 19:47:47305 bool GetStringWithoutPathExpansion(const std::string& key,
[email protected]4dad9ad82009-11-25 20:47:52306 std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33307 bool GetStringWithoutPathExpansion(const std::string& key,
308 string16* out_value) const;
[email protected]a61890e2012-07-27 22:27:11309 bool GetDictionaryWithoutPathExpansion(
310 const std::string& key,
311 const DictionaryValue** out_value) const;
[email protected]e7b418b2010-07-30 19:47:47312 bool GetDictionaryWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11313 DictionaryValue** out_value);
[email protected]e7b418b2010-07-30 19:47:47314 bool GetListWithoutPathExpansion(const std::string& key,
[email protected]a61890e2012-07-27 22:27:11315 const ListValue** out_value) const;
316 bool GetListWithoutPathExpansion(const std::string& key,
317 ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52318
initial.commitd7cae122008-07-26 21:49:38319 // Removes the Value with the specified path from this dictionary (or one
320 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04321 // If |out_value| is non-NULL, the removed Value will be passed out via
322 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
323 // This method returns true if |path| is a valid path; otherwise it will
324 // return false and the DictionaryValue object will be unchanged.
325 virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38326
[email protected]4dad9ad82009-11-25 20:47:52327 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
328 // to be used as paths.
[email protected]6e680cf2012-05-16 15:23:30329 virtual bool RemoveWithoutPathExpansion(const std::string& key,
[email protected]d814a8852013-08-06 13:33:04330 scoped_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52331
[email protected]aa3283392013-11-27 01:38:24332 // Removes a path, clearing out all dictionaries on |path| that remain empty
333 // after removing the value at |path|.
334 virtual bool RemovePath(const std::string& path,
335 scoped_ptr<Value>* out_value);
336
[email protected]ec330b52009-12-02 00:20:32337 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
338 // the copy. This never returns NULL, even if |this| itself is empty.
[email protected]377f25322013-09-11 12:05:56339 DictionaryValue* DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32340
[email protected]13502562012-05-09 21:54:27341 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
342 // sub-dictionaries will be merged as well. In case of key collisions, the
343 // passed in dictionary takes precedence and data already present will be
344 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
345 // be freed any time after this call.
[email protected]c378cca2010-05-14 13:17:40346 void MergeDictionary(const DictionaryValue* dictionary);
347
[email protected]ec5263a2011-05-10 09:23:39348 // Swaps contents with the |other| dictionary.
[email protected]6e680cf2012-05-16 15:23:30349 virtual void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39350
[email protected]32c0e002011-11-08 21:26:41351 // This class provides an iterator over both keys and values in the
352 // dictionary. It can't be used to modify the dictionary.
[email protected]a34cc092012-08-10 12:45:35353 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41354 public:
[email protected]a34cc092012-08-10 12:45:35355 explicit Iterator(const DictionaryValue& target);
[email protected]a8d94b42013-12-10 18:52:22356 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41357
[email protected]a899c0b02013-01-18 14:43:27358 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
[email protected]32c0e002011-11-08 21:26:41359 void Advance() { ++it_; }
360
361 const std::string& key() const { return it_->first; }
362 const Value& value() const { return *it_->second; }
363
364 private:
365 const DictionaryValue& target_;
366 ValueMap::const_iterator it_;
367 };
368
[email protected]a502bbe72011-01-07 18:06:45369 // Overridden from Value:
dcheng56488182014-10-21 10:54:51370 DictionaryValue* DeepCopy() const override;
371 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45372
initial.commitd7cae122008-07-26 21:49:38373 private:
initial.commitd7cae122008-07-26 21:49:38374 ValueMap dictionary_;
[email protected]7867cd02009-09-14 16:56:12375
376 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commitd7cae122008-07-26 21:49:38377};
378
379// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00380class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38381 public:
[email protected]a502bbe72011-01-07 18:06:45382 typedef ValueVector::iterator iterator;
383 typedef ValueVector::const_iterator const_iterator;
384
[email protected]3a3d47472010-07-15 21:03:54385 ListValue();
dcheng56488182014-10-21 10:54:51386 ~ListValue() override;
initial.commitd7cae122008-07-26 21:49:38387
initial.commitd7cae122008-07-26 21:49:38388 // Clears the contents of this ListValue
389 void Clear();
390
391 // Returns the number of Values in this list.
392 size_t GetSize() const { return list_.size(); }
393
[email protected]ec330b52009-12-02 00:20:32394 // Returns whether the list is empty.
395 bool empty() const { return list_.empty(); }
396
initial.commitd7cae122008-07-26 21:49:38397 // Sets the list item at the given index to be the Value specified by
398 // the value given. If the index beyond the current end of the list, null
399 // Values will be used to pad out the list.
400 // Returns true if successful, or false if the index was negative or
401 // the value is a null pointer.
402 bool Set(size_t index, Value* in_value);
403
[email protected]35213ce92010-04-08 19:06:15404 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38405 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15406 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23407 // |out_value| is optional and will only be set if non-NULL.
[email protected]5d30f92bf2012-08-03 08:43:37408 bool Get(size_t index, const Value** out_value) const;
409 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38410
[email protected]35213ce92010-04-08 19:06:15411 // Convenience forms of Get(). Modifies |out_value| (and returns true)
412 // only if the index is valid and the Value at that index can be returned
413 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23414 // |out_value| is optional and will only be set if non-NULL.
[email protected]f82fb4952009-01-20 21:05:32415 bool GetBoolean(size_t index, bool* out_value) const;
416 bool GetInteger(size_t index, int* out_value) const;
[email protected]78c03a42014-03-09 07:13:23417 // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
418 // doubles.
[email protected]fb534c92011-02-01 01:02:07419 bool GetDouble(size_t index, double* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32420 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33421 bool GetString(size_t index, string16* out_value) const;
[email protected]5d30f92bf2012-08-03 08:43:37422 bool GetBinary(size_t index, const BinaryValue** out_value) const;
423 bool GetBinary(size_t index, BinaryValue** out_value);
424 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
425 bool GetDictionary(size_t index, DictionaryValue** out_value);
426 bool GetList(size_t index, const ListValue** out_value) const;
427 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38428
429 // Removes the Value with the specified index from this list.
430 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51431 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38432 // be deleted. This method returns true if |index| is valid; otherwise
433 // it will return false and the ListValue object will be unchanged.
[email protected]d814a8852013-08-06 13:33:04434 virtual bool Remove(size_t index, scoped_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38435
[email protected]6832cbe2009-11-30 19:59:11436 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31437 // deletes it. |index| is the location where |value| was found. Returns false
438 // if not found.
439 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04440
[email protected]3cbe0812012-07-03 02:51:43441 // Removes the element at |iter|. If |out_value| is NULL, the value will be
442 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45443 // Returns an iterator pointing to the location of the element that
444 // followed the erased element.
[email protected]d814a8852013-08-06 13:33:04445 iterator Erase(iterator iter, scoped_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43446
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]095812b2012-09-14 02:14:01450 // Convenience forms of Append.
451 void AppendBoolean(bool in_value);
452 void AppendInteger(int in_value);
453 void AppendDouble(double in_value);
454 void AppendString(const std::string& in_value);
455 void AppendString(const string16& in_value);
456 void AppendStrings(const std::vector<std::string>& in_values);
457 void AppendStrings(const std::vector<string16>& in_values);
458
[email protected]e36eaac2011-03-18 13:56:38459 // Appends a Value if it's not already present. Takes ownership of the
460 // |in_value|. Returns true if successful, or false if the value was already
461 // present. If the value was already present the |in_value| is deleted.
[email protected]840642202010-04-12 21:48:10462 bool AppendIfNotPresent(Value* in_value);
463
[email protected]86c008e82009-08-28 20:26:05464 // Insert a Value at index.
465 // Returns true if successful, or false if the index was out of range.
466 bool Insert(size_t index, Value* in_value);
467
[email protected]5fb35372011-09-19 15:23:10468 // Searches for the first instance of |value| in the list using the Equals
469 // method of the Value type.
470 // Returns a const_iterator to the found item or to end() if none exists.
471 const_iterator Find(const Value& value) const;
472
[email protected]8b8e7c92010-08-19 18:05:56473 // Swaps contents with the |other| list.
[email protected]6e680cf2012-05-16 15:23:30474 virtual void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56475
[email protected]e8789192011-08-11 20:56:32476 // Iteration.
477 iterator begin() { return list_.begin(); }
478 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38479
[email protected]e8789192011-08-11 20:56:32480 const_iterator begin() const { return list_.begin(); }
481 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38482
[email protected]a502bbe72011-01-07 18:06:45483 // Overridden from Value:
dcheng56488182014-10-21 10:54:51484 bool GetAsList(ListValue** out_value) override;
485 bool GetAsList(const ListValue** out_value) const override;
486 ListValue* DeepCopy() const override;
487 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45488
initial.commitd7cae122008-07-26 21:49:38489 private:
initial.commitd7cae122008-07-26 21:49:38490 ValueVector list_;
[email protected]7867cd02009-09-14 16:56:12491
492 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commitd7cae122008-07-26 21:49:38493};
494
prashhir54a994502015-03-05 09:30:57495// This interface is implemented by classes that know how to serialize
496// Value objects.
[email protected]0bea7252011-08-05 15:34:00497class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38498 public:
[email protected]3a3d47472010-07-15 21:03:54499 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59500
initial.commitd7cae122008-07-26 21:49:38501 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57502};
503
504// This interface is implemented by classes that know how to deserialize Value
505// objects.
506class BASE_EXPORT ValueDeserializer {
507 public:
508 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38509
510 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08511 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39512 // Value. If the return value is NULL, and if error_code is non-NULL,
513 // error_code will be set with the underlying error.
514 // If |error_message| is non-null, it will be filled in with a formatted
515 // error message including the location of the error if appropriate.
516 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38517};
518
[email protected]ea0ec052013-04-16 09:04:02519// Stream operator so Values can be used in assertion statements. In order that
520// gtest uses this operator to print readable output on test failures, we must
521// override each specific type. Otherwise, the default template implementation
522// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35523BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
524
[email protected]ea0ec052013-04-16 09:04:02525BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
526 const FundamentalValue& value) {
527 return out << static_cast<const Value&>(value);
528}
529
530BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
531 const StringValue& value) {
532 return out << static_cast<const Value&>(value);
533}
534
535BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
536 const DictionaryValue& value) {
537 return out << static_cast<const Value&>(value);
538}
539
540BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
541 const ListValue& value) {
542 return out << static_cast<const Value&>(value);
543}
544
[email protected]f3a1c642011-07-12 19:15:03545} // namespace base
546
[email protected]101d5422008-09-26 20:22:42547#endif // BASE_VALUES_H_