blob: 0728c015620b6f43403d17fc5732e8633b765e26 [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>;
jdoerrie8e945542017-02-17 13:54:4986
jdoerriedc72ee942016-12-07 15:43:2887 enum class Type {
88 NONE = 0,
89 BOOLEAN,
90 INTEGER,
91 DOUBLE,
92 STRING,
93 BINARY,
94 DICTIONARY,
95 LIST
[email protected]2f03f532013-07-17 08:43:3396 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:4597 };
98
jdoerriece296d42018-08-08 00:11:3799 // Magic IsAlive signature to debug double frees.
100 // TODO(crbug.com/859477): Remove once root cause is found.
101 static constexpr uint32_t kMagicIsAlive = 0x15272f19;
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;
jdoerrie17e71cc2017-03-30 06:40:29116 Value() noexcept; // A null value.
jdoerriecc9f5732017-08-23 14:12:30117
118 // Value's copy constructor and copy assignment operator are deleted. Use this
119 // to obtain a deep copy explicitly.
120 Value Clone() const;
121
jdoerrie05eb3162017-02-01 10:36:56122 explicit Value(Type type);
123 explicit Value(bool in_bool);
124 explicit Value(int in_int);
125 explicit Value(double in_double);
126
jdoerrief38f37b2017-02-01 14:38:32127 // Value(const char*) and Value(const char16*) are required despite
jdoerrie9f90ad72017-09-11 17:23:26128 // Value(StringPiece) and Value(StringPiece16) because otherwise the
jdoerrief38f37b2017-02-01 14:38:32129 // compiler will choose the Value(bool) constructor for these arguments.
130 // Value(std::string&&) allow for efficient move construction.
jdoerrief38f37b2017-02-01 14:38:32131 explicit Value(const char* in_string);
jdoerrief38f37b2017-02-01 14:38:32132 explicit Value(StringPiece in_string);
jdoerrie9f90ad72017-09-11 17:23:26133 explicit Value(std::string&& in_string) noexcept;
134 explicit Value(const char16* in_string16);
135 explicit Value(StringPiece16 in_string16);
jdoerrief38f37b2017-02-01 14:38:32136
jdoerrie9970f20e2018-07-20 21:41:18137 explicit Value(const std::vector<char>& in_blob);
138 explicit Value(base::span<const uint8_t> in_blob);
jdoerrie5f12b6272017-04-18 10:22:41139 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerriee03e80f2017-02-15 08:42:14140
jdoerriecc9f5732017-08-23 14:12:30141 explicit Value(const DictStorage& in_dict);
mkwstcd8067b2017-04-11 06:52:21142 explicit Value(DictStorage&& in_dict) noexcept;
143
jdoerrie2b7d0fcd2017-04-19 07:15:38144 explicit Value(const ListStorage& in_list);
145 explicit Value(ListStorage&& in_list) noexcept;
146
jdoerrie17e71cc2017-03-30 06:40:29147 Value& operator=(Value&& that) noexcept;
jdoerrie05eb3162017-02-01 10:36:56148
jdoerrie8e945542017-02-17 13:54:49149 ~Value();
jdoerrie05eb3162017-02-01 10:36:56150
thestig61709242016-07-19 00:39:30151 // Returns the name for a given |type|.
152 static const char* GetTypeName(Type type);
153
initial.commitd7cae122008-07-26 21:49:38154 // Returns the type of the value stored by the current Value object.
jdoerrie05eb3162017-02-01 10:36:56155 Type type() const { return type_; }
initial.commitd7cae122008-07-26 21:49:38156
157 // Returns true if the current object represents a given type.
jdoerriecc9f5732017-08-23 14:12:30158 bool is_none() const { return type() == Type::NONE; }
jdoerrie05eb3162017-02-01 10:36:56159 bool is_bool() const { return type() == Type::BOOLEAN; }
160 bool is_int() const { return type() == Type::INTEGER; }
161 bool is_double() const { return type() == Type::DOUBLE; }
162 bool is_string() const { return type() == Type::STRING; }
163 bool is_blob() const { return type() == Type::BINARY; }
164 bool is_dict() const { return type() == Type::DICTIONARY; }
165 bool is_list() const { return type() == Type::LIST; }
166
167 // These will all fatally assert if the type doesn't match.
168 bool GetBool() const;
169 int GetInt() const;
170 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrief38f37b2017-02-01 14:38:32171 const std::string& GetString() const;
jdoerrie5f12b6272017-04-18 10:22:41172 const BlobStorage& GetBlob() const;
jdoerriee03e80f2017-02-15 08:42:14173
jdoerrie2b7d0fcd2017-04-19 07:15:38174 ListStorage& GetList();
175 const ListStorage& GetList() const;
176
jdoerrie44efa9d2017-07-14 14:47:20177 // |FindKey| looks up |key| in the underlying dictionary. If found, it returns
jdoerrie78ab7a22017-08-17 19:04:45178 // a pointer to the element. Otherwise it returns nullptr.
179 // returned. Callers are expected to perform a check against null before using
180 // the pointer.
jdoerrie44efa9d2017-07-14 14:47:20181 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45182 //
183 // Example:
184 // auto* found = FindKey("foo");
185 Value* FindKey(StringPiece key);
186 const Value* FindKey(StringPiece key) const;
jdoerrie44efa9d2017-07-14 14:47:20187
188 // |FindKeyOfType| is similar to |FindKey|, but it also requires the found
189 // value to have type |type|. If no type is found, or the found value is of a
jdoerrie78ab7a22017-08-17 19:04:45190 // different type nullptr is returned.
191 // Callers are expected to perform a check against null before using the
192 // pointer.
jdoerrie44efa9d2017-07-14 14:47:20193 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45194 //
195 // Example:
196 // auto* found = FindKey("foo", Type::DOUBLE);
197 Value* FindKeyOfType(StringPiece key, Type type);
198 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie44efa9d2017-07-14 14:47:20199
200 // |SetKey| looks up |key| in the underlying dictionary and sets the mapped
201 // value to |value|. If |key| could not be found, a new element is inserted.
jdoerrie78ab7a22017-08-17 19:04:45202 // A pointer to the modified item is returned.
jdoerrie44efa9d2017-07-14 14:47:20203 // Note: This fatally asserts if type() is not Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45204 //
205 // Example:
206 // SetKey("foo", std::move(myvalue));
207 Value* SetKey(StringPiece key, Value value);
208 // This overload results in a performance improvement for std::string&&.
209 Value* SetKey(std::string&& key, Value value);
jdoerrie46349472017-08-02 02:20:32210 // This overload is necessary to avoid ambiguity for const char* arguments.
jdoerrie78ab7a22017-08-17 19:04:45211 Value* SetKey(const char* key, Value value);
jdoerrie44efa9d2017-07-14 14:47:20212
jdoerrie64783162017-09-04 16:33:32213 // This attemps to remove the value associated with |key|. In case of failure,
214 // e.g. the key does not exist, |false| is returned and the underlying
215 // dictionary is not changed. In case of success, |key| is deleted from the
216 // dictionary and the method returns |true|.
217 // Note: This fatally asserts if type() is not Type::DICTIONARY.
218 //
219 // Example:
220 // bool success = RemoveKey("foo");
221 bool RemoveKey(StringPiece key);
222
Brett Wilsond16cf4ee2017-08-03 00:08:27223 // Searches a hierarchy of dictionary values for a given value. If a path
224 // of dictionaries exist, returns the item at that path. If any of the path
225 // components do not exist or if any but the last path components are not
226 // dictionaries, returns nullptr.
227 //
228 // The type of the leaf Value is not checked.
229 //
230 // Implementation note: This can't return an iterator because the iterator
231 // will actually be into another Value, so it can't be compared to iterators
jdoerrie78ab7a22017-08-17 19:04:45232 // from this one (in particular, the DictItems().end() iterator).
Brett Wilsond16cf4ee2017-08-03 00:08:27233 //
234 // Example:
jdoerrie78ab7a22017-08-17 19:04:45235 // auto* found = FindPath({"foo", "bar"});
jdoerriecd022242017-08-23 08:38:27236 //
237 // std::vector<StringPiece> components = ...
238 // auto* found = FindPath(components);
Lei Zhange0fc6f02017-10-27 00:27:23239 //
240 // Note: If there is only one component in the path, use FindKey() instead.
jdoerriecd022242017-08-23 08:38:27241 Value* FindPath(std::initializer_list<StringPiece> path);
242 Value* FindPath(span<const StringPiece> path);
243 const Value* FindPath(std::initializer_list<StringPiece> path) const;
244 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27245
Lei Zhange0fc6f02017-10-27 00:27:23246 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilsond16cf4ee2017-08-03 00:08:27247 // matches the given type. Will return nullptr otherwise.
Lei Zhange0fc6f02017-10-27 00:27:23248 //
249 // Note: If there is only one component in the path, use FindKeyOfType()
250 // instead.
jdoerriecd022242017-08-23 08:38:27251 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
252 Value* FindPathOfType(span<const StringPiece> path, Type type);
253 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilsond16cf4ee2017-08-03 00:08:27254 Type type) const;
jdoerriecd022242017-08-23 08:38:27255 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27256
257 // Sets the given path, expanding and creating dictionary keys as necessary.
258 //
jdoerrie64783162017-09-04 16:33:32259 // If the current value is not a dictionary, the function returns nullptr. If
260 // path components do not exist, they will be created. If any but the last
261 // components matches a value that is not a dictionary, the function will fail
262 // (it will not overwrite the value) and return nullptr. The last path
263 // component will be unconditionally overwritten if it exists, and created if
264 // it doesn't.
Brett Wilsond16cf4ee2017-08-03 00:08:27265 //
266 // Example:
267 // value.SetPath({"foo", "bar"}, std::move(myvalue));
jdoerriecd022242017-08-23 08:38:27268 //
269 // std::vector<StringPiece> components = ...
270 // value.SetPath(components, std::move(myvalue));
Lei Zhange0fc6f02017-10-27 00:27:23271 //
272 // Note: If there is only one component in the path, use SetKey() instead.
jdoerriecd022242017-08-23 08:38:27273 Value* SetPath(std::initializer_list<StringPiece> path, Value value);
274 Value* SetPath(span<const StringPiece> path, Value value);
Brett Wilsond16cf4ee2017-08-03 00:08:27275
jdoerrie64783162017-09-04 16:33:32276 // Tries to remove a Value at the given path.
277 //
278 // If the current value is not a dictionary or any path components does not
279 // exist, this operation fails, leaves underlying Values untouched and returns
280 // |false|. In case intermediate dictionaries become empty as a result of this
281 // path removal, they will be removed as well.
282 //
283 // Example:
284 // bool success = value.RemovePath({"foo", "bar"});
285 //
286 // std::vector<StringPiece> components = ...
287 // bool success = value.RemovePath(components);
Lei Zhange0fc6f02017-10-27 00:27:23288 //
289 // Note: If there is only one component in the path, use RemoveKey() instead.
jdoerrie64783162017-09-04 16:33:32290 bool RemovePath(std::initializer_list<StringPiece> path);
291 bool RemovePath(span<const StringPiece> path);
292
jdoerrie78ab7a22017-08-17 19:04:45293 using dict_iterator_proxy = detail::dict_iterator_proxy;
294 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie44efa9d2017-07-14 14:47:20295
296 // |DictItems| returns a proxy object that exposes iterators to the underlying
297 // dictionary. These are intended for iteration over all items in the
298 // dictionary and are compatible with for-each loops and standard library
299 // algorithms.
300 // Note: This fatally asserts if type() is not Type::DICTIONARY.
301 dict_iterator_proxy DictItems();
302 const_dict_iterator_proxy DictItems() const;
303
Lei Zhange823c512018-05-07 20:27:30304 // Returns the size of the dictionary, and if the dictionary is empty.
305 // Note: This fatally asserts if type() is not Type::DICTIONARY.
306 size_t DictSize() const;
307 bool DictEmpty() const;
308
jdoerriec1091282018-08-01 17:28:12309 // Merge |dictionary| into this value. This is done recursively, i.e. any
310 // sub-dictionaries will be merged as well. In case of key collisions, the
311 // passed in dictionary takes precedence and data already present will be
312 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
313 // be freed any time after this call.
314 // Note: This fatally asserts if type() or dictionary->type() is not
315 // Type::DICTIONARY.
316 void MergeDictionary(const Value* dictionary);
317
[email protected]2f03f532013-07-17 08:43:33318 // These methods allow the convenient retrieval of the contents of the Value.
319 // If the current object can be converted into the given type, the value is
320 // returned through the |out_value| parameter and true is returned;
321 // otherwise, false is returned and |out_value| is unchanged.
jdoerried4b852612017-06-06 11:48:43322 // DEPRECATED, use GetBool() instead.
jdoerrie8e945542017-02-17 13:54:49323 bool GetAsBoolean(bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43324 // DEPRECATED, use GetInt() instead.
jdoerrie8e945542017-02-17 13:54:49325 bool GetAsInteger(int* out_value) const;
jdoerried4b852612017-06-06 11:48:43326 // DEPRECATED, use GetDouble() instead.
jdoerrie8e945542017-02-17 13:54:49327 bool GetAsDouble(double* out_value) const;
jdoerried4b852612017-06-06 11:48:43328 // DEPRECATED, use GetString() instead.
jdoerrie8e945542017-02-17 13:54:49329 bool GetAsString(std::string* out_value) const;
330 bool GetAsString(string16* out_value) const;
jdoerrie122c4da2017-03-06 11:12:04331 bool GetAsString(const Value** out_value) const;
jdoerrie8e945542017-02-17 13:54:49332 bool GetAsString(StringPiece* out_value) const;
lukaszad1485da72016-11-01 21:49:46333 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerried4b852612017-06-06 11:48:43334 // DEPRECATED, use GetList() instead.
jdoerrie8e945542017-02-17 13:54:49335 bool GetAsList(ListValue** out_value);
336 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:46337 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49338 bool GetAsDictionary(DictionaryValue** out_value);
339 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:33340 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:38341
342 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:56343 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59344 // Subclasses return their own type directly in their overrides;
345 // this works because C++ supports covariant return types.
jdoerriecc9f5732017-08-23 14:12:30346 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17347 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49348 Value* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30349 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43350 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51351 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38352
jdoerrie5c1cee112017-03-28 17:52:00353 // Comparison operators so that Values can easily be used with standard
354 // library algorithms and associative containers.
355 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
356 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
357 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
358 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
359 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
360 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
361
initial.commitd7cae122008-07-26 21:49:38362 // Compares if two Value objects have equal contents.
jdoerrie5c1cee112017-03-28 17:52:00363 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
364 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49365 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:38366
Alexander Yashkinab504e72017-11-30 11:54:45367 // Estimates dynamic memory usage.
368 // See base/trace_event/memory_usage_estimator.h for more info.
369 size_t EstimateMemoryUsage() const;
370
jdoerrie8e945542017-02-17 13:54:49371 protected:
372 // TODO(crbug.com/646113): Make these private once DictionaryValue and
373 // ListValue are properly inlined.
jdoerrie6acf28d2017-02-02 10:56:03374 Type type_;
375
initial.commitd7cae122008-07-26 21:49:38376 union {
jdoerrie05eb3162017-02-01 10:36:56377 bool bool_value_;
378 int int_value_;
[email protected]fb534c92011-02-01 01:02:07379 double double_value_;
Daniel Cheng34ef31b42017-10-12 02:31:07380 std::string string_value_;
381 BlobStorage binary_value_;
382 DictStorage dict_;
383 ListStorage list_;
initial.commitd7cae122008-07-26 21:49:38384 };
jdoerrie8e945542017-02-17 13:54:49385
386 private:
jdoerrie8e945542017-02-17 13:54:49387 void InternalMoveConstructFrom(Value&& that);
jdoerrie8e945542017-02-17 13:54:49388 void InternalCleanup();
jdoerriecc9f5732017-08-23 14:12:30389
jdoerriece296d42018-08-08 00:11:37390 // IsAlive member to debug double frees.
391 // TODO(crbug.com/859477): Remove once root cause is found.
392 uint32_t is_alive_ = kMagicIsAlive;
393
jdoerriecc9f5732017-08-23 14:12:30394 DISALLOW_COPY_AND_ASSIGN(Value);
initial.commitd7cae122008-07-26 21:49:38395};
396
[email protected]9e4cda7332010-07-31 04:56:14397// DictionaryValue provides a key-value dictionary with (optional) "path"
398// parsing for recursive access; see the comment at the top of the file. Keys
399// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00400class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38401 public:
Johan Tibell71bba86c2017-05-17 05:21:12402 using const_iterator = DictStorage::const_iterator;
403 using iterator = DictStorage::iterator;
404
reillyg259c0a32015-09-11 00:25:54405 // Returns |value| if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51406 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54407
[email protected]3a3d47472010-07-15 21:03:54408 DictionaryValue();
jdoerriecc9f5732017-08-23 14:12:30409 explicit DictionaryValue(const DictStorage& in_dict);
410 explicit DictionaryValue(DictStorage&& in_dict) noexcept;
[email protected]5cf906f82011-11-26 01:11:44411
initial.commitd7cae122008-07-26 21:49:38412 // Returns true if the current dictionary has a value for the given key.
jdoerrie43ab3c02017-08-24 20:44:36413 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11414 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38415
[email protected]fb804132c2009-04-15 00:17:53416 // Returns the number of Values in this dictionary.
Daniel Cheng34ef31b42017-10-12 02:31:07417 size_t size() const { return dict_.size(); }
[email protected]4dad9ad82009-11-25 20:47:52418
419 // Returns whether the dictionary is empty.
Daniel Cheng34ef31b42017-10-12 02:31:07420 bool empty() const { return dict_.empty(); }
[email protected]fb804132c2009-04-15 00:17:53421
initial.commitd7cae122008-07-26 21:49:38422 // Clears any current contents of this dictionary.
[email protected]af5ed4a2008-08-04 13:56:25423 void Clear();
initial.commitd7cae122008-07-26 21:49:38424
425 // Sets the Value associated with the given path starting from this object.
426 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
427 // into the next DictionaryValue down. Obviously, "." can't be used
428 // within a key, but there are no other restrictions on keys.
429 // If the key at any step of the way doesn't exist, or exists but isn't
430 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50431 // to the path in that location. |in_value| must be non-null.
jdoerrieb94e5422017-04-28 21:52:58432 // Returns a pointer to the inserted value.
jdoerrie43ab3c02017-08-24 20:44:36433 // DEPRECATED, use Value::SetPath(path, value) instead.
jdoerrieb94e5422017-04-28 21:52:58434 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38435
436 // Convenience forms of Set(). These methods will replace any existing
437 // value at that path, even if it has a different type.
jdoerrie43ab3c02017-08-24 20:44:36438 // DEPRECATED, use Value::SetPath(path, Value(bool)) instead.
jdoerrieb94e5422017-04-28 21:52:58439 Value* SetBoolean(StringPiece path, bool in_value);
jdoerrie43ab3c02017-08-24 20:44:36440 // DEPRECATED, use Value::SetPath(path, Value(int)) instead.
jdoerrieb94e5422017-04-28 21:52:58441 Value* SetInteger(StringPiece path, int in_value);
jdoerrie43ab3c02017-08-24 20:44:36442 // DEPRECATED, use Value::SetPath(path, Value(double)) instead.
jdoerrieb94e5422017-04-28 21:52:58443 Value* SetDouble(StringPiece path, double in_value);
jdoerrie43ab3c02017-08-24 20:44:36444 // DEPRECATED, use Value::SetPath(path, Value(StringPiece)) instead.
jdoerrieb94e5422017-04-28 21:52:58445 Value* SetString(StringPiece path, StringPiece in_value);
jdoerrie43ab3c02017-08-24 20:44:36446 // DEPRECATED, use Value::SetPath(path, Value(const string& 16)) instead.
jdoerrieb94e5422017-04-28 21:52:58447 Value* SetString(StringPiece path, const string16& in_value);
jdoerrie43ab3c02017-08-24 20:44:36448 // DEPRECATED, use Value::SetPath(path, Value(Type::DICTIONARY)) instead.
jdoerrieb94e5422017-04-28 21:52:58449 DictionaryValue* SetDictionary(StringPiece path,
450 std::unique_ptr<DictionaryValue> in_value);
jdoerrie43ab3c02017-08-24 20:44:36451 // DEPRECATED, use Value::SetPath(path, Value(Type::LIST)) instead.
jdoerrieb94e5422017-04-28 21:52:58452 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:52453
454 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
455 // be used as paths.
jdoerrie43ab3c02017-08-24 20:44:36456 // DEPRECATED, use Value::SetKey(key, value) instead.
jdoerrieb94e5422017-04-28 21:52:58457 Value* SetWithoutPathExpansion(StringPiece key,
458 std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38459
460 // Gets the Value associated with the given path starting from this object.
461 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
462 // into the next DictionaryValue down. If the path can be resolved
463 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15464 // through the |out_value| parameter, and the function will return true.
465 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38466 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23467 // |out_value| is optional and will only be set if non-NULL.
jdoerrie43ab3c02017-08-24 20:44:36468 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52469 bool Get(StringPiece path, const Value** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36470 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52471 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38472
473 // These are convenience forms of Get(). The value will be retrieved
474 // and the return value will be true if the path is valid and the value at
475 // the end of the path can be returned in the form specified.
[email protected]78c03a42014-03-09 07:13:23476 // |out_value| is optional and will only be set if non-NULL.
jdoerrie43ab3c02017-08-24 20:44:36477 // DEPRECATED, use Value::FindPath(path) and Value::GetBool() instead.
dcheng16d6f532016-08-25 16:07:11478 bool GetBoolean(StringPiece path, bool* out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36479 // DEPRECATED, use Value::FindPath(path) and Value::GetInt() instead.
dcheng16d6f532016-08-25 16:07:11480 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28481 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23482 // doubles.
jdoerrie43ab3c02017-08-24 20:44:36483 // DEPRECATED, use Value::FindPath(path) and Value::GetDouble() instead.
dcheng16d6f532016-08-25 16:07:11484 bool GetDouble(StringPiece path, double* out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36485 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dcheng16d6f532016-08-25 16:07:11486 bool GetString(StringPiece path, std::string* out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36487 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dcheng16d6f532016-08-25 16:07:11488 bool GetString(StringPiece path, string16* out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36489 // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
dcheng16d6f532016-08-25 16:07:11490 bool GetStringASCII(StringPiece path, std::string* out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36491 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
jdoerrie14b25da2017-04-11 07:45:50492 bool GetBinary(StringPiece path, const Value** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36493 // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead.
jdoerrie14b25da2017-04-11 07:45:50494 bool GetBinary(StringPiece path, Value** out_value);
jdoerrie43ab3c02017-08-24 20:44:36495 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkinedbd26533e2015-06-23 18:22:52496 bool GetDictionary(StringPiece path,
[email protected]a61890e2012-07-27 22:27:11497 const DictionaryValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36498 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkinedbd26533e2015-06-23 18:22:52499 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
jdoerrie43ab3c02017-08-24 20:44:36500 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11501 bool GetList(StringPiece path, const ListValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36502 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11503 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38504
[email protected]4dad9ad82009-11-25 20:47:52505 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
506 // be used as paths.
jdoerrie1e4eeb82017-08-02 23:25:52507 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11508 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52509 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11510 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
jdoerrie1e4eeb82017-08-02 23:25:52511 // DEPRECATED, use Value::FindKey(key) and Value::GetBool() instead.
dcheng16d6f532016-08-25 16:07:11512 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52513 // DEPRECATED, use Value::FindKey(key) and Value::GetInt() instead.
dcheng16d6f532016-08-25 16:07:11514 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52515 // DEPRECATED, use Value::FindKey(key) and Value::GetDouble() instead.
dcheng16d6f532016-08-25 16:07:11516 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52517 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
dcheng16d6f532016-08-25 16:07:11518 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52519 std::string* out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52520 // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead.
dcheng16d6f532016-08-25 16:07:11521 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]698f7f42010-08-04 19:35:33522 string16* out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52523 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
[email protected]a61890e2012-07-27 22:27:11524 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11525 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11526 const DictionaryValue** out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52527 // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead.
dcheng16d6f532016-08-25 16:07:11528 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11529 DictionaryValue** out_value);
jdoerrie1e4eeb82017-08-02 23:25:52530 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11531 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11532 const ListValue** out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52533 // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11534 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52535
initial.commitd7cae122008-07-26 21:49:38536 // Removes the Value with the specified path from this dictionary (or one
537 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04538 // If |out_value| is non-NULL, the removed Value will be passed out via
539 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
540 // This method returns true if |path| is a valid path; otherwise it will
541 // return false and the DictionaryValue object will be unchanged.
jdoerrie64783162017-09-04 16:33:32542 // DEPRECATED, use Value::RemovePath(path) instead.
dcheng5f9cf762016-11-29 05:30:31543 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38544
[email protected]4dad9ad82009-11-25 20:47:52545 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
546 // to be used as paths.
jdoerrie64783162017-09-04 16:33:32547 // DEPRECATED, use Value::RemoveKey(key) instead.
jdoerrie8e945542017-02-17 13:54:49548 bool RemoveWithoutPathExpansion(StringPiece key,
549 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52550
[email protected]aa3283392013-11-27 01:38:24551 // Removes a path, clearing out all dictionaries on |path| that remain empty
552 // after removing the value at |path|.
jdoerrie64783162017-09-04 16:33:32553 // DEPRECATED, use Value::RemovePath(path) instead.
dcheng5f9cf762016-11-29 05:30:31554 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24555
jdoerrie64783162017-09-04 16:33:32556 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
557
[email protected]ec330b52009-12-02 00:20:32558 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
559 // the copy. This never returns NULL, even if |this| itself is empty.
dcheng093de9b2016-04-04 21:25:51560 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32561
[email protected]ec5263a2011-05-10 09:23:39562 // Swaps contents with the |other| dictionary.
jdoerrie8e945542017-02-17 13:54:49563 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39564
[email protected]32c0e002011-11-08 21:26:41565 // This class provides an iterator over both keys and values in the
566 // dictionary. It can't be used to modify the dictionary.
jdoerrie43ab3c02017-08-24 20:44:36567 // DEPRECATED, use Value::DictItems() instead.
[email protected]a34cc092012-08-10 12:45:35568 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41569 public:
[email protected]a34cc092012-08-10 12:45:35570 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31571 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22572 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41573
Daniel Cheng34ef31b42017-10-12 02:31:07574 bool IsAtEnd() const { return it_ == target_.dict_.end(); }
[email protected]32c0e002011-11-08 21:26:41575 void Advance() { ++it_; }
576
577 const std::string& key() const { return it_->first; }
578 const Value& value() const { return *it_->second; }
579
580 private:
581 const DictionaryValue& target_;
jdoerrie8e945542017-02-17 13:54:49582 DictStorage::const_iterator it_;
[email protected]32c0e002011-11-08 21:26:41583 };
584
Johan Tibell71bba86c2017-05-17 05:21:12585 // Iteration.
jdoerrie43ab3c02017-08-24 20:44:36586 // DEPRECATED, use Value::DictItems() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07587 iterator begin() { return dict_.begin(); }
588 iterator end() { return dict_.end(); }
Johan Tibell71bba86c2017-05-17 05:21:12589
jdoerrie43ab3c02017-08-24 20:44:36590 // DEPRECATED, use Value::DictItems() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07591 const_iterator begin() const { return dict_.begin(); }
592 const_iterator end() const { return dict_.end(); }
Johan Tibell71bba86c2017-05-17 05:21:12593
jdoerriecc9f5732017-08-23 14:12:30594 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17595 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49596 DictionaryValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30597 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43598 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51599 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38600};
601
602// This type of Value represents a list of other Value values.
[email protected]0bea7252011-08-05 15:34:00603class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38604 public:
jdoerrie8e945542017-02-17 13:54:49605 using const_iterator = ListStorage::const_iterator;
606 using iterator = ListStorage::iterator;
[email protected]a502bbe72011-01-07 18:06:45607
reillyg259c0a32015-09-11 00:25:54608 // Returns |value| if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51609 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54610
[email protected]3a3d47472010-07-15 21:03:54611 ListValue();
jdoerrie52939ed2017-04-26 18:19:42612 explicit ListValue(const ListStorage& in_list);
613 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:38614
initial.commitd7cae122008-07-26 21:49:38615 // Clears the contents of this ListValue
jdoerried4b852612017-06-06 11:48:43616 // DEPRECATED, use GetList()::clear() instead.
initial.commitd7cae122008-07-26 21:49:38617 void Clear();
618
619 // Returns the number of Values in this list.
jdoerried4b852612017-06-06 11:48:43620 // DEPRECATED, use GetList()::size() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07621 size_t GetSize() const { return list_.size(); }
initial.commitd7cae122008-07-26 21:49:38622
[email protected]ec330b52009-12-02 00:20:32623 // Returns whether the list is empty.
jdoerried4b852612017-06-06 11:48:43624 // DEPRECATED, use GetList()::empty() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07625 bool empty() const { return list_.empty(); }
[email protected]ec330b52009-12-02 00:20:32626
jdoerriea5676c62017-04-11 18:09:14627 // Reserves storage for at least |n| values.
jdoerried4b852612017-06-06 11:48:43628 // DEPRECATED, use GetList()::reserve() instead.
jdoerriea5676c62017-04-11 18:09:14629 void Reserve(size_t n);
630
initial.commitd7cae122008-07-26 21:49:38631 // Sets the list item at the given index to be the Value specified by
632 // the value given. If the index beyond the current end of the list, null
633 // Values will be used to pad out the list.
634 // Returns true if successful, or false if the index was negative or
635 // the value is a null pointer.
jdoerried4b852612017-06-06 11:48:43636 // DEPRECATED, use GetList()::operator[] instead.
dcheng093de9b2016-04-04 21:25:51637 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38638
[email protected]35213ce92010-04-08 19:06:15639 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38640 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15641 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23642 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43643 // DEPRECATED, use GetList()::operator[] instead.
[email protected]5d30f92bf2012-08-03 08:43:37644 bool Get(size_t index, const Value** out_value) const;
645 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38646
[email protected]35213ce92010-04-08 19:06:15647 // Convenience forms of Get(). Modifies |out_value| (and returns true)
648 // only if the index is valid and the Value at that index can be returned
649 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23650 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43651 // DEPRECATED, use GetList()::operator[]::GetBool() instead.
[email protected]f82fb4952009-01-20 21:05:32652 bool GetBoolean(size_t index, bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43653 // DEPRECATED, use GetList()::operator[]::GetInt() instead.
[email protected]f82fb4952009-01-20 21:05:32654 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28655 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23656 // doubles.
jdoerried4b852612017-06-06 11:48:43657 // DEPRECATED, use GetList()::operator[]::GetDouble() instead.
[email protected]fb534c92011-02-01 01:02:07658 bool GetDouble(size_t index, double* out_value) const;
jdoerried4b852612017-06-06 11:48:43659 // DEPRECATED, use GetList()::operator[]::GetString() instead.
[email protected]f82fb4952009-01-20 21:05:32660 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33661 bool GetString(size_t index, string16* out_value) const;
jdoerried4b852612017-06-06 11:48:43662
[email protected]5d30f92bf2012-08-03 08:43:37663 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
664 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:42665
666 using Value::GetList;
jdoerried4b852612017-06-06 11:48:43667 // DEPRECATED, use GetList()::operator[]::GetList() instead.
[email protected]5d30f92bf2012-08-03 08:43:37668 bool GetList(size_t index, const ListValue** out_value) const;
669 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38670
671 // Removes the Value with the specified index from this list.
672 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51673 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38674 // be deleted. This method returns true if |index| is valid; otherwise
675 // it will return false and the ListValue object will be unchanged.
jdoerried4b852612017-06-06 11:48:43676 // DEPRECATED, use GetList()::erase() instead.
jdoerrie8e945542017-02-17 13:54:49677 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38678
[email protected]6832cbe2009-11-30 19:59:11679 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31680 // deletes it. |index| is the location where |value| was found. Returns false
681 // if not found.
jdoerried4b852612017-06-06 11:48:43682 // DEPRECATED, use GetList()::erase() instead.
[email protected]4fc3c5642011-08-13 17:34:31683 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04684
[email protected]3cbe0812012-07-03 02:51:43685 // Removes the element at |iter|. If |out_value| is NULL, the value will be
686 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45687 // Returns an iterator pointing to the location of the element that
688 // followed the erased element.
jdoerried4b852612017-06-06 11:48:43689 // DEPRECATED, use GetList()::erase() instead.
dcheng093de9b2016-04-04 21:25:51690 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43691
initial.commitd7cae122008-07-26 21:49:38692 // Appends a Value to the end of the list.
jdoerried4b852612017-06-06 11:48:43693 // DEPRECATED, use GetList()::push_back() instead.
dcheng093de9b2016-04-04 21:25:51694 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38695
[email protected]095812b2012-09-14 02:14:01696 // Convenience forms of Append.
jdoerried4b852612017-06-06 11:48:43697 // DEPRECATED, use GetList()::emplace_back() instead.
[email protected]095812b2012-09-14 02:14:01698 void AppendBoolean(bool in_value);
699 void AppendInteger(int in_value);
700 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11701 void AppendString(StringPiece in_value);
[email protected]095812b2012-09-14 02:14:01702 void AppendString(const string16& in_value);
jdoerried4b852612017-06-06 11:48:43703 // DEPRECATED, use GetList()::emplace_back() in a loop instead.
[email protected]095812b2012-09-14 02:14:01704 void AppendStrings(const std::vector<std::string>& in_values);
705 void AppendStrings(const std::vector<string16>& in_values);
706
dcheng66c7a4c2016-09-14 05:49:58707 // Appends a Value if it's not already present. Returns true if successful,
708 // or false if the value was already
jdoerried4b852612017-06-06 11:48:43709 // DEPRECATED, use std::find() with GetList()::push_back() instead.
dcheng66c7a4c2016-09-14 05:49:58710 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
[email protected]840642202010-04-12 21:48:10711
[email protected]86c008e82009-08-28 20:26:05712 // Insert a Value at index.
713 // Returns true if successful, or false if the index was out of range.
jdoerried4b852612017-06-06 11:48:43714 // DEPRECATED, use GetList()::insert() instead.
dcheng66c7a4c2016-09-14 05:49:58715 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05716
[email protected]5fb35372011-09-19 15:23:10717 // Searches for the first instance of |value| in the list using the Equals
718 // method of the Value type.
719 // Returns a const_iterator to the found item or to end() if none exists.
jdoerried4b852612017-06-06 11:48:43720 // DEPRECATED, use std::find() instead.
[email protected]5fb35372011-09-19 15:23:10721 const_iterator Find(const Value& value) const;
722
[email protected]8b8e7c92010-08-19 18:05:56723 // Swaps contents with the |other| list.
jdoerried4b852612017-06-06 11:48:43724 // DEPRECATED, use GetList()::swap() instead.
jdoerrie8e945542017-02-17 13:54:49725 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56726
[email protected]e8789192011-08-11 20:56:32727 // Iteration.
jdoerried4b852612017-06-06 11:48:43728 // DEPRECATED, use GetList()::begin() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07729 iterator begin() { return list_.begin(); }
jdoerried4b852612017-06-06 11:48:43730 // DEPRECATED, use GetList()::end() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07731 iterator end() { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38732
jdoerried4b852612017-06-06 11:48:43733 // DEPRECATED, use GetList()::begin() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07734 const_iterator begin() const { return list_.begin(); }
jdoerried4b852612017-06-06 11:48:43735 // DEPRECATED, use GetList()::end() instead.
Daniel Cheng34ef31b42017-10-12 02:31:07736 const_iterator end() const { return list_.end(); }
initial.commitd7cae122008-07-26 21:49:38737
jdoerriecc9f5732017-08-23 14:12:30738 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17739 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49740 ListValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30741 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43742 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51743 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38744};
745
prashhir54a994502015-03-05 09:30:57746// This interface is implemented by classes that know how to serialize
747// Value objects.
[email protected]0bea7252011-08-05 15:34:00748class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38749 public:
[email protected]3a3d47472010-07-15 21:03:54750 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59751
initial.commitd7cae122008-07-26 21:49:38752 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57753};
754
755// This interface is implemented by classes that know how to deserialize Value
756// objects.
757class BASE_EXPORT ValueDeserializer {
758 public:
759 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38760
761 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08762 // If the return value is non-NULL, the caller takes ownership of returned
[email protected]ba399672010-04-06 15:42:39763 // Value. If the return value is NULL, and if error_code is non-NULL,
764 // error_code will be set with the underlying error.
765 // If |error_message| is non-null, it will be filled in with a formatted
766 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51767 virtual std::unique_ptr<Value> Deserialize(int* error_code,
768 std::string* error_str) = 0;
initial.commitd7cae122008-07-26 21:49:38769};
770
[email protected]ea0ec052013-04-16 09:04:02771// Stream operator so Values can be used in assertion statements. In order that
772// gtest uses this operator to print readable output on test failures, we must
773// override each specific type. Otherwise, the default template implementation
774// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35775BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
776
[email protected]ea0ec052013-04-16 09:04:02777BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:02778 const DictionaryValue& value) {
779 return out << static_cast<const Value&>(value);
780}
781
782BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
783 const ListValue& value) {
784 return out << static_cast<const Value&>(value);
785}
786
jdoerriedc72ee942016-12-07 15:43:28787// Stream operator so that enum class Types can be used in log statements.
788BASE_EXPORT std::ostream& operator<<(std::ostream& out,
789 const Value::Type& type);
790
[email protected]f3a1c642011-07-12 19:15:03791} // namespace base
792
[email protected]101d5422008-09-26 20:22:42793#endif // BASE_VALUES_H_