jhawkins@chromium.org | cbb2239 | 2012-05-10 06:54:27 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 5 | // This file specifies a recursive data storage class called Value intended for |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 6 | // storing settings and other persistable data. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | // |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | // |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 12 | // IN PARTICULAR this means that there is no support for int64_t or unsigned |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 13 | // numbers. Writing JSON with such types would violate the spec. If you need |
| 14 | // something like this, either use a double or make a string value containing |
| 15 | // the number you want. |
Daniel Cheng | 5fd7663 | 2017-09-26 09:52:43 +0900 | [diff] [blame] | 16 | // |
| 17 | // NOTE: A Value parameter that is always a Value::STRING should just be passed |
| 18 | // as a std::string. Similarly for Values that are always Value::DICTIONARY |
| 19 | // (should be flat_map), Value::LIST (should be std::vector), et cetera. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 20 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 21 | #ifndef BASE_VALUES_H_ |
| 22 | #define BASE_VALUES_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 24 | #include <stddef.h> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 25 | #include <stdint.h> |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 26 | |
| 27 | #include <iosfwd> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | #include <map> |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 29 | #include <memory> |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 30 | #include <string> |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 31 | #include <utility> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 34 | #include "base/base_export.h" |
mkwst | 62e159c | 2017-04-11 15:52:21 +0900 | [diff] [blame] | 35 | #include "base/containers/flat_map.h" |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 36 | #include "base/containers/span.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 37 | #include "base/macros.h" |
avi@chromium.org | 68a745c | 2013-06-11 05:11:14 +0900 | [diff] [blame] | 38 | #include "base/strings/string16.h" |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 39 | #include "base/strings/string_piece.h" |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 40 | #include "base/value_iterators.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 42 | namespace base { |
| 43 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | class DictionaryValue; |
| 45 | class ListValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 46 | class Value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 47 | |
tfarina@chromium.org | 2e2a902 | 2011-08-06 03:20:05 +0900 | [diff] [blame] | 48 | // The Value class is the base class for Values. A Value can be instantiated |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 49 | // via passing the appropriate type or backing storage to the constructor. |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 50 | // |
| 51 | // See the file-level comment above for more information. |
Brett Wilson | fe1b261 | 2017-09-02 05:11:49 +0900 | [diff] [blame] | 52 | // |
| 53 | // base::Value is currently in the process of being refactored. Design doc: |
| 54 | // https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw |
| 55 | // |
| 56 | // Previously (which is how most code that currently exists is written), Value |
| 57 | // used derived types to implement the individual data types, and base::Value |
| 58 | // was just a base class to refer to them. This required everything be heap |
| 59 | // allocated. |
| 60 | // |
| 61 | // OLD WAY: |
| 62 | // |
| 63 | // std::unique_ptr<base::Value> GetFoo() { |
| 64 | // std::unique_ptr<DictionaryValue> dict; |
| 65 | // dict->SetString("mykey", foo); |
| 66 | // return dict; |
| 67 | // } |
| 68 | // |
| 69 | // The new design makes base::Value a variant type that holds everything in |
| 70 | // a union. It is now recommended to pass by value with std::move rather than |
| 71 | // use heap allocated values. The DictionaryValue and ListValue subclasses |
| 72 | // exist only as a compatibility shim that we're in the process of removing. |
| 73 | // |
| 74 | // NEW WAY: |
| 75 | // |
| 76 | // base::Value GetFoo() { |
| 77 | // base::Value dict(base::Value::Type::DICTIONARY); |
| 78 | // dict.SetKey("mykey", base::Value(foo)); |
| 79 | // return dict; |
| 80 | // } |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 81 | class BASE_EXPORT Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 82 | public: |
jdoerrie | 940a3e5 | 2017-04-18 19:22:41 +0900 | [diff] [blame] | 83 | using BlobStorage = std::vector<char>; |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 84 | using DictStorage = flat_map<std::string, std::unique_ptr<Value>>; |
jdoerrie | 4518400 | 2017-04-12 03:09:14 +0900 | [diff] [blame] | 85 | using ListStorage = std::vector<Value>; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 86 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 87 | enum class Type { |
| 88 | NONE = 0, |
| 89 | BOOLEAN, |
| 90 | INTEGER, |
| 91 | DOUBLE, |
| 92 | STRING, |
| 93 | BINARY, |
| 94 | DICTIONARY, |
| 95 | LIST |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 96 | // Note: Do not add more types. See the file-level comment above for why. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 97 | }; |
| 98 | |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 99 | // For situations where you want to keep ownership of your buffer, this |
| 100 | // factory method creates a new BinaryValue by copying the contents of the |
| 101 | // buffer that's passed in. |
Jeremy Roman | cd0c467 | 2017-08-17 08:27:24 +0900 | [diff] [blame] | 102 | // DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead. |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 103 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 104 | static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, |
| 105 | size_t size); |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 106 | |
Lei Zhang | 7792c55 | 2017-10-24 04:14:46 +0900 | [diff] [blame] | 107 | // Adaptors for converting from the old way to the new way and vice versa. |
| 108 | static Value FromUniquePtrValue(std::unique_ptr<Value> val); |
| 109 | static std::unique_ptr<Value> ToUniquePtrValue(Value val); |
| 110 | |
brettw | ce38c19 | 2017-03-25 01:36:42 +0900 | [diff] [blame] | 111 | Value(Value&& that) noexcept; |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 112 | Value() noexcept; // A null value. |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 113 | |
| 114 | // Value's copy constructor and copy assignment operator are deleted. Use this |
| 115 | // to obtain a deep copy explicitly. |
| 116 | Value Clone() const; |
| 117 | |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 118 | explicit Value(Type type); |
| 119 | explicit Value(bool in_bool); |
| 120 | explicit Value(int in_int); |
| 121 | explicit Value(double in_double); |
| 122 | |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 123 | // Value(const char*) and Value(const char16*) are required despite |
jdoerrie | 0a1d0af | 2017-09-12 02:23:26 +0900 | [diff] [blame] | 124 | // Value(StringPiece) and Value(StringPiece16) because otherwise the |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 125 | // compiler will choose the Value(bool) constructor for these arguments. |
| 126 | // Value(std::string&&) allow for efficient move construction. |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 127 | explicit Value(const char* in_string); |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 128 | explicit Value(StringPiece in_string); |
jdoerrie | 0a1d0af | 2017-09-12 02:23:26 +0900 | [diff] [blame] | 129 | explicit Value(std::string&& in_string) noexcept; |
| 130 | explicit Value(const char16* in_string16); |
| 131 | explicit Value(StringPiece16 in_string16); |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 132 | |
jdoerrie | 940a3e5 | 2017-04-18 19:22:41 +0900 | [diff] [blame] | 133 | explicit Value(const BlobStorage& in_blob); |
| 134 | explicit Value(BlobStorage&& in_blob) noexcept; |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 135 | |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 136 | explicit Value(const DictStorage& in_dict); |
mkwst | 62e159c | 2017-04-11 15:52:21 +0900 | [diff] [blame] | 137 | explicit Value(DictStorage&& in_dict) noexcept; |
| 138 | |
jdoerrie | f72e0cb | 2017-04-19 16:15:38 +0900 | [diff] [blame] | 139 | explicit Value(const ListStorage& in_list); |
| 140 | explicit Value(ListStorage&& in_list) noexcept; |
| 141 | |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 142 | Value& operator=(Value&& that) noexcept; |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 143 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 144 | ~Value(); |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 145 | |
thestig | e44b932 | 2016-07-19 09:39:30 +0900 | [diff] [blame] | 146 | // Returns the name for a given |type|. |
| 147 | static const char* GetTypeName(Type type); |
| 148 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 149 | // Returns the type of the value stored by the current Value object. |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 150 | Type type() const { return type_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 151 | |
| 152 | // Returns true if the current object represents a given type. |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 153 | bool is_none() const { return type() == Type::NONE; } |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 154 | bool is_bool() const { return type() == Type::BOOLEAN; } |
| 155 | bool is_int() const { return type() == Type::INTEGER; } |
| 156 | bool is_double() const { return type() == Type::DOUBLE; } |
| 157 | bool is_string() const { return type() == Type::STRING; } |
| 158 | bool is_blob() const { return type() == Type::BINARY; } |
| 159 | bool is_dict() const { return type() == Type::DICTIONARY; } |
| 160 | bool is_list() const { return type() == Type::LIST; } |
| 161 | |
| 162 | // These will all fatally assert if the type doesn't match. |
| 163 | bool GetBool() const; |
| 164 | int GetInt() const; |
| 165 | double GetDouble() const; // Implicitly converts from int if necessary. |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 166 | const std::string& GetString() const; |
jdoerrie | 940a3e5 | 2017-04-18 19:22:41 +0900 | [diff] [blame] | 167 | const BlobStorage& GetBlob() const; |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 168 | |
jdoerrie | f72e0cb | 2017-04-19 16:15:38 +0900 | [diff] [blame] | 169 | ListStorage& GetList(); |
| 170 | const ListStorage& GetList() const; |
| 171 | |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 172 | // |FindKey| looks up |key| in the underlying dictionary. If found, it returns |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 173 | // a pointer to the element. Otherwise it returns nullptr. |
| 174 | // returned. Callers are expected to perform a check against null before using |
| 175 | // the pointer. |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 176 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 177 | // |
| 178 | // Example: |
| 179 | // auto* found = FindKey("foo"); |
| 180 | Value* FindKey(StringPiece key); |
| 181 | const Value* FindKey(StringPiece key) const; |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 182 | |
| 183 | // |FindKeyOfType| is similar to |FindKey|, but it also requires the found |
| 184 | // value to have type |type|. If no type is found, or the found value is of a |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 185 | // different type nullptr is returned. |
| 186 | // Callers are expected to perform a check against null before using the |
| 187 | // pointer. |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 188 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 189 | // |
| 190 | // Example: |
| 191 | // auto* found = FindKey("foo", Type::DOUBLE); |
| 192 | Value* FindKeyOfType(StringPiece key, Type type); |
| 193 | const Value* FindKeyOfType(StringPiece key, Type type) const; |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 194 | |
| 195 | // |SetKey| looks up |key| in the underlying dictionary and sets the mapped |
| 196 | // value to |value|. If |key| could not be found, a new element is inserted. |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 197 | // A pointer to the modified item is returned. |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 198 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 199 | // |
| 200 | // Example: |
| 201 | // SetKey("foo", std::move(myvalue)); |
| 202 | Value* SetKey(StringPiece key, Value value); |
| 203 | // This overload results in a performance improvement for std::string&&. |
| 204 | Value* SetKey(std::string&& key, Value value); |
jdoerrie | c575514 | 2017-08-02 11:20:32 +0900 | [diff] [blame] | 205 | // This overload is necessary to avoid ambiguity for const char* arguments. |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 206 | Value* SetKey(const char* key, Value value); |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 207 | |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 208 | // This attemps to remove the value associated with |key|. In case of failure, |
| 209 | // e.g. the key does not exist, |false| is returned and the underlying |
| 210 | // dictionary is not changed. In case of success, |key| is deleted from the |
| 211 | // dictionary and the method returns |true|. |
| 212 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
| 213 | // |
| 214 | // Example: |
| 215 | // bool success = RemoveKey("foo"); |
| 216 | bool RemoveKey(StringPiece key); |
| 217 | |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 218 | // Searches a hierarchy of dictionary values for a given value. If a path |
| 219 | // of dictionaries exist, returns the item at that path. If any of the path |
| 220 | // components do not exist or if any but the last path components are not |
| 221 | // dictionaries, returns nullptr. |
| 222 | // |
| 223 | // The type of the leaf Value is not checked. |
| 224 | // |
| 225 | // Implementation note: This can't return an iterator because the iterator |
| 226 | // will actually be into another Value, so it can't be compared to iterators |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 227 | // from this one (in particular, the DictItems().end() iterator). |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 228 | // |
| 229 | // Example: |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 230 | // auto* found = FindPath({"foo", "bar"}); |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 231 | // |
| 232 | // std::vector<StringPiece> components = ... |
| 233 | // auto* found = FindPath(components); |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 234 | // |
| 235 | // Note: If there is only one component in the path, use FindKey() instead. |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 236 | Value* FindPath(std::initializer_list<StringPiece> path); |
| 237 | Value* FindPath(span<const StringPiece> path); |
| 238 | const Value* FindPath(std::initializer_list<StringPiece> path) const; |
| 239 | const Value* FindPath(span<const StringPiece> path) const; |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 240 | |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 241 | // Like FindPath() but will only return the value if the leaf Value type |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 242 | // matches the given type. Will return nullptr otherwise. |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 243 | // |
| 244 | // Note: If there is only one component in the path, use FindKeyOfType() |
| 245 | // instead. |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 246 | Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type); |
| 247 | Value* FindPathOfType(span<const StringPiece> path, Type type); |
| 248 | const Value* FindPathOfType(std::initializer_list<StringPiece> path, |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 249 | Type type) const; |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 250 | const Value* FindPathOfType(span<const StringPiece> path, Type type) const; |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 251 | |
| 252 | // Sets the given path, expanding and creating dictionary keys as necessary. |
| 253 | // |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 254 | // If the current value is not a dictionary, the function returns nullptr. If |
| 255 | // path components do not exist, they will be created. If any but the last |
| 256 | // components matches a value that is not a dictionary, the function will fail |
| 257 | // (it will not overwrite the value) and return nullptr. The last path |
| 258 | // component will be unconditionally overwritten if it exists, and created if |
| 259 | // it doesn't. |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 260 | // |
| 261 | // Example: |
| 262 | // value.SetPath({"foo", "bar"}, std::move(myvalue)); |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 263 | // |
| 264 | // std::vector<StringPiece> components = ... |
| 265 | // value.SetPath(components, std::move(myvalue)); |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 266 | // |
| 267 | // Note: If there is only one component in the path, use SetKey() instead. |
jdoerrie | 859d2fd | 2017-08-23 17:38:27 +0900 | [diff] [blame] | 268 | Value* SetPath(std::initializer_list<StringPiece> path, Value value); |
| 269 | Value* SetPath(span<const StringPiece> path, Value value); |
Brett Wilson | 8b9be6f | 2017-08-03 09:08:27 +0900 | [diff] [blame] | 270 | |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 271 | // Tries to remove a Value at the given path. |
| 272 | // |
| 273 | // If the current value is not a dictionary or any path components does not |
| 274 | // exist, this operation fails, leaves underlying Values untouched and returns |
| 275 | // |false|. In case intermediate dictionaries become empty as a result of this |
| 276 | // path removal, they will be removed as well. |
| 277 | // |
| 278 | // Example: |
| 279 | // bool success = value.RemovePath({"foo", "bar"}); |
| 280 | // |
| 281 | // std::vector<StringPiece> components = ... |
| 282 | // bool success = value.RemovePath(components); |
Lei Zhang | 08499a7 | 2017-10-27 09:27:23 +0900 | [diff] [blame] | 283 | // |
| 284 | // Note: If there is only one component in the path, use RemoveKey() instead. |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 285 | bool RemovePath(std::initializer_list<StringPiece> path); |
| 286 | bool RemovePath(span<const StringPiece> path); |
| 287 | |
jdoerrie | acd36c5 | 2017-08-18 04:04:45 +0900 | [diff] [blame] | 288 | using dict_iterator_proxy = detail::dict_iterator_proxy; |
| 289 | using const_dict_iterator_proxy = detail::const_dict_iterator_proxy; |
jdoerrie | 673fa77 | 2017-07-14 23:47:20 +0900 | [diff] [blame] | 290 | |
| 291 | // |DictItems| returns a proxy object that exposes iterators to the underlying |
| 292 | // dictionary. These are intended for iteration over all items in the |
| 293 | // dictionary and are compatible with for-each loops and standard library |
| 294 | // algorithms. |
| 295 | // Note: This fatally asserts if type() is not Type::DICTIONARY. |
| 296 | dict_iterator_proxy DictItems(); |
| 297 | const_dict_iterator_proxy DictItems() const; |
| 298 | |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 299 | // These methods allow the convenient retrieval of the contents of the Value. |
| 300 | // If the current object can be converted into the given type, the value is |
| 301 | // returned through the |out_value| parameter and true is returned; |
| 302 | // otherwise, false is returned and |out_value| is unchanged. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 303 | // DEPRECATED, use GetBool() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 304 | bool GetAsBoolean(bool* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 305 | // DEPRECATED, use GetInt() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 306 | bool GetAsInteger(int* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 307 | // DEPRECATED, use GetDouble() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 308 | bool GetAsDouble(double* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 309 | // DEPRECATED, use GetString() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 310 | bool GetAsString(std::string* out_value) const; |
| 311 | bool GetAsString(string16* out_value) const; |
jdoerrie | 0d1295b | 2017-03-06 20:12:04 +0900 | [diff] [blame] | 312 | bool GetAsString(const Value** out_value) const; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 313 | bool GetAsString(StringPiece* out_value) const; |
lukasza | f0601bd | 2016-11-02 06:49:46 +0900 | [diff] [blame] | 314 | // ListValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 315 | // DEPRECATED, use GetList() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 316 | bool GetAsList(ListValue** out_value); |
| 317 | bool GetAsList(const ListValue** out_value) const; |
lukasza | f0601bd | 2016-11-02 06:49:46 +0900 | [diff] [blame] | 318 | // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 319 | bool GetAsDictionary(DictionaryValue** out_value); |
| 320 | bool GetAsDictionary(const DictionaryValue** out_value) const; |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 321 | // Note: Do not add more types. See the file-level comment above for why. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 322 | |
| 323 | // This creates a deep copy of the entire Value tree, and returns a pointer |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 324 | // to the copy. The caller gets ownership of the copy, of course. |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 325 | // Subclasses return their own type directly in their overrides; |
| 326 | // this works because C++ supports covariant return types. |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 327 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 328 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 329 | Value* DeepCopy() const; |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 330 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 331 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 332 | std::unique_ptr<Value> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 333 | |
jdoerrie | 046caee | 2017-03-29 02:52:00 +0900 | [diff] [blame] | 334 | // Comparison operators so that Values can easily be used with standard |
| 335 | // library algorithms and associative containers. |
| 336 | BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs); |
| 337 | BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs); |
| 338 | BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs); |
| 339 | BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs); |
| 340 | BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs); |
| 341 | BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs); |
| 342 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 343 | // Compares if two Value objects have equal contents. |
jdoerrie | 046caee | 2017-03-29 02:52:00 +0900 | [diff] [blame] | 344 | // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 345 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 346 | bool Equals(const Value* other) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 347 | |
Alexander Yashkin | b580256 | 2017-11-30 20:54:45 +0900 | [diff] [blame] | 348 | // Estimates dynamic memory usage. |
| 349 | // See base/trace_event/memory_usage_estimator.h for more info. |
| 350 | size_t EstimateMemoryUsage() const; |
| 351 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 352 | protected: |
| 353 | // TODO(crbug.com/646113): Make these private once DictionaryValue and |
| 354 | // ListValue are properly inlined. |
jdoerrie | 34ee9f6 | 2017-02-02 19:56:03 +0900 | [diff] [blame] | 355 | Type type_; |
| 356 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 357 | union { |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 358 | bool bool_value_; |
| 359 | int int_value_; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 360 | double double_value_; |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 361 | std::string string_value_; |
| 362 | BlobStorage binary_value_; |
| 363 | DictStorage dict_; |
| 364 | ListStorage list_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 365 | }; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 366 | |
| 367 | private: |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 368 | void InternalMoveConstructFrom(Value&& that); |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 369 | void InternalCleanup(); |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 370 | |
| 371 | DISALLOW_COPY_AND_ASSIGN(Value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 372 | }; |
| 373 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 374 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 375 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 376 | // are |std::string|s and should be UTF-8 encoded. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 377 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 378 | public: |
Johan Tibell | b6751c8 | 2017-05-17 14:21:12 +0900 | [diff] [blame] | 379 | using const_iterator = DictStorage::const_iterator; |
| 380 | using iterator = DictStorage::iterator; |
| 381 | |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 382 | // Returns |value| if it is a dictionary, nullptr otherwise. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 383 | static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 384 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 385 | DictionaryValue(); |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 386 | explicit DictionaryValue(const DictStorage& in_dict); |
| 387 | explicit DictionaryValue(DictStorage&& in_dict) noexcept; |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 388 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 389 | // Returns true if the current dictionary has a value for the given key. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 390 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 391 | bool HasKey(StringPiece key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 392 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 393 | // Returns the number of Values in this dictionary. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 394 | size_t size() const { return dict_.size(); } |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 395 | |
| 396 | // Returns whether the dictionary is empty. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 397 | bool empty() const { return dict_.empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 398 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 399 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 400 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 401 | |
| 402 | // Sets the Value associated with the given path starting from this object. |
| 403 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 404 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 405 | // within a key, but there are no other restrictions on keys. |
| 406 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 407 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 408 | // to the path in that location. |in_value| must be non-null. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 409 | // Returns a pointer to the inserted value. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 410 | // DEPRECATED, use Value::SetPath(path, value) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 411 | Value* Set(StringPiece path, std::unique_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 412 | |
| 413 | // Convenience forms of Set(). These methods will replace any existing |
| 414 | // value at that path, even if it has a different type. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 415 | // DEPRECATED, use Value::SetPath(path, Value(bool)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 416 | Value* SetBoolean(StringPiece path, bool in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 417 | // DEPRECATED, use Value::SetPath(path, Value(int)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 418 | Value* SetInteger(StringPiece path, int in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 419 | // DEPRECATED, use Value::SetPath(path, Value(double)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 420 | Value* SetDouble(StringPiece path, double in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 421 | // DEPRECATED, use Value::SetPath(path, Value(StringPiece)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 422 | Value* SetString(StringPiece path, StringPiece in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 423 | // DEPRECATED, use Value::SetPath(path, Value(const string& 16)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 424 | Value* SetString(StringPiece path, const string16& in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 425 | // DEPRECATED, use Value::SetPath(path, Value(Type::DICTIONARY)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 426 | DictionaryValue* SetDictionary(StringPiece path, |
| 427 | std::unique_ptr<DictionaryValue> in_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 428 | // DEPRECATED, use Value::SetPath(path, Value(Type::LIST)) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 429 | ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 430 | |
| 431 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 432 | // be used as paths. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 433 | // DEPRECATED, use Value::SetKey(key, value) instead. |
jdoerrie | 9ac28c4 | 2017-04-29 06:52:58 +0900 | [diff] [blame] | 434 | Value* SetWithoutPathExpansion(StringPiece key, |
| 435 | std::unique_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 436 | |
| 437 | // Gets the Value associated with the given path starting from this object. |
| 438 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 439 | // into the next DictionaryValue down. If the path can be resolved |
| 440 | // successfully, the value for the last key in the path will be returned |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 441 | // through the |out_value| parameter, and the function will return true. |
| 442 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 443 | // Note that the dictionary always owns the value that's returned. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 444 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 445 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 446 | bool Get(StringPiece path, const Value** out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 447 | // DEPRECATED, use Value::FindPath(path) instead. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 448 | bool Get(StringPiece path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 449 | |
| 450 | // These are convenience forms of Get(). The value will be retrieved |
| 451 | // and the return value will be true if the path is valid and the value at |
| 452 | // the end of the path can be returned in the form specified. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 453 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 454 | // DEPRECATED, use Value::FindPath(path) and Value::GetBool() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 455 | bool GetBoolean(StringPiece path, bool* out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 456 | // DEPRECATED, use Value::FindPath(path) and Value::GetInt() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 457 | bool GetInteger(StringPiece path, int* out_value) const; |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 458 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 459 | // doubles. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 460 | // DEPRECATED, use Value::FindPath(path) and Value::GetDouble() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 461 | bool GetDouble(StringPiece path, double* out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 462 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 463 | bool GetString(StringPiece path, std::string* out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 464 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 465 | bool GetString(StringPiece path, string16* out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 466 | // DEPRECATED, use Value::FindPath(path) and Value::GetString() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 467 | bool GetStringASCII(StringPiece path, std::string* out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 468 | // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead. |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 469 | bool GetBinary(StringPiece path, const Value** out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 470 | // DEPRECATED, use Value::FindPath(path) and Value::GetBlob() instead. |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 471 | bool GetBinary(StringPiece path, Value** out_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 472 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 473 | bool GetDictionary(StringPiece path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 474 | const DictionaryValue** out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 475 | // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 476 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 477 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 478 | bool GetList(StringPiece path, const ListValue** out_value) const; |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 479 | // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 480 | bool GetList(StringPiece path, ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 481 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 482 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 483 | // be used as paths. |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 484 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 485 | bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 486 | // DEPRECATED, use Value::FindKey(key) instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 487 | bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 488 | // DEPRECATED, use Value::FindKey(key) and Value::GetBool() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 489 | bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 490 | // DEPRECATED, use Value::FindKey(key) and Value::GetInt() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 491 | bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 492 | // DEPRECATED, use Value::FindKey(key) and Value::GetDouble() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 493 | bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 494 | // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 495 | bool GetStringWithoutPathExpansion(StringPiece key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 496 | std::string* out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 497 | // DEPRECATED, use Value::FindKey(key) and Value::GetString() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 498 | bool GetStringWithoutPathExpansion(StringPiece key, |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 499 | string16* out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 500 | // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead. |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 501 | bool GetDictionaryWithoutPathExpansion( |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 502 | StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 503 | const DictionaryValue** out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 504 | // DEPRECATED, use Value::FindKey(key) and Value's Dictionary API instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 505 | bool GetDictionaryWithoutPathExpansion(StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 506 | DictionaryValue** out_value); |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 507 | // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 508 | bool GetListWithoutPathExpansion(StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 509 | const ListValue** out_value) const; |
jdoerrie | 45c13a7 | 2017-08-03 08:25:52 +0900 | [diff] [blame] | 510 | // DEPRECATED, use Value::FindKey(key) and Value::GetList() instead. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 511 | bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 512 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 513 | // Removes the Value with the specified path from this dictionary (or one |
| 514 | // of its child dictionaries, if the path is more than just a local key). |
bauerb@chromium.org | ae43e0a | 2013-08-06 22:33:04 +0900 | [diff] [blame] | 515 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 516 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 517 | // This method returns true if |path| is a valid path; otherwise it will |
| 518 | // return false and the DictionaryValue object will be unchanged. |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 519 | // DEPRECATED, use Value::RemovePath(path) instead. |
dcheng | ba05520 | 2016-11-29 14:30:31 +0900 | [diff] [blame] | 520 | bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 521 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 522 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 523 | // to be used as paths. |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 524 | // DEPRECATED, use Value::RemoveKey(key) instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 525 | bool RemoveWithoutPathExpansion(StringPiece key, |
| 526 | std::unique_ptr<Value>* out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 527 | |
gab@chromium.org | 903d5c9 | 2013-11-27 10:38:24 +0900 | [diff] [blame] | 528 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 529 | // after removing the value at |path|. |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 530 | // DEPRECATED, use Value::RemovePath(path) instead. |
dcheng | ba05520 | 2016-11-29 14:30:31 +0900 | [diff] [blame] | 531 | bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
gab@chromium.org | 903d5c9 | 2013-11-27 10:38:24 +0900 | [diff] [blame] | 532 | |
jdoerrie | 374e45e | 2017-09-05 01:33:32 +0900 | [diff] [blame] | 533 | using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise. |
| 534 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 535 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 536 | // the copy. This never returns NULL, even if |this| itself is empty. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 537 | std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 538 | |
jhawkins@chromium.org | cbb2239 | 2012-05-10 06:54:27 +0900 | [diff] [blame] | 539 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 540 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 541 | // passed in dictionary takes precedence and data already present will be |
| 542 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 543 | // be freed any time after this call. |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 544 | void MergeDictionary(const DictionaryValue* dictionary); |
| 545 | |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 546 | // Swaps contents with the |other| dictionary. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 547 | void Swap(DictionaryValue* other); |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 548 | |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 549 | // This class provides an iterator over both keys and values in the |
| 550 | // dictionary. It can't be used to modify the dictionary. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 551 | // DEPRECATED, use Value::DictItems() instead. |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 552 | class BASE_EXPORT Iterator { |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 553 | public: |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 554 | explicit Iterator(const DictionaryValue& target); |
vmpstr | c8651bf | 2016-02-25 09:50:31 +0900 | [diff] [blame] | 555 | Iterator(const Iterator& other); |
phajdan.jr@chromium.org | 8e2b208 | 2013-12-11 03:52:22 +0900 | [diff] [blame] | 556 | ~Iterator(); |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 557 | |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 558 | bool IsAtEnd() const { return it_ == target_.dict_.end(); } |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 559 | void Advance() { ++it_; } |
| 560 | |
| 561 | const std::string& key() const { return it_->first; } |
| 562 | const Value& value() const { return *it_->second; } |
| 563 | |
| 564 | private: |
| 565 | const DictionaryValue& target_; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 566 | DictStorage::const_iterator it_; |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 567 | }; |
| 568 | |
Johan Tibell | b6751c8 | 2017-05-17 14:21:12 +0900 | [diff] [blame] | 569 | // Iteration. |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 570 | // DEPRECATED, use Value::DictItems() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 571 | iterator begin() { return dict_.begin(); } |
| 572 | iterator end() { return dict_.end(); } |
Johan Tibell | b6751c8 | 2017-05-17 14:21:12 +0900 | [diff] [blame] | 573 | |
jdoerrie | bc7c44e | 2017-08-25 05:44:36 +0900 | [diff] [blame] | 574 | // DEPRECATED, use Value::DictItems() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 575 | const_iterator begin() const { return dict_.begin(); } |
| 576 | const_iterator end() const { return dict_.end(); } |
Johan Tibell | b6751c8 | 2017-05-17 14:21:12 +0900 | [diff] [blame] | 577 | |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 578 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 579 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 580 | DictionaryValue* DeepCopy() const; |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 581 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 582 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 583 | std::unique_ptr<DictionaryValue> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | // This type of Value represents a list of other Value values. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 587 | class BASE_EXPORT ListValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 588 | public: |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 589 | using const_iterator = ListStorage::const_iterator; |
| 590 | using iterator = ListStorage::iterator; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 591 | |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 592 | // Returns |value| if it is a list, nullptr otherwise. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 593 | static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 594 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 595 | ListValue(); |
jdoerrie | 41be5e8 | 2017-04-27 03:19:42 +0900 | [diff] [blame] | 596 | explicit ListValue(const ListStorage& in_list); |
| 597 | explicit ListValue(ListStorage&& in_list) noexcept; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 598 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 599 | // Clears the contents of this ListValue |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 600 | // DEPRECATED, use GetList()::clear() instead. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 601 | void Clear(); |
| 602 | |
| 603 | // Returns the number of Values in this list. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 604 | // DEPRECATED, use GetList()::size() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 605 | size_t GetSize() const { return list_.size(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 606 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 607 | // Returns whether the list is empty. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 608 | // DEPRECATED, use GetList()::empty() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 609 | bool empty() const { return list_.empty(); } |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 610 | |
jdoerrie | 4518400 | 2017-04-12 03:09:14 +0900 | [diff] [blame] | 611 | // Reserves storage for at least |n| values. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 612 | // DEPRECATED, use GetList()::reserve() instead. |
jdoerrie | 4518400 | 2017-04-12 03:09:14 +0900 | [diff] [blame] | 613 | void Reserve(size_t n); |
| 614 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 615 | // Sets the list item at the given index to be the Value specified by |
| 616 | // the value given. If the index beyond the current end of the list, null |
| 617 | // Values will be used to pad out the list. |
| 618 | // Returns true if successful, or false if the index was negative or |
| 619 | // the value is a null pointer. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 620 | // DEPRECATED, use GetList()::operator[] instead. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 621 | bool Set(size_t index, std::unique_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 622 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 623 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 624 | // only if the index falls within the current list range. |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 625 | // Note that the list always owns the Value passed out via |out_value|. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 626 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 627 | // DEPRECATED, use GetList()::operator[] instead. |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 628 | bool Get(size_t index, const Value** out_value) const; |
| 629 | bool Get(size_t index, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 630 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 631 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 632 | // only if the index is valid and the Value at that index can be returned |
| 633 | // in the specified form. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 634 | // |out_value| is optional and will only be set if non-NULL. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 635 | // DEPRECATED, use GetList()::operator[]::GetBool() instead. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 636 | bool GetBoolean(size_t index, bool* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 637 | // DEPRECATED, use GetList()::operator[]::GetInt() instead. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 638 | bool GetInteger(size_t index, int* out_value) const; |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 639 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 640 | // doubles. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 641 | // DEPRECATED, use GetList()::operator[]::GetDouble() instead. |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 642 | bool GetDouble(size_t index, double* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 643 | // DEPRECATED, use GetList()::operator[]::GetString() instead. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 644 | bool GetString(size_t index, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 645 | bool GetString(size_t index, string16* out_value) const; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 646 | |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 647 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 648 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
jdoerrie | 41be5e8 | 2017-04-27 03:19:42 +0900 | [diff] [blame] | 649 | |
| 650 | using Value::GetList; |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 651 | // DEPRECATED, use GetList()::operator[]::GetList() instead. |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 652 | bool GetList(size_t index, const ListValue** out_value) const; |
| 653 | bool GetList(size_t index, ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 654 | |
| 655 | // Removes the Value with the specified index from this list. |
| 656 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
ericroman@google.com | 3d19ee5 | 2009-01-06 03:37:51 +0900 | [diff] [blame] | 657 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 658 | // be deleted. This method returns true if |index| is valid; otherwise |
| 659 | // it will return false and the ListValue object will be unchanged. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 660 | // DEPRECATED, use GetList()::erase() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 661 | bool Remove(size_t index, std::unique_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 662 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 663 | // Removes the first instance of |value| found in the list, if any, and |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 664 | // deletes it. |index| is the location where |value| was found. Returns false |
| 665 | // if not found. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 666 | // DEPRECATED, use GetList()::erase() instead. |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 667 | bool Remove(const Value& value, size_t* index); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 668 | |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 669 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
| 670 | // deleted, otherwise ownership of the value is passed back to the caller. |
dubroy@chromium.org | af2a396 | 2013-02-19 01:31:45 +0900 | [diff] [blame] | 671 | // Returns an iterator pointing to the location of the element that |
| 672 | // followed the erased element. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 673 | // DEPRECATED, use GetList()::erase() instead. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 674 | iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 675 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 676 | // Appends a Value to the end of the list. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 677 | // DEPRECATED, use GetList()::push_back() instead. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 678 | void Append(std::unique_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 679 | |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 680 | // Convenience forms of Append. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 681 | // DEPRECATED, use GetList()::emplace_back() instead. |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 682 | void AppendBoolean(bool in_value); |
| 683 | void AppendInteger(int in_value); |
| 684 | void AppendDouble(double in_value); |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 685 | void AppendString(StringPiece in_value); |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 686 | void AppendString(const string16& in_value); |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 687 | // DEPRECATED, use GetList()::emplace_back() in a loop instead. |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 688 | void AppendStrings(const std::vector<std::string>& in_values); |
| 689 | void AppendStrings(const std::vector<string16>& in_values); |
| 690 | |
dcheng | 46cc6ec | 2016-09-14 14:49:58 +0900 | [diff] [blame] | 691 | // Appends a Value if it's not already present. Returns true if successful, |
| 692 | // or false if the value was already |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 693 | // DEPRECATED, use std::find() with GetList()::push_back() instead. |
dcheng | 46cc6ec | 2016-09-14 14:49:58 +0900 | [diff] [blame] | 694 | bool AppendIfNotPresent(std::unique_ptr<Value> in_value); |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 695 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 696 | // Insert a Value at index. |
| 697 | // Returns true if successful, or false if the index was out of range. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 698 | // DEPRECATED, use GetList()::insert() instead. |
dcheng | 46cc6ec | 2016-09-14 14:49:58 +0900 | [diff] [blame] | 699 | bool Insert(size_t index, std::unique_ptr<Value> in_value); |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 700 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 701 | // Searches for the first instance of |value| in the list using the Equals |
| 702 | // method of the Value type. |
| 703 | // Returns a const_iterator to the found item or to end() if none exists. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 704 | // DEPRECATED, use std::find() instead. |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 705 | const_iterator Find(const Value& value) const; |
| 706 | |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 707 | // Swaps contents with the |other| list. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 708 | // DEPRECATED, use GetList()::swap() instead. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 709 | void Swap(ListValue* other); |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 710 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 711 | // Iteration. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 712 | // DEPRECATED, use GetList()::begin() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 713 | iterator begin() { return list_.begin(); } |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 714 | // DEPRECATED, use GetList()::end() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 715 | iterator end() { return list_.end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 716 | |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 717 | // DEPRECATED, use GetList()::begin() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 718 | const_iterator begin() const { return list_.begin(); } |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 719 | // DEPRECATED, use GetList()::end() instead. |
Daniel Cheng | d6513e1 | 2017-10-12 11:31:07 +0900 | [diff] [blame] | 720 | const_iterator end() const { return list_.end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 721 | |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 722 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 723 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 724 | ListValue* DeepCopy() const; |
jdoerrie | 062d0fc | 2017-08-23 23:12:30 +0900 | [diff] [blame] | 725 | // DEPRECATED, use Value::Clone() instead. |
jdoerrie | 849fea5 | 2017-06-06 20:48:43 +0900 | [diff] [blame] | 726 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 727 | std::unique_ptr<ListValue> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 728 | }; |
| 729 | |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 730 | // This interface is implemented by classes that know how to serialize |
| 731 | // Value objects. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 732 | class BASE_EXPORT ValueSerializer { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 733 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 734 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 735 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 736 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | // This interface is implemented by classes that know how to deserialize Value |
| 740 | // objects. |
| 741 | class BASE_EXPORT ValueDeserializer { |
| 742 | public: |
| 743 | virtual ~ValueDeserializer(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 744 | |
| 745 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 746 | // If the return value is non-NULL, the caller takes ownership of returned |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 747 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 748 | // error_code will be set with the underlying error. |
| 749 | // If |error_message| is non-null, it will be filled in with a formatted |
| 750 | // error message including the location of the error if appropriate. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 751 | virtual std::unique_ptr<Value> Deserialize(int* error_code, |
| 752 | std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 753 | }; |
| 754 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 755 | // Stream operator so Values can be used in assertion statements. In order that |
| 756 | // gtest uses this operator to print readable output on test failures, we must |
| 757 | // override each specific type. Otherwise, the default template implementation |
| 758 | // is preferred over an upcast. |
kalman@chromium.org | 829cf75 | 2012-09-12 12:39:35 +0900 | [diff] [blame] | 759 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 760 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 761 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 762 | const DictionaryValue& value) { |
| 763 | return out << static_cast<const Value&>(value); |
| 764 | } |
| 765 | |
| 766 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 767 | const ListValue& value) { |
| 768 | return out << static_cast<const Value&>(value); |
| 769 | } |
| 770 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 771 | // Stream operator so that enum class Types can be used in log statements. |
| 772 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 773 | const Value::Type& type); |
| 774 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 775 | } // namespace base |
| 776 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 777 | #endif // BASE_VALUES_H_ |