blob: 60263b568f8f91133b81ccf5652c97256d0c4e11 [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.
Daniel Cheng9d949902017-09-26 00:52:4316//
17// NOTE: A Value parameter that is always a Value::STRING should just be passed
18// as a std::string. Similarly for Values that are always Value::DICTIONARY
19// (should be flat_map), Value::LIST (should be std::vector), et cetera.
initial.commitd7cae122008-07-26 21:49:3820
[email protected]101d5422008-09-26 20:22:4221#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:3823
[email protected]c014f2b32013-09-03 23:29:1224#include <stddef.h>
avi9b6f42932015-12-26 22:15:1425#include <stdint.h>
[email protected]c014f2b32013-09-03 23:29:1226
27#include <iosfwd>
initial.commitd7cae122008-07-26 21:49:3828#include <map>
dcheng093de9b2016-04-04 21:25:5129#include <memory>
[email protected]8e50b602009-03-03 22:59:4330#include <string>
[email protected]c014f2b32013-09-03 23:29:1231#include <utility>
initial.commitd7cae122008-07-26 21:49:3832#include <vector>
33
[email protected]0bea7252011-08-05 15:34:0034#include "base/base_export.h"
mkwstcd8067b2017-04-11 06:52:2135#include "base/containers/flat_map.h"
jdoerriecd022242017-08-23 08:38:2736#include "base/containers/span.h"
avi9b6f42932015-12-26 22:15:1437#include "base/macros.h"
[email protected]c851cfd2013-06-10 20:11:1438#include "base/strings/string16.h"
asvitkinedbd26533e2015-06-23 18:22:5239#include "base/strings/string_piece.h"
jdoerrie44efa9d2017-07-14 14:47:2040#include "base/value_iterators.h"
initial.commitd7cae122008-07-26 21:49:3841
[email protected]f3a1c642011-07-12 19:15:0342namespace base {
43
initial.commitd7cae122008-07-26 21:49:3844class DictionaryValue;
45class ListValue;
[email protected]68d9d352011-02-21 16:35:0446class Value;
initial.commitd7cae122008-07-26 21:49:3847
[email protected]b59ea312011-08-05 18:20:0548// The Value class is the base class for Values. A Value can be instantiated
jdoerrie43ab3c02017-08-24 20:44:3649// via passing the appropriate type or backing storage to the constructor.
[email protected]2f03f532013-07-17 08:43:3350//
51// See the file-level comment above for more information.
Brett Wilson4bef8ee2017-09-01 20:11:4952//
53// base::Value is currently in the process of being refactored. Design doc:
54// https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw
55//
56// Previously (which is how most code that currently exists is written), Value
57// used derived types to implement the individual data types, and base::Value
58// was just a base class to refer to them. This required everything be heap
59// allocated.
60//
61// OLD WAY:
62//
63// std::unique_ptr<base::Value> GetFoo() {
64// std::unique_ptr<DictionaryValue> dict;
65// dict->SetString("mykey", foo);
66// return dict;
67// }
68//
69// The new design makes base::Value a variant type that holds everything in
70// a union. It is now recommended to pass by value with std::move rather than
71// use heap allocated values. The DictionaryValue and ListValue subclasses
72// exist only as a compatibility shim that we're in the process of removing.
73//
74// NEW WAY:
75//
76// base::Value GetFoo() {
77// base::Value dict(base::Value::Type::DICTIONARY);
78// dict.SetKey("mykey", base::Value(foo));
79// return dict;
80// }
[email protected]0bea7252011-08-05 15:34:0081class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3882 public:
jdoerrie9970f20e2018-07-20 21:41:1883 using BlobStorage = std::vector<uint8_t>;
Lei Zhange0fc6f02017-10-27 00:27:2384 using DictStorage = flat_map<std::string, std::unique_ptr<Value>>;
jdoerriea5676c62017-04-11 18:09:1485 using ListStorage = std::vector<Value>;
David 'Digit' Turner2f287312019-04-03 14:32:0986 // See technical note below explaining why this is used.
87 using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; };
jdoerrie8e945542017-02-17 13:54:4988
Jose Dapena Paz7685422a2019-04-03 18:35:0489 enum class Type : unsigned char {
jdoerriedc72ee942016-12-07 15:43:2890 NONE = 0,
91 BOOLEAN,
92 INTEGER,
93 DOUBLE,
94 STRING,
95 BINARY,
96 DICTIONARY,
jdoerriee1b1f3a2019-03-16 04:08:0197 LIST,
98 // TODO(crbug.com/859477): Remove once root cause is found.
99 DEAD
[email protected]2f03f532013-07-17 08:43:33100 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:45101 };
102
jdoerriee03e80f2017-02-15 08:42:14103 // For situations where you want to keep ownership of your buffer, this
104 // factory method creates a new BinaryValue by copying the contents of the
105 // buffer that's passed in.
Jeremy Roman9532f252017-08-16 23:27:24106 // DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead.
jdoerriee03e80f2017-02-15 08:42:14107 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie14b25da2017-04-11 07:45:50108 static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
109 size_t size);
jdoerriee03e80f2017-02-15 08:42:14110
Lei Zhang30895d52017-10-23 19:14:46111 // Adaptors for converting from the old way to the new way and vice versa.
112 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
113 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
114
brettwf78cc272017-03-24 16:36:42115 Value(Value&& that) noexcept;
David 'Digit' Turner2f287312019-04-03 14:32:09116 Value() noexcept {} // A null value
117 // Fun fact: using '= default' above instead of '{}' does not work because
118 // the compiler complains that the default constructor was deleted since
119 // the inner union contains fields with non-default constructors.
jdoerriecc9f5732017-08-23 14:12:30120
121 // Value's copy constructor and copy assignment operator are deleted. Use this
122 // to obtain a deep copy explicitly.
123 Value Clone() const;
124
jdoerrie05eb3162017-02-01 10:36:56125 explicit Value(Type type);
126 explicit Value(bool in_bool);
127 explicit Value(int in_int);
128 explicit Value(double in_double);
129
jdoerrief38f37b2017-02-01 14:38:32130 // Value(const char*) and Value(const char16*) are required despite
jdoerrie9f90ad72017-09-11 17:23:26131 // Value(StringPiece) and Value(StringPiece16) because otherwise the
jdoerrief38f37b2017-02-01 14:38:32132 // compiler will choose the Value(bool) constructor for these arguments.
133 // Value(std::string&&) allow for efficient move construction.
jdoerrief38f37b2017-02-01 14:38:32134 explicit Value(const char* in_string);
jdoerrief38f37b2017-02-01 14:38:32135 explicit Value(StringPiece in_string);
jdoerrie9f90ad72017-09-11 17:23:26136 explicit Value(std::string&& in_string) noexcept;
137 explicit Value(const char16* in_string16);
138 explicit Value(StringPiece16 in_string16);
jdoerrief38f37b2017-02-01 14:38:32139
jdoerrie9970f20e2018-07-20 21:41:18140 explicit Value(const std::vector<char>& in_blob);
141 explicit Value(base::span<const uint8_t> in_blob);
jdoerrie5f12b6272017-04-18 10:22:41142 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerriee03e80f2017-02-15 08:42:14143
jdoerriecc9f5732017-08-23 14:12:30144 explicit Value(const DictStorage& in_dict);
mkwstcd8067b2017-04-11 06:52:21145 explicit Value(DictStorage&& in_dict) noexcept;
146
jdoerrie2b7d0fcd2017-04-19 07:15:38147 explicit Value(const ListStorage& in_list);
148 explicit Value(ListStorage&& in_list) noexcept;
149
jdoerrie17e71cc2017-03-30 06:40:29150 Value& operator=(Value&& that) noexcept;
jdoerrie05eb3162017-02-01 10:36:56151
jdoerrie8e945542017-02-17 13:54:49152 ~Value();
jdoerrie05eb3162017-02-01 10:36:56153
thestig61709242016-07-19 00:39:30154 // Returns the name for a given |type|.
155 static const char* GetTypeName(Type type);
156
initial.commitd7cae122008-07-26 21:49:38157 // Returns the type of the value stored by the current Value object.
jdoerrie05eb3162017-02-01 10:36:56158 Type type() const { return type_; }
initial.commitd7cae122008-07-26 21:49:38159
160 // Returns true if the current object represents a given type.
jdoerriecc9f5732017-08-23 14:12:30161 bool is_none() const { return type() == Type::NONE; }
jdoerrie05eb3162017-02-01 10:36:56162 bool is_bool() const { return type() == Type::BOOLEAN; }
163 bool is_int() const { return type() == Type::INTEGER; }
164 bool is_double() const { return type() == Type::DOUBLE; }
165 bool is_string() const { return type() == Type::STRING; }
166 bool is_blob() const { return type() == Type::BINARY; }
167 bool is_dict() const { return type() == Type::DICTIONARY; }
168 bool is_list() const { return type() == Type::LIST; }
169
170 // These will all fatally assert if the type doesn't match.
171 bool GetBool() const;
172 int GetInt() const;
173 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrief38f37b2017-02-01 14:38:32174 const std::string& GetString() const;
jdoerrie5f12b6272017-04-18 10:22:41175 const BlobStorage& GetBlob() const;
jdoerriee03e80f2017-02-15 08:42:14176
jdoerrie2b7d0fcd2017-04-19 07:15:38177 ListStorage& GetList();
178 const ListStorage& GetList() const;
179
jdoerrie44efa9d2017-07-14 14:47:20180 // |FindKey| looks up |key| in the underlying dictionary. If found, it returns
jdoerrie78ab7a22017-08-17 19:04:45181 // a pointer to the element. Otherwise it returns nullptr.
182 // returned. Callers are expected to perform a check against null before using
183 // the pointer.
jdoerrie44efa9d2017-07-14 14:47:20184 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45185 //
186 // Example:
187 // auto* found = FindKey("foo");
188 Value* FindKey(StringPiece key);
189 const Value* FindKey(StringPiece key) const;
jdoerrie44efa9d2017-07-14 14:47:20190
191 // |FindKeyOfType| is similar to |FindKey|, but it also requires the found
192 // value to have type |type|. If no type is found, or the found value is of a
jdoerrie78ab7a22017-08-17 19:04:45193 // different type nullptr is returned.
194 // Callers are expected to perform a check against null before using the
195 // pointer.
jdoerrie44efa9d2017-07-14 14:47:20196 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45197 //
198 // Example:
199 // auto* found = FindKey("foo", Type::DOUBLE);
200 Value* FindKeyOfType(StringPiece key, Type type);
201 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie44efa9d2017-07-14 14:47:20202
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16203 // These are convenience forms of |FindKey|. They return |base::nullopt| if
204 // the value is not found or doesn't have the type specified in the
205 // function's name.
206 base::Optional<bool> FindBoolKey(StringPiece key) const;
207 base::Optional<int> FindIntKey(StringPiece key) const;
David 'Digit' Turnerc2c467f2019-03-20 21:41:09208 // Note FindDoubleKey() will auto-convert INTEGER keys to their double
209 // value, for consistency with GetDouble().
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16210 base::Optional<double> FindDoubleKey(StringPiece key) const;
211
212 // |FindStringKey| returns |nullptr| if value is not found or not a string.
213 const std::string* FindStringKey(StringPiece key) const;
214
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06215 // Returns nullptr is value is not found or not a binary.
216 const BlobStorage* FindBlobKey(StringPiece key) const;
217
218 // Returns nullptr if value is not found or not a dictionary.
219 const Value* FindDictKey(StringPiece key) const;
220 Value* FindDictKey(StringPiece key);
221
222 // Returns nullptr if value is not found or not a list.
223 const Value* FindListKey(StringPiece key) const;
224 Value* FindListKey(StringPiece key);
225
jdoerrie44efa9d2017-07-14 14:47:20226 // |SetKey| looks up |key| in the underlying dictionary and sets the mapped
227 // value to |value|. If |key| could not be found, a new element is inserted.
jdoerrie78ab7a22017-08-17 19:04:45228 // A pointer to the modified item is returned.
jdoerrie44efa9d2017-07-14 14:47:20229 // Note: This fatally asserts if type() is not Type::DICTIONARY.
David 'Digit' Turnere169e6c2019-03-28 22:06:29230 // Note: Prefer Set<Type>Key() for simple values.
jdoerrie78ab7a22017-08-17 19:04:45231 //
232 // Example:
233 // SetKey("foo", std::move(myvalue));
Sergey Abbakumov0e1215882019-03-15 22:32:16234 Value* SetKey(StringPiece key, Value&& value);
jdoerrie78ab7a22017-08-17 19:04:45235 // This overload results in a performance improvement for std::string&&.
Sergey Abbakumov0e1215882019-03-15 22:32:16236 Value* SetKey(std::string&& key, Value&& value);
jdoerrie46349472017-08-02 02:20:32237 // This overload is necessary to avoid ambiguity for const char* arguments.
Sergey Abbakumov0e1215882019-03-15 22:32:16238 Value* SetKey(const char* key, Value&& value);
jdoerrie44efa9d2017-07-14 14:47:20239
David 'Digit' Turnere169e6c2019-03-28 22:06:29240 // |Set<Type>Key| looks up |key| in the underlying dictionary and associates
241 // a corresponding Value() constructed from the second parameter. Compared
242 // to SetKey(), this avoids un-necessary temporary Value() creation, as well
243 // ambiguities in the value type.
244 Value* SetBoolKey(StringPiece key, bool val);
245 Value* SetIntKey(StringPiece key, int val);
246 Value* SetDoubleKey(StringPiece key, double val);
247 Value* SetStringKey(StringPiece key, StringPiece val);
248 // NOTE: These two overloads are provided as performance / code generation
249 // optimizations.
250 Value* SetStringKey(StringPiece key, const char* val);
251 Value* SetStringKey(StringPiece key, std::string&& val);
252 Value* SetStringKey(StringPiece key, StringPiece16 val);
253
jdoerrie64783162017-09-04 16:33:32254 // This attemps to remove the value associated with |key|. In case of failure,
255 // e.g. the key does not exist, |false| is returned and the underlying
256 // dictionary is not changed. In case of success, |key| is deleted from the
257 // dictionary and the method returns |true|.
258 // Note: This fatally asserts if type() is not Type::DICTIONARY.
259 //
260 // Example:
261 // bool success = RemoveKey("foo");
262 bool RemoveKey(StringPiece key);
263
Brett Wilsond16cf4ee2017-08-03 00:08:27264 // Searches a hierarchy of dictionary values for a given value. If a path
265 // of dictionaries exist, returns the item at that path. If any of the path
266 // components do not exist or if any but the last path components are not
267 // dictionaries, returns nullptr.
268 //
269 // The type of the leaf Value is not checked.
270 //
271 // Implementation note: This can't return an iterator because the iterator
272 // will actually be into another Value, so it can't be compared to iterators
jdoerrie78ab7a22017-08-17 19:04:45273 // from this one (in particular, the DictItems().end() iterator).
Brett Wilsond16cf4ee2017-08-03 00:08:27274 //
David 'Digit' Turner43ce6492019-04-04 16:04:44275 // This version takes a StringPiece for the path, using dots as separators.
Brett Wilsond16cf4ee2017-08-03 00:08:27276 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44277 // auto* found = FindPath("foo.bar");
278 Value* FindPath(StringPiece path);
279 const Value* FindPath(StringPiece path) const;
280
281 // There are also deprecated versions that take the path parameter
282 // as either a std::initializer_list<StringPiece> or a
283 // span<const StringPiece>. The latter is useful to use a
284 // std::vector<std::string> as a parameter but creates huge dynamic
285 // allocations and should be avoided! Note: If there is only one component in
286 // the path, use FindKey() instead.
287 // Example:
jdoerriecd022242017-08-23 08:38:27288 // std::vector<StringPiece> components = ...
289 // auto* found = FindPath(components);
Lei Zhange0fc6f02017-10-27 00:27:23290 //
291 // Note: If there is only one component in the path, use FindKey() instead.
jdoerriecd022242017-08-23 08:38:27292 Value* FindPath(std::initializer_list<StringPiece> path);
293 Value* FindPath(span<const StringPiece> path);
294 const Value* FindPath(std::initializer_list<StringPiece> path) const;
295 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27296
Lei Zhange0fc6f02017-10-27 00:27:23297 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilsond16cf4ee2017-08-03 00:08:27298 // matches the given type. Will return nullptr otherwise.
David 'Digit' Turner43ce6492019-04-04 16:04:44299 // Note: Prefer Find<Type>Path() for simple values.
Lei Zhange0fc6f02017-10-27 00:27:23300 //
301 // Note: If there is only one component in the path, use FindKeyOfType()
David 'Digit' Turner43ce6492019-04-04 16:04:44302 // instead for slightly better performance.
303 Value* FindPathOfType(StringPiece path, Type type);
304 const Value* FindPathOfType(StringPiece path, Type type) const;
305
306 // Convenience accessors used when the expected type of a value is known.
307 // Similar to Find<Type>Key() but accepts paths instead of keys.
308 base::Optional<bool> FindBoolPath(StringPiece path) const;
309 base::Optional<int> FindIntPath(StringPiece path) const;
310 base::Optional<double> FindDoublePath(StringPiece path) const;
311 const std::string* FindStringPath(StringPiece path) const;
312 const BlobStorage* FindBlobPath(StringPiece path) const;
313 Value* FindDictPath(StringPiece path);
314 const Value* FindDictPath(StringPiece path) const;
315 Value* FindListPath(StringPiece path);
316 const Value* FindListPath(StringPiece path) const;
317
318 // The following forms are deprecated too, use the ones that take the path
319 // as a single StringPiece instead.
jdoerriecd022242017-08-23 08:38:27320 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
321 Value* FindPathOfType(span<const StringPiece> path, Type type);
322 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilsond16cf4ee2017-08-03 00:08:27323 Type type) const;
jdoerriecd022242017-08-23 08:38:27324 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27325
326 // Sets the given path, expanding and creating dictionary keys as necessary.
327 //
jdoerrie64783162017-09-04 16:33:32328 // If the current value is not a dictionary, the function returns nullptr. If
329 // path components do not exist, they will be created. If any but the last
330 // components matches a value that is not a dictionary, the function will fail
331 // (it will not overwrite the value) and return nullptr. The last path
332 // component will be unconditionally overwritten if it exists, and created if
333 // it doesn't.
Brett Wilsond16cf4ee2017-08-03 00:08:27334 //
335 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44336 // value.SetPath("foo.bar", std::move(myvalue));
Lei Zhange0fc6f02017-10-27 00:27:23337 //
338 // Note: If there is only one component in the path, use SetKey() instead.
David 'Digit' Turner43ce6492019-04-04 16:04:44339 // Note: Using Set<Type>Path() might be more convenient and efficient.
340 Value* SetPath(StringPiece path, Value&& value);
341
342 // These setters are more convenient and efficient than the corresponding
343 // SetPath(...) call.
344 Value* SetBoolPath(StringPiece path, bool value);
345 Value* SetIntPath(StringPiece path, int value);
346 Value* SetDoublePath(StringPiece path, double value);
347 Value* SetStringPath(StringPiece path, StringPiece value);
348 Value* SetStringPath(StringPiece path, const char* value);
349 Value* SetStringPath(StringPiece path, std::string&& value);
350 Value* SetStringPath(StringPiece path, StringPiece16 value);
351
352 // Deprecated: use the ones that take a StringPiece path parameter instead.
Sergey Abbakumov0e1215882019-03-15 22:32:16353 Value* SetPath(std::initializer_list<StringPiece> path, Value&& value);
354 Value* SetPath(span<const StringPiece> path, Value&& value);
Brett Wilsond16cf4ee2017-08-03 00:08:27355
jdoerrie64783162017-09-04 16:33:32356 // Tries to remove a Value at the given path.
357 //
358 // If the current value is not a dictionary or any path components does not
359 // exist, this operation fails, leaves underlying Values untouched and returns
360 // |false|. In case intermediate dictionaries become empty as a result of this
361 // path removal, they will be removed as well.
362 //
363 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44364 // bool success = value.RemovePath("foo.bar");
jdoerrie64783162017-09-04 16:33:32365 //
366 // std::vector<StringPiece> components = ...
367 // bool success = value.RemovePath(components);
Lei Zhange0fc6f02017-10-27 00:27:23368 //
369 // Note: If there is only one component in the path, use RemoveKey() instead.
David 'Digit' Turner43ce6492019-04-04 16:04:44370 bool RemovePath(StringPiece path);
371
372 // Deprecated versions
jdoerrie64783162017-09-04 16:33:32373 bool RemovePath(std::initializer_list<StringPiece> path);
374 bool RemovePath(span<const StringPiece> path);
375
jdoerrie78ab7a22017-08-17 19:04:45376 using dict_iterator_proxy = detail::dict_iterator_proxy;
377 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie44efa9d2017-07-14 14:47:20378
379 // |DictItems| returns a proxy object that exposes iterators to the underlying
380 // dictionary. These are intended for iteration over all items in the
381 // dictionary and are compatible with for-each loops and standard library
382 // algorithms.
383 // Note: This fatally asserts if type() is not Type::DICTIONARY.
384 dict_iterator_proxy DictItems();
385 const_dict_iterator_proxy DictItems() const;
386
Lei Zhange823c512018-05-07 20:27:30387 // Returns the size of the dictionary, and if the dictionary is empty.
388 // Note: This fatally asserts if type() is not Type::DICTIONARY.
389 size_t DictSize() const;
390 bool DictEmpty() const;
391
jdoerriec1091282018-08-01 17:28:12392 // Merge |dictionary| into this value. This is done recursively, i.e. any
393 // sub-dictionaries will be merged as well. In case of key collisions, the
394 // passed in dictionary takes precedence and data already present will be
395 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
396 // be freed any time after this call.
397 // Note: This fatally asserts if type() or dictionary->type() is not
398 // Type::DICTIONARY.
399 void MergeDictionary(const Value* dictionary);
400
[email protected]2f03f532013-07-17 08:43:33401 // These methods allow the convenient retrieval of the contents of the Value.
402 // If the current object can be converted into the given type, the value is
403 // returned through the |out_value| parameter and true is returned;
404 // otherwise, false is returned and |out_value| is unchanged.
jdoerried4b852612017-06-06 11:48:43405 // DEPRECATED, use GetBool() instead.
jdoerrie8e945542017-02-17 13:54:49406 bool GetAsBoolean(bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43407 // DEPRECATED, use GetInt() instead.
jdoerrie8e945542017-02-17 13:54:49408 bool GetAsInteger(int* out_value) const;
jdoerried4b852612017-06-06 11:48:43409 // DEPRECATED, use GetDouble() instead.
jdoerrie8e945542017-02-17 13:54:49410 bool GetAsDouble(double* out_value) const;
jdoerried4b852612017-06-06 11:48:43411 // DEPRECATED, use GetString() instead.
jdoerrie8e945542017-02-17 13:54:49412 bool GetAsString(std::string* out_value) const;
413 bool GetAsString(string16* out_value) const;
jdoerrie122c4da2017-03-06 11:12:04414 bool GetAsString(const Value** out_value) const;
jdoerrie8e945542017-02-17 13:54:49415 bool GetAsString(StringPiece* out_value) const;
lukaszad1485da72016-11-01 21:49:46416 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerried4b852612017-06-06 11:48:43417 // DEPRECATED, use GetList() instead.
jdoerrie8e945542017-02-17 13:54:49418 bool GetAsList(ListValue** out_value);
419 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:46420 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49421 bool GetAsDictionary(DictionaryValue** out_value);
422 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:33423 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:38424
425 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:56426 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59427 // Subclasses return their own type directly in their overrides;
428 // this works because C++ supports covariant return types.
jdoerriecc9f5732017-08-23 14:12:30429 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17430 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49431 Value* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30432 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43433 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51434 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38435
jdoerrie5c1cee112017-03-28 17:52:00436 // Comparison operators so that Values can easily be used with standard
437 // library algorithms and associative containers.
438 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
439 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
440 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
441 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
442 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
443 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
444
initial.commitd7cae122008-07-26 21:49:38445 // Compares if two Value objects have equal contents.
jdoerrie5c1cee112017-03-28 17:52:00446 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
447 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49448 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:38449
Alexander Yashkinab504e72017-11-30 11:54:45450 // Estimates dynamic memory usage.
451 // See base/trace_event/memory_usage_estimator.h for more info.
452 size_t EstimateMemoryUsage() const;
453
jdoerrie8e945542017-02-17 13:54:49454 protected:
David 'Digit' Turner2f287312019-04-03 14:32:09455 // Special case for doubles, which are aligned to 8 bytes on some
456 // 32-bit architectures. In this case, a simple declaration as a
457 // double member would make the whole union 8 byte-aligned, which
458 // would also force 4 bytes of wasted padding space before it in
459 // the Value layout.
David 'Digit' Turner806dedb82019-03-06 17:43:11460 //
David 'Digit' Turner2f287312019-04-03 14:32:09461 // To override this, store the value as an array of 32-bit integers, and
462 // perform the appropriate bit casts when reading / writing to it.
463 Type type_ = Type::NONE;
464
initial.commitd7cae122008-07-26 21:49:38465 union {
David 'Digit' Turner2f287312019-04-03 14:32:09466 bool bool_value_;
467 int int_value_;
468 DoubleStorage double_value_;
469 std::string string_value_;
470 BlobStorage binary_value_;
471 DictStorage dict_;
472 ListStorage list_;
initial.commitd7cae122008-07-26 21:49:38473 };
jdoerrie8e945542017-02-17 13:54:49474
475 private:
David 'Digit' Turner806dedb82019-03-06 17:43:11476 friend class ValuesTest_SizeOfValue_Test;
David 'Digit' Turner2f287312019-04-03 14:32:09477 double AsDoubleInternal() const;
jdoerrie8e945542017-02-17 13:54:49478 void InternalMoveConstructFrom(Value&& that);
jdoerrie8e945542017-02-17 13:54:49479 void InternalCleanup();
jdoerriecc9f5732017-08-23 14:12:30480
David 'Digit' Turnere169e6c2019-03-28 22:06:29481 // NOTE: Using a movable reference here is done for performance (it avoids
482 // creating + moving + destroying a temporary unique ptr).
483 Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr);
David 'Digit' Turner43ce6492019-04-04 16:04:44484 Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr);
David 'Digit' Turnere169e6c2019-03-28 22:06:29485
jdoerriecc9f5732017-08-23 14:12:30486 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commitd7cae122008-07-26 21:49:38487};
488
[email protected]9e4cda7332010-07-31 04:56:14489// DictionaryValue provides a key-value dictionary with (optional) "path"
490// parsing for recursive access; see the comment at the top of the file. Keys
491// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00492class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38493 public:
Johan Tibell71bba86c2017-05-17 05:21:12494 using const_iterator = DictStorage::const_iterator;
495 using iterator = DictStorage::iterator;
496
reillyg259c0a32015-09-11 00:25:54497 // Returns |value| if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51498 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54499
[email protected]3a3d47472010-07-15 21:03:54500 DictionaryValue();
jdoerriecc9f5732017-08-23 14:12:30501 explicit DictionaryValue(const DictStorage& in_dict);
502 explicit DictionaryValue(DictStorage&& in_dict) noexcept;
[email protected]5cf906f82011-11-26 01:11:44503
initial.commitd7cae122008-07-26 21:49:38504 // Returns true if the current dictionary has a value for the given key.
jdoerrie43ab3c02017-08-24 20:44:36505 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11506 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38507
[email protected]fb804132c2009-04-15 00:17:53508 // Returns the number of Values in this dictionary.
Daniel Cheng34ef31b42017-10-12 02:31:07509 size_t size() const { return dict_.size(); }
[email protected]4dad9ad82009-11-25 20:47:52510
511 // Returns whether the dictionary is empty.
Daniel Cheng34ef31b42017-10-12 02:31:07512 bool empty() const { return dict_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53513
initial.commitd7cae122008-07-26 21:49:38514 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25515 void Clear();
initial.commitd7cae122008-07-26 21:49:38516
517 // Sets the Value associated with the given path starting from this object.
518 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
519 // into the next DictionaryValue down. Obviously, "." can't be used
520 // within a key, but there are no other restrictions on keys.
521 // If the key at any step of the way doesn't exist, or exists but isn't
522 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50523 // to the path in that location. |in_value| must be non-null.
jdoerrieb94e5422017-04-28 21:52:58524 // Returns a pointer to the inserted value.
jdoerrie43ab3c02017-08-24 20:44:36525 // DEPRECATED, use Value::SetPath(path, value) instead.
jdoerrieb94e5422017-04-28 21:52:58526 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38527
528 // Convenience forms of Set(). These methods will replace any existing
529 // value at that path, even if it has a different type.
David 'Digit' Turner43ce6492019-04-04 16:04:44530 // DEPRECATED, use Value::SetBoolKey() or Value::SetBoolPath().
jdoerrieb94e5422017-04-28 21:52:58531 Value* SetBoolean(StringPiece path, bool in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44532 // DEPRECATED, use Value::SetIntPath().
jdoerrieb94e5422017-04-28 21:52:58533 Value* SetInteger(StringPiece path, int in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44534 // DEPRECATED, use Value::SetDoublePath().
jdoerrieb94e5422017-04-28 21:52:58535 Value* SetDouble(StringPiece path, double in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44536 // DEPRECATED, use Value::SetStringPath().
jdoerrieb94e5422017-04-28 21:52:58537 Value* SetString(StringPiece path, StringPiece in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44538 // DEPRECATED, use Value::SetStringPath().
jdoerrieb94e5422017-04-28 21:52:58539 Value* SetString(StringPiece path, const string16& in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44540 // DEPRECATED, use Value::SetPath() or Value::SetDictPath()
jdoerrieb94e5422017-04-28 21:52:58541 DictionaryValue* SetDictionary(StringPiece path,
542 std::unique_ptr<DictionaryValue> in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44543 // DEPRECATED, use Value::SetPath() or Value::SetListPath()
jdoerrieb94e5422017-04-28 21:52:58544 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:52545
546 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
547 // be used as paths.
jdoerrie43ab3c02017-08-24 20:44:36548 // DEPRECATED, use Value::SetKey(key, value) instead.
jdoerrieb94e5422017-04-28 21:52:58549 Value* SetWithoutPathExpansion(StringPiece key,
550 std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38551
552 // Gets the Value associated with the given path starting from this object.
553 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
554 // into the next DictionaryValue down. If the path can be resolved
555 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15556 // through the |out_value| parameter, and the function will return true.
557 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38558 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23559 // |out_value| is optional and will only be set if non-NULL.
jdoerrie43ab3c02017-08-24 20:44:36560 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52561 bool Get(StringPiece path, const Value** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36562 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52563 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38564
565 // These are convenience forms of Get(). The value will be retrieved
566 // and the return value will be true if the path is valid and the value at
567 // the end of the path can be returned in the form specified.
[email protected]78c03a42014-03-09 07:13:23568 // |out_value| is optional and will only be set if non-NULL.
David 'Digit' Turner43ce6492019-04-04 16:04:44569 // DEPRECATED, use Value::FindBoolPath(path) instead.
dcheng16d6f532016-08-25 16:07:11570 bool GetBoolean(StringPiece path, bool* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44571 // DEPRECATED, use Value::FindIntPath(path) isntead.
dcheng16d6f532016-08-25 16:07:11572 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28573 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23574 // doubles.
David 'Digit' Turner43ce6492019-04-04 16:04:44575 // DEPRECATED, use Value::FindDoublePath(path).
dcheng16d6f532016-08-25 16:07:11576 bool GetDouble(StringPiece path, double* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44577 // DEPRECATED, use Value::FindStringPath(path) instead.
dcheng16d6f532016-08-25 16:07:11578 bool GetString(StringPiece path, std::string* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44579 // DEPRECATED, use Value::FindStringPath(path) instead.
dcheng16d6f532016-08-25 16:07:11580 bool GetString(StringPiece path, string16* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44581 // DEPRECATED, use Value::FindString(path) and IsAsciiString() instead.
dcheng16d6f532016-08-25 16:07:11582 bool GetStringASCII(StringPiece path, std::string* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44583 // DEPRECATED, use Value::FindBlobPath(path) instead.
jdoerrie14b25da2017-04-11 07:45:50584 bool GetBinary(StringPiece path, const Value** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44585 // DEPRECATED, use Value::FindBlobPath(path) instead.
jdoerrie14b25da2017-04-11 07:45:50586 bool GetBinary(StringPiece path, Value** out_value);
jdoerrie43ab3c02017-08-24 20:44:36587 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkinedbd26533e2015-06-23 18:22:52588 bool GetDictionary(StringPiece path,
[email protected]a61890e2012-07-27 22:27:11589 const DictionaryValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36590 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkinedbd26533e2015-06-23 18:22:52591 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
jdoerrie43ab3c02017-08-24 20:44:36592 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11593 bool GetList(StringPiece path, const ListValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36594 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11595 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38596
[email protected]4dad9ad82009-11-25 20:47:52597 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
598 // be used as paths.
jdoerrie1e4eeb82017-08-02 23:25:52599 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11600 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52601 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11602 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16603 // DEPRECATED, use Value::FindBoolKey(key) instead.
dcheng16d6f532016-08-25 16:07:11604 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16605 // DEPRECATED, use Value::FindIntKey(key) instead.
dcheng16d6f532016-08-25 16:07:11606 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16607 // DEPRECATED, use Value::FindDoubleKey(key) instead.
dcheng16d6f532016-08-25 16:07:11608 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16609 // DEPRECATED, use Value::FindStringKey(key) instead.
dcheng16d6f532016-08-25 16:07:11610 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52611 std::string* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16612 // DEPRECATED, use Value::FindStringKey(key) and UTF8ToUTF16() instead.
dcheng16d6f532016-08-25 16:07:11613 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]698f7f42010-08-04 19:35:33614 string16* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44615 // DEPRECATED, use Value::FindDictKey(key) instead.
[email protected]a61890e2012-07-27 22:27:11616 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11617 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11618 const DictionaryValue** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44619 // DEPRECATED, use Value::FindDictKey(key) instead.
dcheng16d6f532016-08-25 16:07:11620 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11621 DictionaryValue** out_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44622 // DEPRECATED, use Value::FindListKey(key) instead.
dcheng16d6f532016-08-25 16:07:11623 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11624 const ListValue** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44625 // DEPRECATED, use Value::FindListKey(key) instead.
dcheng16d6f532016-08-25 16:07:11626 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52627
initial.commitd7cae122008-07-26 21:49:38628 // Removes the Value with the specified path from this dictionary (or one
629 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04630 // If |out_value| is non-NULL, the removed Value will be passed out via
631 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
632 // This method returns true if |path| is a valid path; otherwise it will
633 // return false and the DictionaryValue object will be unchanged.
jdoerrie64783162017-09-04 16:33:32634 // DEPRECATED, use Value::RemovePath(path) instead.
dcheng5f9cf762016-11-29 05:30:31635 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38636
[email protected]4dad9ad82009-11-25 20:47:52637 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
638 // to be used as paths.
jdoerrie64783162017-09-04 16:33:32639 // DEPRECATED, use Value::RemoveKey(key) instead.
jdoerrie8e945542017-02-17 13:54:49640 bool RemoveWithoutPathExpansion(StringPiece key,
641 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52642
[email protected]aa3283392013-11-27 01:38:24643 // Removes a path, clearing out all dictionaries on |path| that remain empty
644 // after removing the value at |path|.
jdoerrie64783162017-09-04 16:33:32645 // DEPRECATED, use Value::RemovePath(path) instead.
dcheng5f9cf762016-11-29 05:30:31646 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24647
jdoerrie64783162017-09-04 16:33:32648 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
649
[email protected]ec330b52009-12-02 00:20:32650 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
651 // the copy. This never returns NULL, even if |this| itself is empty.
dcheng093de9b2016-04-04 21:25:51652 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32653
[email protected]ec5263a2011-05-10 09:23:39654 // Swaps contents with the |other| dictionary.
jdoerrie8e945542017-02-17 13:54:49655 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39656
[email protected]32c0e002011-11-08 21:26:41657 // This class provides an iterator over both keys and values in the
658 // dictionary. It can't be used to modify the dictionary.
jdoerrie43ab3c02017-08-24 20:44:36659 // DEPRECATED, use Value::DictItems() instead.
[email protected]a34cc092012-08-10 12:45:35660 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41661 public:
[email protected]a34cc092012-08-10 12:45:35662 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31663 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22664 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41665
Daniel Cheng34ef31b42017-10-12 02:31:07666 bool IsAtEnd() const { return it_ == target_.dict_.end(); }
[email protected]32c0e002011-11-08 21:26:41667 void Advance() { ++it_; }
668
669 const std::string& key() const { return it_->first; }
670 const Value& value() const { return *it_->second; }
671
672 private:
673 const DictionaryValue& target_;
jdoerrie8e945542017-02-17 13:54:49674 DictStorage::const_iterator it_;
[email protected]32c0e002011-11-08 21:26:41675 };
676
Johan Tibell71bba86c2017-05-17 05:21:12677 // Iteration.
jdoerrie43ab3c02017-08-24 20:44:36678 // DEPRECATED, use Value::DictItems() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07679 iterator begin() { return dict_.begin(); }
680 iterator end() { return dict_.end(); }
Johan Tibell71bba86c2017-05-17 05:21:12681
jdoerrie43ab3c02017-08-24 20:44:36682 // DEPRECATED, use Value::DictItems() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07683 const_iterator begin() const { return dict_.begin(); }
684 const_iterator end() const { return dict_.end(); }
Johan Tibell71bba86c2017-05-17 05:21:12685
jdoerriecc9f5732017-08-23 14:12:30686 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17687 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49688 DictionaryValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30689 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43690 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51691 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38692};
693
694// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00695class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38696 public:
jdoerrie8e945542017-02-17 13:54:49697 using const_iterator = ListStorage::const_iterator;
698 using iterator = ListStorage::iterator;
[email protected]a502bbe72011-01-07 18:06:45699
reillyg259c0a32015-09-11 00:25:54700 // Returns |value| if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51701 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54702
[email protected]3a3d47472010-07-15 21:03:54703 ListValue();
jdoerrie52939ed2017-04-26 18:19:42704 explicit ListValue(const ListStorage& in_list);
705 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:38706
initial.commitd7cae122008-07-26 21:49:38707 // Clears the contents of this ListValue
jdoerried4b852612017-06-06 11:48:43708 // DEPRECATED, use GetList()::clear() instead.
initial.commitd7cae122008-07-26 21:49:38709 void Clear();
710
711 // Returns the number of Values in this list.
jdoerried4b852612017-06-06 11:48:43712 // DEPRECATED, use GetList()::size() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07713 size_t GetSize() const { return list_.size(); }
initial.commitd7cae122008-07-26 21:49:38714
[email protected]ec330b52009-12-02 00:20:32715 // Returns whether the list is empty.
jdoerried4b852612017-06-06 11:48:43716 // DEPRECATED, use GetList()::empty() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07717 bool empty() const { return list_.empty(); }
[email protected]ec330b52009-12-02 00:20:32718
jdoerriea5676c62017-04-11 18:09:14719 // Reserves storage for at least |n| values.
jdoerried4b852612017-06-06 11:48:43720 // DEPRECATED, use GetList()::reserve() instead.
jdoerriea5676c62017-04-11 18:09:14721 void Reserve(size_t n);
722
initial.commitd7cae122008-07-26 21:49:38723 // Sets the list item at the given index to be the Value specified by
724 // the value given. If the index beyond the current end of the list, null
725 // Values will be used to pad out the list.
726 // Returns true if successful, or false if the index was negative or
727 // the value is a null pointer.
jdoerried4b852612017-06-06 11:48:43728 // DEPRECATED, use GetList()::operator[] instead.
dcheng093de9b2016-04-04 21:25:51729 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38730
[email protected]35213ce92010-04-08 19:06:15731 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38732 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15733 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23734 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43735 // DEPRECATED, use GetList()::operator[] instead.
[email protected]5d30f92bf2012-08-03 08:43:37736 bool Get(size_t index, const Value** out_value) const;
737 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38738
[email protected]35213ce92010-04-08 19:06:15739 // Convenience forms of Get(). Modifies |out_value| (and returns true)
740 // only if the index is valid and the Value at that index can be returned
741 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23742 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43743 // DEPRECATED, use GetList()::operator[]::GetBool() instead.
[email protected]f82fb4952009-01-20 21:05:32744 bool GetBoolean(size_t index, bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43745 // DEPRECATED, use GetList()::operator[]::GetInt() instead.
[email protected]f82fb4952009-01-20 21:05:32746 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28747 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23748 // doubles.
jdoerried4b852612017-06-06 11:48:43749 // DEPRECATED, use GetList()::operator[]::GetDouble() instead.
[email protected]fb534c92011-02-01 01:02:07750 bool GetDouble(size_t index, double* out_value) const;
jdoerried4b852612017-06-06 11:48:43751 // DEPRECATED, use GetList()::operator[]::GetString() instead.
[email protected]f82fb4952009-01-20 21:05:32752 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33753 bool GetString(size_t index, string16* out_value) const;
jdoerried4b852612017-06-06 11:48:43754
[email protected]5d30f92bf2012-08-03 08:43:37755 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
756 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:42757
758 using Value::GetList;
jdoerried4b852612017-06-06 11:48:43759 // DEPRECATED, use GetList()::operator[]::GetList() instead.
[email protected]5d30f92bf2012-08-03 08:43:37760 bool GetList(size_t index, const ListValue** out_value) const;
761 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38762
763 // Removes the Value with the specified index from this list.
764 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51765 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38766 // be deleted. This method returns true if |index| is valid; otherwise
767 // it will return false and the ListValue object will be unchanged.
jdoerried4b852612017-06-06 11:48:43768 // DEPRECATED, use GetList()::erase() instead.
jdoerrie8e945542017-02-17 13:54:49769 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38770
[email protected]6832cbe2009-11-30 19:59:11771 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31772 // deletes it. |index| is the location where |value| was found. Returns false
773 // if not found.
jdoerried4b852612017-06-06 11:48:43774 // DEPRECATED, use GetList()::erase() instead.
[email protected]4fc3c5642011-08-13 17:34:31775 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04776
[email protected]3cbe0812012-07-03 02:51:43777 // Removes the element at |iter|. If |out_value| is NULL, the value will be
778 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45779 // Returns an iterator pointing to the location of the element that
780 // followed the erased element.
jdoerried4b852612017-06-06 11:48:43781 // DEPRECATED, use GetList()::erase() instead.
dcheng093de9b2016-04-04 21:25:51782 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43783
initial.commitd7cae122008-07-26 21:49:38784 // Appends a Value to the end of the list.
jdoerried4b852612017-06-06 11:48:43785 // DEPRECATED, use GetList()::push_back() instead.
dcheng093de9b2016-04-04 21:25:51786 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38787
[email protected]095812b2012-09-14 02:14:01788 // Convenience forms of Append.
jdoerried4b852612017-06-06 11:48:43789 // DEPRECATED, use GetList()::emplace_back() instead.
[email protected]095812b2012-09-14 02:14:01790 void AppendBoolean(bool in_value);
791 void AppendInteger(int in_value);
792 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11793 void AppendString(StringPiece in_value);
[email protected]095812b2012-09-14 02:14:01794 void AppendString(const string16& in_value);
jdoerried4b852612017-06-06 11:48:43795 // DEPRECATED, use GetList()::emplace_back() in a loop instead.
[email protected]095812b2012-09-14 02:14:01796 void AppendStrings(const std::vector<std::string>& in_values);
797 void AppendStrings(const std::vector<string16>& in_values);
798
dcheng66c7a4c2016-09-14 05:49:58799 // Appends a Value if it's not already present. Returns true if successful,
800 // or false if the value was already
jdoerried4b852612017-06-06 11:48:43801 // DEPRECATED, use std::find() with GetList()::push_back() instead.
dcheng66c7a4c2016-09-14 05:49:58802 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
[email protected]840642202010-04-12 21:48:10803
[email protected]86c008e82009-08-28 20:26:05804 // Insert a Value at index.
805 // Returns true if successful, or false if the index was out of range.
jdoerried4b852612017-06-06 11:48:43806 // DEPRECATED, use GetList()::insert() instead.
dcheng66c7a4c2016-09-14 05:49:58807 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05808
[email protected]5fb35372011-09-19 15:23:10809 // Searches for the first instance of |value| in the list using the Equals
810 // method of the Value type.
811 // Returns a const_iterator to the found item or to end() if none exists.
jdoerried4b852612017-06-06 11:48:43812 // DEPRECATED, use std::find() instead.
[email protected]5fb35372011-09-19 15:23:10813 const_iterator Find(const Value& value) const;
814
[email protected]8b8e7c92010-08-19 18:05:56815 // Swaps contents with the |other| list.
jdoerried4b852612017-06-06 11:48:43816 // DEPRECATED, use GetList()::swap() instead.
jdoerrie8e945542017-02-17 13:54:49817 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56818
[email protected]e8789192011-08-11 20:56:32819 // Iteration.
jdoerried4b852612017-06-06 11:48:43820 // DEPRECATED, use GetList()::begin() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07821 iterator begin() { return list_.begin(); }
jdoerried4b852612017-06-06 11:48:43822 // DEPRECATED, use GetList()::end() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07823 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38824
jdoerried4b852612017-06-06 11:48:43825 // DEPRECATED, use GetList()::begin() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07826 const_iterator begin() const { return list_.begin(); }
jdoerried4b852612017-06-06 11:48:43827 // DEPRECATED, use GetList()::end() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07828 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38829
jdoerriecc9f5732017-08-23 14:12:30830 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17831 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49832 ListValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30833 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43834 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51835 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38836};
837
prashhir54a994502015-03-05 09:30:57838// This interface is implemented by classes that know how to serialize
839// Value objects.
[email protected]0bea7252011-08-05 15:34:00840class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38841 public:
[email protected]3a3d47472010-07-15 21:03:54842 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59843
initial.commitd7cae122008-07-26 21:49:38844 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57845};
846
847// This interface is implemented by classes that know how to deserialize Value
848// objects.
849class BASE_EXPORT ValueDeserializer {
850 public:
851 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38852
853 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08854 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39855 // Value. If the return value is NULL, and if error_code is non-NULL,
856 // error_code will be set with the underlying error.
857 // If |error_message| is non-null, it will be filled in with a formatted
858 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51859 virtual std::unique_ptr<Value> Deserialize(int* error_code,
860 std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38861};
862
[email protected]ea0ec052013-04-16 09:04:02863// Stream operator so Values can be used in assertion statements. In order that
864// gtest uses this operator to print readable output on test failures, we must
865// override each specific type. Otherwise, the default template implementation
866// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35867BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
868
[email protected]ea0ec052013-04-16 09:04:02869BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:02870 const DictionaryValue& value) {
871 return out << static_cast<const Value&>(value);
872}
873
874BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
875 const ListValue& value) {
876 return out << static_cast<const Value&>(value);
877}
878
jdoerriedc72ee942016-12-07 15:43:28879// Stream operator so that enum class Types can be used in log statements.
880BASE_EXPORT std::ostream& operator<<(std::ostream& out,
881 const Value::Type& type);
882
[email protected]f3a1c642011-07-12 19:15:03883} // namespace base
884
[email protected]101d5422008-09-26 20:22:42885#endif // BASE_VALUES_H_