[email protected] | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 5 | // This file specifies a recursive data storage class called Value intended for |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 6 | // storing settings and other persistable data. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | // |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 8 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | // |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | // IN PARTICULAR this means that there is no support for int64_t or unsigned |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 13 | // numbers. Writing JSON with such types would violate the spec. If you need |
| 14 | // something like this, either use a double or make a string value containing |
| 15 | // the number you want. |
Daniel Cheng | 9d94990 | 2017-09-26 00:52:43 | [diff] [blame] | 16 | // |
| 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 21 | #ifndef BASE_VALUES_H_ |
| 22 | #define BASE_VALUES_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 24 | #include <stddef.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | #include <stdint.h> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 26 | |
| 27 | #include <iosfwd> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | #include <map> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 29 | #include <memory> |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 30 | #include <string> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 31 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 34 | #include "base/base_export.h" |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 35 | #include "base/containers/checked_iterators.h" |
Jan Wilken Dörrie | 8d9034f1 | 2019-11-28 14:48:57 | [diff] [blame] | 36 | #include "base/containers/checked_range.h" |
mkwst | cd8067b | 2017-04-11 06:52:21 | [diff] [blame] | 37 | #include "base/containers/flat_map.h" |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 38 | #include "base/containers/span.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 39 | #include "base/strings/string16.h" |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 40 | #include "base/strings/string_piece.h" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 41 | #include "base/value_iterators.h" |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 42 | #include "third_party/abseil-cpp/absl/types/variant.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 44 | namespace base { |
| 45 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | class DictionaryValue; |
| 47 | class ListValue; |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 48 | class Value; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 49 | |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 50 | // The Value class is the base class for Values. A Value can be instantiated |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 51 | // via passing the appropriate type or backing storage to the constructor. |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 52 | // |
| 53 | // See the file-level comment above for more information. |
Brett Wilson | 4bef8ee | 2017-09-01 20:11:49 | [diff] [blame] | 54 | // |
| 55 | // base::Value is currently in the process of being refactored. Design doc: |
| 56 | // https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw |
| 57 | // |
| 58 | // Previously (which is how most code that currently exists is written), Value |
| 59 | // used derived types to implement the individual data types, and base::Value |
| 60 | // was just a base class to refer to them. This required everything be heap |
| 61 | // allocated. |
| 62 | // |
| 63 | // OLD WAY: |
| 64 | // |
| 65 | // std::unique_ptr<base::Value> GetFoo() { |
| 66 | // std::unique_ptr<DictionaryValue> dict; |
| 67 | // dict->SetString("mykey", foo); |
| 68 | // return dict; |
| 69 | // } |
| 70 | // |
| 71 | // The new design makes base::Value a variant type that holds everything in |
| 72 | // a union. It is now recommended to pass by value with std::move rather than |
| 73 | // use heap allocated values. The DictionaryValue and ListValue subclasses |
| 74 | // exist only as a compatibility shim that we're in the process of removing. |
| 75 | // |
| 76 | // NEW WAY: |
| 77 | // |
| 78 | // base::Value GetFoo() { |
| 79 | // base::Value dict(base::Value::Type::DICTIONARY); |
| 80 | // dict.SetKey("mykey", base::Value(foo)); |
| 81 | // return dict; |
| 82 | // } |
Jan Wilken Dörrie | cf4ce552 | 2020-10-27 14:41:04 | [diff] [blame] | 83 | // |
| 84 | // The new design tries to avoid losing type information. Thus when migrating |
| 85 | // off deprecated types, existing usages of base::ListValue should be replaced |
Jan Wilken Dörrie | 2a06d6e9 | 2020-11-09 09:32:49 | [diff] [blame] | 86 | // by std::vector<base::Value>, and existing usages of base::DictionaryValue |
| 87 | // should be replaced with base::flat_map<std::string, base::Value>. |
Jan Wilken Dörrie | cf4ce552 | 2020-10-27 14:41:04 | [diff] [blame] | 88 | // |
Jan Wilken Dörrie | 2a06d6e9 | 2020-11-09 09:32:49 | [diff] [blame] | 89 | // OLD WAY: |
| 90 | // |
| 91 | // void AlwaysTakesList(std::unique_ptr<base::ListValue> list); |
| 92 | // void AlwaysTakesDict(std::unique_ptr<base::DictionaryValue> dict); |
| 93 | // |
| 94 | // NEW WAY: |
| 95 | // |
| 96 | // void AlwaysTakesList(std::vector<base::Value> list); |
| 97 | // void AlwaysTakesDict(base::flat_map<std::string, base::Value> dict); |
| 98 | // |
| 99 | // Migrating code will require conversions on API boundaries. This can be done |
| 100 | // cheaply by making use of overloaded base::Value constructors and the |
| 101 | // Value::TakeList() and Value::TakeDict() APIs. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 102 | class BASE_EXPORT Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | public: |
jdoerrie | 9970f20e | 2018-07-20 21:41:18 | [diff] [blame] | 104 | using BlobStorage = std::vector<uint8_t>; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 105 | using ListStorage = std::vector<Value>; |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 106 | using DictStorage = flat_map<std::string, Value>; |
| 107 | |
| 108 | // Like `DictStorage`, but with std::unique_ptr in the mapped type. This is |
| 109 | // due to legacy reasons, and should be removed once no caller relies on |
| 110 | // stability of pointers anymore. |
| 111 | using LegacyDictStorage = flat_map<std::string, std::unique_ptr<Value>>; |
Jan Wilken Dörrie | 8d9034f1 | 2019-11-28 14:48:57 | [diff] [blame] | 112 | |
| 113 | using ListView = CheckedContiguousRange<ListStorage>; |
| 114 | using ConstListView = CheckedContiguousConstRange<ListStorage>; |
| 115 | |
Jose Dapena Paz | 7685422a | 2019-04-03 18:35:04 | [diff] [blame] | 116 | enum class Type : unsigned char { |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 117 | NONE = 0, |
| 118 | BOOLEAN, |
| 119 | INTEGER, |
| 120 | DOUBLE, |
| 121 | STRING, |
| 122 | BINARY, |
| 123 | DICTIONARY, |
jdoerrie | e1b1f3a | 2019-03-16 04:08:01 | [diff] [blame] | 124 | LIST, |
| 125 | // TODO(crbug.com/859477): Remove once root cause is found. |
| 126 | DEAD |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 127 | // Note: Do not add more types. See the file-level comment above for why. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 128 | }; |
| 129 | |
Lei Zhang | 30895d5 | 2017-10-23 19:14:46 | [diff] [blame] | 130 | // Adaptors for converting from the old way to the new way and vice versa. |
| 131 | static Value FromUniquePtrValue(std::unique_ptr<Value> val); |
| 132 | static std::unique_ptr<Value> ToUniquePtrValue(Value val); |
Lei Zhang | 8c1432b | 2019-10-08 18:48:54 | [diff] [blame] | 133 | static const DictionaryValue& AsDictionaryValue(const Value& val); |
| 134 | static const ListValue& AsListValue(const Value& val); |
Lei Zhang | 30895d5 | 2017-10-23 19:14:46 | [diff] [blame] | 135 | |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 136 | Value() noexcept; |
brettw | f78cc27 | 2017-03-24 16:36:42 | [diff] [blame] | 137 | Value(Value&& that) noexcept; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 138 | |
| 139 | // Value's copy constructor and copy assignment operator are deleted. Use this |
| 140 | // to obtain a deep copy explicitly. |
| 141 | Value Clone() const; |
| 142 | |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 143 | explicit Value(Type type); |
| 144 | explicit Value(bool in_bool); |
| 145 | explicit Value(int in_int); |
| 146 | explicit Value(double in_double); |
| 147 | |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 148 | // Value(const char*) and Value(const char16*) are required despite |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 149 | // Value(StringPiece) and Value(StringPiece16) because otherwise the |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 150 | // compiler will choose the Value(bool) constructor for these arguments. |
| 151 | // Value(std::string&&) allow for efficient move construction. |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 152 | explicit Value(const char* in_string); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 153 | explicit Value(StringPiece in_string); |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 154 | explicit Value(std::string&& in_string) noexcept; |
| 155 | explicit Value(const char16* in_string16); |
| 156 | explicit Value(StringPiece16 in_string16); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 157 | |
jdoerrie | 9970f20e | 2018-07-20 21:41:18 | [diff] [blame] | 158 | explicit Value(const std::vector<char>& in_blob); |
| 159 | explicit Value(base::span<const uint8_t> in_blob); |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 160 | explicit Value(BlobStorage&& in_blob) noexcept; |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 161 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 162 | explicit Value(const DictStorage& in_dict); |
mkwst | cd8067b | 2017-04-11 06:52:21 | [diff] [blame] | 163 | explicit Value(DictStorage&& in_dict) noexcept; |
| 164 | |
Jan Wilken Dörrie | 53e009b | 2019-09-09 14:17:41 | [diff] [blame] | 165 | explicit Value(span<const Value> in_list); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 166 | explicit Value(ListStorage&& in_list) noexcept; |
| 167 | |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 168 | Value& operator=(Value&& that) noexcept; |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 169 | Value(const Value&) = delete; |
| 170 | Value& operator=(const Value&) = delete; |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 171 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 172 | ~Value(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 173 | |
thestig | 6170924 | 2016-07-19 00:39:30 | [diff] [blame] | 174 | // Returns the name for a given |type|. |
| 175 | static const char* GetTypeName(Type type); |
| 176 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | // Returns the type of the value stored by the current Value object. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 178 | Type type() const { return static_cast<Type>(data_.index()); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | |
| 180 | // Returns true if the current object represents a given type. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 181 | bool is_none() const { return type() == Type::NONE; } |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 182 | bool is_bool() const { return type() == Type::BOOLEAN; } |
| 183 | bool is_int() const { return type() == Type::INTEGER; } |
| 184 | bool is_double() const { return type() == Type::DOUBLE; } |
| 185 | bool is_string() const { return type() == Type::STRING; } |
| 186 | bool is_blob() const { return type() == Type::BINARY; } |
| 187 | bool is_dict() const { return type() == Type::DICTIONARY; } |
| 188 | bool is_list() const { return type() == Type::LIST; } |
| 189 | |
Alexander Hendrich | e86ee51 | 2019-06-12 16:01:57 | [diff] [blame] | 190 | // These will all CHECK that the type matches. |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 191 | bool GetBool() const; |
| 192 | int GetInt() const; |
| 193 | double GetDouble() const; // Implicitly converts from int if necessary. |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 194 | const std::string& GetString() const; |
Dominic Battre | 08cbe97 | 2019-07-31 03:57:19 | [diff] [blame] | 195 | std::string& GetString(); |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 196 | const BlobStorage& GetBlob() const; |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 197 | |
Jan Wilken Dörrie | 69a65a3a | 2020-01-16 15:03:30 | [diff] [blame] | 198 | // Returns the Values in a list as a view. The mutable overload allows for |
| 199 | // modification of the underlying values, but does not allow changing the |
| 200 | // structure of the list. If this is desired, use TakeList(), perform the |
| 201 | // operations, and return the list back to the Value via move assignment. |
| 202 | ListView GetList(); |
Jan Wilken Dörrie | 8d9034f1 | 2019-11-28 14:48:57 | [diff] [blame] | 203 | ConstListView GetList() const; |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 204 | |
Lei Zhang | 20b21af8 | 2020-08-10 18:31:58 | [diff] [blame] | 205 | // Transfers ownership of the underlying list to the caller. Subsequent |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 206 | // calls to GetList() will return an empty list. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 207 | // Note: This requires that type() is Type::LIST. |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 208 | ListStorage TakeList(); |
| 209 | |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 210 | // Appends |value| to the end of the list. |
| 211 | // Note: These CHECK that type() is Type::LIST. |
| 212 | void Append(bool value); |
| 213 | void Append(int value); |
| 214 | void Append(double value); |
| 215 | void Append(const char* value); |
| 216 | void Append(StringPiece value); |
| 217 | void Append(std::string&& value); |
| 218 | void Append(const char16* value); |
| 219 | void Append(StringPiece16 value); |
| 220 | void Append(Value&& value); |
| 221 | |
Jan Wilken Dörrie | 9065545e1 | 2019-10-30 10:44:51 | [diff] [blame] | 222 | // Inserts |value| before |pos|. |
Jan Wilken Dörrie | 69a65a3a | 2020-01-16 15:03:30 | [diff] [blame] | 223 | // Note: This CHECK that type() is Type::LIST. |
Jan Wilken Dörrie | 9065545e1 | 2019-10-30 10:44:51 | [diff] [blame] | 224 | CheckedContiguousIterator<Value> Insert( |
| 225 | CheckedContiguousConstIterator<Value> pos, |
| 226 | Value&& value); |
| 227 | |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 228 | // Erases the Value pointed to by |iter|. Returns false if |iter| is out of |
| 229 | // bounds. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 230 | // Note: This requires that type() is Type::LIST. |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 231 | bool EraseListIter(CheckedContiguousConstIterator<Value> iter); |
| 232 | |
| 233 | // Erases all Values that compare equal to |val|. Returns the number of |
| 234 | // deleted Values. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 235 | // Note: This requires that type() is Type::LIST. |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 236 | size_t EraseListValue(const Value& val); |
| 237 | |
| 238 | // Erases all Values for which |pred| returns true. Returns the number of |
| 239 | // deleted Values. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 240 | // Note: This requires that type() is Type::LIST. |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 241 | template <typename Predicate> |
| 242 | size_t EraseListValueIf(Predicate pred) { |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 243 | return base::EraseIf(list(), pred); |
Jan Wilken Dörrie | 7e7a979 | 2019-10-15 14:42:05 | [diff] [blame] | 244 | } |
| 245 | |
Jan Wilken Dörrie | 02577a2 | 2019-12-04 14:27:39 | [diff] [blame] | 246 | // Erases all Values from the list. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 247 | // Note: This requires that type() is Type::LIST. |
Jan Wilken Dörrie | 02577a2 | 2019-12-04 14:27:39 | [diff] [blame] | 248 | void ClearList(); |
| 249 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 250 | // |FindKey| looks up |key| in the underlying dictionary. If found, it returns |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 251 | // a pointer to the element. Otherwise it returns nullptr. |
| 252 | // returned. Callers are expected to perform a check against null before using |
| 253 | // the pointer. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 254 | // Note: This requires that type() is Type::DICTIONARY. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 255 | // |
| 256 | // Example: |
| 257 | // auto* found = FindKey("foo"); |
| 258 | Value* FindKey(StringPiece key); |
| 259 | const Value* FindKey(StringPiece key) const; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 260 | |
| 261 | // |FindKeyOfType| is similar to |FindKey|, but it also requires the found |
| 262 | // value to have type |type|. If no type is found, or the found value is of a |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 263 | // different type nullptr is returned. |
| 264 | // Callers are expected to perform a check against null before using the |
| 265 | // pointer. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 266 | // Note: This requires that type() is Type::DICTIONARY. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 267 | // |
| 268 | // Example: |
| 269 | // auto* found = FindKey("foo", Type::DOUBLE); |
| 270 | Value* FindKeyOfType(StringPiece key, Type type); |
| 271 | const Value* FindKeyOfType(StringPiece key, Type type) const; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 272 | |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 273 | // These are convenience forms of |FindKey|. They return |base::nullopt| if |
| 274 | // the value is not found or doesn't have the type specified in the |
| 275 | // function's name. |
| 276 | base::Optional<bool> FindBoolKey(StringPiece key) const; |
| 277 | base::Optional<int> FindIntKey(StringPiece key) const; |
David 'Digit' Turner | c2c467f | 2019-03-20 21:41:09 | [diff] [blame] | 278 | // Note FindDoubleKey() will auto-convert INTEGER keys to their double |
| 279 | // value, for consistency with GetDouble(). |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 280 | base::Optional<double> FindDoubleKey(StringPiece key) const; |
| 281 | |
| 282 | // |FindStringKey| returns |nullptr| if value is not found or not a string. |
| 283 | const std::string* FindStringKey(StringPiece key) const; |
Dominic Battre | 08cbe97 | 2019-07-31 03:57:19 | [diff] [blame] | 284 | std::string* FindStringKey(StringPiece key); |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 285 | |
David 'Digit' Turner | fca8c4b5 | 2019-03-26 11:14:06 | [diff] [blame] | 286 | // Returns nullptr is value is not found or not a binary. |
| 287 | const BlobStorage* FindBlobKey(StringPiece key) const; |
| 288 | |
| 289 | // Returns nullptr if value is not found or not a dictionary. |
| 290 | const Value* FindDictKey(StringPiece key) const; |
| 291 | Value* FindDictKey(StringPiece key); |
| 292 | |
| 293 | // Returns nullptr if value is not found or not a list. |
| 294 | const Value* FindListKey(StringPiece key) const; |
| 295 | Value* FindListKey(StringPiece key); |
| 296 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 297 | // |SetKey| looks up |key| in the underlying dictionary and sets the mapped |
| 298 | // value to |value|. If |key| could not be found, a new element is inserted. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 299 | // A pointer to the modified item is returned. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 300 | // Note: This requires that type() is Type::DICTIONARY. |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 301 | // Note: Prefer Set<Type>Key() for simple values. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 302 | // |
| 303 | // Example: |
| 304 | // SetKey("foo", std::move(myvalue)); |
Sergey Abbakumov | 0e121588 | 2019-03-15 22:32:16 | [diff] [blame] | 305 | Value* SetKey(StringPiece key, Value&& value); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 306 | // This overload results in a performance improvement for std::string&&. |
Sergey Abbakumov | 0e121588 | 2019-03-15 22:32:16 | [diff] [blame] | 307 | Value* SetKey(std::string&& key, Value&& value); |
jdoerrie | 4634947 | 2017-08-02 02:20:32 | [diff] [blame] | 308 | // This overload is necessary to avoid ambiguity for const char* arguments. |
Sergey Abbakumov | 0e121588 | 2019-03-15 22:32:16 | [diff] [blame] | 309 | Value* SetKey(const char* key, Value&& value); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 310 | |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 311 | // |Set<Type>Key| looks up |key| in the underlying dictionary and associates |
| 312 | // a corresponding Value() constructed from the second parameter. Compared |
| 313 | // to SetKey(), this avoids un-necessary temporary Value() creation, as well |
| 314 | // ambiguities in the value type. |
| 315 | Value* SetBoolKey(StringPiece key, bool val); |
| 316 | Value* SetIntKey(StringPiece key, int val); |
| 317 | Value* SetDoubleKey(StringPiece key, double val); |
| 318 | Value* SetStringKey(StringPiece key, StringPiece val); |
Jan Wilken Dörrie | 293405a | 2020-05-12 19:45:11 | [diff] [blame] | 319 | Value* SetStringKey(StringPiece key, StringPiece16 val); |
| 320 | // NOTE: The following two overloads are provided as performance / code |
| 321 | // generation optimizations. |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 322 | Value* SetStringKey(StringPiece key, const char* val); |
| 323 | Value* SetStringKey(StringPiece key, std::string&& val); |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 324 | |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 325 | // This attempts to remove the value associated with |key|. In case of |
| 326 | // failure, e.g. the key does not exist, false is returned and the underlying |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 327 | // dictionary is not changed. In case of success, |key| is deleted from the |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 328 | // dictionary and the method returns true. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 329 | // Note: This requires that type() is Type::DICTIONARY. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 330 | // |
| 331 | // Example: |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 332 | // bool success = dict.RemoveKey("foo"); |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 333 | bool RemoveKey(StringPiece key); |
| 334 | |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 335 | // This attempts to extract the value associated with |key|. In case of |
| 336 | // failure, e.g. the key does not exist, nullopt is returned and the |
| 337 | // underlying dictionary is not changed. In case of success, |key| is deleted |
| 338 | // from the dictionary and the method returns the extracted Value. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 339 | // Note: This requires that type() is Type::DICTIONARY. |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 340 | // |
| 341 | // Example: |
| 342 | // Optional<Value> maybe_value = dict.ExtractKey("foo"); |
| 343 | Optional<Value> ExtractKey(StringPiece key); |
| 344 | |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 345 | // Searches a hierarchy of dictionary values for a given value. If a path |
| 346 | // of dictionaries exist, returns the item at that path. If any of the path |
| 347 | // components do not exist or if any but the last path components are not |
| 348 | // dictionaries, returns nullptr. |
| 349 | // |
| 350 | // The type of the leaf Value is not checked. |
| 351 | // |
| 352 | // Implementation note: This can't return an iterator because the iterator |
| 353 | // will actually be into another Value, so it can't be compared to iterators |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 354 | // from this one (in particular, the DictItems().end() iterator). |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 355 | // |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 356 | // This version takes a StringPiece for the path, using dots as separators. |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 357 | // Example: |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 358 | // auto* found = FindPath("foo.bar"); |
| 359 | Value* FindPath(StringPiece path); |
| 360 | const Value* FindPath(StringPiece path) const; |
| 361 | |
| 362 | // There are also deprecated versions that take the path parameter |
| 363 | // as either a std::initializer_list<StringPiece> or a |
| 364 | // span<const StringPiece>. The latter is useful to use a |
| 365 | // std::vector<std::string> as a parameter but creates huge dynamic |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 366 | // allocations and should be avoided! |
| 367 | // Note: If there is only one component in the path, use FindKey() instead. |
| 368 | // |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 369 | // Example: |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 370 | // std::vector<StringPiece> components = ... |
| 371 | // auto* found = FindPath(components); |
| 372 | Value* FindPath(std::initializer_list<StringPiece> path); |
| 373 | Value* FindPath(span<const StringPiece> path); |
| 374 | const Value* FindPath(std::initializer_list<StringPiece> path) const; |
| 375 | const Value* FindPath(span<const StringPiece> path) const; |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 376 | |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame] | 377 | // Like FindPath() but will only return the value if the leaf Value type |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 378 | // matches the given type. Will return nullptr otherwise. |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 379 | // Note: Prefer Find<Type>Path() for simple values. |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame] | 380 | // |
| 381 | // Note: If there is only one component in the path, use FindKeyOfType() |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 382 | // instead for slightly better performance. |
| 383 | Value* FindPathOfType(StringPiece path, Type type); |
| 384 | const Value* FindPathOfType(StringPiece path, Type type) const; |
| 385 | |
| 386 | // Convenience accessors used when the expected type of a value is known. |
| 387 | // Similar to Find<Type>Key() but accepts paths instead of keys. |
| 388 | base::Optional<bool> FindBoolPath(StringPiece path) const; |
| 389 | base::Optional<int> FindIntPath(StringPiece path) const; |
| 390 | base::Optional<double> FindDoublePath(StringPiece path) const; |
| 391 | const std::string* FindStringPath(StringPiece path) const; |
Dominic Battre | 08cbe97 | 2019-07-31 03:57:19 | [diff] [blame] | 392 | std::string* FindStringPath(StringPiece path); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 393 | const BlobStorage* FindBlobPath(StringPiece path) const; |
| 394 | Value* FindDictPath(StringPiece path); |
| 395 | const Value* FindDictPath(StringPiece path) const; |
| 396 | Value* FindListPath(StringPiece path); |
| 397 | const Value* FindListPath(StringPiece path) const; |
| 398 | |
| 399 | // The following forms are deprecated too, use the ones that take the path |
| 400 | // as a single StringPiece instead. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 401 | Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type); |
| 402 | Value* FindPathOfType(span<const StringPiece> path, Type type); |
| 403 | const Value* FindPathOfType(std::initializer_list<StringPiece> path, |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 404 | Type type) const; |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 405 | const Value* FindPathOfType(span<const StringPiece> path, Type type) const; |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 406 | |
| 407 | // Sets the given path, expanding and creating dictionary keys as necessary. |
| 408 | // |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 409 | // If the current value is not a dictionary, the function returns nullptr. If |
| 410 | // path components do not exist, they will be created. If any but the last |
| 411 | // components matches a value that is not a dictionary, the function will fail |
| 412 | // (it will not overwrite the value) and return nullptr. The last path |
| 413 | // component will be unconditionally overwritten if it exists, and created if |
| 414 | // it doesn't. |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 415 | // |
| 416 | // Example: |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 417 | // value.SetPath("foo.bar", std::move(myvalue)); |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame] | 418 | // |
| 419 | // Note: If there is only one component in the path, use SetKey() instead. |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 420 | // Note: Using Set<Type>Path() might be more convenient and efficient. |
| 421 | Value* SetPath(StringPiece path, Value&& value); |
| 422 | |
| 423 | // These setters are more convenient and efficient than the corresponding |
| 424 | // SetPath(...) call. |
| 425 | Value* SetBoolPath(StringPiece path, bool value); |
| 426 | Value* SetIntPath(StringPiece path, int value); |
| 427 | Value* SetDoublePath(StringPiece path, double value); |
| 428 | Value* SetStringPath(StringPiece path, StringPiece value); |
| 429 | Value* SetStringPath(StringPiece path, const char* value); |
| 430 | Value* SetStringPath(StringPiece path, std::string&& value); |
| 431 | Value* SetStringPath(StringPiece path, StringPiece16 value); |
| 432 | |
| 433 | // Deprecated: use the ones that take a StringPiece path parameter instead. |
Sergey Abbakumov | 0e121588 | 2019-03-15 22:32:16 | [diff] [blame] | 434 | Value* SetPath(std::initializer_list<StringPiece> path, Value&& value); |
| 435 | Value* SetPath(span<const StringPiece> path, Value&& value); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 436 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 437 | // Tries to remove a Value at the given path. |
| 438 | // |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 439 | // If the current value is not a dictionary or any path component does not |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 440 | // exist, this operation fails, leaves underlying Values untouched and returns |
| 441 | // |false|. In case intermediate dictionaries become empty as a result of this |
| 442 | // path removal, they will be removed as well. |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 443 | // Note: If there is only one component in the path, use ExtractKey() instead. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 444 | // |
| 445 | // Example: |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 446 | // bool success = value.RemovePath("foo.bar"); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 447 | bool RemovePath(StringPiece path); |
| 448 | |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 449 | // Tries to extract a Value at the given path. |
| 450 | // |
| 451 | // If the current value is not a dictionary or any path component does not |
| 452 | // exist, this operation fails, leaves underlying Values untouched and returns |
| 453 | // nullopt. In case intermediate dictionaries become empty as a result of this |
| 454 | // path removal, they will be removed as well. Returns the extracted value on |
| 455 | // success. |
| 456 | // Note: If there is only one component in the path, use ExtractKey() instead. |
| 457 | // |
| 458 | // Example: |
| 459 | // Optional<Value> maybe_value = value.ExtractPath("foo.bar"); |
| 460 | Optional<Value> ExtractPath(StringPiece path); |
| 461 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 462 | using dict_iterator_proxy = detail::dict_iterator_proxy; |
| 463 | using const_dict_iterator_proxy = detail::const_dict_iterator_proxy; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 464 | |
| 465 | // |DictItems| returns a proxy object that exposes iterators to the underlying |
| 466 | // dictionary. These are intended for iteration over all items in the |
| 467 | // dictionary and are compatible with for-each loops and standard library |
| 468 | // algorithms. |
David Van Cleve | 373381d | 2020-01-07 18:16:13 | [diff] [blame] | 469 | // |
| 470 | // Unlike with std::map, a range-for over the non-const version of DictItems() |
| 471 | // will range over items of type pair<const std::string&, Value&>, so code of |
| 472 | // the form |
| 473 | // for (auto kv : my_value.DictItems()) |
| 474 | // Mutate(kv.second); |
| 475 | // will actually alter |my_value| in place (if it isn't const). |
| 476 | // |
Alexander Hendrich | e86ee51 | 2019-06-12 16:01:57 | [diff] [blame] | 477 | // Note: These CHECK that type() is Type::DICTIONARY. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 478 | dict_iterator_proxy DictItems(); |
| 479 | const_dict_iterator_proxy DictItems() const; |
| 480 | |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 481 | // Transfers ownership of the underlying dict to the caller. Subsequent |
| 482 | // calls to DictItems() will return an empty dict. |
| 483 | // Note: This requires that type() is Type::DICTIONARY. |
| 484 | DictStorage TakeDict(); |
| 485 | |
Panos Astithas | 0532a86 | 2020-10-29 04:15:07 | [diff] [blame] | 486 | // Returns the size of the dictionary, if the dictionary is empty, and clears |
| 487 | // the dictionary. Note: These CHECK that type() is Type::DICTIONARY. |
Lei Zhang | e823c51 | 2018-05-07 20:27:30 | [diff] [blame] | 488 | size_t DictSize() const; |
| 489 | bool DictEmpty() const; |
Panos Astithas | 0532a86 | 2020-10-29 04:15:07 | [diff] [blame] | 490 | void DictClear(); |
Lei Zhang | e823c51 | 2018-05-07 20:27:30 | [diff] [blame] | 491 | |
jdoerrie | c109128 | 2018-08-01 17:28:12 | [diff] [blame] | 492 | // Merge |dictionary| into this value. This is done recursively, i.e. any |
| 493 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 494 | // passed in dictionary takes precedence and data already present will be |
| 495 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 496 | // be freed any time after this call. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 497 | // Note: This requires that type() and dictionary->type() is Type::DICTIONARY. |
jdoerrie | c109128 | 2018-08-01 17:28:12 | [diff] [blame] | 498 | void MergeDictionary(const Value* dictionary); |
| 499 | |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 500 | // These methods allow the convenient retrieval of the contents of the Value. |
| 501 | // If the current object can be converted into the given type, the value is |
| 502 | // returned through the |out_value| parameter and true is returned; |
| 503 | // otherwise, false is returned and |out_value| is unchanged. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 504 | // DEPRECATED, use GetBool() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 505 | bool GetAsBoolean(bool* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 506 | // DEPRECATED, use GetInt() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 507 | bool GetAsInteger(int* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 508 | // DEPRECATED, use GetDouble() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 509 | bool GetAsDouble(double* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 510 | // DEPRECATED, use GetString() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 511 | bool GetAsString(std::string* out_value) const; |
| 512 | bool GetAsString(string16* out_value) const; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 513 | bool GetAsString(const Value** out_value) const; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 514 | bool GetAsString(StringPiece* out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 515 | // ListValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 516 | // DEPRECATED, use GetList() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 517 | bool GetAsList(ListValue** out_value); |
| 518 | bool GetAsList(const ListValue** out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 519 | // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 520 | bool GetAsDictionary(DictionaryValue** out_value); |
| 521 | bool GetAsDictionary(const DictionaryValue** out_value) const; |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 522 | // Note: Do not add more types. See the file-level comment above for why. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 523 | |
| 524 | // This creates a deep copy of the entire Value tree, and returns a pointer |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 525 | // to the copy. The caller gets ownership of the copy, of course. |
[email protected] | 16f47e08 | 2011-01-18 02:16:59 | [diff] [blame] | 526 | // Subclasses return their own type directly in their overrides; |
| 527 | // this works because C++ supports covariant return types. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 528 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 529 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 530 | Value* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 531 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 532 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 533 | std::unique_ptr<Value> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 535 | // Comparison operators so that Values can easily be used with standard |
| 536 | // library algorithms and associative containers. |
| 537 | BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs); |
| 538 | BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs); |
| 539 | BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs); |
| 540 | BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs); |
| 541 | BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs); |
| 542 | BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs); |
| 543 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 544 | // Compares if two Value objects have equal contents. |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 545 | // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 546 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 547 | bool Equals(const Value* other) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 548 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 549 | // Estimates dynamic memory usage. Requires tracing support |
| 550 | // (enable_base_tracing gn flag), otherwise always returns 0. See |
| 551 | // base/trace_event/memory_usage_estimator.h for more info. |
Alexander Yashkin | ab504e7 | 2017-11-30 11:54:45 | [diff] [blame] | 552 | size_t EstimateMemoryUsage() const; |
| 553 | |
Alan Cutter | 2dd8303 | 2020-12-08 21:55:00 | [diff] [blame^] | 554 | // Serializes to a string for logging and debug purposes. |
| 555 | std::string DebugString() const; |
| 556 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 557 | protected: |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 558 | // Checked convenience accessors for dict and list. |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 559 | const LegacyDictStorage& dict() const { |
| 560 | return absl::get<LegacyDictStorage>(data_); |
| 561 | } |
| 562 | LegacyDictStorage& dict() { return absl::get<LegacyDictStorage>(data_); } |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 563 | const ListStorage& list() const { return absl::get<ListStorage>(data_); } |
| 564 | ListStorage& list() { return absl::get<ListStorage>(data_); } |
| 565 | |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 566 | // Internal constructors, allowing the simplify the implementation of Clone(). |
| 567 | explicit Value(const LegacyDictStorage& storage); |
| 568 | explicit Value(LegacyDictStorage&& storage) noexcept; |
| 569 | |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 570 | private: |
David 'Digit' Turner | 2f28731 | 2019-04-03 14:32:09 | [diff] [blame] | 571 | // Special case for doubles, which are aligned to 8 bytes on some |
| 572 | // 32-bit architectures. In this case, a simple declaration as a |
| 573 | // double member would make the whole union 8 byte-aligned, which |
| 574 | // would also force 4 bytes of wasted padding space before it in |
| 575 | // the Value layout. |
David 'Digit' Turner | 806dedb8 | 2019-03-06 17:43:11 | [diff] [blame] | 576 | // |
David 'Digit' Turner | 2f28731 | 2019-04-03 14:32:09 | [diff] [blame] | 577 | // To override this, store the value as an array of 32-bit integers, and |
| 578 | // perform the appropriate bit casts when reading / writing to it. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 579 | using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; }; |
David 'Digit' Turner | 2f28731 | 2019-04-03 14:32:09 | [diff] [blame] | 580 | |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 581 | // Internal constructors, allowing the simplify the implementation of Clone(). |
| 582 | explicit Value(absl::monostate); |
| 583 | explicit Value(DoubleStorage storage); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 584 | |
David 'Digit' Turner | 806dedb8 | 2019-03-06 17:43:11 | [diff] [blame] | 585 | friend class ValuesTest_SizeOfValue_Test; |
David 'Digit' Turner | 2f28731 | 2019-04-03 14:32:09 | [diff] [blame] | 586 | double AsDoubleInternal() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 587 | |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 588 | // NOTE: Using a movable reference here is done for performance (it avoids |
| 589 | // creating + moving + destroying a temporary unique ptr). |
| 590 | Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 591 | Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr); |
David 'Digit' Turner | e169e6c | 2019-03-28 22:06:29 | [diff] [blame] | 592 | |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 593 | absl::variant<absl::monostate, |
| 594 | bool, |
| 595 | int, |
| 596 | DoubleStorage, |
| 597 | std::string, |
| 598 | BlobStorage, |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 599 | LegacyDictStorage, |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 600 | ListStorage> |
| 601 | data_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 602 | }; |
| 603 | |
[email protected] | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 604 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 605 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 606 | // are |std::string|s and should be UTF-8 encoded. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 607 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 608 | public: |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 609 | using const_iterator = LegacyDictStorage::const_iterator; |
| 610 | using iterator = LegacyDictStorage::iterator; |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 611 | |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 612 | // Returns |value| if it is a dictionary, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 613 | static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 614 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 615 | DictionaryValue(); |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 616 | explicit DictionaryValue(const LegacyDictStorage& in_dict); |
| 617 | explicit DictionaryValue(LegacyDictStorage&& in_dict) noexcept; |
[email protected] | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 618 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 619 | // Returns true if the current dictionary has a value for the given key. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 620 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 621 | bool HasKey(StringPiece key) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 622 | |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 623 | // Returns the number of Values in this dictionary. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 624 | size_t size() const { return dict().size(); } |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 625 | |
| 626 | // Returns whether the dictionary is empty. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 627 | bool empty() const { return dict().empty(); } |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 628 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 629 | // Clears any current contents of this dictionary. |
Panos Astithas | 0532a86 | 2020-10-29 04:15:07 | [diff] [blame] | 630 | // DEPRECATED, use Value::DictClear() instead. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 631 | void Clear(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 632 | |
| 633 | // Sets the Value associated with the given path starting from this object. |
| 634 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 635 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 636 | // within a key, but there are no other restrictions on keys. |
| 637 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 638 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 639 | // to the path in that location. |in_value| must be non-null. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 640 | // Returns a pointer to the inserted value. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 641 | // DEPRECATED, use Value::SetPath(path, value) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 642 | Value* Set(StringPiece path, std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 643 | |
| 644 | // Convenience forms of Set(). These methods will replace any existing |
| 645 | // value at that path, even if it has a different type. |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 646 | // DEPRECATED, use Value::SetBoolKey() or Value::SetBoolPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 647 | Value* SetBoolean(StringPiece path, bool in_value); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 648 | // DEPRECATED, use Value::SetIntPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 649 | Value* SetInteger(StringPiece path, int in_value); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 650 | // DEPRECATED, use Value::SetDoublePath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 651 | Value* SetDouble(StringPiece path, double in_value); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 652 | // DEPRECATED, use Value::SetStringPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 653 | Value* SetString(StringPiece path, StringPiece in_value); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 654 | // DEPRECATED, use Value::SetStringPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 655 | Value* SetString(StringPiece path, const string16& in_value); |
Lei Zhang | e9c1bf2 | 2020-10-02 01:48:38 | [diff] [blame] | 656 | // DEPRECATED, use Value::SetPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 657 | DictionaryValue* SetDictionary(StringPiece path, |
| 658 | std::unique_ptr<DictionaryValue> in_value); |
Lei Zhang | e9c1bf2 | 2020-10-02 01:48:38 | [diff] [blame] | 659 | // DEPRECATED, use Value::SetPath(). |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 660 | ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 661 | |
| 662 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 663 | // be used as paths. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 664 | // DEPRECATED, use Value::SetKey(key, value) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 665 | Value* SetWithoutPathExpansion(StringPiece key, |
| 666 | std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 667 | |
| 668 | // Gets the Value associated with the given path starting from this object. |
| 669 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 670 | // into the next DictionaryValue down. If the path can be resolved |
| 671 | // successfully, the value for the last key in the path will be returned |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 672 | // through the |out_value| parameter, and the function will return true. |
| 673 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 674 | // Note that the dictionary always owns the value that's returned. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 675 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 676 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 677 | bool Get(StringPiece path, const Value** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 678 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 679 | bool Get(StringPiece path, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 680 | |
| 681 | // These are convenience forms of Get(). The value will be retrieved |
| 682 | // and the return value will be true if the path is valid and the value at |
| 683 | // the end of the path can be returned in the form specified. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 684 | // |out_value| is optional and will only be set if non-NULL. |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 685 | // DEPRECATED, use Value::FindBoolPath(path) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 686 | bool GetBoolean(StringPiece path, bool* out_value) const; |
Rainhard Findling | b01268b | 2020-03-12 17:45:02 | [diff] [blame] | 687 | // DEPRECATED, use Value::FindIntPath(path) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 688 | bool GetInteger(StringPiece path, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 689 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 690 | // doubles. |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 691 | // DEPRECATED, use Value::FindDoublePath(path). |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 692 | bool GetDouble(StringPiece path, double* out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 693 | // DEPRECATED, use Value::FindStringPath(path) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 694 | bool GetString(StringPiece path, std::string* out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 695 | // DEPRECATED, use Value::FindStringPath(path) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 696 | bool GetString(StringPiece path, string16* out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 697 | // DEPRECATED, use Value::FindString(path) and IsAsciiString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 698 | bool GetStringASCII(StringPiece path, std::string* out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 699 | // DEPRECATED, use Value::FindBlobPath(path) instead. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 700 | bool GetBinary(StringPiece path, const Value** out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 701 | // DEPRECATED, use Value::FindBlobPath(path) instead. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 702 | bool GetBinary(StringPiece path, Value** out_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 703 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 704 | bool GetDictionary(StringPiece path, const DictionaryValue** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 705 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 706 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 707 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 708 | bool GetList(StringPiece path, const ListValue** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 709 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 710 | bool GetList(StringPiece path, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 711 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 712 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 713 | // be used as paths. |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 714 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 715 | bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 716 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 717 | bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 718 | // DEPRECATED, use Value::FindBoolKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 719 | bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 720 | // DEPRECATED, use Value::FindIntKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 721 | bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 722 | // DEPRECATED, use Value::FindDoubleKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 723 | bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 724 | // DEPRECATED, use Value::FindStringKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 725 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 726 | std::string* out_value) const; |
Vladislav Kuzkokov | 193a2ba | 2019-01-08 11:33:16 | [diff] [blame] | 727 | // DEPRECATED, use Value::FindStringKey(key) and UTF8ToUTF16() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 728 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 729 | string16* out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 730 | // DEPRECATED, use Value::FindDictKey(key) instead. |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 731 | bool GetDictionaryWithoutPathExpansion( |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 732 | StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 733 | const DictionaryValue** out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 734 | // DEPRECATED, use Value::FindDictKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 735 | bool GetDictionaryWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 736 | DictionaryValue** out_value); |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 737 | // DEPRECATED, use Value::FindListKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 738 | bool GetListWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 739 | const ListValue** out_value) const; |
David 'Digit' Turner | 43ce649 | 2019-04-04 16:04:44 | [diff] [blame] | 740 | // DEPRECATED, use Value::FindListKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 741 | bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 742 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 743 | // Removes the Value with the specified path from this dictionary (or one |
| 744 | // of its child dictionaries, if the path is more than just a local key). |
[email protected] | d814a885 | 2013-08-06 13:33:04 | [diff] [blame] | 745 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 746 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 747 | // This method returns true if |path| is a valid path; otherwise it will |
| 748 | // return false and the DictionaryValue object will be unchanged. |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 749 | // DEPRECATED, use Value::RemovePath(path) or Value::ExtractPath(path) |
| 750 | // instead. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 751 | bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 752 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 753 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 754 | // to be used as paths. |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 755 | // DEPRECATED, use Value::RemoveKey(key) or Value::ExtractKey(key) instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 756 | bool RemoveWithoutPathExpansion(StringPiece key, |
| 757 | std::unique_ptr<Value>* out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 758 | |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 759 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 760 | // after removing the value at |path|. |
jdoerrie | c209c7d | 2019-04-05 09:51:46 | [diff] [blame] | 761 | // DEPRECATED, use Value::RemovePath(path) or Value::ExtractPath(path) |
| 762 | // instead. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 763 | bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 764 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 765 | using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise. |
| 766 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 767 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 768 | // the copy. This never returns NULL, even if |this| itself is empty. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 769 | std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 770 | |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 771 | // Swaps contents with the |other| dictionary. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 772 | void Swap(DictionaryValue* other); |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 773 | |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 774 | // This class provides an iterator over both keys and values in the |
| 775 | // dictionary. It can't be used to modify the dictionary. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 776 | // DEPRECATED, use Value::DictItems() instead. |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 777 | class BASE_EXPORT Iterator { |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 778 | public: |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 779 | explicit Iterator(const DictionaryValue& target); |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 780 | Iterator(const Iterator& other); |
[email protected] | a8d94b4 | 2013-12-10 18:52:22 | [diff] [blame] | 781 | ~Iterator(); |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 782 | |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 783 | bool IsAtEnd() const { return it_ == target_.end(); } |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 784 | void Advance() { ++it_; } |
| 785 | |
| 786 | const std::string& key() const { return it_->first; } |
| 787 | const Value& value() const { return *it_->second; } |
| 788 | |
| 789 | private: |
| 790 | const DictionaryValue& target_; |
Jan Wilken Dörrie | f961a37 | 2020-11-02 20:30:34 | [diff] [blame] | 791 | LegacyDictStorage::const_iterator it_; |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 792 | }; |
| 793 | |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 794 | // Iteration. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 795 | // DEPRECATED, use Value::DictItems() instead. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 796 | iterator begin() { return dict().begin(); } |
| 797 | iterator end() { return dict().end(); } |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 798 | |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 799 | // DEPRECATED, use Value::DictItems() instead. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 800 | const_iterator begin() const { return dict().begin(); } |
| 801 | const_iterator end() const { return dict().end(); } |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 802 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 803 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 804 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 805 | DictionaryValue* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 806 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 807 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 808 | std::unique_ptr<DictionaryValue> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | // This type of Value represents a list of other Value values. |
Jan Wilken Dörrie | cf4ce552 | 2020-10-27 14:41:04 | [diff] [blame] | 812 | // DEPRECATED: Use std::vector<base::Value> instead. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 813 | class BASE_EXPORT ListValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 814 | public: |
Jan Wilken Dörrie | 46992f02 | 2020-02-20 11:25:47 | [diff] [blame] | 815 | using const_iterator = ListView::const_iterator; |
| 816 | using iterator = ListView::iterator; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 817 | |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 818 | // Returns |value| if it is a list, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 819 | static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 820 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 821 | ListValue(); |
Jan Wilken Dörrie | 53e009b | 2019-09-09 14:17:41 | [diff] [blame] | 822 | explicit ListValue(span<const Value> in_list); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 823 | explicit ListValue(ListStorage&& in_list) noexcept; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 824 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 825 | // Clears the contents of this ListValue |
Jan Wilken Dörrie | 02577a2 | 2019-12-04 14:27:39 | [diff] [blame] | 826 | // DEPRECATED, use ClearList() instead. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 827 | void Clear(); |
| 828 | |
| 829 | // Returns the number of Values in this list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 830 | // DEPRECATED, use GetList()::size() instead. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 831 | size_t GetSize() const { return list().size(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 832 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 833 | // Returns whether the list is empty. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 834 | // DEPRECATED, use GetList()::empty() instead. |
Jan Wilken Dörrie | 79d02214 | 2020-08-19 18:18:32 | [diff] [blame] | 835 | bool empty() const { return list().empty(); } |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 836 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 837 | // Sets the list item at the given index to be the Value specified by |
| 838 | // the value given. If the index beyond the current end of the list, null |
| 839 | // Values will be used to pad out the list. |
| 840 | // Returns true if successful, or false if the index was negative or |
| 841 | // the value is a null pointer. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 842 | // DEPRECATED, use GetList()::operator[] instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 843 | bool Set(size_t index, std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 844 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 845 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 846 | // only if the index falls within the current list range. |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 847 | // Note that the list always owns the Value passed out via |out_value|. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 848 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 849 | // DEPRECATED, use GetList()::operator[] instead. |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 850 | bool Get(size_t index, const Value** out_value) const; |
| 851 | bool Get(size_t index, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 852 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 853 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 854 | // only if the index is valid and the Value at that index can be returned |
| 855 | // in the specified form. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 856 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 857 | // DEPRECATED, use GetList()::operator[]::GetBool() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 858 | bool GetBoolean(size_t index, bool* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 859 | // DEPRECATED, use GetList()::operator[]::GetInt() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 860 | bool GetInteger(size_t index, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 861 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 862 | // doubles. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 863 | // DEPRECATED, use GetList()::operator[]::GetDouble() instead. |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 864 | bool GetDouble(size_t index, double* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 865 | // DEPRECATED, use GetList()::operator[]::GetString() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 866 | bool GetString(size_t index, std::string* out_value) const; |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 867 | bool GetString(size_t index, string16* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 868 | |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 869 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 870 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 871 | |
| 872 | using Value::GetList; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 873 | // DEPRECATED, use GetList()::operator[]::GetList() instead. |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 874 | bool GetList(size_t index, const ListValue** out_value) const; |
| 875 | bool GetList(size_t index, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 876 | |
| 877 | // Removes the Value with the specified index from this list. |
| 878 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
[email protected] | b930d13 | 2009-01-05 18:37:51 | [diff] [blame] | 879 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 880 | // be deleted. This method returns true if |index| is valid; otherwise |
| 881 | // it will return false and the ListValue object will be unchanged. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 882 | // DEPRECATED, use GetList()::erase() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 883 | bool Remove(size_t index, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 884 | |
[email protected] | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 885 | // Removes the first instance of |value| found in the list, if any, and |
[email protected] | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 886 | // deletes it. |index| is the location where |value| was found. Returns false |
| 887 | // if not found. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 888 | // DEPRECATED, use GetList()::erase() instead. |
[email protected] | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 889 | bool Remove(const Value& value, size_t* index); |
[email protected] | e7f5c6f | 2009-05-09 00:33:04 | [diff] [blame] | 890 | |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 891 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
| 892 | // deleted, otherwise ownership of the value is passed back to the caller. |
[email protected] | a8d379cc | 2013-02-18 16:31:45 | [diff] [blame] | 893 | // Returns an iterator pointing to the location of the element that |
| 894 | // followed the erased element. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 895 | // DEPRECATED, use GetList()::erase() instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 896 | iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 897 | |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 898 | using Value::Append; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 899 | // Appends a Value to the end of the list. |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 900 | // DEPRECATED, use Value::Append() instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 901 | void Append(std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 902 | |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 903 | // Convenience forms of Append. |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 904 | // DEPRECATED, use Value::Append() instead. |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 905 | void AppendBoolean(bool in_value); |
| 906 | void AppendInteger(int in_value); |
| 907 | void AppendDouble(double in_value); |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 908 | void AppendString(StringPiece in_value); |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 909 | void AppendString(const string16& in_value); |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 910 | // DEPRECATED, use Value::Append() in a loop instead. |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 911 | void AppendStrings(const std::vector<std::string>& in_values); |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 912 | |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 913 | // Appends a Value if it's not already present. Returns true if successful, |
| 914 | // or false if the value was already |
Jan Wilken Dörrie | 55b0b2b | 2019-09-10 05:40:24 | [diff] [blame] | 915 | // DEPRECATED, use std::find() with Value::Append() instead. |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 916 | bool AppendIfNotPresent(std::unique_ptr<Value> in_value); |
[email protected] | 84064220 | 2010-04-12 21:48:10 | [diff] [blame] | 917 | |
Jan Wilken Dörrie | 9065545e1 | 2019-10-30 10:44:51 | [diff] [blame] | 918 | using Value::Insert; |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 919 | // Insert a Value at index. |
| 920 | // Returns true if successful, or false if the index was out of range. |
Jan Wilken Dörrie | 9065545e1 | 2019-10-30 10:44:51 | [diff] [blame] | 921 | // DEPRECATED, use Value::Insert() instead. |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 922 | bool Insert(size_t index, std::unique_ptr<Value> in_value); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 923 | |
[email protected] | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 924 | // Searches for the first instance of |value| in the list using the Equals |
| 925 | // method of the Value type. |
| 926 | // Returns a const_iterator to the found item or to end() if none exists. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 927 | // DEPRECATED, use std::find() instead. |
[email protected] | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 928 | const_iterator Find(const Value& value) const; |
| 929 | |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 930 | // Swaps contents with the |other| list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 931 | // DEPRECATED, use GetList()::swap() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 932 | void Swap(ListValue* other); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 933 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 934 | // Iteration. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 935 | // DEPRECATED, use GetList()::begin() instead. |
Jan Wilken Dörrie | 46992f02 | 2020-02-20 11:25:47 | [diff] [blame] | 936 | iterator begin() { return GetList().begin(); } |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 937 | // DEPRECATED, use GetList()::end() instead. |
Jan Wilken Dörrie | 46992f02 | 2020-02-20 11:25:47 | [diff] [blame] | 938 | iterator end() { return GetList().end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 939 | |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 940 | // DEPRECATED, use GetList()::begin() instead. |
Jan Wilken Dörrie | 46992f02 | 2020-02-20 11:25:47 | [diff] [blame] | 941 | const_iterator begin() const { return GetList().begin(); } |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 942 | // DEPRECATED, use GetList()::end() instead. |
Jan Wilken Dörrie | 46992f02 | 2020-02-20 11:25:47 | [diff] [blame] | 943 | const_iterator end() const { return GetList().end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 944 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 945 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 946 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 947 | ListValue* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 948 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 949 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 950 | std::unique_ptr<ListValue> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 951 | }; |
| 952 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 953 | // This interface is implemented by classes that know how to serialize |
| 954 | // Value objects. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 955 | class BASE_EXPORT ValueSerializer { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 956 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 957 | virtual ~ValueSerializer(); |
[email protected] | abb9d0c | 2008-08-06 15:46:59 | [diff] [blame] | 958 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 959 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 960 | }; |
| 961 | |
| 962 | // This interface is implemented by classes that know how to deserialize Value |
| 963 | // objects. |
| 964 | class BASE_EXPORT ValueDeserializer { |
| 965 | public: |
| 966 | virtual ~ValueDeserializer(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 967 | |
| 968 | // This method deserializes the subclass-specific format into a Value object. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 969 | // If the return value is non-NULL, the caller takes ownership of returned |
Nigel Tao | 410788e | 2020-06-24 07:12:27 | [diff] [blame] | 970 | // Value. |
| 971 | // |
| 972 | // If the return value is nullptr, and if |error_code| is non-nullptr, |
| 973 | // |*error_code| will be set to an integer value representing the underlying |
| 974 | // error. See "enum ErrorCode" below for more detail about the integer value. |
| 975 | // |
| 976 | // If |error_message| is non-nullptr, it will be filled in with a formatted |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 977 | // error message including the location of the error if appropriate. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 978 | virtual std::unique_ptr<Value> Deserialize(int* error_code, |
Nigel Tao | 410788e | 2020-06-24 07:12:27 | [diff] [blame] | 979 | std::string* error_message) = 0; |
| 980 | |
| 981 | // The integer-valued error codes form four groups: |
| 982 | // - The value 0 means no error. |
| 983 | // - Values between 1 and 999 inclusive mean an error in the data (i.e. |
| 984 | // content). The bytes being deserialized are not in the right format. |
| 985 | // - Values 1000 and above mean an error in the metadata (i.e. context). The |
| 986 | // file could not be read, the network is down, etc. |
| 987 | // - Negative values are reserved. |
| 988 | enum ErrorCode { |
| 989 | kErrorCodeNoError = 0, |
| 990 | // kErrorCodeInvalidFormat is a generic error code for "the data is not in |
| 991 | // the right format". Subclasses of ValueDeserializer may return other |
| 992 | // values for more specific errors. |
| 993 | kErrorCodeInvalidFormat = 1, |
| 994 | // kErrorCodeFirstMetadataError is the minimum value (inclusive) of the |
| 995 | // range of metadata errors. |
| 996 | kErrorCodeFirstMetadataError = 1000, |
| 997 | }; |
| 998 | |
| 999 | // The |error_code| argument can be one of the ErrorCode values, but it is |
| 1000 | // not restricted to only being 0, 1 or 1000. Subclasses of ValueDeserializer |
| 1001 | // can define their own error code values. |
| 1002 | static inline bool ErrorCodeIsDataError(int error_code) { |
| 1003 | return (kErrorCodeInvalidFormat <= error_code) && |
| 1004 | (error_code < kErrorCodeFirstMetadataError); |
| 1005 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1006 | }; |
| 1007 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 1008 | // Stream operator so Values can be used in assertion statements. In order that |
| 1009 | // gtest uses this operator to print readable output on test failures, we must |
| 1010 | // override each specific type. Otherwise, the default template implementation |
| 1011 | // is preferred over an upcast. |
[email protected] | e4ef831 | 2012-09-12 03:39:35 | [diff] [blame] | 1012 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 1013 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 1014 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 1015 | const DictionaryValue& value) { |
| 1016 | return out << static_cast<const Value&>(value); |
| 1017 | } |
| 1018 | |
| 1019 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 1020 | const ListValue& value) { |
| 1021 | return out << static_cast<const Value&>(value); |
| 1022 | } |
| 1023 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1024 | // Stream operator so that enum class Types can be used in log statements. |
| 1025 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 1026 | const Value::Type& type); |
| 1027 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 1028 | } // namespace base |
| 1029 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 1030 | #endif // BASE_VALUES_H_ |