jhawkins@chromium.org | cbb2239 | 2012-05-10 06:54:27 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 5 | // This file specifies a recursive data storage class called Value intended for |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 6 | // storing settings and other persistable data. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | // |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 8 | // A Value represents something that can be stored in JSON or passed to/from |
| 9 | // JavaScript. As such, it is NOT a generalized variant type, since only the |
| 10 | // types supported by JavaScript/JSON are supported. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | // |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 16 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 17 | #ifndef BASE_VALUES_H_ |
| 18 | #define BASE_VALUES_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
| 22 | #include <iosfwd> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | #include <map> |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 24 | #include <string> |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 25 | #include <utility> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 28 | #include "base/base_export.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 29 | #include "base/basictypes.h" |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 30 | #include "base/compiler_specific.h" |
rpaquay@chromium.org | 7b1dfba | 2013-01-08 12:16:22 +0900 | [diff] [blame] | 31 | #include "base/memory/scoped_ptr.h" |
avi@chromium.org | 68a745c | 2013-06-11 05:11:14 +0900 | [diff] [blame] | 32 | #include "base/strings/string16.h" |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 33 | #include "base/strings/string_piece.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 35 | namespace base { |
| 36 | |
pneubeck | b57f3f5 | 2015-01-20 20:26:36 +0900 | [diff] [blame] | 37 | class BinaryValue; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 38 | class DictionaryValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 39 | class FundamentalValue; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 40 | class ListValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 41 | class StringValue; |
| 42 | class Value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | |
| 44 | typedef std::vector<Value*> ValueVector; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 45 | typedef std::map<std::string, Value*> ValueMap; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | |
tfarina@chromium.org | 2e2a902 | 2011-08-06 03:20:05 +0900 | [diff] [blame] | 47 | // The Value class is the base class for Values. A Value can be instantiated |
| 48 | // via the Create*Value() factory methods, or by directly creating instances of |
| 49 | // the subclasses. |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 50 | // |
| 51 | // See the file-level comment above for more information. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 52 | class BASE_EXPORT Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 53 | public: |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 54 | enum Type { |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 55 | TYPE_NULL = 0, |
| 56 | TYPE_BOOLEAN, |
| 57 | TYPE_INTEGER, |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 58 | TYPE_DOUBLE, |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 59 | TYPE_STRING, |
| 60 | TYPE_BINARY, |
| 61 | TYPE_DICTIONARY, |
| 62 | TYPE_LIST |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 63 | // Note: Do not add more types. See the file-level comment above for why. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | virtual ~Value(); |
| 67 | |
estade | 033e61e | 2015-05-13 03:11:50 +0900 | [diff] [blame] | 68 | static scoped_ptr<Value> CreateNullValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 69 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 70 | // Returns the type of the value stored by the current Value object. |
| 71 | // Each type will be implemented by only one subclass of Value, so it's |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 72 | // safe to use the Type to determine whether you can cast from |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 73 | // Value* to (Implementing Class)*. Also, a Value object never changes |
| 74 | // its type after construction. |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 75 | Type GetType() const { return type_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 76 | |
| 77 | // Returns true if the current object represents a given type. |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 78 | bool IsType(Type type) const { return type == type_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 79 | |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 80 | // These methods allow the convenient retrieval of the contents of the Value. |
| 81 | // If the current object can be converted into the given type, the value is |
| 82 | // returned through the |out_value| parameter and true is returned; |
| 83 | // otherwise, false is returned and |out_value| is unchanged. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 84 | virtual bool GetAsBoolean(bool* out_value) const; |
| 85 | virtual bool GetAsInteger(int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 86 | virtual bool GetAsDouble(double* out_value) const; |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 87 | virtual bool GetAsString(std::string* out_value) const; |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 88 | virtual bool GetAsString(string16* out_value) const; |
estade@chromium.org | 0fd309d | 2014-02-06 05:42:06 +0900 | [diff] [blame] | 89 | virtual bool GetAsString(const StringValue** out_value) const; |
pneubeck | b57f3f5 | 2015-01-20 20:26:36 +0900 | [diff] [blame] | 90 | virtual bool GetAsBinary(const BinaryValue** out_value) const; |
scottbyer@google.com | 673b876 | 2010-12-07 09:35:29 +0900 | [diff] [blame] | 91 | virtual bool GetAsList(ListValue** out_value); |
bauerb@chromium.org | 6878e50 | 2011-07-12 18:04:38 +0900 | [diff] [blame] | 92 | virtual bool GetAsList(const ListValue** out_value) const; |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 93 | virtual bool GetAsDictionary(DictionaryValue** out_value); |
| 94 | virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
brettw@chromium.org | 1037483 | 2013-07-17 17:43:33 +0900 | [diff] [blame] | 95 | // Note: Do not add more types. See the file-level comment above for why. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 96 | |
| 97 | // This creates a deep copy of the entire Value tree, and returns a pointer |
| 98 | // to the copy. The caller gets ownership of the copy, of course. |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 99 | // |
| 100 | // Subclasses return their own type directly in their overrides; |
| 101 | // this works because C++ supports covariant return types. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 102 | virtual Value* DeepCopy() const; |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 103 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
| 104 | scoped_ptr<Value> CreateDeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 105 | |
| 106 | // Compares if two Value objects have equal contents. |
| 107 | virtual bool Equals(const Value* other) const; |
| 108 | |
bauerb@chromium.org | fbd55e1 | 2010-12-07 03:13:43 +0900 | [diff] [blame] | 109 | // Compares if two Value objects have equal contents. Can handle NULLs. |
| 110 | // NULLs are considered equal but different from Value::CreateNullValue(). |
| 111 | static bool Equals(const Value* a, const Value* b); |
| 112 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 113 | protected: |
estade@chromium.org | e2d2b72 | 2012-11-21 05:37:55 +0900 | [diff] [blame] | 114 | // These aren't safe for end-users, but they are useful for subclasses. |
tfarina@chromium.org | 254780a | 2011-08-13 05:59:02 +0900 | [diff] [blame] | 115 | explicit Value(Type type); |
estade@chromium.org | e2d2b72 | 2012-11-21 05:37:55 +0900 | [diff] [blame] | 116 | Value(const Value& that); |
| 117 | Value& operator=(const Value& that); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 118 | |
| 119 | private: |
sky@chromium.org | 8d103c7 | 2011-08-25 06:17:59 +0900 | [diff] [blame] | 120 | Type type_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // FundamentalValue represents the simple fundamental types of values. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 124 | class BASE_EXPORT FundamentalValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 125 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 126 | explicit FundamentalValue(bool in_value); |
| 127 | explicit FundamentalValue(int in_value); |
| 128 | explicit FundamentalValue(double in_value); |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 129 | ~FundamentalValue() override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 130 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 131 | // Overridden from Value: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 132 | bool GetAsBoolean(bool* out_value) const override; |
| 133 | bool GetAsInteger(int* out_value) const override; |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 134 | // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 135 | // doubles. |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 136 | bool GetAsDouble(double* out_value) const override; |
| 137 | FundamentalValue* DeepCopy() const override; |
| 138 | bool Equals(const Value* other) const override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 139 | |
| 140 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 141 | union { |
| 142 | bool boolean_value_; |
| 143 | int integer_value_; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 144 | double double_value_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 145 | }; |
| 146 | }; |
| 147 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 148 | class BASE_EXPORT StringValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 149 | public: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 150 | // Initializes a StringValue with a UTF-8 narrow character string. |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 151 | explicit StringValue(const std::string& in_value); |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 152 | |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 153 | // Initializes a StringValue with a string16. |
| 154 | explicit StringValue(const string16& in_value); |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 155 | |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 156 | ~StringValue() override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 157 | |
estade@chromium.org | 0fd309d | 2014-02-06 05:42:06 +0900 | [diff] [blame] | 158 | // Returns |value_| as a pointer or reference. |
| 159 | std::string* GetString(); |
| 160 | const std::string& GetString() const; |
| 161 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 162 | // Overridden from Value: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 163 | bool GetAsString(std::string* out_value) const override; |
| 164 | bool GetAsString(string16* out_value) const override; |
| 165 | bool GetAsString(const StringValue** out_value) const override; |
| 166 | StringValue* DeepCopy() const override; |
| 167 | bool Equals(const Value* other) const override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 168 | |
| 169 | private: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 170 | std::string value_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 171 | }; |
| 172 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 173 | class BASE_EXPORT BinaryValue: public Value { |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 174 | public: |
rpaquay@chromium.org | 7b1dfba | 2013-01-08 12:16:22 +0900 | [diff] [blame] | 175 | // Creates a BinaryValue with a null buffer and size of 0. |
| 176 | BinaryValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 177 | |
rpaquay@chromium.org | 7b1dfba | 2013-01-08 12:16:22 +0900 | [diff] [blame] | 178 | // Creates a BinaryValue, taking ownership of the bytes pointed to by |
| 179 | // |buffer|. |
| 180 | BinaryValue(scoped_ptr<char[]> buffer, size_t size); |
| 181 | |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 182 | ~BinaryValue() override; |
jhawkins@chromium.org | 592520e | 2012-05-20 11:34:57 +0900 | [diff] [blame] | 183 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 184 | // For situations where you want to keep ownership of your buffer, this |
| 185 | // factory method creates a new BinaryValue by copying the contents of the |
| 186 | // buffer that's passed in. |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 187 | static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 188 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 189 | size_t GetSize() const { return size_; } |
rpaquay@chromium.org | 7b1dfba | 2013-01-08 12:16:22 +0900 | [diff] [blame] | 190 | |
| 191 | // May return NULL. |
| 192 | char* GetBuffer() { return buffer_.get(); } |
| 193 | const char* GetBuffer() const { return buffer_.get(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 194 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 195 | // Overridden from Value: |
pneubeck | b57f3f5 | 2015-01-20 20:26:36 +0900 | [diff] [blame] | 196 | bool GetAsBinary(const BinaryValue** out_value) const override; |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 197 | BinaryValue* DeepCopy() const override; |
| 198 | bool Equals(const Value* other) const override; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 199 | |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 200 | private: |
rpaquay@chromium.org | 7b1dfba | 2013-01-08 12:16:22 +0900 | [diff] [blame] | 201 | scoped_ptr<char[]> buffer_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 202 | size_t size_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 203 | |
| 204 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 205 | }; |
| 206 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 207 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 208 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 209 | // are |std::string|s and should be UTF-8 encoded. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 210 | class BASE_EXPORT DictionaryValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 211 | public: |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame^] | 212 | // Returns |value| if it is a dictionary, nullptr otherwise. |
| 213 | static scoped_ptr<DictionaryValue> From(scoped_ptr<Value> value); |
| 214 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 215 | DictionaryValue(); |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 216 | ~DictionaryValue() override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 217 | |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 218 | // Overridden from Value: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 219 | bool GetAsDictionary(DictionaryValue** out_value) override; |
| 220 | bool GetAsDictionary(const DictionaryValue** out_value) const override; |
battre@chromium.org | 29eaa25 | 2011-11-26 10:11:44 +0900 | [diff] [blame] | 221 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 222 | // Returns true if the current dictionary has a value for the given key. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 223 | bool HasKey(const std::string& key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 224 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 225 | // Returns the number of Values in this dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 226 | size_t size() const { return dictionary_.size(); } |
| 227 | |
| 228 | // Returns whether the dictionary is empty. |
| 229 | bool empty() const { return dictionary_.empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 230 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 231 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 232 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 233 | |
| 234 | // Sets the Value associated with the given path starting from this object. |
| 235 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 236 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 237 | // within a key, but there are no other restrictions on keys. |
| 238 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 239 | // a DictionaryValue, a new DictionaryValue will be created and attached |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 240 | // to the path in that location. |in_value| must be non-null. |
| 241 | void Set(const std::string& path, scoped_ptr<Value> in_value); |
| 242 | // Deprecated version of the above. TODO(estade): remove. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 243 | void Set(const std::string& path, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 244 | |
| 245 | // Convenience forms of Set(). These methods will replace any existing |
| 246 | // value at that path, even if it has a different type. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 247 | void SetBoolean(const std::string& path, bool in_value); |
| 248 | void SetInteger(const std::string& path, int in_value); |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 249 | void SetDouble(const std::string& path, double in_value); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 250 | void SetString(const std::string& path, const std::string& in_value); |
viettrungluu@chromium.org | b7b6aa0 | 2010-08-05 01:58:12 +0900 | [diff] [blame] | 251 | void SetString(const std::string& path, const string16& in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 252 | |
| 253 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 254 | // be used as paths. |
estade | 948395d | 2015-01-07 05:06:50 +0900 | [diff] [blame] | 255 | void SetWithoutPathExpansion(const std::string& key, |
| 256 | scoped_ptr<Value> in_value); |
| 257 | // Deprecated version of the above. TODO(estade): remove. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 258 | void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 259 | |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 260 | // Convenience forms of SetWithoutPathExpansion(). |
| 261 | void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); |
| 262 | void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); |
| 263 | void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); |
| 264 | void SetStringWithoutPathExpansion(const std::string& path, |
| 265 | const std::string& in_value); |
| 266 | void SetStringWithoutPathExpansion(const std::string& path, |
| 267 | const string16& in_value); |
| 268 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 269 | // Gets the Value associated with the given path starting from this object. |
| 270 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 271 | // into the next DictionaryValue down. If the path can be resolved |
| 272 | // successfully, the value for the last key in the path will be returned |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 273 | // through the |out_value| parameter, and the function will return true. |
| 274 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 275 | // Note that the dictionary always owns the value that's returned. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 276 | // |out_value| is optional and will only be set if non-NULL. |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 277 | bool Get(StringPiece path, const Value** out_value) const; |
| 278 | bool Get(StringPiece path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 279 | |
| 280 | // These are convenience forms of Get(). The value will be retrieved |
| 281 | // and the return value will be true if the path is valid and the value at |
| 282 | // the end of the path can be returned in the form specified. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 283 | // |out_value| is optional and will only be set if non-NULL. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 284 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 285 | bool GetInteger(const std::string& path, int* out_value) const; |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 286 | // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 287 | // doubles. |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 288 | bool GetDouble(const std::string& path, double* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 289 | bool GetString(const std::string& path, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 290 | bool GetString(const std::string& path, string16* out_value) const; |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 291 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 292 | bool GetBinary(const std::string& path, const BinaryValue** out_value) const; |
| 293 | bool GetBinary(const std::string& path, BinaryValue** out_value); |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 294 | bool GetDictionary(StringPiece path, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 295 | const DictionaryValue** out_value) const; |
asvitkine | 22632bf | 2015-06-24 03:22:52 +0900 | [diff] [blame] | 296 | bool GetDictionary(StringPiece path, DictionaryValue** out_value); |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 297 | bool GetList(const std::string& path, const ListValue** out_value) const; |
| 298 | bool GetList(const std::string& path, ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 299 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 300 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 301 | // be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 302 | bool GetWithoutPathExpansion(const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 303 | const Value** out_value) const; |
| 304 | bool GetWithoutPathExpansion(const std::string& key, Value** out_value); |
pneubeck@chromium.org | 3b60054 | 2012-11-07 01:46:55 +0900 | [diff] [blame] | 305 | bool GetBooleanWithoutPathExpansion(const std::string& key, |
| 306 | bool* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 307 | bool GetIntegerWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 308 | int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 309 | bool GetDoubleWithoutPathExpansion(const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 310 | double* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 311 | bool GetStringWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 312 | std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 313 | bool GetStringWithoutPathExpansion(const std::string& key, |
| 314 | string16* out_value) const; |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 315 | bool GetDictionaryWithoutPathExpansion( |
| 316 | const std::string& key, |
| 317 | const DictionaryValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 318 | bool GetDictionaryWithoutPathExpansion(const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 319 | DictionaryValue** out_value); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 320 | bool GetListWithoutPathExpansion(const std::string& key, |
vabr@chromium.org | 7456243 | 2012-07-28 07:27:11 +0900 | [diff] [blame] | 321 | const ListValue** out_value) const; |
| 322 | bool GetListWithoutPathExpansion(const std::string& key, |
| 323 | ListValue** out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 324 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 325 | // Removes the Value with the specified path from this dictionary (or one |
| 326 | // of its child dictionaries, if the path is more than just a local key). |
bauerb@chromium.org | ae43e0a | 2013-08-06 22:33:04 +0900 | [diff] [blame] | 327 | // If |out_value| is non-NULL, the removed Value will be passed out via |
| 328 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
| 329 | // This method returns true if |path| is a valid path; otherwise it will |
| 330 | // return false and the DictionaryValue object will be unchanged. |
| 331 | virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 332 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 333 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 334 | // to be used as paths. |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 335 | virtual bool RemoveWithoutPathExpansion(const std::string& key, |
bauerb@chromium.org | ae43e0a | 2013-08-06 22:33:04 +0900 | [diff] [blame] | 336 | scoped_ptr<Value>* out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 337 | |
gab@chromium.org | 903d5c9 | 2013-11-27 10:38:24 +0900 | [diff] [blame] | 338 | // Removes a path, clearing out all dictionaries on |path| that remain empty |
| 339 | // after removing the value at |path|. |
| 340 | virtual bool RemovePath(const std::string& path, |
| 341 | scoped_ptr<Value>* out_value); |
| 342 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 343 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 344 | // the copy. This never returns NULL, even if |this| itself is empty. |
estade | 51a57ab | 2015-05-23 11:42:09 +0900 | [diff] [blame] | 345 | scoped_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const; |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 346 | |
jhawkins@chromium.org | cbb2239 | 2012-05-10 06:54:27 +0900 | [diff] [blame] | 347 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
| 348 | // sub-dictionaries will be merged as well. In case of key collisions, the |
| 349 | // passed in dictionary takes precedence and data already present will be |
| 350 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
| 351 | // be freed any time after this call. |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 352 | void MergeDictionary(const DictionaryValue* dictionary); |
| 353 | |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 354 | // Swaps contents with the |other| dictionary. |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 355 | virtual void Swap(DictionaryValue* other); |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 356 | |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 357 | // This class provides an iterator over both keys and values in the |
| 358 | // dictionary. It can't be used to modify the dictionary. |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 359 | class BASE_EXPORT Iterator { |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 360 | public: |
hans@chromium.org | d5d38e3 | 2012-08-10 21:45:35 +0900 | [diff] [blame] | 361 | explicit Iterator(const DictionaryValue& target); |
phajdan.jr@chromium.org | 8e2b208 | 2013-12-11 03:52:22 +0900 | [diff] [blame] | 362 | ~Iterator(); |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 363 | |
pneubeck@chromium.org | a2fbefc | 2013-01-18 23:43:27 +0900 | [diff] [blame] | 364 | bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } |
kalman@chromium.org | 1604ad6 | 2011-11-09 06:26:41 +0900 | [diff] [blame] | 365 | void Advance() { ++it_; } |
| 366 | |
| 367 | const std::string& key() const { return it_->first; } |
| 368 | const Value& value() const { return *it_->second; } |
| 369 | |
| 370 | private: |
| 371 | const DictionaryValue& target_; |
| 372 | ValueMap::const_iterator it_; |
| 373 | }; |
| 374 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 375 | // Overridden from Value: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 376 | DictionaryValue* DeepCopy() const override; |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 377 | // Preferred version of DeepCopy. TODO(estade): remove the above. |
| 378 | scoped_ptr<DictionaryValue> CreateDeepCopy() const; |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 379 | bool Equals(const Value* other) const override; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 380 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 381 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 382 | ValueMap dictionary_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 383 | |
| 384 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | // This type of Value represents a list of other Value values. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 388 | class BASE_EXPORT ListValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 389 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 390 | typedef ValueVector::iterator iterator; |
| 391 | typedef ValueVector::const_iterator const_iterator; |
| 392 | |
reillyg | d39049b | 2015-09-11 09:25:54 +0900 | [diff] [blame^] | 393 | // Returns |value| if it is a list, nullptr otherwise. |
| 394 | static scoped_ptr<ListValue> From(scoped_ptr<Value> value); |
| 395 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 396 | ListValue(); |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 397 | ~ListValue() override; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 398 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 405 | // Returns whether the list is empty. |
| 406 | bool empty() const { return list_.empty(); } |
| 407 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 414 | // Preferred version of the above. TODO(estade): remove the above. |
| 415 | bool Set(size_t index, scoped_ptr<Value> in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 416 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 417 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 418 | // only if the index falls within the current list range. |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 419 | // Note that the list always owns the Value passed out via |out_value|. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 420 | // |out_value| is optional and will only be set if non-NULL. |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [diff] [blame] | 421 | bool Get(size_t index, const Value** out_value) const; |
| 422 | bool Get(size_t index, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 423 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [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. |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 427 | // |out_value| is optional and will only be set if non-NULL. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 428 | bool GetBoolean(size_t index, bool* out_value) const; |
| 429 | bool GetInteger(size_t index, int* out_value) const; |
gab@chromium.org | b3fa814 | 2014-03-09 16:13:23 +0900 | [diff] [blame] | 430 | // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as |
| 431 | // doubles. |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 432 | bool GetDouble(size_t index, double* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 433 | bool GetString(size_t index, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 434 | bool GetString(size_t index, string16* out_value) const; |
vabr@chromium.org | a8ca4fd | 2012-08-03 17:43:37 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 |
ericroman@google.com | 3d19ee5 | 2009-01-06 03:37:51 +0900 | [diff] [blame] | 444 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 445 | // be deleted. This method returns true if |index| is valid; otherwise |
| 446 | // it will return false and the ListValue object will be unchanged. |
bauerb@chromium.org | ae43e0a | 2013-08-06 22:33:04 +0900 | [diff] [blame] | 447 | virtual bool Remove(size_t index, scoped_ptr<Value>* out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 448 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 449 | // Removes the first instance of |value| found in the list, if any, and |
tfarina@chromium.org | 09cf434 | 2011-08-14 02:34:31 +0900 | [diff] [blame] | 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); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 453 | |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [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. |
dubroy@chromium.org | af2a396 | 2013-02-19 01:31:45 +0900 | [diff] [blame] | 456 | // Returns an iterator pointing to the location of the element that |
| 457 | // followed the erased element. |
bauerb@chromium.org | ae43e0a | 2013-08-06 22:33:04 +0900 | [diff] [blame] | 458 | iterator Erase(iterator iter, scoped_ptr<Value>* out_value); |
estade@chromium.org | 52852d5 | 2012-07-03 11:51:43 +0900 | [diff] [blame] | 459 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 460 | // Appends a Value to the end of the list. |
estade | 77a018f | 2015-05-07 10:53:08 +0900 | [diff] [blame] | 461 | void Append(scoped_ptr<Value> in_value); |
| 462 | // Deprecated version of the above. TODO(estade): remove. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 463 | void Append(Value* in_value); |
| 464 | |
kalman@chromium.org | 5081d8e | 2012-09-14 11:14:01 +0900 | [diff] [blame] | 465 | // Convenience forms of Append. |
| 466 | void AppendBoolean(bool in_value); |
| 467 | void AppendInteger(int in_value); |
| 468 | void AppendDouble(double in_value); |
| 469 | void AppendString(const std::string& in_value); |
| 470 | void AppendString(const string16& in_value); |
| 471 | void AppendStrings(const std::vector<std::string>& in_values); |
| 472 | void AppendStrings(const std::vector<string16>& in_values); |
| 473 | |
markusheintz@chromium.org | 7862187 | 2011-03-18 22:56:38 +0900 | [diff] [blame] | 474 | // Appends a Value if it's not already present. Takes ownership of the |
| 475 | // |in_value|. Returns true if successful, or false if the value was already |
| 476 | // present. If the value was already present the |in_value| is deleted. |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 477 | bool AppendIfNotPresent(Value* in_value); |
| 478 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 479 | // Insert a Value at index. |
| 480 | // Returns true if successful, or false if the index was out of range. |
| 481 | bool Insert(size_t index, Value* in_value); |
| 482 | |
pastarmovj@chromium.org | 1602a47 | 2011-09-20 00:23:10 +0900 | [diff] [blame] | 483 | // Searches for the first instance of |value| in the list using the Equals |
| 484 | // method of the Value type. |
| 485 | // Returns a const_iterator to the found item or to end() if none exists. |
| 486 | const_iterator Find(const Value& value) const; |
| 487 | |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 488 | // Swaps contents with the |other| list. |
rsesek@chromium.org | c2d2b20 | 2012-05-17 00:23:30 +0900 | [diff] [blame] | 489 | virtual void Swap(ListValue* other); |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 490 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 491 | // Iteration. |
| 492 | iterator begin() { return list_.begin(); } |
| 493 | iterator end() { return list_.end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 494 | |
tfarina@chromium.org | cc17740 | 2011-08-12 05:56:32 +0900 | [diff] [blame] | 495 | const_iterator begin() const { return list_.begin(); } |
| 496 | const_iterator end() const { return list_.end(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 497 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 498 | // Overridden from Value: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 499 | bool GetAsList(ListValue** out_value) override; |
| 500 | bool GetAsList(const ListValue** out_value) const override; |
| 501 | ListValue* DeepCopy() const override; |
| 502 | bool Equals(const Value* other) const override; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 503 | |
estade | 033e61e | 2015-05-13 03:11:50 +0900 | [diff] [blame] | 504 | // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. |
| 505 | scoped_ptr<ListValue> CreateDeepCopy() const; |
| 506 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 507 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 508 | ValueVector list_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 509 | |
| 510 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 511 | }; |
| 512 | |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 513 | // This interface is implemented by classes that know how to serialize |
| 514 | // Value objects. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 515 | class BASE_EXPORT ValueSerializer { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 516 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 517 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 518 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 519 | virtual bool Serialize(const Value& root) = 0; |
prashhir | 58322f4 | 2015-03-05 18:30:57 +0900 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | // This interface is implemented by classes that know how to deserialize Value |
| 523 | // objects. |
| 524 | class BASE_EXPORT ValueDeserializer { |
| 525 | public: |
| 526 | virtual ~ValueDeserializer(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 527 | |
| 528 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 529 | // If the return value is non-NULL, the caller takes ownership of returned |
erikkay@chromium.org | 64b2cf4 | 2010-04-07 00:42:39 +0900 | [diff] [blame] | 530 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 531 | // error_code will be set with the underlying error. |
| 532 | // If |error_message| is non-null, it will be filled in with a formatted |
| 533 | // error message including the location of the error if appropriate. |
| 534 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 535 | }; |
| 536 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 537 | // Stream operator so Values can be used in assertion statements. In order that |
| 538 | // gtest uses this operator to print readable output on test failures, we must |
| 539 | // override each specific type. Otherwise, the default template implementation |
| 540 | // is preferred over an upcast. |
kalman@chromium.org | 829cf75 | 2012-09-12 12:39:35 +0900 | [diff] [blame] | 541 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
| 542 | |
pneubeck@google.com | 6671d3f | 2013-04-16 18:04:02 +0900 | [diff] [blame] | 543 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 544 | const FundamentalValue& value) { |
| 545 | return out << static_cast<const Value&>(value); |
| 546 | } |
| 547 | |
| 548 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 549 | const StringValue& value) { |
| 550 | return out << static_cast<const Value&>(value); |
| 551 | } |
| 552 | |
| 553 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 554 | const DictionaryValue& value) { |
| 555 | return out << static_cast<const Value&>(value); |
| 556 | } |
| 557 | |
| 558 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
| 559 | const ListValue& value) { |
| 560 | return out << static_cast<const Value&>(value); |
| 561 | } |
| 562 | |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 563 | } // namespace base |
| 564 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 565 | #endif // BASE_VALUES_H_ |