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