[email protected] | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 5 | // This file specifies a recursive data storage class called Value intended for |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 6 | // storing settings and other persistable data. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | // |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 8 | // A Value represents something that can be stored in JSON or passed to/from |
| 9 | // JavaScript. As such, it is NOT a generalized variant type, since only the |
| 10 | // types supported by JavaScript/JSON are supported. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | // |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | // IN PARTICULAR this means that there is no support for int64_t or unsigned |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 13 | // numbers. Writing JSON with such types would violate the spec. If you need |
| 14 | // something like this, either use a double or make a string value containing |
| 15 | // the number you want. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 17 | #ifndef BASE_VALUES_H_ |
| 18 | #define BASE_VALUES_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 20 | #include <stddef.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 21 | #include <stdint.h> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 22 | |
| 23 | #include <iosfwd> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | #include <map> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 25 | #include <memory> |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 26 | #include <string> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 27 | #include <utility> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 30 | #include "base/base_export.h" |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 31 | #include "base/compiler_specific.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 32 | #include "base/macros.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 33 | #include "base/strings/string16.h" |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 34 | #include "base/strings/string_piece.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 36 | namespace base { |
| 37 | |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 38 | class BinaryValue; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | class DictionaryValue; |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 40 | class FundamentalValue; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | class ListValue; |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 42 | class StringValue; |
| 43 | class Value; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 45 | // The Value class is the base class for Values. A Value can be instantiated |
| 46 | // via the Create*Value() factory methods, or by directly creating instances of |
| 47 | // the subclasses. |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 48 | // |
| 49 | // See the file-level comment above for more information. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 50 | class BASE_EXPORT Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | public: |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame^] | 52 | enum class Type { |
| 53 | NONE = 0, |
| 54 | BOOLEAN, |
| 55 | INTEGER, |
| 56 | DOUBLE, |
| 57 | STRING, |
| 58 | BINARY, |
| 59 | DICTIONARY, |
| 60 | LIST |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 61 | // Note: Do not add more types. See the file-level comment above for why. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 62 | }; |
| 63 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | virtual ~Value(); |
| 65 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 66 | static std::unique_ptr<Value> CreateNullValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | |
thestig | 6170924 | 2016-07-19 00:39:30 | [diff] [blame] | 68 | // Returns the name for a given |type|. |
| 69 | static const char* GetTypeName(Type type); |
| 70 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 71 | // Returns the type of the value stored by the current Value object. |
| 72 | // Each type will be implemented by only one subclass of Value, so it's |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 73 | // safe to use the Type to determine whether you can cast from |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | // Value* to (Implementing Class)*. Also, a Value object never changes |
| 75 | // its type after construction. |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 76 | Type GetType() const { return type_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | |
| 78 | // Returns true if the current object represents a given type. |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 79 | bool IsType(Type type) const { return type == type_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 81 | // These methods allow the convenient retrieval of the contents of the Value. |
| 82 | // If the current object can be converted into the given type, the value is |
| 83 | // returned through the |out_value| parameter and true is returned; |
| 84 | // otherwise, false is returned and |out_value| is unchanged. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | virtual bool GetAsBoolean(bool* out_value) const; |
| 86 | virtual bool GetAsInteger(int* out_value) const; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 87 | virtual bool GetAsDouble(double* out_value) const; |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 88 | virtual bool GetAsString(std::string* out_value) const; |
[email protected] | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 89 | virtual bool GetAsString(string16* out_value) const; |
[email protected] | c037331 | 2014-02-05 20:42:06 | [diff] [blame] | 90 | virtual bool GetAsString(const StringValue** out_value) const; |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 91 | virtual bool GetAsBinary(const BinaryValue** out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 92 | // ListValue::From is the equivalent for std::unique_ptr conversions. |
[email protected] | 81f9fe0b | 2010-12-07 00:35:29 | [diff] [blame] | 93 | virtual bool GetAsList(ListValue** out_value); |
[email protected] | 35552dc5 | 2011-07-12 09:04:38 | [diff] [blame] | 94 | virtual bool GetAsList(const ListValue** out_value) const; |
lukasza | d1485da7 | 2016-11-01 21:49:46 | [diff] [blame] | 95 | // DictionaryValue::From is the equivalent for std::unique_ptr conversions. |
[email protected] | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 96 | virtual bool GetAsDictionary(DictionaryValue** out_value); |
| 97 | virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
[email protected] | 2f03f53 | 2013-07-17 08:43:33 | [diff] [blame] | 98 | // Note: Do not add more types. See the file-level comment above for why. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | |
| 100 | // This creates a deep copy of the entire Value tree, and returns a pointer |
| 101 | // to the copy. The caller gets ownership of the copy, of course. |
[email protected] | 16f47e08 | 2011-01-18 02:16:59 | [diff] [blame] | 102 | // |
| 103 | // Subclasses return their own type directly in their overrides; |
| 104 | // this works because C++ supports covariant return types. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | virtual Value* DeepCopy() const; |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 106 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 107 | std::unique_ptr<Value> CreateDeepCopy() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | |
| 109 | // Compares if two Value objects have equal contents. |
| 110 | virtual bool Equals(const Value* other) const; |
| 111 | |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 112 | // Compares if two Value objects have equal contents. Can handle NULLs. |
| 113 | // NULLs are considered equal but different from Value::CreateNullValue(). |
| 114 | static bool Equals(const Value* a, const Value* b); |
| 115 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | protected: |
[email protected] | 09d7a3a | 2012-11-20 20:37:55 | [diff] [blame] | 117 | // These aren't safe for end-users, but they are useful for subclasses. |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 118 | explicit Value(Type type); |
[email protected] | 09d7a3a | 2012-11-20 20:37:55 | [diff] [blame] | 119 | Value(const Value& that); |
| 120 | Value& operator=(const Value& that); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | |
| 122 | private: |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 123 | Type type_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | // FundamentalValue represents the simple fundamental types of values. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 127 | class BASE_EXPORT FundamentalValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 129 | explicit FundamentalValue(bool in_value); |
| 130 | explicit FundamentalValue(int in_value); |
| 131 | explicit FundamentalValue(double in_value); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 132 | ~FundamentalValue() override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 134 | // Overridden from Value: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 135 | bool GetAsBoolean(bool* out_value) const override; |
| 136 | bool GetAsInteger(int* out_value) const override; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame^] | 137 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 138 | // doubles. |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 139 | bool GetAsDouble(double* out_value) const override; |
| 140 | FundamentalValue* DeepCopy() const override; |
| 141 | bool Equals(const Value* other) const override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | |
| 143 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 144 | union { |
| 145 | bool boolean_value_; |
| 146 | int integer_value_; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 147 | double double_value_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | }; |
| 149 | }; |
| 150 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 151 | class BASE_EXPORT StringValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | public: |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 153 | // Initializes a StringValue with a UTF-8 narrow character string. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 154 | explicit StringValue(StringPiece in_value); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 155 | |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 156 | // Initializes a StringValue with a string16. |
| 157 | explicit StringValue(const string16& in_value); |
[email protected] | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 158 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 159 | ~StringValue() override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | |
[email protected] | c037331 | 2014-02-05 20:42:06 | [diff] [blame] | 161 | // Returns |value_| as a pointer or reference. |
| 162 | std::string* GetString(); |
| 163 | const std::string& GetString() const; |
| 164 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 165 | // Overridden from Value: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 166 | bool GetAsString(std::string* out_value) const override; |
| 167 | bool GetAsString(string16* out_value) const override; |
| 168 | bool GetAsString(const StringValue** out_value) const override; |
| 169 | StringValue* DeepCopy() const override; |
| 170 | bool Equals(const Value* other) const override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 171 | |
| 172 | private: |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 173 | std::string value_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | }; |
| 175 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 176 | class BASE_EXPORT BinaryValue: public Value { |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 177 | public: |
[email protected] | 2d6504b7 | 2013-01-08 03:16:22 | [diff] [blame] | 178 | // Creates a BinaryValue with a null buffer and size of 0. |
| 179 | BinaryValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 180 | |
[email protected] | 2d6504b7 | 2013-01-08 03:16:22 | [diff] [blame] | 181 | // Creates a BinaryValue, taking ownership of the bytes pointed to by |
| 182 | // |buffer|. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 183 | BinaryValue(std::unique_ptr<char[]> buffer, size_t size); |
[email protected] | 2d6504b7 | 2013-01-08 03:16:22 | [diff] [blame] | 184 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 185 | ~BinaryValue() override; |
[email protected] | 0d0ed0c | 2012-05-20 02:34:57 | [diff] [blame] | 186 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | // For situations where you want to keep ownership of your buffer, this |
| 188 | // factory method creates a new BinaryValue by copying the contents of the |
| 189 | // buffer that's passed in. |
dcheng | 338b8829 | 2016-06-16 10:48:42 | [diff] [blame] | 190 | static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer, |
| 191 | size_t size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 192 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 193 | size_t GetSize() const { return size_; } |
[email protected] | 2d6504b7 | 2013-01-08 03:16:22 | [diff] [blame] | 194 | |
| 195 | // May return NULL. |
| 196 | char* GetBuffer() { return buffer_.get(); } |
| 197 | const char* GetBuffer() const { return buffer_.get(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 199 | // Overridden from Value: |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 200 | bool GetAsBinary(const BinaryValue** out_value) const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 201 | BinaryValue* DeepCopy() const override; |
| 202 | bool Equals(const Value* other) const override; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 203 | |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 204 | private: |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 205 | std::unique_ptr<char[]> buffer_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | size_t size_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 207 | |
| 208 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | }; |
| 210 | |
[email protected] | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 211 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 212 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 213 | // are |std::string|s and should be UTF-8 encoded. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 214 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | public: |
dcheng | cb60e70 | 2016-05-25 18:30:47 | [diff] [blame] | 216 | using Storage = std::map<std::string, std::unique_ptr<Value>>; |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 217 | // Returns |value| if it is a dictionary, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 218 | static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 219 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 220 | DictionaryValue(); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 221 | ~DictionaryValue() override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 222 | |
[email protected] | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 223 | // Overridden from Value: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 224 | bool GetAsDictionary(DictionaryValue** out_value) override; |
| 225 | bool GetAsDictionary(const DictionaryValue** out_value) const override; |
[email protected] | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 226 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 227 | // Returns true if the current dictionary has a value for the given key. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 228 | bool HasKey(StringPiece key) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 229 | |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 230 | // Returns the number of Values in this dictionary. |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 231 | size_t size() const { return dictionary_.size(); } |
| 232 | |
| 233 | // Returns whether the dictionary is empty. |
| 234 | bool empty() const { return dictionary_.empty(); } |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 235 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | // Clears any current contents of this dictionary. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 237 | void Clear(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | |
| 239 | // Sets the Value associated with the given path starting from this object. |
| 240 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 241 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 242 | // within a key, but there are no other restrictions on keys. |
| 243 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 244 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 245 | // to the path in that location. |in_value| must be non-null. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 246 | void Set(StringPiece path, std::unique_ptr<Value> in_value); |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 247 | // Deprecated version of the above. TODO(estade): remove. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 248 | void Set(StringPiece path, Value* in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 249 | |
| 250 | // Convenience forms of Set(). These methods will replace any existing |
| 251 | // value at that path, even if it has a different type. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 252 | void SetBoolean(StringPiece path, bool in_value); |
| 253 | void SetInteger(StringPiece path, int in_value); |
| 254 | void SetDouble(StringPiece path, double in_value); |
| 255 | void SetString(StringPiece path, StringPiece in_value); |
| 256 | void SetString(StringPiece path, const string16& in_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 257 | |
| 258 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 259 | // be used as paths. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 260 | void SetWithoutPathExpansion(StringPiece key, |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 261 | std::unique_ptr<Value> in_value); |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 262 | // Deprecated version of the above. TODO(estade): remove. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 263 | void SetWithoutPathExpansion(StringPiece key, Value* in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 264 | |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 265 | // Convenience forms of SetWithoutPathExpansion(). |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 266 | void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value); |
| 267 | void SetIntegerWithoutPathExpansion(StringPiece path, int in_value); |
| 268 | void SetDoubleWithoutPathExpansion(StringPiece path, double in_value); |
| 269 | void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value); |
| 270 | void SetStringWithoutPathExpansion(StringPiece path, |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 271 | const string16& in_value); |
| 272 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 273 | // Gets the Value associated with the given path starting from this object. |
| 274 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 275 | // into the next DictionaryValue down. If the path can be resolved |
| 276 | // successfully, the value for the last key in the path will be returned |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 277 | // through the |out_value| parameter, and the function will return true. |
| 278 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | // Note that the dictionary always owns the value that's returned. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 280 | // |out_value| is optional and will only be set if non-NULL. |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 281 | bool Get(StringPiece path, const Value** out_value) const; |
| 282 | bool Get(StringPiece path, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 283 | |
| 284 | // These are convenience forms of Get(). The value will be retrieved |
| 285 | // and the return value will be true if the path is valid and the value at |
| 286 | // the end of the path can be returned in the form specified. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 287 | // |out_value| is optional and will only be set if non-NULL. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 288 | bool GetBoolean(StringPiece path, bool* out_value) const; |
| 289 | bool GetInteger(StringPiece path, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame^] | 290 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 291 | // doubles. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 292 | bool GetDouble(StringPiece path, double* out_value) const; |
| 293 | bool GetString(StringPiece path, std::string* out_value) const; |
| 294 | bool GetString(StringPiece path, string16* out_value) const; |
| 295 | bool GetStringASCII(StringPiece path, std::string* out_value) const; |
| 296 | bool GetBinary(StringPiece path, const BinaryValue** out_value) const; |
| 297 | bool GetBinary(StringPiece path, BinaryValue** out_value); |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 298 | bool GetDictionary(StringPiece path, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 299 | const DictionaryValue** out_value) const; |
asvitkine | dbd26533e | 2015-06-23 18:22:52 | [diff] [blame] | 300 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 301 | bool GetList(StringPiece path, const ListValue** out_value) const; |
| 302 | bool GetList(StringPiece path, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 304 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 305 | // be used as paths. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 306 | bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const; |
| 307 | bool GetWithoutPathExpansion(StringPiece key, Value** out_value); |
| 308 | bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const; |
| 309 | bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const; |
| 310 | bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const; |
| 311 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 312 | std::string* out_value) const; |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 313 | bool GetStringWithoutPathExpansion(StringPiece key, |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 314 | string16* out_value) const; |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 315 | bool GetDictionaryWithoutPathExpansion( |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 316 | StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 317 | const DictionaryValue** out_value) const; |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 318 | bool GetDictionaryWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 319 | DictionaryValue** out_value); |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 320 | bool GetListWithoutPathExpansion(StringPiece key, |
[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 321 | const ListValue** out_value) const; |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 322 | bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 323 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 324 | // Removes the Value with the specified path from this dictionary (or one |
| 325 | // of its child dictionaries, if the path is more than just a local key). |
[email protected] | d814a885 | 2013-08-06 13:33:04 | [diff] [blame] | 326 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 327 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 328 | // This method returns true if |path| is a valid path; otherwise it will |
| 329 | // return false and the DictionaryValue object will be unchanged. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 330 | bool Remove(StringPiece path, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 331 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 332 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 333 | // to be used as paths. |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 334 | virtual bool RemoveWithoutPathExpansion(StringPiece key, |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 335 | std::unique_ptr<Value>* out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 336 | |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 337 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 338 | // after removing the value at |path|. |
dcheng | 5f9cf76 | 2016-11-29 05:30:31 | [diff] [blame] | 339 | bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 340 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 341 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 342 | // the copy. This never returns NULL, even if |this| itself is empty. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 343 | std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 344 | |
[email protected] | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 345 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 346 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 347 | // passed in dictionary takes precedence and data already present will be |
| 348 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 349 | // be freed any time after this call. |
[email protected] | c378cca | 2010-05-14 13:17:40 | [diff] [blame] | 350 | void MergeDictionary(const DictionaryValue* dictionary); |
| 351 | |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 352 | // Swaps contents with the |other| dictionary. |
[email protected] | 6e680cf | 2012-05-16 15:23:30 | [diff] [blame] | 353 | virtual void Swap(DictionaryValue* other); |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 354 | |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 355 | // This class provides an iterator over both keys and values in the |
| 356 | // dictionary. It can't be used to modify the dictionary. |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 357 | class BASE_EXPORT Iterator { |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 358 | public: |
[email protected] | a34cc09 | 2012-08-10 12:45:35 | [diff] [blame] | 359 | explicit Iterator(const DictionaryValue& target); |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 360 | Iterator(const Iterator& other); |
[email protected] | a8d94b4 | 2013-12-10 18:52:22 | [diff] [blame] | 361 | ~Iterator(); |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 362 | |
[email protected] | a899c0b0 | 2013-01-18 14:43:27 | [diff] [blame] | 363 | bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 364 | void Advance() { ++it_; } |
| 365 | |
| 366 | const std::string& key() const { return it_->first; } |
| 367 | const Value& value() const { return *it_->second; } |
| 368 | |
| 369 | private: |
| 370 | const DictionaryValue& target_; |
dcheng | cb60e70 | 2016-05-25 18:30:47 | [diff] [blame] | 371 | Storage::const_iterator it_; |
[email protected] | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 372 | }; |
| 373 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 374 | // Overridden from Value: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 375 | DictionaryValue* DeepCopy() const override; |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 376 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 377 | std::unique_ptr<DictionaryValue> CreateDeepCopy() const; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 378 | bool Equals(const Value* other) const override; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 379 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 380 | private: |
dcheng | cb60e70 | 2016-05-25 18:30:47 | [diff] [blame] | 381 | Storage dictionary_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 382 | |
| 383 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | // This type of Value represents a list of other Value values. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 387 | class BASE_EXPORT ListValue : public Value { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | public: |
dcheng | cb60e70 | 2016-05-25 18:30:47 | [diff] [blame] | 389 | using Storage = std::vector<std::unique_ptr<Value>>; |
| 390 | using const_iterator = Storage::const_iterator; |
| 391 | using iterator = Storage::iterator; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 392 | |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 393 | // Returns |value| if it is a list, nullptr otherwise. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 394 | static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); |
reillyg | 259c0a3 | 2015-09-11 00:25:54 | [diff] [blame] | 395 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 396 | ListValue(); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 397 | ~ListValue() override; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 398 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | // Clears the contents of this ListValue |
| 400 | void Clear(); |
| 401 | |
| 402 | // Returns the number of Values in this list. |
| 403 | size_t GetSize() const { return list_.size(); } |
| 404 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 405 | // Returns whether the list is empty. |
| 406 | bool empty() const { return list_.empty(); } |
| 407 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | // Sets the list item at the given index to be the Value specified by |
| 409 | // the value given. If the index beyond the current end of the list, null |
| 410 | // Values will be used to pad out the list. |
| 411 | // Returns true if successful, or false if the index was negative or |
| 412 | // the value is a null pointer. |
| 413 | bool Set(size_t index, Value* in_value); |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 414 | // Preferred version of the above. TODO(estade): remove the above. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 415 | bool Set(size_t index, std::unique_ptr<Value> in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 416 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 417 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 418 | // only if the index falls within the current list range. |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 419 | // Note that the list always owns the Value passed out via |out_value|. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 420 | // |out_value| is optional and will only be set if non-NULL. |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 421 | bool Get(size_t index, const Value** out_value) const; |
| 422 | bool Get(size_t index, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 424 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 425 | // only if the index is valid and the Value at that index can be returned |
| 426 | // in the specified form. |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 427 | // |out_value| is optional and will only be set if non-NULL. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 428 | bool GetBoolean(size_t index, bool* out_value) const; |
| 429 | bool GetInteger(size_t index, int* out_value) const; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame^] | 430 | // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as |
[email protected] | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 431 | // doubles. |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 432 | bool GetDouble(size_t index, double* out_value) const; |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 433 | bool GetString(size_t index, std::string* out_value) const; |
[email protected] | 698f7f4 | 2010-08-04 19:35:33 | [diff] [blame] | 434 | bool GetString(size_t index, string16* out_value) const; |
[email protected] | 5d30f92bf | 2012-08-03 08:43:37 | [diff] [blame] | 435 | bool GetBinary(size_t index, const BinaryValue** out_value) const; |
| 436 | bool GetBinary(size_t index, BinaryValue** out_value); |
| 437 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
| 438 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
| 439 | bool GetList(size_t index, const ListValue** out_value) const; |
| 440 | bool GetList(size_t index, ListValue** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 441 | |
| 442 | // Removes the Value with the specified index from this list. |
| 443 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
[email protected] | b930d13 | 2009-01-05 18:37:51 | [diff] [blame] | 444 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 445 | // be deleted. This method returns true if |index| is valid; otherwise |
| 446 | // it will return false and the ListValue object will be unchanged. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 447 | virtual bool Remove(size_t index, std::unique_ptr<Value>* out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 448 | |
[email protected] | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 449 | // Removes the first instance of |value| found in the list, if any, and |
[email protected] | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 450 | // deletes it. |index| is the location where |value| was found. Returns false |
| 451 | // if not found. |
| 452 | bool Remove(const Value& value, size_t* index); |
[email protected] | e7f5c6f | 2009-05-09 00:33:04 | [diff] [blame] | 453 | |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 454 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
| 455 | // deleted, otherwise ownership of the value is passed back to the caller. |
[email protected] | a8d379cc | 2013-02-18 16:31:45 | [diff] [blame] | 456 | // Returns an iterator pointing to the location of the element that |
| 457 | // followed the erased element. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 458 | iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 459 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 460 | // Appends a Value to the end of the list. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 461 | void Append(std::unique_ptr<Value> in_value); |
dcheng | 23779e84 | 2016-10-17 02:19:00 | [diff] [blame] | 462 | #if !defined(OS_LINUX) |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 463 | // Deprecated version of the above. TODO(estade): remove. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 464 | void Append(Value* in_value); |
dcheng | 71bb845 | 2016-09-17 01:30:15 | [diff] [blame] | 465 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 466 | |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 467 | // Convenience forms of Append. |
| 468 | void AppendBoolean(bool in_value); |
| 469 | void AppendInteger(int in_value); |
| 470 | void AppendDouble(double in_value); |
dcheng | 16d6f53 | 2016-08-25 16:07:11 | [diff] [blame] | 471 | void AppendString(StringPiece in_value); |
[email protected] | 095812b | 2012-09-14 02:14:01 | [diff] [blame] | 472 | void AppendString(const string16& in_value); |
| 473 | void AppendStrings(const std::vector<std::string>& in_values); |
| 474 | void AppendStrings(const std::vector<string16>& in_values); |
| 475 | |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 476 | // Appends a Value if it's not already present. Returns true if successful, |
| 477 | // or false if the value was already |
| 478 | bool AppendIfNotPresent(std::unique_ptr<Value> in_value); |
[email protected] | 84064220 | 2010-04-12 21:48:10 | [diff] [blame] | 479 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 480 | // Insert a Value at index. |
| 481 | // Returns true if successful, or false if the index was out of range. |
dcheng | 66c7a4c | 2016-09-14 05:49:58 | [diff] [blame] | 482 | bool Insert(size_t index, std::unique_ptr<Value> in_value); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 483 | |
[email protected] | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 484 | // Searches for the first instance of |value| in the list using the Equals |
| 485 | // method of the Value type. |
| 486 | // Returns a const_iterator to the found item or to end() if none exists. |
| 487 | const_iterator Find(const Value& value) const; |
| 488 | |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 489 | // Swaps contents with the |other| list. |
[email protected] | 6e680cf | 2012-05-16 15:23:30 | [diff] [blame] | 490 | virtual void Swap(ListValue* other); |
[email protected] | 8b8e7c9 | 2010-08-19 18:05:56 | [diff] [blame] | 491 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 492 | // Iteration. |
| 493 | iterator begin() { return list_.begin(); } |
| 494 | iterator end() { return list_.end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 495 | |
[email protected] | e878919 | 2011-08-11 20:56:32 | [diff] [blame] | 496 | const_iterator begin() const { return list_.begin(); } |
| 497 | const_iterator end() const { return list_.end(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 498 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 499 | // Overridden from Value: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 500 | bool GetAsList(ListValue** out_value) override; |
| 501 | bool GetAsList(const ListValue** out_value) const override; |
| 502 | ListValue* DeepCopy() const override; |
| 503 | bool Equals(const Value* other) const override; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 504 | |
estade | a68b044 | 2015-05-12 18:11:50 | [diff] [blame] | 505 | // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 506 | std::unique_ptr<ListValue> CreateDeepCopy() const; |
estade | a68b044 | 2015-05-12 18:11:50 | [diff] [blame] | 507 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 508 | private: |
dcheng | cb60e70 | 2016-05-25 18:30:47 | [diff] [blame] | 509 | Storage list_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 510 | |
| 511 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 512 | }; |
| 513 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 514 | // This interface is implemented by classes that know how to serialize |
| 515 | // Value objects. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 516 | class BASE_EXPORT ValueSerializer { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 517 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 518 | virtual ~ValueSerializer(); |
[email protected] | abb9d0c | 2008-08-06 15:46:59 | [diff] [blame] | 519 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 520 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | // This interface is implemented by classes that know how to deserialize Value |
| 524 | // objects. |
| 525 | class BASE_EXPORT ValueDeserializer { |
| 526 | public: |
| 527 | virtual ~ValueDeserializer(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 528 | |
| 529 | // This method deserializes the subclass-specific format into a Value object. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 530 | // If the return value is non-NULL, the caller takes ownership of returned |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 531 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 532 | // error_code will be set with the underlying error. |
| 533 | // If |error_message| is non-null, it will be filled in with a formatted |
| 534 | // error message including the location of the error if appropriate. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 535 | virtual std::unique_ptr<Value> Deserialize(int* error_code, |
| 536 | std::string* error_str) = 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 537 | }; |
| 538 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 539 | // Stream operator so Values can be used in assertion statements. In order that |
| 540 | // gtest uses this operator to print readable output on test failures, we must |
| 541 | // override each specific type. Otherwise, the default template implementation |
| 542 | // is preferred over an upcast. |
[email protected] | e4ef831 | 2012-09-12 03:39:35 | [diff] [blame] | 543 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 544 | |
[email protected] | ea0ec05 | 2013-04-16 09:04:02 | [diff] [blame] | 545 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 546 | const FundamentalValue& value) { |
| 547 | return out << static_cast<const Value&>(value); |
| 548 | } |
| 549 | |
| 550 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 551 | const StringValue& value) { |
| 552 | return out << static_cast<const Value&>(value); |
| 553 | } |
| 554 | |
| 555 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 556 | const DictionaryValue& value) { |
| 557 | return out << static_cast<const Value&>(value); |
| 558 | } |
| 559 | |
| 560 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 561 | const ListValue& value) { |
| 562 | return out << static_cast<const Value&>(value); |
| 563 | } |
| 564 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame^] | 565 | // Stream operator so that enum class Types can be used in log statements. |
| 566 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 567 | const Value::Type& type); |
| 568 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 569 | } // namespace base |
| 570 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 571 | #endif // BASE_VALUES_H_ |