[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" |
mkwst | cd8067b | 2017-04-11 06:52:21 | [diff] [blame] | 35 | #include "base/containers/flat_map.h" |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 36 | #include "base/containers/span.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 37 | #include "base/macros.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 38 | #include "base/strings/string16.h" |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 39 | #include "base/strings/string_piece.h" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 40 | #include "base/value_iterators.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 42 | namespace base { |
| 43 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | class DictionaryValue; |
| 45 | class ListValue; |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 46 | class Value; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 48 | // The Value class is the base class for Values. A Value can be instantiated |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 49 | // via passing the appropriate type or backing storage to the constructor. |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 50 | // |
| 51 | // See the file-level comment above for more information. |
Brett Wilson | 4bef8ee | 2017-09-01 20:11:49 | [diff] [blame] | 52 | // |
| 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] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 81 | class BASE_EXPORT Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | public: |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 83 | using BlobStorage = std::vector<char>; |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 84 | using DictStorage = flat_map<std::string, std::unique_ptr<Value>>; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 85 | using ListStorage = std::vector<Value>; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 86 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 87 | enum class Type { |
| 88 | NONE = 0, |
| 89 | BOOLEAN, |
| 90 | INTEGER, |
| 91 | DOUBLE, |
| 92 | STRING, |
| 93 | BINARY, |
| 94 | DICTIONARY, |
| 95 | LIST |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 96 | // 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] | 97 | }; |
| 98 | |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 99 | // For situations where you want to keep ownership of your buffer, this |
| 100 | // factory method creates a new BinaryValue by copying the contents of the |
| 101 | // buffer that's passed in. |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 102 | // DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead. |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 103 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 104 | static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, |
| 105 | size_t size); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 106 | |
Lei Zhang | 30895d5 | 2017-10-23 19:14:46 | [diff] [blame] | 107 | // Adaptors for converting from the old way to the new way and vice versa. |
| 108 | static Value FromUniquePtrValue(std::unique_ptr<Value> val); |
| 109 | static std::unique_ptr<Value> ToUniquePtrValue(Value val); |
| 110 | |
brettw | f78cc27 | 2017-03-24 16:36:42 | [diff] [blame] | 111 | Value(Value&& that) noexcept; |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 112 | Value() noexcept; // A null value. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 113 | |
| 114 | // Value's copy constructor and copy assignment operator are deleted. Use this |
| 115 | // to obtain a deep copy explicitly. |
| 116 | Value Clone() const; |
| 117 | |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 118 | explicit Value(Type type); |
| 119 | explicit Value(bool in_bool); |
| 120 | explicit Value(int in_int); |
| 121 | explicit Value(double in_double); |
| 122 | |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 123 | // Value(const char*) and Value(const char16*) are required despite |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 124 | // Value(StringPiece) and Value(StringPiece16) because otherwise the |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 125 | // compiler will choose the Value(bool) constructor for these arguments. |
| 126 | // Value(std::string&&) allow for efficient move construction. |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 127 | explicit Value(const char* in_string); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 128 | explicit Value(StringPiece in_string); |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 129 | explicit Value(std::string&& in_string) noexcept; |
| 130 | explicit Value(const char16* in_string16); |
| 131 | explicit Value(StringPiece16 in_string16); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 132 | |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 133 | explicit Value(const BlobStorage& in_blob); |
| 134 | explicit Value(BlobStorage&& in_blob) noexcept; |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 135 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 136 | explicit Value(const DictStorage& in_dict); |
mkwst | cd8067b | 2017-04-11 06:52:21 | [diff] [blame] | 137 | explicit Value(DictStorage&& in_dict) noexcept; |
| 138 | |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 139 | explicit Value(const ListStorage& in_list); |
| 140 | explicit Value(ListStorage&& in_list) noexcept; |
| 141 | |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 142 | Value& operator=(Value&& that) noexcept; |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 143 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 144 | ~Value(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 145 | |
thestig | 6170924 | 2016-07-19 00:39:30 | [diff] [blame] | 146 | // Returns the name for a given |type|. |
| 147 | static const char* GetTypeName(Type type); |
| 148 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | // Returns the type of the value stored by the current Value object. |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 150 | Type GetType() const { return type_; } // DEPRECATED, use type(). |
| 151 | Type type() const { return type_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | |
| 153 | // Returns true if the current object represents a given type. |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 154 | bool IsType(Type type) const { return type == type_; } |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 155 | bool is_none() const { return type() == Type::NONE; } |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 156 | bool is_bool() const { return type() == Type::BOOLEAN; } |
| 157 | bool is_int() const { return type() == Type::INTEGER; } |
| 158 | bool is_double() const { return type() == Type::DOUBLE; } |
| 159 | bool is_string() const { return type() == Type::STRING; } |
| 160 | bool is_blob() const { return type() == Type::BINARY; } |
| 161 | bool is_dict() const { return type() == Type::DICTIONARY; } |
| 162 | bool is_list() const { return type() == Type::LIST; } |
| 163 | |
| 164 | // These will all fatally assert if the type doesn't match. |
| 165 | bool GetBool() const; |
| 166 | int GetInt() const; |
| 167 | double GetDouble() const; // Implicitly converts from int if necessary. |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 168 | const std::string& GetString() const; |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 169 | const BlobStorage& GetBlob() const; |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 170 | |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 171 | ListStorage& GetList(); |
| 172 | const ListStorage& GetList() const; |
| 173 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 174 | // |FindKey| looks up |key| in the underlying dictionary. If found, it returns |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 175 | // a pointer to the element. Otherwise it returns nullptr. |
| 176 | // returned. Callers are expected to perform a check against null before using |
| 177 | // the pointer. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 178 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 179 | // |
| 180 | // Example: |
| 181 | // auto* found = FindKey("foo"); |
| 182 | Value* FindKey(StringPiece key); |
| 183 | const Value* FindKey(StringPiece key) const; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 184 | |
| 185 | // |FindKeyOfType| is similar to |FindKey|, but it also requires the found |
| 186 | // 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] | 187 | // different type nullptr is returned. |
| 188 | // Callers are expected to perform a check against null before using the |
| 189 | // pointer. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 190 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 191 | // |
| 192 | // Example: |
| 193 | // auto* found = FindKey("foo", Type::DOUBLE); |
| 194 | Value* FindKeyOfType(StringPiece key, Type type); |
| 195 | const Value* FindKeyOfType(StringPiece key, Type type) const; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 196 | |
| 197 | // |SetKey| looks up |key| in the underlying dictionary and sets the mapped |
| 198 | // value to |value|. If |key| could not be found, a new element is inserted. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 199 | // A pointer to the modified item is returned. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 200 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 201 | // |
| 202 | // Example: |
| 203 | // SetKey("foo", std::move(myvalue)); |
| 204 | Value* SetKey(StringPiece key, Value value); |
| 205 | // This overload results in a performance improvement for std::string&&. |
| 206 | Value* SetKey(std::string&& key, Value value); |
jdoerrie | 4634947 | 2017-08-02 02:20:32 | [diff] [blame] | 207 | // This overload is necessary to avoid ambiguity for const char* arguments. |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 208 | Value* SetKey(const char* key, Value value); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 209 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 210 | // This attemps to remove the value associated with |key|. In case of failure, |
| 211 | // e.g. the key does not exist, |false| is returned and the underlying |
| 212 | // dictionary is not changed. In case of success, |key| is deleted from the |
| 213 | // dictionary and the method returns |true|. |
| 214 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
| 215 | // |
| 216 | // Example: |
| 217 | // bool success = RemoveKey("foo"); |
| 218 | bool RemoveKey(StringPiece key); |
| 219 | |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 220 | // Searches a hierarchy of dictionary values for a given value. If a path |
| 221 | // of dictionaries exist, returns the item at that path. If any of the path |
| 222 | // components do not exist or if any but the last path components are not |
| 223 | // dictionaries, returns nullptr. |
| 224 | // |
| 225 | // The type of the leaf Value is not checked. |
| 226 | // |
| 227 | // Implementation note: This can't return an iterator because the iterator |
| 228 | // will actually be into another Value, so it can't be compared to iterators |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 229 | // from this one (in particular, the DictItems().end() iterator). |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 230 | // |
| 231 | // Example: |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 232 | // auto* found = FindPath({"foo", "bar"}); |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 233 | // |
| 234 | // std::vector<StringPiece> components = ... |
| 235 | // auto* found = FindPath(components); |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 236 | // |
| 237 | // Note: If there is only one component in the path, use FindKey() instead. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 238 | Value* FindPath(std::initializer_list<StringPiece> path); |
| 239 | Value* FindPath(span<const StringPiece> path); |
| 240 | const Value* FindPath(std::initializer_list<StringPiece> path) const; |
| 241 | const Value* FindPath(span<const StringPiece> path) const; |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 242 | |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 243 | // Like FindPath() but will only return the value if the leaf Value type |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 244 | // matches the given type. Will return nullptr otherwise. |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 245 | // |
| 246 | // Note: If there is only one component in the path, use FindKeyOfType() |
| 247 | // instead. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 248 | Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type); |
| 249 | Value* FindPathOfType(span<const StringPiece> path, Type type); |
| 250 | const Value* FindPathOfType(std::initializer_list<StringPiece> path, |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 251 | Type type) const; |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 252 | const Value* FindPathOfType(span<const StringPiece> path, Type type) const; |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 253 | |
| 254 | // Sets the given path, expanding and creating dictionary keys as necessary. |
| 255 | // |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 256 | // If the current value is not a dictionary, the function returns nullptr. If |
| 257 | // path components do not exist, they will be created. If any but the last |
| 258 | // components matches a value that is not a dictionary, the function will fail |
| 259 | // (it will not overwrite the value) and return nullptr. The last path |
| 260 | // component will be unconditionally overwritten if it exists, and created if |
| 261 | // it doesn't. |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 262 | // |
| 263 | // Example: |
| 264 | // value.SetPath({"foo", "bar"}, std::move(myvalue)); |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 265 | // |
| 266 | // std::vector<StringPiece> components = ... |
| 267 | // value.SetPath(components, std::move(myvalue)); |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 268 | // |
| 269 | // Note: If there is only one component in the path, use SetKey() instead. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 270 | Value* SetPath(std::initializer_list<StringPiece> path, Value value); |
| 271 | Value* SetPath(span<const StringPiece> path, Value value); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 272 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 273 | // Tries to remove a Value at the given path. |
| 274 | // |
| 275 | // If the current value is not a dictionary or any path components does not |
| 276 | // exist, this operation fails, leaves underlying Values untouched and returns |
| 277 | // |false|. In case intermediate dictionaries become empty as a result of this |
| 278 | // path removal, they will be removed as well. |
| 279 | // |
| 280 | // Example: |
| 281 | // bool success = value.RemovePath({"foo", "bar"}); |
| 282 | // |
| 283 | // std::vector<StringPiece> components = ... |
| 284 | // bool success = value.RemovePath(components); |
Lei Zhang | e0fc6f0 | 2017-10-27 00:27:23 | [diff] [blame^] | 285 | // |
| 286 | // Note: If there is only one component in the path, use RemoveKey() instead. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 287 | bool RemovePath(std::initializer_list<StringPiece> path); |
| 288 | bool RemovePath(span<const StringPiece> path); |
| 289 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 290 | using dict_iterator_proxy = detail::dict_iterator_proxy; |
| 291 | using const_dict_iterator_proxy = detail::const_dict_iterator_proxy; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 292 | |
| 293 | // |DictItems| returns a proxy object that exposes iterators to the underlying |
| 294 | // dictionary. These are intended for iteration over all items in the |
| 295 | // dictionary and are compatible with for-each loops and standard library |
| 296 | // algorithms. |
| 297 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
| 298 | dict_iterator_proxy DictItems(); |
| 299 | const_dict_iterator_proxy DictItems() const; |
| 300 | |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 301 | // These methods allow the convenient retrieval of the contents of the Value. |
| 302 | // If the current object can be converted into the given type, the value is |
| 303 | // returned through the |out_value| parameter and true is returned; |
| 304 | // otherwise, false is returned and |out_value| is unchanged. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 305 | // DEPRECATED, use GetBool() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 306 | bool GetAsBoolean(bool* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 307 | // DEPRECATED, use GetInt() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 308 | bool GetAsInteger(int* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 309 | // DEPRECATED, use GetDouble() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 310 | bool GetAsDouble(double* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 311 | // DEPRECATED, use GetString() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 312 | bool GetAsString(std::string* out_value) const; |
| 313 | bool GetAsString(string16* out_value) const; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 314 | bool GetAsString(const Value** out_value) const; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 315 | bool GetAsString(StringPiece* out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 316 | // ListValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 317 | // DEPRECATED, use GetList() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 318 | bool GetAsList(ListValue** out_value); |
| 319 | bool GetAsList(const ListValue** out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 320 | // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 321 | bool GetAsDictionary(DictionaryValue** out_value); |
| 322 | bool GetAsDictionary(const DictionaryValue** out_value) const; |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 323 | // 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] | 324 | |
| 325 | // This creates a deep copy of the entire Value tree, and returns a pointer |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 326 | // to the copy. The caller gets ownership of the copy, of course. |
[email protected] | 16f47e08 | 2011-01-18 02:16:59 | [diff] [blame] | 327 | // Subclasses return their own type directly in their overrides; |
| 328 | // this works because C++ supports covariant return types. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 329 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 330 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 331 | Value* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 332 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 333 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 334 | std::unique_ptr<Value> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 335 | |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 336 | // Comparison operators so that Values can easily be used with standard |
| 337 | // library algorithms and associative containers. |
| 338 | BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs); |
| 339 | BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs); |
| 340 | BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs); |
| 341 | BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs); |
| 342 | BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs); |
| 343 | BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs); |
| 344 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 345 | // Compares if two Value objects have equal contents. |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 346 | // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 347 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 348 | bool Equals(const Value* other) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 349 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 350 | protected: |
| 351 | // TODO(crbug.com/646113): Make these private once DictionaryValue and |
| 352 | // ListValue are properly inlined. |
jdoerrie | 6acf28d | 2017-02-02 10:56:03 | [diff] [blame] | 353 | Type type_; |
| 354 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 355 | union { |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 356 | bool bool_value_; |
| 357 | int int_value_; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 358 | double double_value_; |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 359 | std::string string_value_; |
| 360 | BlobStorage binary_value_; |
| 361 | DictStorage dict_; |
| 362 | ListStorage list_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 363 | }; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 364 | |
| 365 | private: |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 366 | void InternalMoveConstructFrom(Value&& that); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 367 | void InternalCleanup(); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 368 | |
| 369 | DISALLOW_COPY_AND_ASSIGN(Value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | }; |
| 371 | |
[email protected] | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 372 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 373 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 374 | // are |std::string|s and should be UTF-8 encoded. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 375 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 376 | public: |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 377 | using const_iterator = DictStorage::const_iterator; |
| 378 | using iterator = DictStorage::iterator; |
| 379 | |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 380 | // Returns |value| if it is a dictionary, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 381 | static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 382 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 383 | DictionaryValue(); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 384 | explicit DictionaryValue(const DictStorage& in_dict); |
| 385 | explicit DictionaryValue(DictStorage&& in_dict) noexcept; |
[email protected] | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 386 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 387 | // Returns true if the current dictionary has a value for the given key. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 388 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 389 | bool HasKey(StringPiece key) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 391 | // Returns the number of Values in this dictionary. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 392 | size_t size() const { return dict_.size(); } |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 393 | |
| 394 | // Returns whether the dictionary is empty. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 395 | bool empty() const { return dict_.empty(); } |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 396 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 397 | // Clears any current contents of this dictionary. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 398 | void Clear(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | |
| 400 | // Sets the Value associated with the given path starting from this object. |
| 401 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 402 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 403 | // within a key, but there are no other restrictions on keys. |
| 404 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 405 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 406 | // to the path in that location. |in_value| must be non-null. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 407 | // Returns a pointer to the inserted value. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 408 | // DEPRECATED, use Value::SetPath(path, value) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 409 | Value* Set(StringPiece path, std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 410 | |
| 411 | // Convenience forms of Set(). These methods will replace any existing |
| 412 | // value at that path, even if it has a different type. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 413 | // DEPRECATED, use Value::SetPath(path, Value(bool)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 414 | Value* SetBoolean(StringPiece path, bool in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 415 | // DEPRECATED, use Value::SetPath(path, Value(int)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 416 | Value* SetInteger(StringPiece path, int in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 417 | // DEPRECATED, use Value::SetPath(path, Value(double)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 418 | Value* SetDouble(StringPiece path, double in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 419 | // DEPRECATED, use Value::SetPath(path, Value(StringPiece)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 420 | Value* SetString(StringPiece path, StringPiece in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 421 | // DEPRECATED, use Value::SetPath(path, Value(const string& 16)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 422 | Value* SetString(StringPiece path, const string16& in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 423 | // DEPRECATED, use Value::SetPath(path, Value(Type::DICTIONARY)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 424 | DictionaryValue* SetDictionary(StringPiece path, |
| 425 | std::unique_ptr<DictionaryValue> in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 426 | // DEPRECATED, use Value::SetPath(path, Value(Type::LIST)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 427 | ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 428 | |
| 429 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 430 | // be used as paths. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 431 | // DEPRECATED, use Value::SetKey(key, value) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 432 | Value* SetWithoutPathExpansion(StringPiece key, |
| 433 | std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 435 | // Convenience forms of SetWithoutPathExpansion(). |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 436 | // DEPRECATED, use Value::SetKey(key, Value(Type::DICTIONARY)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 437 | DictionaryValue* SetDictionaryWithoutPathExpansion( |
| 438 | StringPiece path, |
| 439 | std::unique_ptr<DictionaryValue> in_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 440 | // DEPRECATED, use Value::SetKey(key, Value(Type::LIST)) instead. |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 441 | ListValue* SetListWithoutPathExpansion(StringPiece path, |
| 442 | std::unique_ptr<ListValue> in_value); |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 443 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 444 | // Gets the Value associated with the given path starting from this object. |
| 445 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 446 | // into the next DictionaryValue down. If the path can be resolved |
| 447 | // successfully, the value for the last key in the path will be returned |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 448 | // through the |out_value| parameter, and the function will return true. |
| 449 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 450 | // Note that the dictionary always owns the value that's returned. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 451 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 452 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 453 | bool Get(StringPiece path, const Value** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 454 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 455 | bool Get(StringPiece path, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 456 | |
| 457 | // These are convenience forms of Get(). The value will be retrieved |
| 458 | // and the return value will be true if the path is valid and the value at |
| 459 | // the end of the path can be returned in the form specified. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 460 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 461 | // DEPRECATED, use Value::FindPath(path) and Value::GetBool() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 462 | bool GetBoolean(StringPiece path, bool* out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 463 | // DEPRECATED, use Value::FindPath(path) and Value::GetInt() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 464 | bool GetInteger(StringPiece path, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 465 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 466 | // doubles. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 467 | // DEPRECATED, use Value::FindPath(path) and Value::GetDouble() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 468 | bool GetDouble(StringPiece path, double* out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 469 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 470 | bool GetString(StringPiece path, std::string* out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 471 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 472 | bool GetString(StringPiece path, string16* out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 473 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 474 | bool GetStringASCII(StringPiece path, std::string* out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 475 | // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 476 | bool GetBinary(StringPiece path, const Value** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 477 | // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 478 | bool GetBinary(StringPiece path, Value** out_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 479 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 480 | bool GetDictionary(StringPiece path, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 481 | const DictionaryValue** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 482 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 483 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 484 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 485 | bool GetList(StringPiece path, const ListValue** out_value) const; |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 486 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 487 | bool GetList(StringPiece path, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 488 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 489 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 490 | // be used as paths. |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 491 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 492 | bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 493 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 494 | bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 495 | // DEPRECATED, use Value::FindKey(key) and Value::GetBool() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 496 | bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 497 | // DEPRECATED, use Value::FindKey(key) and Value::GetInt() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 498 | bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 499 | // DEPRECATED, use Value::FindKey(key) and Value::GetDouble() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 500 | bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 501 | // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 502 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 503 | std::string* out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 504 | // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 505 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 506 | string16* out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 507 | // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead. |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 508 | bool GetDictionaryWithoutPathExpansion( |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 509 | StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 510 | const DictionaryValue** out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 511 | // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 512 | bool GetDictionaryWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 513 | DictionaryValue** out_value); |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 514 | // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 515 | bool GetListWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 516 | const ListValue** out_value) const; |
jdoerrie | 1e4eeb8 | 2017-08-02 23:25:52 | [diff] [blame] | 517 | // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 518 | bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 519 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 520 | // Removes the Value with the specified path from this dictionary (or one |
| 521 | // 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] | 522 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 523 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 524 | // This method returns true if |path| is a valid path; otherwise it will |
| 525 | // return false and the DictionaryValue object will be unchanged. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 526 | // DEPRECATED, use Value::RemovePath(path) instead. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 527 | bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 528 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 529 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 530 | // to be used as paths. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 531 | // DEPRECATED, use Value::RemoveKey(key) instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 532 | bool RemoveWithoutPathExpansion(StringPiece key, |
| 533 | std::unique_ptr<Value>* out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 534 | |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 535 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 536 | // after removing the value at |path|. |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 537 | // DEPRECATED, use Value::RemovePath(path) instead. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 538 | bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 539 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 540 | using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise. |
| 541 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 542 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 543 | // the copy. This never returns NULL, even if |this| itself is empty. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 544 | std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 545 | |
[email protected] | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 546 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 547 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 548 | // passed in dictionary takes precedence and data already present will be |
| 549 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 550 | // be freed any time after this call. |
[email protected] | c378cca | 2010-05-14 13:17:40 | [diff] [blame] | 551 | void MergeDictionary(const DictionaryValue* dictionary); |
| 552 | |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 553 | // Swaps contents with the |other| dictionary. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 554 | void Swap(DictionaryValue* other); |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 555 | |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 556 | // This class provides an iterator over both keys and values in the |
| 557 | // dictionary. It can't be used to modify the dictionary. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 558 | // DEPRECATED, use Value::DictItems() instead. |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 559 | class BASE_EXPORT Iterator { |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 560 | public: |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 561 | explicit Iterator(const DictionaryValue& target); |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 562 | Iterator(const Iterator& other); |
[email protected] | a8d94b4 | 2013-12-10 18:52:22 | [diff] [blame] | 563 | ~Iterator(); |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 564 | |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 565 | bool IsAtEnd() const { return it_ == target_.dict_.end(); } |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 566 | void Advance() { ++it_; } |
| 567 | |
| 568 | const std::string& key() const { return it_->first; } |
| 569 | const Value& value() const { return *it_->second; } |
| 570 | |
| 571 | private: |
| 572 | const DictionaryValue& target_; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 573 | DictStorage::const_iterator it_; |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 574 | }; |
| 575 | |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 576 | // Iteration. |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 577 | // DEPRECATED, use Value::DictItems() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 578 | iterator begin() { return dict_.begin(); } |
| 579 | iterator end() { return dict_.end(); } |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 580 | |
jdoerrie | 43ab3c0 | 2017-08-24 20:44:36 | [diff] [blame] | 581 | // DEPRECATED, use Value::DictItems() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 582 | const_iterator begin() const { return dict_.begin(); } |
| 583 | const_iterator end() const { return dict_.end(); } |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 584 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 585 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 586 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 587 | DictionaryValue* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 588 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 589 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 590 | std::unique_ptr<DictionaryValue> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 591 | }; |
| 592 | |
| 593 | // This type of Value represents a list of other Value values. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 594 | class BASE_EXPORT ListValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 595 | public: |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 596 | using const_iterator = ListStorage::const_iterator; |
| 597 | using iterator = ListStorage::iterator; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 598 | |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 599 | // Returns |value| if it is a list, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 600 | static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 601 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 602 | ListValue(); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 603 | explicit ListValue(const ListStorage& in_list); |
| 604 | explicit ListValue(ListStorage&& in_list) noexcept; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 605 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 606 | // Clears the contents of this ListValue |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 607 | // DEPRECATED, use GetList()::clear() instead. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 608 | void Clear(); |
| 609 | |
| 610 | // Returns the number of Values in this list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 611 | // DEPRECATED, use GetList()::size() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 612 | size_t GetSize() const { return list_.size(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 613 | |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 614 | // Returns the capacity of storage for Values in this list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 615 | // DEPRECATED, use GetList()::capacity() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 616 | size_t capacity() const { return list_.capacity(); } |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 617 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 618 | // Returns whether the list is empty. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 619 | // DEPRECATED, use GetList()::empty() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 620 | bool empty() const { return list_.empty(); } |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 621 | |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 622 | // Reserves storage for at least |n| values. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 623 | // DEPRECATED, use GetList()::reserve() instead. |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 624 | void Reserve(size_t n); |
| 625 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 626 | // Sets the list item at the given index to be the Value specified by |
| 627 | // the value given. If the index beyond the current end of the list, null |
| 628 | // Values will be used to pad out the list. |
| 629 | // Returns true if successful, or false if the index was negative or |
| 630 | // the value is a null pointer. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 631 | // DEPRECATED, use GetList()::operator[] instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 632 | bool Set(size_t index, std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 633 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 634 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 635 | // only if the index falls within the current list range. |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 636 | // Note that the list always owns the Value passed out via |out_value|. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 637 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 638 | // DEPRECATED, use GetList()::operator[] instead. |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 639 | bool Get(size_t index, const Value** out_value) const; |
| 640 | bool Get(size_t index, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 641 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 642 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 643 | // only if the index is valid and the Value at that index can be returned |
| 644 | // in the specified form. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 645 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 646 | // DEPRECATED, use GetList()::operator[]::GetBool() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 647 | bool GetBoolean(size_t index, bool* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 648 | // DEPRECATED, use GetList()::operator[]::GetInt() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 649 | bool GetInteger(size_t index, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 650 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 651 | // doubles. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 652 | // DEPRECATED, use GetList()::operator[]::GetDouble() instead. |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 653 | bool GetDouble(size_t index, double* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 654 | // DEPRECATED, use GetList()::operator[]::GetString() instead. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 655 | bool GetString(size_t index, std::string* out_value) const; |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 656 | bool GetString(size_t index, string16* out_value) const; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 657 | // DEPRECATED, use GetList()::operator[]::GetBlob() instead. |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 658 | bool GetBinary(size_t index, const Value** out_value) const; |
| 659 | bool GetBinary(size_t index, Value** out_value); |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 660 | |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 661 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 662 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 663 | |
| 664 | using Value::GetList; |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 665 | // DEPRECATED, use GetList()::operator[]::GetList() instead. |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 666 | bool GetList(size_t index, const ListValue** out_value) const; |
| 667 | bool GetList(size_t index, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 668 | |
| 669 | // Removes the Value with the specified index from this list. |
| 670 | // 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] | 671 | // 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] | 672 | // be deleted. This method returns true if |index| is valid; otherwise |
| 673 | // it will return false and the ListValue object will be unchanged. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 674 | // DEPRECATED, use GetList()::erase() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 675 | bool Remove(size_t index, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 676 | |
[email protected] | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 677 | // Removes the first instance of |value| found in the list, if any, and |
[email protected] | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 678 | // deletes it. |index| is the location where |value| was found. Returns false |
| 679 | // if not found. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 680 | // DEPRECATED, use GetList()::erase() instead. |
[email protected] | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 681 | bool Remove(const Value& value, size_t* index); |
[email protected] | e7f5c6f | 2009-05-09 00:33:04 | [diff] [blame] | 682 | |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 683 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
| 684 | // deleted, otherwise ownership of the value is passed back to the caller. |
[email protected] | a8d379cc | 2013-02-18 16:31:45 | [diff] [blame] | 685 | // Returns an iterator pointing to the location of the element that |
| 686 | // followed the erased element. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 687 | // DEPRECATED, use GetList()::erase() instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 688 | iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 689 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 690 | // Appends a Value to the end of the list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 691 | // DEPRECATED, use GetList()::push_back() instead. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 692 | void Append(std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 693 | |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 694 | // Convenience forms of Append. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 695 | // DEPRECATED, use GetList()::emplace_back() instead. |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 696 | void AppendBoolean(bool in_value); |
| 697 | void AppendInteger(int in_value); |
| 698 | void AppendDouble(double in_value); |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 699 | void AppendString(StringPiece in_value); |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 700 | void AppendString(const string16& in_value); |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 701 | // DEPRECATED, use GetList()::emplace_back() in a loop instead. |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 702 | void AppendStrings(const std::vector<std::string>& in_values); |
| 703 | void AppendStrings(const std::vector<string16>& in_values); |
| 704 | |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 705 | // Appends a Value if it's not already present. Returns true if successful, |
| 706 | // or false if the value was already |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 707 | // DEPRECATED, use std::find() with GetList()::push_back() instead. |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 708 | bool AppendIfNotPresent(std::unique_ptr<Value> in_value); |
[email protected] | 84064220 | 2010-04-12 21:48:10 | [diff] [blame] | 709 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 710 | // Insert a Value at index. |
| 711 | // Returns true if successful, or false if the index was out of range. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 712 | // DEPRECATED, use GetList()::insert() instead. |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 713 | bool Insert(size_t index, std::unique_ptr<Value> in_value); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 714 | |
[email protected] | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 715 | // Searches for the first instance of |value| in the list using the Equals |
| 716 | // method of the Value type. |
| 717 | // Returns a const_iterator to the found item or to end() if none exists. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 718 | // DEPRECATED, use std::find() instead. |
[email protected] | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 719 | const_iterator Find(const Value& value) const; |
| 720 | |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 721 | // Swaps contents with the |other| list. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 722 | // DEPRECATED, use GetList()::swap() instead. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 723 | void Swap(ListValue* other); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 724 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 725 | // Iteration. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 726 | // DEPRECATED, use GetList()::begin() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 727 | iterator begin() { return list_.begin(); } |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 728 | // DEPRECATED, use GetList()::end() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 729 | iterator end() { return list_.end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 730 | |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 731 | // DEPRECATED, use GetList()::begin() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 732 | const_iterator begin() const { return list_.begin(); } |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 733 | // DEPRECATED, use GetList()::end() instead. |
Daniel Cheng | 34ef31b4 | 2017-10-12 02:31:07 | [diff] [blame] | 734 | const_iterator end() const { return list_.end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 735 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 736 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | e964d9a | 2017-04-05 06:44:17 | [diff] [blame] | 737 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 738 | ListValue* DeepCopy() const; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 739 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | d4b85261 | 2017-06-06 11:48:43 | [diff] [blame] | 740 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 741 | std::unique_ptr<ListValue> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 742 | }; |
| 743 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 744 | // This interface is implemented by classes that know how to serialize |
| 745 | // Value objects. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 746 | class BASE_EXPORT ValueSerializer { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 747 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 748 | virtual ~ValueSerializer(); |
[email protected] | abb9d0c | 2008-08-06 15:46:59 | [diff] [blame] | 749 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 750 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 751 | }; |
| 752 | |
| 753 | // This interface is implemented by classes that know how to deserialize Value |
| 754 | // objects. |
| 755 | class BASE_EXPORT ValueDeserializer { |
| 756 | public: |
| 757 | virtual ~ValueDeserializer(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 758 | |
| 759 | // This method deserializes the subclass-specific format into a Value object. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 760 | // If the return value is non-NULL, the caller takes ownership of returned |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 761 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 762 | // error_code will be set with the underlying error. |
| 763 | // If |error_message| is non-null, it will be filled in with a formatted |
| 764 | // error message including the location of the error if appropriate. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 765 | virtual std::unique_ptr<Value> Deserialize(int* error_code, |
| 766 | std::string* error_str) = 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 767 | }; |
| 768 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 769 | // Stream operator so Values can be used in assertion statements. In order that |
| 770 | // gtest uses this operator to print readable output on test failures, we must |
| 771 | // override each specific type. Otherwise, the default template implementation |
| 772 | // is preferred over an upcast. |
[email protected] | e4ef831 | 2012-09-12 03:39:35 | [diff] [blame] | 773 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 774 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 775 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 776 | const DictionaryValue& value) { |
| 777 | return out << static_cast<const Value&>(value); |
| 778 | } |
| 779 | |
| 780 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 781 | const ListValue& value) { |
| 782 | return out << static_cast<const Value&>(value); |
| 783 | } |
| 784 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 785 | // Stream operator so that enum class Types can be used in log statements. |
| 786 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 787 | const Value::Type& type); |
| 788 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 789 | } // namespace base |
| 790 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 791 | #endif // BASE_VALUES_H_ |