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