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