blob: 6cd68a7dc53928d9961a75da6caa5c2cf8949059 [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"
mkwstcd8067b2017-04-11 06:52:2132#include "base/containers/flat_map.h"
avi9b6f42932015-12-26 22:15:1433#include "base/macros.h"
jdoerrief38f37b2017-02-01 14:38:3234#include "base/memory/manual_constructor.h"
[email protected]c851cfd2013-06-10 20:11:1435#include "base/strings/string16.h"
asvitkinedbd26533e2015-06-23 18:22:5236#include "base/strings/string_piece.h"
initial.commitd7cae122008-07-26 21:49:3837
[email protected]f3a1c642011-07-12 19:15:0338namespace base {
39
initial.commitd7cae122008-07-26 21:49:3840class DictionaryValue;
41class ListValue;
[email protected]68d9d352011-02-21 16:35:0442class Value;
initial.commitd7cae122008-07-26 21:49:3843
[email protected]b59ea312011-08-05 18:20:0544// The Value class is the base class for Values. A Value can be instantiated
45// via the Create*Value() factory methods, or by directly creating instances of
46// the subclasses.
[email protected]2f03f532013-07-17 08:43:3347//
48// See the file-level comment above for more information.
[email protected]0bea7252011-08-05 15:34:0049class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3850 public:
jdoerrie5f12b6272017-04-18 10:22:4151 using BlobStorage = std::vector<char>;
mkwstcd8067b2017-04-11 06:52:2152 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>;
jdoerriea5676c62017-04-11 18:09:1453 using ListStorage = std::vector<Value>;
jdoerrie8e945542017-02-17 13:54:4954
jdoerriedc72ee942016-12-07 15:43:2855 enum class Type {
56 NONE = 0,
57 BOOLEAN,
58 INTEGER,
59 DOUBLE,
60 STRING,
61 BINARY,
62 DICTIONARY,
63 LIST
[email protected]2f03f532013-07-17 08:43:3364 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:4565 };
66
jdoerriee03e80f2017-02-15 08:42:1467 // For situations where you want to keep ownership of your buffer, this
68 // factory method creates a new BinaryValue by copying the contents of the
69 // buffer that's passed in.
jdoerrie5f12b6272017-04-18 10:22:4170 // DEPRECATED, use MakeUnique<Value>(const BlobStorage&) instead.
jdoerriee03e80f2017-02-15 08:42:1471 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie14b25da2017-04-11 07:45:5072 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
73 size_t size);
jdoerriee03e80f2017-02-15 08:42:1474
jdoerrie05eb3162017-02-01 10:36:5675 Value(const Value& that);
brettwf78cc272017-03-24 16:36:4276 Value(Value&& that) noexcept;
jdoerrie17e71cc2017-03-30 06:40:2977 Value() noexcept; // A null value.
jdoerrie05eb3162017-02-01 10:36:5678 explicit Value(Type type);
79 explicit Value(bool in_bool);
80 explicit Value(int in_int);
81 explicit Value(double in_double);
82
jdoerrief38f37b2017-02-01 14:38:3283 // Value(const char*) and Value(const char16*) are required despite
84 // Value(const std::string&) and Value(const string16&) because otherwise the
85 // compiler will choose the Value(bool) constructor for these arguments.
86 // Value(std::string&&) allow for efficient move construction.
87 // Value(StringPiece) exists due to many callsites passing StringPieces as
88 // arguments.
89 explicit Value(const char* in_string);
90 explicit Value(const std::string& in_string);
jdoerrie17e71cc2017-03-30 06:40:2991 explicit Value(std::string&& in_string) noexcept;
jdoerrief38f37b2017-02-01 14:38:3292 explicit Value(const char16* in_string);
93 explicit Value(const string16& in_string);
94 explicit Value(StringPiece in_string);
95
jdoerrie5f12b6272017-04-18 10:22:4196 explicit Value(const BlobStorage& in_blob);
97 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerriee03e80f2017-02-15 08:42:1498
mkwstcd8067b2017-04-11 06:52:2199 explicit Value(DictStorage&& in_dict) noexcept;
100
jdoerrie2b7d0fcd2017-04-19 07:15:38101 explicit Value(const ListStorage& in_list);
102 explicit Value(ListStorage&& in_list) noexcept;
103
jdoerrie05eb3162017-02-01 10:36:56104 Value& operator=(const Value& that);
jdoerrie17e71cc2017-03-30 06:40:29105 Value& operator=(Value&& that) noexcept;
jdoerrie05eb3162017-02-01 10:36:56106
jdoerrie8e945542017-02-17 13:54:49107 ~Value();
jdoerrie05eb3162017-02-01 10:36:56108
thestig61709242016-07-19 00:39:30109 // Returns the name for a given |type|.
110 static const char* GetTypeName(Type type);
111
initial.commitd7cae122008-07-26 21:49:38112 // Returns the type of the value stored by the current Value object.
113 // Each type will be implemented by only one subclass of Value, so it's
[email protected]bab1c13f2011-08-12 20:59:02114 // safe to use the Type to determine whether you can cast from
initial.commitd7cae122008-07-26 21:49:38115 // Value* to (Implementing Class)*. Also, a Value object never changes
116 // its type after construction.
jdoerrie05eb3162017-02-01 10:36:56117 Type GetType() const { return type_; } // DEPRECATED, use type().
118 Type type() const { return type_; }
initial.commitd7cae122008-07-26 21:49:38119
120 // Returns true if the current object represents a given type.
[email protected]bab1c13f2011-08-12 20:59:02121 bool IsType(Type type) const { return type == type_; }
jdoerrie05eb3162017-02-01 10:36:56122 bool is_bool() const { return type() == Type::BOOLEAN; }
123 bool is_int() const { return type() == Type::INTEGER; }
124 bool is_double() const { return type() == Type::DOUBLE; }
125 bool is_string() const { return type() == Type::STRING; }
126 bool is_blob() const { return type() == Type::BINARY; }
127 bool is_dict() const { return type() == Type::DICTIONARY; }
128 bool is_list() const { return type() == Type::LIST; }
129
130 // These will all fatally assert if the type doesn't match.
131 bool GetBool() const;
132 int GetInt() const;
133 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrief38f37b2017-02-01 14:38:32134 const std::string& GetString() const;
jdoerrie5f12b6272017-04-18 10:22:41135 const BlobStorage& GetBlob() const;
jdoerriee03e80f2017-02-15 08:42:14136
jdoerrie2b7d0fcd2017-04-19 07:15:38137 ListStorage& GetList();
138 const ListStorage& GetList() const;
139
[email protected]2f03f532013-07-17 08:43:33140 // These methods allow the convenient retrieval of the contents of the Value.
141 // If the current object can be converted into the given type, the value is
142 // returned through the |out_value| parameter and true is returned;
143 // otherwise, false is returned and |out_value| is unchanged.
jdoerrie8e945542017-02-17 13:54:49144 bool GetAsBoolean(bool* out_value) const;
145 bool GetAsInteger(int* out_value) const;
146 bool GetAsDouble(double* out_value) const;
147 bool GetAsString(std::string* out_value) const;
148 bool GetAsString(string16* out_value) const;
jdoerrie122c4da2017-03-06 11:12:04149 bool GetAsString(const Value** out_value) const;
jdoerrie8e945542017-02-17 13:54:49150 bool GetAsString(StringPiece* out_value) const;
lukaszad1485da72016-11-01 21:49:46151 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49152 bool GetAsList(ListValue** out_value);
153 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:46154 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49155 bool GetAsDictionary(DictionaryValue** out_value);
156 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:33157 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:38158
159 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:56160 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59161 // Subclasses return their own type directly in their overrides;
162 // this works because C++ supports covariant return types.
jdoerriee964d9a2017-04-05 06:44:17163 // DEPRECATED, use Value's copy constructor instead.
164 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49165 Value* DeepCopy() const;
estade7bc801fb2015-05-07 01:53:08166 // Preferred version of DeepCopy. TODO(estade): remove the above.
dcheng093de9b2016-04-04 21:25:51167 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38168
jdoerrie5c1cee112017-03-28 17:52:00169 // Comparison operators so that Values can easily be used with standard
170 // library algorithms and associative containers.
171 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
172 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
173 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
174 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
175 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
176 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
177
initial.commitd7cae122008-07-26 21:49:38178 // Compares if two Value objects have equal contents.
jdoerrie5c1cee112017-03-28 17:52:00179 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
180 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49181 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:38182
[email protected]73c47932010-12-06 18:13:43183 // Compares if two Value objects have equal contents. Can handle NULLs.
jdoerriee067999a2017-04-07 06:39:00184 // NULLs are considered equal but different from Value(Value::Type::NONE).
jdoerrie5c1cee112017-03-28 17:52:00185 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
186 // TODO(crbug.com/646113): Delete this and migrate callsites.
[email protected]73c47932010-12-06 18:13:43187 static bool Equals(const Value* a, const Value* b);
188
jdoerrie8e945542017-02-17 13:54:49189 protected:
190 // TODO(crbug.com/646113): Make these private once DictionaryValue and
191 // ListValue are properly inlined.
jdoerrie6acf28d2017-02-02 10:56:03192 Type type_;
193
initial.commitd7cae122008-07-26 21:49:38194 union {
jdoerrie05eb3162017-02-01 10:36:56195 bool bool_value_;
196 int int_value_;
[email protected]fb534c92011-02-01 01:02:07197 double double_value_;
jdoerrief38f37b2017-02-01 14:38:32198 ManualConstructor<std::string> string_value_;
jdoerrie5f12b6272017-04-18 10:22:41199 ManualConstructor<BlobStorage> binary_value_;
brettw82cef812017-04-14 19:46:51200 ManualConstructor<DictStorage> dict_;
jdoerrie8e945542017-02-17 13:54:49201 ManualConstructor<ListStorage> list_;
initial.commitd7cae122008-07-26 21:49:38202 };
jdoerrie8e945542017-02-17 13:54:49203
204 private:
205 void InternalCopyFundamentalValue(const Value& that);
206 void InternalCopyConstructFrom(const Value& that);
207 void InternalMoveConstructFrom(Value&& that);
vabr182756a2017-03-07 23:34:30208 void InternalCopyAssignFromSameType(const Value& that);
jdoerrie8e945542017-02-17 13:54:49209 void InternalCleanup();
initial.commitd7cae122008-07-26 21:49:38210};
211
[email protected]9e4cda7332010-07-31 04:56:14212// DictionaryValue provides a key-value dictionary with (optional) "path"
213// parsing for recursive access; see the comment at the top of the file. Keys
214// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00215class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38216 public:
Johan Tibell71bba86c2017-05-17 05:21:12217 using const_iterator = DictStorage::const_iterator;
218 using iterator = DictStorage::iterator;
219
reillyg259c0a32015-09-11 00:25:54220 // Returns |value| if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51221 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54222
[email protected]3a3d47472010-07-15 21:03:54223 DictionaryValue();
[email protected]5cf906f82011-11-26 01:11:44224
initial.commitd7cae122008-07-26 21:49:38225 // Returns true if the current dictionary has a value for the given key.
dcheng16d6f532016-08-25 16:07:11226 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38227
[email protected]fb804132c2009-04-15 00:17:53228 // Returns the number of Values in this dictionary.
brettw82cef812017-04-14 19:46:51229 size_t size() const { return dict_->size(); }
[email protected]4dad9ad82009-11-25 20:47:52230
231 // Returns whether the dictionary is empty.
brettw82cef812017-04-14 19:46:51232 bool empty() const { return dict_->empty(); }
[email protected]fb804132c2009-04-15 00:17:53233
initial.commitd7cae122008-07-26 21:49:38234 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25235 void Clear();
initial.commitd7cae122008-07-26 21:49:38236
237 // Sets the Value associated with the given path starting from this object.
238 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
239 // into the next DictionaryValue down. Obviously, "." can't be used
240 // within a key, but there are no other restrictions on keys.
241 // If the key at any step of the way doesn't exist, or exists but isn't
242 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50243 // to the path in that location. |in_value| must be non-null.
jdoerrieb94e5422017-04-28 21:52:58244 // Returns a pointer to the inserted value.
245 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
estadeca798482015-01-06 20:06:50246 // Deprecated version of the above. TODO(estade): remove.
jdoerrieb94e5422017-04-28 21:52:58247 Value* Set(StringPiece path, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38248
249 // Convenience forms of Set(). These methods will replace any existing
250 // value at that path, even if it has a different type.
jdoerrieb94e5422017-04-28 21:52:58251 Value* SetBoolean(StringPiece path, bool in_value);
252 Value* SetInteger(StringPiece path, int in_value);
253 Value* SetDouble(StringPiece path, double in_value);
254 Value* SetString(StringPiece path, StringPiece in_value);
255 Value* SetString(StringPiece path, const string16& in_value);
256 DictionaryValue* SetDictionary(StringPiece path,
257 std::unique_ptr<DictionaryValue> in_value);
258 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:52259
260 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
261 // be used as paths.
jdoerrieb94e5422017-04-28 21:52:58262 Value* SetWithoutPathExpansion(StringPiece key,
263 std::unique_ptr<Value> in_value);
estadeca798482015-01-06 20:06:50264 // Deprecated version of the above. TODO(estade): remove.
jdoerrieb94e5422017-04-28 21:52:58265 Value* SetWithoutPathExpansion(StringPiece key, Value* in_value);
initial.commitd7cae122008-07-26 21:49:38266
[email protected]095812b2012-09-14 02:14:01267 // Convenience forms of SetWithoutPathExpansion().
jdoerrieb94e5422017-04-28 21:52:58268 Value* SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
269 Value* SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
270 Value* SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
271 Value* SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
272 Value* SetStringWithoutPathExpansion(StringPiece path,
273 const string16& in_value);
274 DictionaryValue* SetDictionaryWithoutPathExpansion(
275 StringPiece path,
276 std::unique_ptr<DictionaryValue> in_value);
277 ListValue* SetListWithoutPathExpansion(StringPiece path,
278 std::unique_ptr<ListValue> in_value);
[email protected]095812b2012-09-14 02:14:01279
initial.commitd7cae122008-07-26 21:49:38280 // Gets the Value associated with the given path starting from this object.
281 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
282 // into the next DictionaryValue down. If the path can be resolved
283 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15284 // through the |out_value| parameter, and the function will return true.
285 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38286 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23287 // |out_value| is optional and will only be set if non-NULL.
asvitkinedbd26533e2015-06-23 18:22:52288 bool Get(StringPiece path, const Value** out_value) const;
289 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38290
291 // These are convenience forms of Get(). The value will be retrieved
292 // and the return value will be true if the path is valid and the value at
293 // the end of the path can be returned in the form specified.
[email protected]78c03a42014-03-09 07:13:23294 // |out_value| is optional and will only be set if non-NULL.
dcheng16d6f532016-08-25 16:07:11295 bool GetBoolean(StringPiece path, bool* out_value) const;
296 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28297 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23298 // doubles.
dcheng16d6f532016-08-25 16:07:11299 bool GetDouble(StringPiece path, double* out_value) const;
300 bool GetString(StringPiece path, std::string* out_value) const;
301 bool GetString(StringPiece path, string16* out_value) const;
302 bool GetStringASCII(StringPiece path, std::string* out_value) const;
jdoerrie14b25da2017-04-11 07:45:50303 bool GetBinary(StringPiece path, const Value** out_value) const;
304 bool GetBinary(StringPiece path, Value** out_value);
asvitkinedbd26533e2015-06-23 18:22:52305 bool GetDictionary(StringPiece path,
[email protected]a61890e2012-07-27 22:27:11306 const DictionaryValue** out_value) const;
asvitkinedbd26533e2015-06-23 18:22:52307 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
dcheng16d6f532016-08-25 16:07:11308 bool GetList(StringPiece path, const ListValue** out_value) const;
309 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38310
[email protected]4dad9ad82009-11-25 20:47:52311 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
312 // be used as paths.
dcheng16d6f532016-08-25 16:07:11313 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
314 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
315 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
316 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
317 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
318 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52319 std::string* out_value) const;
dcheng16d6f532016-08-25 16:07:11320 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]698f7f42010-08-04 19:35:33321 string16* out_value) const;
[email protected]a61890e2012-07-27 22:27:11322 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11323 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11324 const DictionaryValue** out_value) const;
dcheng16d6f532016-08-25 16:07:11325 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11326 DictionaryValue** out_value);
dcheng16d6f532016-08-25 16:07:11327 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11328 const ListValue** out_value) const;
dcheng16d6f532016-08-25 16:07:11329 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52330
initial.commitd7cae122008-07-26 21:49:38331 // Removes the Value with the specified path from this dictionary (or one
332 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04333 // If |out_value| is non-NULL, the removed Value will be passed out via
334 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
335 // This method returns true if |path| is a valid path; otherwise it will
336 // return false and the DictionaryValue object will be unchanged.
dcheng5f9cf762016-11-29 05:30:31337 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38338
[email protected]4dad9ad82009-11-25 20:47:52339 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
340 // to be used as paths.
jdoerrie8e945542017-02-17 13:54:49341 bool RemoveWithoutPathExpansion(StringPiece key,
342 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52343
[email protected]aa3283392013-11-27 01:38:24344 // Removes a path, clearing out all dictionaries on |path| that remain empty
345 // after removing the value at |path|.
dcheng5f9cf762016-11-29 05:30:31346 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24347
[email protected]ec330b52009-12-02 00:20:32348 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
349 // the copy. This never returns NULL, even if |this| itself is empty.
dcheng093de9b2016-04-04 21:25:51350 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32351
[email protected]13502562012-05-09 21:54:27352 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
353 // sub-dictionaries will be merged as well. In case of key collisions, the
354 // passed in dictionary takes precedence and data already present will be
355 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
356 // be freed any time after this call.
[email protected]c378cca2010-05-14 13:17:40357 void MergeDictionary(const DictionaryValue* dictionary);
358
[email protected]ec5263a2011-05-10 09:23:39359 // Swaps contents with the |other| dictionary.
jdoerrie8e945542017-02-17 13:54:49360 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39361
[email protected]32c0e002011-11-08 21:26:41362 // This class provides an iterator over both keys and values in the
363 // dictionary. It can't be used to modify the dictionary.
[email protected]a34cc092012-08-10 12:45:35364 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41365 public:
[email protected]a34cc092012-08-10 12:45:35366 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31367 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22368 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41369
brettw82cef812017-04-14 19:46:51370 bool IsAtEnd() const { return it_ == target_.dict_->end(); }
[email protected]32c0e002011-11-08 21:26:41371 void Advance() { ++it_; }
372
373 const std::string& key() const { return it_->first; }
374 const Value& value() const { return *it_->second; }
375
376 private:
377 const DictionaryValue& target_;
jdoerrie8e945542017-02-17 13:54:49378 DictStorage::const_iterator it_;
[email protected]32c0e002011-11-08 21:26:41379 };
380
Johan Tibell71bba86c2017-05-17 05:21:12381 // Iteration.
382 iterator begin() { return dict_->begin(); }
383 iterator end() { return dict_->end(); }
384
385 const_iterator begin() const { return dict_->begin(); }
386 const_iterator end() const { return dict_->end(); }
387
jdoerriee964d9a2017-04-05 06:44:17388 // DEPRECATED, use DictionaryValue's copy constructor instead.
389 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49390 DictionaryValue* DeepCopy() const;
estade7bc801fb2015-05-07 01:53:08391 // Preferred version of DeepCopy. TODO(estade): remove the above.
dcheng093de9b2016-04-04 21:25:51392 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38393};
394
395// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00396class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38397 public:
jdoerrie8e945542017-02-17 13:54:49398 using const_iterator = ListStorage::const_iterator;
399 using iterator = ListStorage::iterator;
[email protected]a502bbe72011-01-07 18:06:45400
reillyg259c0a32015-09-11 00:25:54401 // Returns |value| if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51402 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54403
[email protected]3a3d47472010-07-15 21:03:54404 ListValue();
jdoerrie52939ed2017-04-26 18:19:42405 explicit ListValue(const ListStorage& in_list);
406 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:38407
initial.commitd7cae122008-07-26 21:49:38408 // Clears the contents of this ListValue
409 void Clear();
410
411 // Returns the number of Values in this list.
jdoerrie8e945542017-02-17 13:54:49412 size_t GetSize() const { return list_->size(); }
initial.commitd7cae122008-07-26 21:49:38413
jdoerriea5676c62017-04-11 18:09:14414 // Returns the capacity of storage for Values in this list.
415 size_t capacity() const { return list_->capacity(); }
416
[email protected]ec330b52009-12-02 00:20:32417 // Returns whether the list is empty.
jdoerrie8e945542017-02-17 13:54:49418 bool empty() const { return list_->empty(); }
[email protected]ec330b52009-12-02 00:20:32419
jdoerriea5676c62017-04-11 18:09:14420 // Reserves storage for at least |n| values.
421 void Reserve(size_t n);
422
initial.commitd7cae122008-07-26 21:49:38423 // Sets the list item at the given index to be the Value specified by
424 // the value given. If the index beyond the current end of the list, null
425 // Values will be used to pad out the list.
426 // Returns true if successful, or false if the index was negative or
427 // the value is a null pointer.
dcheng093de9b2016-04-04 21:25:51428 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38429
[email protected]35213ce92010-04-08 19:06:15430 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38431 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15432 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23433 // |out_value| is optional and will only be set if non-NULL.
[email protected]5d30f92bf2012-08-03 08:43:37434 bool Get(size_t index, const Value** out_value) const;
435 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38436
[email protected]35213ce92010-04-08 19:06:15437 // Convenience forms of Get(). Modifies |out_value| (and returns true)
438 // only if the index is valid and the Value at that index can be returned
439 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23440 // |out_value| is optional and will only be set if non-NULL.
[email protected]f82fb4952009-01-20 21:05:32441 bool GetBoolean(size_t index, bool* out_value) const;
442 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28443 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23444 // doubles.
[email protected]fb534c92011-02-01 01:02:07445 bool GetDouble(size_t index, double* out_value) const;
[email protected]f82fb4952009-01-20 21:05:32446 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33447 bool GetString(size_t index, string16* out_value) const;
jdoerrie14b25da2017-04-11 07:45:50448 bool GetBinary(size_t index, const Value** out_value) const;
449 bool GetBinary(size_t index, Value** out_value);
[email protected]5d30f92bf2012-08-03 08:43:37450 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
451 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:42452
453 using Value::GetList;
[email protected]5d30f92bf2012-08-03 08:43:37454 bool GetList(size_t index, const ListValue** out_value) const;
455 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38456
457 // Removes the Value with the specified index from this list.
458 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51459 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38460 // be deleted. This method returns true if |index| is valid; otherwise
461 // it will return false and the ListValue object will be unchanged.
jdoerrie8e945542017-02-17 13:54:49462 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38463
[email protected]6832cbe2009-11-30 19:59:11464 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31465 // deletes it. |index| is the location where |value| was found. Returns false
466 // if not found.
467 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04468
[email protected]3cbe0812012-07-03 02:51:43469 // Removes the element at |iter|. If |out_value| is NULL, the value will be
470 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45471 // Returns an iterator pointing to the location of the element that
472 // followed the erased element.
dcheng093de9b2016-04-04 21:25:51473 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43474
initial.commitd7cae122008-07-26 21:49:38475 // Appends a Value to the end of the list.
dcheng093de9b2016-04-04 21:25:51476 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38477
[email protected]095812b2012-09-14 02:14:01478 // Convenience forms of Append.
479 void AppendBoolean(bool in_value);
480 void AppendInteger(int in_value);
481 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11482 void AppendString(StringPiece in_value);
[email protected]095812b2012-09-14 02:14:01483 void AppendString(const string16& in_value);
484 void AppendStrings(const std::vector<std::string>& in_values);
485 void AppendStrings(const std::vector<string16>& in_values);
486
dcheng66c7a4c2016-09-14 05:49:58487 // Appends a Value if it's not already present. Returns true if successful,
488 // or false if the value was already
489 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
[email protected]840642202010-04-12 21:48:10490
[email protected]86c008e82009-08-28 20:26:05491 // Insert a Value at index.
492 // Returns true if successful, or false if the index was out of range.
dcheng66c7a4c2016-09-14 05:49:58493 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05494
[email protected]5fb35372011-09-19 15:23:10495 // Searches for the first instance of |value| in the list using the Equals
496 // method of the Value type.
497 // Returns a const_iterator to the found item or to end() if none exists.
498 const_iterator Find(const Value& value) const;
499
[email protected]8b8e7c92010-08-19 18:05:56500 // Swaps contents with the |other| list.
jdoerrie8e945542017-02-17 13:54:49501 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56502
[email protected]e8789192011-08-11 20:56:32503 // Iteration.
jdoerrie8e945542017-02-17 13:54:49504 iterator begin() { return list_->begin(); }
505 iterator end() { return list_->end(); }
initial.commitd7cae122008-07-26 21:49:38506
jdoerrie8e945542017-02-17 13:54:49507 const_iterator begin() const { return list_->begin(); }
508 const_iterator end() const { return list_->end(); }
initial.commitd7cae122008-07-26 21:49:38509
jdoerriee964d9a2017-04-05 06:44:17510 // DEPRECATED, use ListValue's copy constructor instead.
511 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49512 ListValue* DeepCopy() const;
estadea68b0442015-05-12 18:11:50513 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
dcheng093de9b2016-04-04 21:25:51514 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38515};
516
prashhir54a994502015-03-05 09:30:57517// This interface is implemented by classes that know how to serialize
518// Value objects.
[email protected]0bea7252011-08-05 15:34:00519class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38520 public:
[email protected]3a3d47472010-07-15 21:03:54521 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59522
initial.commitd7cae122008-07-26 21:49:38523 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57524};
525
526// This interface is implemented by classes that know how to deserialize Value
527// objects.
528class BASE_EXPORT ValueDeserializer {
529 public:
530 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38531
532 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08533 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39534 // Value. If the return value is NULL, and if error_code is non-NULL,
535 // error_code will be set with the underlying error.
536 // If |error_message| is non-null, it will be filled in with a formatted
537 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51538 virtual std::unique_ptr<Value> Deserialize(int* error_code,
539 std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38540};
541
[email protected]ea0ec052013-04-16 09:04:02542// Stream operator so Values can be used in assertion statements. In order that
543// gtest uses this operator to print readable output on test failures, we must
544// override each specific type. Otherwise, the default template implementation
545// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35546BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
547
[email protected]ea0ec052013-04-16 09:04:02548BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:02549 const DictionaryValue& value) {
550 return out << static_cast<const Value&>(value);
551}
552
553BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
554 const ListValue& value) {
555 return out << static_cast<const Value&>(value);
556}
557
jdoerriedc72ee942016-12-07 15:43:28558// Stream operator so that enum class Types can be used in log statements.
559BASE_EXPORT std::ostream& operator<<(std::ostream& out,
560 const Value::Type& type);
561
[email protected]f3a1c642011-07-12 19:15:03562} // namespace base
563
[email protected]101d5422008-09-26 20:22:42564#endif // BASE_VALUES_H_