blob: 14a02e4077ea3ea7cad83a9ee1275360c033fbc1 [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//
avi9b6f42932015-12-26 22:15:1412// IN PARTICULAR this means that there is no support for int64_t or unsigned
[email protected]2f03f532013-07-17 08:43:3313// 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>
avi9b6f42932015-12-26 22:15:1421#include <stdint.h>
[email protected]c014f2b32013-09-03 23:29:1222
23#include <iosfwd>
initial.commitd7cae122008-07-26 21:49:3824#include <map>
dcheng093de9b2016-04-04 21:25:5125#include <memory>
[email protected]8e50b602009-03-03 22:59:4326#include <string>
[email protected]c014f2b32013-09-03 23:29:1227#include <utility>
initial.commitd7cae122008-07-26 21:49:3828#include <vector>
29
[email protected]0bea7252011-08-05 15:34:0030#include "base/base_export.h"
[email protected]e8789192011-08-11 20:56:3231#include "base/compiler_specific.h"
avi9b6f42932015-12-26 22:15:1432#include "base/macros.h"
[email protected]c851cfd2013-06-10 20:11:1433#include "base/strings/string16.h"
asvitkinedbd26533e2015-06-23 18:22:5234#include "base/strings/string_piece.h"
initial.commitd7cae122008-07-26 21:49:3835
[email protected]f3a1c642011-07-12 19:15:0336namespace base {
37
pneubeck93871252015-01-20 11:26:3638class BinaryValue;
initial.commitd7cae122008-07-26 21:49:3839class DictionaryValue;
[email protected]68d9d352011-02-21 16:35:0440class FundamentalValue;
initial.commitd7cae122008-07-26 21:49:3841class ListValue;
[email protected]68d9d352011-02-21 16:35:0442class StringValue;
43class Value;
initial.commitd7cae122008-07-26 21:49:3844
[email protected]b59ea312011-08-05 18:20:0545// The Value class is the base class for Values. A Value can be instantiated
46// via the Create*Value() factory methods, or by directly creating instances of
47// the subclasses.
[email protected]2f03f532013-07-17 08:43:3348//
49// See the file-level comment above for more information.
[email protected]0bea7252011-08-05 15:34:0050class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3851 public:
jdoerriedc72ee942016-12-07 15:43:2852 enum class Type {
53 NONE = 0,
54 BOOLEAN,
55 INTEGER,
56 DOUBLE,
57 STRING,
58 BINARY,
59 DICTIONARY,
60 LIST
[email protected]2f03f532013-07-17 08:43:3361 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:4562 };
63
initial.commitd7cae122008-07-26 21:49:3864 virtual ~Value();
65
dcheng093de9b2016-04-04 21:25:5166 static std::unique_ptr<Value> CreateNullValue();
initial.commitd7cae122008-07-26 21:49:3867
thestig61709242016-07-19 00:39:3068 // Returns the name for a given |type|.
69 static const char* GetTypeName(Type type);
70
initial.commitd7cae122008-07-26 21:49:3871 // Returns the type of the value stored by the current Value object.
72 // Each type will be implemented by only one subclass of Value, so it's
[email protected]bab1c13f2011-08-12 20:59:0273 // safe to use the Type to determine whether you can cast from
initial.commitd7cae122008-07-26 21:49:3874 // Value* to (Implementing Class)*. Also, a Value object never changes
75 // its type after construction.
[email protected]bab1c13f2011-08-12 20:59:0276 Type GetType() const { return type_; }
initial.commitd7cae122008-07-26 21:49:3877
78 // Returns true if the current object represents a given type.
[email protected]bab1c13f2011-08-12 20:59:0279 bool IsType(Type type) const { return type == type_; }
initial.commitd7cae122008-07-26 21:49:3880
[email protected]2f03f532013-07-17 08:43:3381 // These methods allow the convenient retrieval of the contents of the Value.
82 // If the current object can be converted into the given type, the value is
83 // returned through the |out_value| parameter and true is returned;
84 // otherwise, false is returned and |out_value| is unchanged.
initial.commitd7cae122008-07-26 21:49:3885 virtual bool GetAsBoolean(bool* out_value) const;
86 virtual bool GetAsInteger(int* out_value) const;
[email protected]fb534c92011-02-01 01:02:0787 virtual bool GetAsDouble(double* out_value) const;
[email protected]4cd5f6a2008-12-11 01:23:1788 virtual bool GetAsString(std::string* out_value) const;
[email protected]e2e593d2010-08-03 15:42:5889 virtual bool GetAsString(string16* out_value) const;
[email protected]c0373312014-02-05 20:42:0690 virtual bool GetAsString(const StringValue** out_value) const;
pneubeck93871252015-01-20 11:26:3691 virtual bool GetAsBinary(const BinaryValue** out_value) const;
lukaszad1485da72016-11-01 21:49:4692 // ListValue::From is the equivalent for std::unique_ptr conversions.
[email protected]81f9fe0b2010-12-07 00:35:2993 virtual bool GetAsList(ListValue** out_value);
[email protected]35552dc52011-07-12 09:04:3894 virtual bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:4695 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
[email protected]5cf906f82011-11-26 01:11:4496 virtual bool GetAsDictionary(DictionaryValue** out_value);
97 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:3398 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:3899
100 // This creates a deep copy of the entire Value tree, and returns a pointer
101 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59102 //
103 // Subclasses return their own type directly in their overrides;
104 // this works because C++ supports covariant return types.
initial.commitd7cae122008-07-26 21:49:38105 virtual Value* DeepCopy() const;
estade7bc801fb2015-05-07 01:53:08106 // Preferred version of DeepCopy. TODO(estade): remove the above.
dcheng093de9b2016-04-04 21:25:51107 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38108
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:
[email protected]09d7a3a2012-11-20 20:37:55117 // These aren't safe for end-users, but they are useful for subclasses.
[email protected]bab1c13f2011-08-12 20:59:02118 explicit Value(Type type);
[email protected]09d7a3a2012-11-20 20:37:55119 Value(const Value& that);
120 Value& operator=(const Value& that);
initial.commitd7cae122008-07-26 21:49:38121
122 private:
[email protected]9b5f56b42011-08-24 21:17:59123 Type type_;
initial.commitd7cae122008-07-26 21:49:38124};
125
126// FundamentalValue represents the simple fundamental types of values.
[email protected]0bea7252011-08-05 15:34:00127class BASE_EXPORT FundamentalValue : public Value {
initial.commitd7cae122008-07-26 21:49:38128 public:
[email protected]3a3d47472010-07-15 21:03:54129 explicit FundamentalValue(bool in_value);
130 explicit FundamentalValue(int in_value);
131 explicit FundamentalValue(double in_value);
dcheng56488182014-10-21 10:54:51132 ~FundamentalValue() override;
initial.commitd7cae122008-07-26 21:49:38133
[email protected]e8789192011-08-11 20:56:32134 // Overridden from Value:
dcheng56488182014-10-21 10:54:51135 bool GetAsBoolean(bool* out_value) const override;
136 bool GetAsInteger(int* out_value) const override;
jdoerriedc72ee942016-12-07 15:43:28137 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23138 // doubles.
dcheng56488182014-10-21 10:54:51139 bool GetAsDouble(double* out_value) const override;
140 FundamentalValue* DeepCopy() const override;
141 bool Equals(const Value* other) const override;
initial.commitd7cae122008-07-26 21:49:38142
143 private:
initial.commitd7cae122008-07-26 21:49:38144 union {
145 bool boolean_value_;
146 int integer_value_;
[email protected]fb534c92011-02-01 01:02:07147 double double_value_;
initial.commitd7cae122008-07-26 21:49:38148 };
149};
150
[email protected]0bea7252011-08-05 15:34:00151class BASE_EXPORT StringValue : public Value {
initial.commitd7cae122008-07-26 21:49:38152 public:
[email protected]4cd5f6a2008-12-11 01:23:17153 // Initializes a StringValue with a UTF-8 narrow character string.
dcheng16d6f532016-08-25 16:07:11154 explicit StringValue(StringPiece in_value);
[email protected]4cd5f6a2008-12-11 01:23:17155
[email protected]9101ef1e2010-01-15 20:09:03156 // Initializes a StringValue with a string16.
157 explicit StringValue(const string16& in_value);
[email protected]e2e593d2010-08-03 15:42:58158
dcheng56488182014-10-21 10:54:51159 ~StringValue() override;
initial.commitd7cae122008-07-26 21:49:38160
[email protected]c0373312014-02-05 20:42:06161 // Returns |value_| as a pointer or reference.
162 std::string* GetString();
163 const std::string& GetString() const;
164
[email protected]e8789192011-08-11 20:56:32165 // Overridden from Value:
dcheng56488182014-10-21 10:54:51166 bool GetAsString(std::string* out_value) const override;
167 bool GetAsString(string16* out_value) const override;
168 bool GetAsString(const StringValue** out_value) const override;
169 StringValue* DeepCopy() const override;
170 bool Equals(const Value* other) const override;
initial.commitd7cae122008-07-26 21:49:38171
172 private:
[email protected]4cd5f6a2008-12-11 01:23:17173 std::string value_;
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]2d6504b72013-01-08 03:16:22178 // Creates a BinaryValue with a null buffer and size of 0.
179 BinaryValue();
initial.commitd7cae122008-07-26 21:49:38180
[email protected]2d6504b72013-01-08 03:16:22181 // Creates a BinaryValue, taking ownership of the bytes pointed to by
182 // |buffer|.
dcheng093de9b2016-04-04 21:25:51183 BinaryValue(std::unique_ptr<char[]> buffer, size_t size);
[email protected]2d6504b72013-01-08 03:16:22184
dcheng56488182014-10-21 10:54:51185 ~BinaryValue() override;
[email protected]0d0ed0c2012-05-20 02:34:57186
initial.commitd7cae122008-07-26 21:49:38187 // For situations where you want to keep ownership of your buffer, this
188 // factory method creates a new BinaryValue by copying the contents of the
189 // buffer that's passed in.
dcheng338b88292016-06-16 10:48:42190 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer,
191 size_t size);
initial.commitd7cae122008-07-26 21:49:38192
initial.commitd7cae122008-07-26 21:49:38193 size_t GetSize() const { return size_; }
[email protected]2d6504b72013-01-08 03:16:22194
195 // May return NULL.
196 char* GetBuffer() { return buffer_.get(); }
197 const char* GetBuffer() const { return buffer_.get(); }
initial.commitd7cae122008-07-26 21:49:38198
[email protected]a502bbe72011-01-07 18:06:45199 // Overridden from Value:
pneubeck93871252015-01-20 11:26:36200 bool GetAsBinary(const BinaryValue** out_value) const override;
dcheng56488182014-10-21 10:54:51201 BinaryValue* DeepCopy() const override;
202 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45203
[email protected]7867cd02009-09-14 16:56:12204 private:
dcheng093de9b2016-04-04 21:25:51205 std::unique_ptr<char[]> buffer_;
initial.commitd7cae122008-07-26 21:49:38206 size_t size_;
[email protected]7867cd02009-09-14 16:56:12207
208 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
initial.commitd7cae122008-07-26 21:49:38209};
210
[email protected]9e4cda7332010-07-31 04:56:14211// DictionaryValue provides a key-value dictionary with (optional) "path"
212// parsing for recursive access; see the comment at the top of the file. Keys
213// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00214class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38215 public:
dchengcb60e702016-05-25 18:30:47216 using Storage = std::map<std::string, std::unique_ptr<Value>>;
reillyg259c0a32015-09-11 00:25:54217 // Returns |value| if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51218 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54219
[email protected]3a3d47472010-07-15 21:03:54220 DictionaryValue();
dcheng56488182014-10-21 10:54:51221 ~DictionaryValue() override;
initial.commitd7cae122008-07-26 21:49:38222
[email protected]5cf906f82011-11-26 01:11:44223 // Overridden from Value:
dcheng56488182014-10-21 10:54:51224 bool GetAsDictionary(DictionaryValue** out_value) override;
225 bool GetAsDictionary(const DictionaryValue** out_value) const override;
[email protected]5cf906f82011-11-26 01:11:44226
initial.commitd7cae122008-07-26 21:49:38227 // Returns true if the current dictionary has a value for the given key.
dcheng16d6f532016-08-25 16:07:11228 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38229
[email protected]fb804132c2009-04-15 00:17:53230 // Returns the number of Values in this dictionary.
[email protected]4dad9ad82009-11-25 20:47:52231 size_t size() const { return dictionary_.size(); }
232
233 // Returns whether the dictionary is empty.
234 bool empty() const { return dictionary_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53235
initial.commitd7cae122008-07-26 21:49:38236 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25237 void Clear();
initial.commitd7cae122008-07-26 21:49:38238
239 // Sets the Value associated with the given path starting from this object.
240 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
241 // into the next DictionaryValue down. Obviously, "." can't be used
242 // within a key, but there are no other restrictions on keys.
243 // If the key at any step of the way doesn't exist, or exists but isn't
244 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50245 // to the path in that location. |in_value| must be non-null.
dcheng16d6f532016-08-25 16:07:11246 void Set(StringPiece path, std::unique_ptr<Value> in_value);
estadeca798482015-01-06 20:06:50247 // Deprecated version of the above. TODO(estade): remove.
dcheng16d6f532016-08-25 16:07:11248 void Set(StringPiece path, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38249
250 // Convenience forms of Set(). These methods will replace any existing
251 // value at that path, even if it has a different type.
dcheng16d6f532016-08-25 16:07:11252 void SetBoolean(StringPiece path, bool in_value);
253 void SetInteger(StringPiece path, int in_value);
254 void SetDouble(StringPiece path, double in_value);
255 void SetString(StringPiece path, StringPiece in_value);
256 void SetString(StringPiece path, 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.
dcheng16d6f532016-08-25 16:07:11260 void SetWithoutPathExpansion(StringPiece key,
dcheng093de9b2016-04-04 21:25:51261 std::unique_ptr<Value> in_value);
estadeca798482015-01-06 20:06:50262 // Deprecated version of the above. TODO(estade): remove.
dcheng16d6f532016-08-25 16:07:11263 void SetWithoutPathExpansion(StringPiece key, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38264
[email protected]095812b2012-09-14 02:14:01265 // Convenience forms of SetWithoutPathExpansion().
dcheng16d6f532016-08-25 16:07:11266 void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
267 void SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
268 void SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
269 void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
270 void SetStringWithoutPathExpansion(StringPiece path,
[email protected]095812b2012-09-14 02:14:01271 const string16& in_value);
272
initial.commitd7cae122008-07-26 21:49:38273 // Gets the Value associated with the given path starting from this object.
274 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
275 // into the next DictionaryValue down. If the path can be resolved
276 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15277 // through the |out_value| parameter, and the function will return true.
278 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38279 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23280 // |out_value| is optional and will only be set if non-NULL.
asvitkinedbd26533e2015-06-23 18:22:52281 bool Get(StringPiece path, const Value** out_value) const;
282 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38283
284 // These are convenience forms of Get(). The value will be retrieved
285 // and the return value will be true if the path is valid and the value at
286 // the end of the path can be returned in the form specified.
[email protected]78c03a42014-03-09 07:13:23287 // |out_value| is optional and will only be set if non-NULL.
dcheng16d6f532016-08-25 16:07:11288 bool GetBoolean(StringPiece path, bool* out_value) const;
289 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28290 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23291 // doubles.
dcheng16d6f532016-08-25 16:07:11292 bool GetDouble(StringPiece path, double* out_value) const;
293 bool GetString(StringPiece path, std::string* out_value) const;
294 bool GetString(StringPiece path, string16* out_value) const;
295 bool GetStringASCII(StringPiece path, std::string* out_value) const;
296 bool GetBinary(StringPiece path, const BinaryValue** out_value) const;
297 bool GetBinary(StringPiece path, BinaryValue** out_value);
asvitkinedbd26533e2015-06-23 18:22:52298 bool GetDictionary(StringPiece path,
[email protected]a61890e2012-07-27 22:27:11299 const DictionaryValue** out_value) const;
asvitkinedbd26533e2015-06-23 18:22:52300 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
dcheng16d6f532016-08-25 16:07:11301 bool GetList(StringPiece path, const ListValue** out_value) const;
302 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38303
[email protected]4dad9ad82009-11-25 20:47:52304 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
305 // be used as paths.
dcheng16d6f532016-08-25 16:07:11306 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
307 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
308 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
309 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
310 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
311 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52312 std::string* out_value) const;
dcheng16d6f532016-08-25 16:07:11313 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]698f7f42010-08-04 19:35:33314 string16* out_value) const;
[email protected]a61890e2012-07-27 22:27:11315 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11316 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11317 const DictionaryValue** out_value) const;
dcheng16d6f532016-08-25 16:07:11318 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11319 DictionaryValue** out_value);
dcheng16d6f532016-08-25 16:07:11320 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11321 const ListValue** out_value) const;
dcheng16d6f532016-08-25 16:07:11322 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52323
initial.commitd7cae122008-07-26 21:49:38324 // Removes the Value with the specified path from this dictionary (or one
325 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04326 // If |out_value| is non-NULL, the removed Value will be passed out via
327 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
328 // This method returns true if |path| is a valid path; otherwise it will
329 // return false and the DictionaryValue object will be unchanged.
dcheng5f9cf762016-11-29 05:30:31330 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38331
[email protected]4dad9ad82009-11-25 20:47:52332 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
333 // to be used as paths.
dcheng16d6f532016-08-25 16:07:11334 virtual bool RemoveWithoutPathExpansion(StringPiece key,
dcheng093de9b2016-04-04 21:25:51335 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52336
[email protected]aa3283392013-11-27 01:38:24337 // Removes a path, clearing out all dictionaries on |path| that remain empty
338 // after removing the value at |path|.
dcheng5f9cf762016-11-29 05:30:31339 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24340
[email protected]ec330b52009-12-02 00:20:32341 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
342 // the copy. This never returns NULL, even if |this| itself is empty.
dcheng093de9b2016-04-04 21:25:51343 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32344
[email protected]13502562012-05-09 21:54:27345 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
346 // sub-dictionaries will be merged as well. In case of key collisions, the
347 // passed in dictionary takes precedence and data already present will be
348 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
349 // be freed any time after this call.
[email protected]c378cca2010-05-14 13:17:40350 void MergeDictionary(const DictionaryValue* dictionary);
351
[email protected]ec5263a2011-05-10 09:23:39352 // Swaps contents with the |other| dictionary.
[email protected]6e680cf2012-05-16 15:23:30353 virtual void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39354
[email protected]32c0e002011-11-08 21:26:41355 // This class provides an iterator over both keys and values in the
356 // dictionary. It can't be used to modify the dictionary.
[email protected]a34cc092012-08-10 12:45:35357 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41358 public:
[email protected]a34cc092012-08-10 12:45:35359 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31360 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22361 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41362
[email protected]a899c0b02013-01-18 14:43:27363 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
[email protected]32c0e002011-11-08 21:26:41364 void Advance() { ++it_; }
365
366 const std::string& key() const { return it_->first; }
367 const Value& value() const { return *it_->second; }
368
369 private:
370 const DictionaryValue& target_;
dchengcb60e702016-05-25 18:30:47371 Storage::const_iterator it_;
[email protected]32c0e002011-11-08 21:26:41372 };
373
[email protected]a502bbe72011-01-07 18:06:45374 // Overridden from Value:
dcheng56488182014-10-21 10:54:51375 DictionaryValue* DeepCopy() const override;
estade7bc801fb2015-05-07 01:53:08376 // Preferred version of DeepCopy. TODO(estade): remove the above.
dcheng093de9b2016-04-04 21:25:51377 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
dcheng56488182014-10-21 10:54:51378 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45379
initial.commitd7cae122008-07-26 21:49:38380 private:
dchengcb60e702016-05-25 18:30:47381 Storage dictionary_;
[email protected]7867cd02009-09-14 16:56:12382
383 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
initial.commitd7cae122008-07-26 21:49:38384};
385
386// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00387class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38388 public:
dchengcb60e702016-05-25 18:30:47389 using Storage = std::vector<std::unique_ptr<Value>>;
390 using const_iterator = Storage::const_iterator;
391 using iterator = Storage::iterator;
[email protected]a502bbe72011-01-07 18:06:45392
reillyg259c0a32015-09-11 00:25:54393 // Returns |value| if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51394 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54395
[email protected]3a3d47472010-07-15 21:03:54396 ListValue();
dcheng56488182014-10-21 10:54:51397 ~ListValue() override;
initial.commitd7cae122008-07-26 21:49:38398
initial.commitd7cae122008-07-26 21:49:38399 // Clears the contents of this ListValue
400 void Clear();
401
402 // Returns the number of Values in this list.
403 size_t GetSize() const { return list_.size(); }
404
[email protected]ec330b52009-12-02 00:20:32405 // Returns whether the list is empty.
406 bool empty() const { return list_.empty(); }
407
initial.commitd7cae122008-07-26 21:49:38408 // Sets the list item at the given index to be the Value specified by
409 // the value given. If the index beyond the current end of the list, null
410 // Values will be used to pad out the list.
411 // Returns true if successful, or false if the index was negative or
412 // the value is a null pointer.
413 bool Set(size_t index, Value* in_value);
estade7bc801fb2015-05-07 01:53:08414 // Preferred version of the above. TODO(estade): remove the above.
dcheng093de9b2016-04-04 21:25:51415 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38416
[email protected]35213ce92010-04-08 19:06:15417 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38418 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15419 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23420 // |out_value| is optional and will only be set if non-NULL.
[email protected]5d30f92bf2012-08-03 08:43:37421 bool Get(size_t index, const Value** out_value) const;
422 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38423
[email protected]35213ce92010-04-08 19:06:15424 // Convenience forms of Get(). Modifies |out_value| (and returns true)
425 // only if the index is valid and the Value at that index can be returned
426 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23427 // |out_value| is optional and will only be set if non-NULL.
[email protected]f82fb4952009-01-20 21:05:32428 bool GetBoolean(size_t index, bool* out_value) const;
429 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28430 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23431 // doubles.
[email protected]fb534c92011-02-01 01:02:07432 bool GetDouble(size_t index, double* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32433 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33434 bool GetString(size_t index, string16* out_value) const;
[email protected]5d30f92bf2012-08-03 08:43:37435 bool GetBinary(size_t index, const BinaryValue** out_value) const;
436 bool GetBinary(size_t index, BinaryValue** out_value);
437 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
438 bool GetDictionary(size_t index, DictionaryValue** out_value);
439 bool GetList(size_t index, const ListValue** out_value) const;
440 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38441
442 // Removes the Value with the specified index from this list.
443 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51444 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38445 // be deleted. This method returns true if |index| is valid; otherwise
446 // it will return false and the ListValue object will be unchanged.
dcheng093de9b2016-04-04 21:25:51447 virtual bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38448
[email protected]6832cbe2009-11-30 19:59:11449 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31450 // deletes it. |index| is the location where |value| was found. Returns false
451 // if not found.
452 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04453
[email protected]3cbe0812012-07-03 02:51:43454 // Removes the element at |iter|. If |out_value| is NULL, the value will be
455 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45456 // Returns an iterator pointing to the location of the element that
457 // followed the erased element.
dcheng093de9b2016-04-04 21:25:51458 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43459
initial.commitd7cae122008-07-26 21:49:38460 // Appends a Value to the end of the list.
dcheng093de9b2016-04-04 21:25:51461 void Append(std::unique_ptr<Value> in_value);
dcheng23779e842016-10-17 02:19:00462#if !defined(OS_LINUX)
estade7bc801fb2015-05-07 01:53:08463 // Deprecated version of the above. TODO(estade): remove.
initial.commitd7cae122008-07-26 21:49:38464 void Append(Value* in_value);
dcheng71bb8452016-09-17 01:30:15465#endif
initial.commitd7cae122008-07-26 21:49:38466
[email protected]095812b2012-09-14 02:14:01467 // Convenience forms of Append.
468 void AppendBoolean(bool in_value);
469 void AppendInteger(int in_value);
470 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11471 void AppendString(StringPiece in_value);
[email protected]095812b2012-09-14 02:14:01472 void AppendString(const string16& in_value);
473 void AppendStrings(const std::vector<std::string>& in_values);
474 void AppendStrings(const std::vector<string16>& in_values);
475
dcheng66c7a4c2016-09-14 05:49:58476 // Appends a Value if it's not already present. Returns true if successful,
477 // or false if the value was already
478 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
[email protected]840642202010-04-12 21:48:10479
[email protected]86c008e82009-08-28 20:26:05480 // Insert a Value at index.
481 // Returns true if successful, or false if the index was out of range.
dcheng66c7a4c2016-09-14 05:49:58482 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05483
[email protected]5fb35372011-09-19 15:23:10484 // Searches for the first instance of |value| in the list using the Equals
485 // method of the Value type.
486 // Returns a const_iterator to the found item or to end() if none exists.
487 const_iterator Find(const Value& value) const;
488
[email protected]8b8e7c92010-08-19 18:05:56489 // Swaps contents with the |other| list.
[email protected]6e680cf2012-05-16 15:23:30490 virtual void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56491
[email protected]e8789192011-08-11 20:56:32492 // Iteration.
493 iterator begin() { return list_.begin(); }
494 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38495
[email protected]e8789192011-08-11 20:56:32496 const_iterator begin() const { return list_.begin(); }
497 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38498
[email protected]a502bbe72011-01-07 18:06:45499 // Overridden from Value:
dcheng56488182014-10-21 10:54:51500 bool GetAsList(ListValue** out_value) override;
501 bool GetAsList(const ListValue** out_value) const override;
502 ListValue* DeepCopy() const override;
503 bool Equals(const Value* other) const override;
[email protected]a502bbe72011-01-07 18:06:45504
estadea68b0442015-05-12 18:11:50505 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
dcheng093de9b2016-04-04 21:25:51506 std::unique_ptr<ListValue> CreateDeepCopy() const;
estadea68b0442015-05-12 18:11:50507
initial.commitd7cae122008-07-26 21:49:38508 private:
dchengcb60e702016-05-25 18:30:47509 Storage list_;
[email protected]7867cd02009-09-14 16:56:12510
511 DISALLOW_COPY_AND_ASSIGN(ListValue);
initial.commitd7cae122008-07-26 21:49:38512};
513
prashhir54a994502015-03-05 09:30:57514// This interface is implemented by classes that know how to serialize
515// Value objects.
[email protected]0bea7252011-08-05 15:34:00516class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38517 public:
[email protected]3a3d47472010-07-15 21:03:54518 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59519
initial.commitd7cae122008-07-26 21:49:38520 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57521};
522
523// This interface is implemented by classes that know how to deserialize Value
524// objects.
525class BASE_EXPORT ValueDeserializer {
526 public:
527 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38528
529 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08530 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39531 // Value. If the return value is NULL, and if error_code is non-NULL,
532 // error_code will be set with the underlying error.
533 // If |error_message| is non-null, it will be filled in with a formatted
534 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51535 virtual std::unique_ptr<Value> Deserialize(int* error_code,
536 std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38537};
538
[email protected]ea0ec052013-04-16 09:04:02539// Stream operator so Values can be used in assertion statements. In order that
540// gtest uses this operator to print readable output on test failures, we must
541// override each specific type. Otherwise, the default template implementation
542// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35543BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
544
[email protected]ea0ec052013-04-16 09:04:02545BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
546 const FundamentalValue& value) {
547 return out << static_cast<const Value&>(value);
548}
549
550BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
551 const StringValue& value) {
552 return out << static_cast<const Value&>(value);
553}
554
555BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
556 const DictionaryValue& value) {
557 return out << static_cast<const Value&>(value);
558}
559
560BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
561 const ListValue& value) {
562 return out << static_cast<const Value&>(value);
563}
564
jdoerriedc72ee942016-12-07 15:43:28565// Stream operator so that enum class Types can be used in log statements.
566BASE_EXPORT std::ostream& operator<<(std::ostream& out,
567 const Value::Type& type);
568
[email protected]f3a1c642011-07-12 19:15:03569} // namespace base
570
[email protected]101d5422008-09-26 20:22:42571#endif // BASE_VALUES_H_