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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 16 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 17 | #ifndef BASE_VALUES_H_ |
| 18 | #define BASE_VALUES_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 20 | #include <stddef.h> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 21 | #include <stdint.h> |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 22 | |
| 23 | #include <iosfwd> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | #include <map> |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 25 | #include <memory> |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 26 | #include <string> |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 27 | #include <utility> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 30 | #include "base/base_export.h" |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 31 | #include "base/compiler_specific.h" |
mkwst | 62e159c | 2017-04-11 15:52:21 +0900 | [diff] [blame] | 32 | #include "base/containers/flat_map.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 33 | #include "base/macros.h" |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 34 | #include "base/memory/manual_constructor.h" |
avi@chromium.org | 68a745c | 2013-06-11 05:11:14 +0900 | [diff] [blame] | 35 | #include "base/strings/string16.h" |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 36 | #include "base/strings/string_piece.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 37 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 38 | namespace base { |
| 39 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 40 | class DictionaryValue; |
| 41 | class ListValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 42 | class Value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | |
tfarina@chromium.org | 2e2a902 | 2011-08-06 03:20:05 +0900 | [diff] [blame] | 44 | // The Value class is the base class for Values. A Value can be instantiated |
| 45 | // via the Create*Value() factory methods, or by directly creating instances of |
| 46 | // the subclasses. |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 47 | // |
| 48 | // See the file-level comment above for more information. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 49 | class BASE_EXPORT Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 50 | public: |
mkwst | 62e159c | 2017-04-11 15:52:21 +0900 | [diff] [blame] | 51 | using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; |
rouslan | 7ce5d82 | 2017-04-11 23:58:00 +0900 | [diff] [blame] | 52 | using ListStorage = std::vector<std::unique_ptr<Value>>; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 53 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 54 | enum class Type { |
| 55 | NONE = 0, |
| 56 | BOOLEAN, |
| 57 | INTEGER, |
| 58 | DOUBLE, |
| 59 | STRING, |
| 60 | BINARY, |
| 61 | DICTIONARY, |
| 62 | LIST |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 63 | // 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] | 64 | }; |
| 65 | |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 66 | // For situations where you want to keep ownership of your buffer, this |
| 67 | // factory method creates a new BinaryValue by copying the contents of the |
| 68 | // buffer that's passed in. |
| 69 | // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. |
| 70 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 71 | static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer, |
| 72 | size_t size); |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 73 | |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 74 | Value(const Value& that); |
brettw | ce38c19 | 2017-03-25 01:36:42 +0900 | [diff] [blame] | 75 | Value(Value&& that) noexcept; |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 76 | Value() noexcept; // A null value. |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 77 | explicit Value(Type type); |
| 78 | explicit Value(bool in_bool); |
| 79 | explicit Value(int in_int); |
| 80 | explicit Value(double in_double); |
| 81 | |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 82 | // Value(const char*) and Value(const char16*) are required despite |
| 83 | // Value(const std::string&) and Value(const string16&) because otherwise the |
| 84 | // compiler will choose the Value(bool) constructor for these arguments. |
| 85 | // Value(std::string&&) allow for efficient move construction. |
| 86 | // Value(StringPiece) exists due to many callsites passing StringPieces as |
| 87 | // arguments. |
| 88 | explicit Value(const char* in_string); |
| 89 | explicit Value(const std::string& in_string); |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 90 | explicit Value(std::string&& in_string) noexcept; |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 91 | explicit Value(const char16* in_string); |
| 92 | explicit Value(const string16& in_string); |
| 93 | explicit Value(StringPiece in_string); |
| 94 | |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 95 | explicit Value(const std::vector<char>& in_blob); |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 96 | explicit Value(std::vector<char>&& in_blob) noexcept; |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 97 | |
mkwst | 62e159c | 2017-04-11 15:52:21 +0900 | [diff] [blame] | 98 | explicit Value(DictStorage&& in_dict) noexcept; |
| 99 | |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 100 | Value& operator=(const Value& that); |
jdoerrie | 74f7680 | 2017-03-30 15:40:29 +0900 | [diff] [blame] | 101 | Value& operator=(Value&& that) noexcept; |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 102 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 103 | ~Value(); |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 104 | |
thestig | e44b932 | 2016-07-19 09:39:30 +0900 | [diff] [blame] | 105 | // Returns the name for a given |type|. |
| 106 | static const char* GetTypeName(Type type); |
| 107 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | // Returns the type of the value stored by the current Value object. |
| 109 | // Each type will be implemented by only one subclass of Value, so it's |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 110 | // safe to use the Type to determine whether you can cast from |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 111 | // Value* to (Implementing Class)*. Also, a Value object never changes |
| 112 | // its type after construction. |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 113 | Type GetType() const { return type_; } // DEPRECATED, use type(). |
| 114 | Type type() const { return type_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 115 | |
| 116 | // Returns true if the current object represents a given type. |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 117 | bool IsType(Type type) const { return type == type_; } |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 118 | bool is_bool() const { return type() == Type::BOOLEAN; } |
| 119 | bool is_int() const { return type() == Type::INTEGER; } |
| 120 | bool is_double() const { return type() == Type::DOUBLE; } |
| 121 | bool is_string() const { return type() == Type::STRING; } |
| 122 | bool is_blob() const { return type() == Type::BINARY; } |
| 123 | bool is_dict() const { return type() == Type::DICTIONARY; } |
| 124 | bool is_list() const { return type() == Type::LIST; } |
| 125 | |
| 126 | // These will all fatally assert if the type doesn't match. |
| 127 | bool GetBool() const; |
| 128 | int GetInt() const; |
| 129 | double GetDouble() const; // Implicitly converts from int if necessary. |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 130 | const std::string& GetString() const; |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 131 | const std::vector<char>& GetBlob() const; |
| 132 | |
| 133 | size_t GetSize() const; // DEPRECATED, use GetBlob().size() instead. |
| 134 | const char* GetBuffer() const; // DEPRECATED, use GetBlob().data() instead. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 135 | |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 136 | // These methods allow the convenient retrieval of the contents of the Value. |
| 137 | // If the current object can be converted into the given type, the value is |
| 138 | // returned through the |out_value| parameter and true is returned; |
| 139 | // otherwise, false is returned and |out_value| is unchanged. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 140 | bool GetAsBoolean(bool* out_value) const; |
| 141 | bool GetAsInteger(int* out_value) const; |
| 142 | bool GetAsDouble(double* out_value) const; |
| 143 | bool GetAsString(std::string* out_value) const; |
| 144 | bool GetAsString(string16* out_value) const; |
jdoerrie | 0d1295b | 2017-03-06 20:12:04 +0900 | [diff] [blame] | 145 | bool GetAsString(const Value** out_value) const; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 146 | bool GetAsString(StringPiece* out_value) const; |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 147 | bool GetAsBinary(const Value** out_value) const; |
lukasza | f0601bd | 2016-11-02 06:49:46 +0900 | [diff] [blame] | 148 | // ListValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 149 | bool GetAsList(ListValue** out_value); |
| 150 | bool GetAsList(const ListValue** out_value) const; |
lukasza | f0601bd | 2016-11-02 06:49:46 +0900 | [diff] [blame] | 151 | // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 152 | bool GetAsDictionary(DictionaryValue** out_value); |
| 153 | bool GetAsDictionary(const DictionaryValue** out_value) const; |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 154 | // 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] | 155 | |
| 156 | // 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] | 157 | // 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] | 158 | // Subclasses return their own type directly in their overrides; |
| 159 | // this works because C++ supports covariant return types. |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 160 | // DEPRECATED, use Value's copy constructor instead. |
| 161 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 162 | Value* DeepCopy() const; |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 163 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 164 | std::unique_ptr<Value> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 165 | |
jdoerrie | 046caee | 2017-03-29 02:52:00 +0900 | [diff] [blame] | 166 | // Comparison operators so that Values can easily be used with standard |
| 167 | // library algorithms and associative containers. |
| 168 | BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs); |
| 169 | BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs); |
| 170 | BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs); |
| 171 | BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs); |
| 172 | BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs); |
| 173 | BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs); |
| 174 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 175 | // Compares if two Value objects have equal contents. |
jdoerrie | 046caee | 2017-03-29 02:52:00 +0900 | [diff] [blame] | 176 | // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 177 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 178 | bool Equals(const Value* other) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 179 | |
bauerb@chromium.org | fbd55e1 | 2010-12-07 03:13:43 +0900 | [diff] [blame] | 180 | // Compares if two Value objects have equal contents. Can handle NULLs. |
jdoerrie | fcdbae7 | 2017-04-07 15:39:00 +0900 | [diff] [blame] | 181 | // NULLs are considered equal but different from Value(Value::Type::NONE). |
jdoerrie | 046caee | 2017-03-29 02:52:00 +0900 | [diff] [blame] | 182 | // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead. |
| 183 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
bauerb@chromium.org | fbd55e1 | 2010-12-07 03:13:43 +0900 | [diff] [blame] | 184 | static bool Equals(const Value* a, const Value* b); |
| 185 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 186 | protected: |
| 187 | // TODO(crbug.com/646113): Make these private once DictionaryValue and |
| 188 | // ListValue are properly inlined. |
jdoerrie | 34ee9f6 | 2017-02-02 19:56:03 +0900 | [diff] [blame] | 189 | Type type_; |
| 190 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 191 | union { |
jdoerrie | 93a0cf3 | 2017-02-01 19:36:56 +0900 | [diff] [blame] | 192 | bool bool_value_; |
| 193 | int int_value_; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 194 | double double_value_; |
jdoerrie | 3129913 | 2017-02-01 23:38:32 +0900 | [diff] [blame] | 195 | ManualConstructor<std::string> string_value_; |
jdoerrie | 58325a4 | 2017-02-15 17:42:14 +0900 | [diff] [blame] | 196 | ManualConstructor<std::vector<char>> binary_value_; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 197 | // For current gcc and clang sizeof(DictStorage) = 48, which would result |
| 198 | // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it |
| 199 | // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC. |
| 200 | ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_; |
| 201 | ManualConstructor<ListStorage> list_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 202 | }; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 203 | |
| 204 | private: |
| 205 | void InternalCopyFundamentalValue(const Value& that); |
| 206 | void InternalCopyConstructFrom(const Value& that); |
| 207 | void InternalMoveConstructFrom(Value&& that); |
vabr | 7eac835 | 2017-03-08 08:34:30 +0900 | [diff] [blame] | 208 | void InternalCopyAssignFromSameType(const Value& that); |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 209 | void InternalCleanup(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 210 | }; |
| 211 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 212 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 213 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 214 | // are |std::string|s and should be UTF-8 encoded. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 215 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 216 | public: |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 217 | // Returns |value| if it is a dictionary, nullptr otherwise. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 218 | static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 219 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 220 | DictionaryValue(); |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 221 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 222 | // Returns true if the current dictionary has a value for the given key. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 223 | bool HasKey(StringPiece key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 224 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 225 | // Returns the number of Values in this dictionary. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 226 | size_t size() const { return (*dict_ptr_)->size(); } |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 227 | |
| 228 | // Returns whether the dictionary is empty. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 229 | bool empty() const { return (*dict_ptr_)->empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 230 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 231 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 232 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 233 | |
| 234 | // Sets the Value associated with the given path starting from this object. |
| 235 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 236 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 237 | // within a key, but there are no other restrictions on keys. |
| 238 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 239 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 240 | // to the path in that location. |in_value| must be non-null. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 241 | void Set(StringPiece path, std::unique_ptr<Value> in_value); |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 242 | // Deprecated version of the above. TODO(estade): remove. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 243 | void Set(StringPiece path, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 244 | |
| 245 | // Convenience forms of Set(). These methods will replace any existing |
| 246 | // value at that path, even if it has a different type. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 247 | void SetBoolean(StringPiece path, bool in_value); |
| 248 | void SetInteger(StringPiece path, int in_value); |
| 249 | void SetDouble(StringPiece path, double in_value); |
| 250 | void SetString(StringPiece path, StringPiece in_value); |
| 251 | void SetString(StringPiece path, const string16& in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 252 | |
| 253 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 254 | // be used as paths. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 255 | void SetWithoutPathExpansion(StringPiece key, |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 256 | std::unique_ptr<Value> in_value); |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 257 | // Deprecated version of the above. TODO(estade): remove. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 258 | void SetWithoutPathExpansion(StringPiece key, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 259 | |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 260 | // Convenience forms of SetWithoutPathExpansion(). |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 261 | void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value); |
| 262 | void SetIntegerWithoutPathExpansion(StringPiece path, int in_value); |
| 263 | void SetDoubleWithoutPathExpansion(StringPiece path, double in_value); |
| 264 | void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value); |
| 265 | void SetStringWithoutPathExpansion(StringPiece path, |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 266 | const string16& in_value); |
| 267 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 268 | // Gets the Value associated with the given path starting from this object. |
| 269 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 270 | // into the next DictionaryValue down. If the path can be resolved |
| 271 | // 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] | 272 | // through the |out_value| parameter, and the function will return true. |
| 273 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 274 | // Note that the dictionary always owns the value that's returned. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 275 | // |out_value| is optional and will only be set if non-NULL. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 276 | bool Get(StringPiece path, const Value** out_value) const; |
| 277 | bool Get(StringPiece path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 278 | |
| 279 | // These are convenience forms of Get(). The value will be retrieved |
| 280 | // and the return value will be true if the path is valid and the value at |
| 281 | // 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] | 282 | // |out_value| is optional and will only be set if non-NULL. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 283 | bool GetBoolean(StringPiece path, bool* out_value) const; |
| 284 | bool GetInteger(StringPiece path, int* out_value) const; |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 285 | // 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] | 286 | // doubles. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 287 | bool GetDouble(StringPiece path, double* out_value) const; |
| 288 | bool GetString(StringPiece path, std::string* out_value) const; |
| 289 | bool GetString(StringPiece path, string16* out_value) const; |
| 290 | bool GetStringASCII(StringPiece path, std::string* out_value) const; |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 291 | bool GetBinary(StringPiece path, const Value** out_value) const; |
| 292 | bool GetBinary(StringPiece path, Value** out_value); |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 293 | bool GetDictionary(StringPiece path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 294 | const DictionaryValue** out_value) const; |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 295 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 296 | bool GetList(StringPiece path, const ListValue** out_value) const; |
| 297 | bool GetList(StringPiece path, ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 298 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 299 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 300 | // be used as paths. |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 301 | bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
| 302 | bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
| 303 | bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
| 304 | bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
| 305 | bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
| 306 | bool GetStringWithoutPathExpansion(StringPiece key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 307 | std::string* out_value) const; |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 308 | bool GetStringWithoutPathExpansion(StringPiece key, |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 309 | string16* out_value) const; |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 310 | bool GetDictionaryWithoutPathExpansion( |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 311 | StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 312 | const DictionaryValue** out_value) const; |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 313 | bool GetDictionaryWithoutPathExpansion(StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 314 | DictionaryValue** out_value); |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 315 | bool GetListWithoutPathExpansion(StringPiece key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 316 | const ListValue** out_value) const; |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 317 | bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 318 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 319 | // Removes the Value with the specified path from this dictionary (or one |
| 320 | // 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] | 321 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 322 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 323 | // This method returns true if |path| is a valid path; otherwise it will |
| 324 | // return false and the DictionaryValue object will be unchanged. |
dcheng | ba05520 | 2016-11-29 14:30:31 +0900 | [diff] [blame] | 325 | bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 326 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 327 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 328 | // to be used as paths. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 329 | bool RemoveWithoutPathExpansion(StringPiece key, |
| 330 | std::unique_ptr<Value>* out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 331 | |
gab@chromium.org | 903d5c9 | 2013-11-27 10:38:24 +0900 | [diff] [blame] | 332 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 333 | // after removing the value at |path|. |
dcheng | ba05520 | 2016-11-29 14:30:31 +0900 | [diff] [blame] | 334 | bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
gab@chromium.org | 903d5c9 | 2013-11-27 10:38:24 +0900 | [diff] [blame] | 335 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 336 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 337 | // the copy. This never returns NULL, even if |this| itself is empty. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 338 | std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 339 | |
jhawkins@chromium.org | cbb2239 | 2012-05-10 06:54:27 +0900 | [diff] [blame] | 340 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 341 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 342 | // passed in dictionary takes precedence and data already present will be |
| 343 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 344 | // be freed any time after this call. |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 345 | void MergeDictionary(const DictionaryValue* dictionary); |
| 346 | |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 347 | // Swaps contents with the |other| dictionary. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 348 | void Swap(DictionaryValue* other); |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 349 | |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 350 | // This class provides an iterator over both keys and values in the |
| 351 | // dictionary. It can't be used to modify the dictionary. |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 352 | class BASE_EXPORT Iterator { |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 353 | public: |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 354 | explicit Iterator(const DictionaryValue& target); |
vmpstr | c8651bf | 2016-02-25 09:50:31 +0900 | [diff] [blame] | 355 | Iterator(const Iterator& other); |
phajdan.jr@chromium.org | 8e2b208 | 2013-12-11 03:52:22 +0900 | [diff] [blame] | 356 | ~Iterator(); |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 357 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 358 | bool IsAtEnd() const { return it_ == (*target_.dict_ptr_)->end(); } |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 359 | void Advance() { ++it_; } |
| 360 | |
| 361 | const std::string& key() const { return it_->first; } |
| 362 | const Value& value() const { return *it_->second; } |
| 363 | |
| 364 | private: |
| 365 | const DictionaryValue& target_; |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 366 | DictStorage::const_iterator it_; |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 367 | }; |
| 368 | |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 369 | // DEPRECATED, use DictionaryValue's copy constructor instead. |
| 370 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 371 | DictionaryValue* DeepCopy() const; |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 372 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 373 | std::unique_ptr<DictionaryValue> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | // This type of Value represents a list of other Value values. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 377 | class BASE_EXPORT ListValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 378 | public: |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 379 | using const_iterator = ListStorage::const_iterator; |
| 380 | using iterator = ListStorage::iterator; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 381 | |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame] | 382 | // Returns |value| if it is a list, nullptr otherwise. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 383 | static std::unique_ptr<ListValue> 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 | ListValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 386 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 387 | // Clears the contents of this ListValue |
| 388 | void Clear(); |
| 389 | |
| 390 | // Returns the number of Values in this list. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 391 | size_t GetSize() const { return list_->size(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 392 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 393 | // Returns whether the list is empty. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 394 | bool empty() const { return list_->empty(); } |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 395 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 396 | // Sets the list item at the given index to be the Value specified by |
| 397 | // the value given. If the index beyond the current end of the list, null |
| 398 | // Values will be used to pad out the list. |
| 399 | // Returns true if successful, or false if the index was negative or |
| 400 | // the value is a null pointer. |
| 401 | bool Set(size_t index, Value* in_value); |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 402 | // Preferred version of the above. TODO(estade): remove the above. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 403 | bool Set(size_t index, std::unique_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 404 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 405 | // 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] | 406 | // only if the index falls within the current list range. |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 407 | // 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] | 408 | // |out_value| is optional and will only be set if non-NULL. |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 409 | bool Get(size_t index, const Value** out_value) const; |
| 410 | bool Get(size_t index, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 411 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 412 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 413 | // only if the index is valid and the Value at that index can be returned |
| 414 | // in the specified form. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 415 | // |out_value| is optional and will only be set if non-NULL. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 416 | bool GetBoolean(size_t index, bool* out_value) const; |
| 417 | bool GetInteger(size_t index, int* out_value) const; |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 418 | // 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] | 419 | // doubles. |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 420 | bool GetDouble(size_t index, double* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 421 | bool GetString(size_t index, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 422 | bool GetString(size_t index, string16* out_value) const; |
jdoerrie | c56cc7f | 2017-04-11 16:45:50 +0900 | [diff] [blame] | 423 | bool GetBinary(size_t index, const Value** out_value) const; |
| 424 | bool GetBinary(size_t index, Value** out_value); |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 425 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 426 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
| 427 | bool GetList(size_t index, const ListValue** out_value) const; |
| 428 | bool GetList(size_t index, ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 429 | |
| 430 | // Removes the Value with the specified index from this list. |
| 431 | // 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] | 432 | // 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] | 433 | // be deleted. This method returns true if |index| is valid; otherwise |
| 434 | // it will return false and the ListValue object will be unchanged. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 435 | bool Remove(size_t index, std::unique_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 436 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 437 | // 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] | 438 | // deletes it. |index| is the location where |value| was found. Returns false |
| 439 | // if not found. |
| 440 | bool Remove(const Value& value, size_t* index); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 441 | |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 442 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
| 443 | // 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] | 444 | // Returns an iterator pointing to the location of the element that |
| 445 | // followed the erased element. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 446 | iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 447 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 448 | // Appends a Value to the end of the list. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 449 | void Append(std::unique_ptr<Value> in_value); |
dcheng | d66aba8 | 2016-10-17 11:19:00 +0900 | [diff] [blame] | 450 | #if !defined(OS_LINUX) |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 451 | // Deprecated version of the above. TODO(estade): remove. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 452 | void Append(Value* in_value); |
dcheng | 17f21de | 2016-09-17 10:30:15 +0900 | [diff] [blame] | 453 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 454 | |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 455 | // Convenience forms of Append. |
| 456 | void AppendBoolean(bool in_value); |
| 457 | void AppendInteger(int in_value); |
| 458 | void AppendDouble(double in_value); |
dcheng | ae1c2d6 | 2016-08-26 01:07:11 +0900 | [diff] [blame] | 459 | void AppendString(StringPiece in_value); |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 460 | void AppendString(const string16& in_value); |
| 461 | void AppendStrings(const std::vector<std::string>& in_values); |
| 462 | void AppendStrings(const std::vector<string16>& in_values); |
| 463 | |
dcheng | 46cc6ec | 2016-09-14 14:49:58 +0900 | [diff] [blame] | 464 | // Appends a Value if it's not already present. Returns true if successful, |
| 465 | // or false if the value was already |
| 466 | bool AppendIfNotPresent(std::unique_ptr<Value> in_value); |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 467 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 468 | // Insert a Value at index. |
| 469 | // Returns true if successful, or false if the index was out of range. |
dcheng | 46cc6ec | 2016-09-14 14:49:58 +0900 | [diff] [blame] | 470 | bool Insert(size_t index, std::unique_ptr<Value> in_value); |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 471 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 472 | // Searches for the first instance of |value| in the list using the Equals |
| 473 | // method of the Value type. |
| 474 | // Returns a const_iterator to the found item or to end() if none exists. |
| 475 | const_iterator Find(const Value& value) const; |
| 476 | |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 477 | // Swaps contents with the |other| list. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 478 | void Swap(ListValue* other); |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 479 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 480 | // Iteration. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 481 | iterator begin() { return list_->begin(); } |
| 482 | iterator end() { return list_->end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 483 | |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 484 | const_iterator begin() const { return list_->begin(); } |
| 485 | const_iterator end() const { return list_->end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 486 | |
jdoerrie | 751d7d7 | 2017-04-05 15:44:17 +0900 | [diff] [blame] | 487 | // DEPRECATED, use ListValue's copy constructor instead. |
| 488 | // TODO(crbug.com/646113): Delete this and migrate callsites. |
jdoerrie | 1d5b348 | 2017-02-17 22:54:49 +0900 | [diff] [blame] | 489 | ListValue* DeepCopy() const; |
estade | 033e61e | 2015-05-13 03:11:50 +0900 | [diff] [blame] | 490 | // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 491 | std::unique_ptr<ListValue> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 492 | }; |
| 493 | |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 494 | // This interface is implemented by classes that know how to serialize |
| 495 | // Value objects. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 496 | class BASE_EXPORT ValueSerializer { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 497 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 498 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 499 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 500 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | // This interface is implemented by classes that know how to deserialize Value |
| 504 | // objects. |
| 505 | class BASE_EXPORT ValueDeserializer { |
| 506 | public: |
| 507 | virtual ~ValueDeserializer(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 508 | |
| 509 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 510 | // 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] | 511 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 512 | // error_code will be set with the underlying error. |
| 513 | // If |error_message| is non-null, it will be filled in with a formatted |
| 514 | // error message including the location of the error if appropriate. |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 515 | virtual std::unique_ptr<Value> Deserialize(int* error_code, |
| 516 | std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 517 | }; |
| 518 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 519 | // Stream operator so Values can be used in assertion statements. In order that |
| 520 | // gtest uses this operator to print readable output on test failures, we must |
| 521 | // override each specific type. Otherwise, the default template implementation |
| 522 | // is preferred over an upcast. |
kalman@chromium.org | 829cf75 | 2012-09-12 12:39:35 +0900 | [diff] [blame] | 523 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 524 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 525 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 526 | const DictionaryValue& value) { |
| 527 | return out << static_cast<const Value&>(value); |
| 528 | } |
| 529 | |
| 530 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 531 | const ListValue& value) { |
| 532 | return out << static_cast<const Value&>(value); |
| 533 | } |
| 534 | |
jdoerrie | 89ee31a | 2016-12-08 00:43:28 +0900 | [diff] [blame] | 535 | // Stream operator so that enum class Types can be used in log statements. |
| 536 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 537 | const Value::Type& type); |
| 538 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 539 | } // namespace base |
| 540 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 541 | #endif // BASE_VALUES_H_ |