erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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 |
| 6 | // storing setting and other persistable data. It includes the ability to |
| 7 | // specify (recursive) lists and dictionaries, so it's fairly expressive. |
| 8 | // However, the API is optimized for the common case, namely storing a |
| 9 | // hierarchical tree of simple values. Given a DictionaryValue root, you can |
| 10 | // easily do things like: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | // |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 12 | // root->SetString("global.pages.homepage", "http://goateleporter.com"); |
| 13 | // std::string homepage = "http://google.com"; // default/fallback value |
| 14 | // root->GetString("global.pages.homepage", &homepage); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | // |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 16 | // where "global" and "pages" are also DictionaryValues, and "homepage" is a |
| 17 | // string setting. If some elements of the path didn't exist yet, the |
| 18 | // SetString() method would create the missing elements and attach them to root |
| 19 | // before attaching the homepage value. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 20 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 21 | #ifndef BASE_VALUES_H_ |
| 22 | #define BASE_VALUES_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 23 | #pragma once |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | |
| 25 | #include <iterator> |
| 26 | #include <map> |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 27 | #include <string> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 30 | #include "base/base_api.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 31 | #include "base/basictypes.h" |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 32 | #include "base/string16.h" |
| 33 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | class BinaryValue; |
| 36 | class DictionaryValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 37 | class FundamentalValue; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 38 | class ListValue; |
bauerb@chromium.org | 6fca958 | 2011-02-22 01:35:04 +0900 | [diff] [blame] | 39 | class StringValue; |
| 40 | class Value; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | |
| 42 | typedef std::vector<Value*> ValueVector; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 43 | typedef std::map<std::string, Value*> ValueMap; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | |
| 45 | // The Value class is the base class for Values. A Value can be |
| 46 | // instantiated via the Create*Value() factory methods, or by directly |
| 47 | // creating instances of the subclasses. |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 48 | class BASE_API Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 49 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 50 | enum ValueType { |
| 51 | TYPE_NULL = 0, |
| 52 | TYPE_BOOLEAN, |
| 53 | TYPE_INTEGER, |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 54 | TYPE_DOUBLE, |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 55 | TYPE_STRING, |
| 56 | TYPE_BINARY, |
| 57 | TYPE_DICTIONARY, |
| 58 | TYPE_LIST |
| 59 | }; |
| 60 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 61 | virtual ~Value(); |
| 62 | |
| 63 | // Convenience methods for creating Value objects for various |
| 64 | // kinds of values without thinking about which class implements them. |
| 65 | // These can always be expected to return a valid Value*. |
| 66 | static Value* CreateNullValue(); |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 67 | static FundamentalValue* CreateBooleanValue(bool in_value); |
| 68 | static FundamentalValue* CreateIntegerValue(int in_value); |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 69 | static FundamentalValue* CreateDoubleValue(double in_value); |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 70 | static StringValue* CreateStringValue(const std::string& in_value); |
| 71 | static StringValue* CreateStringValue(const string16& in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 72 | |
| 73 | // This one can return NULL if the input isn't valid. If the return value |
| 74 | // is non-null, the new object has taken ownership of the buffer pointer. |
| 75 | static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
| 76 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 77 | // Returns the type of the value stored by the current Value object. |
| 78 | // Each type will be implemented by only one subclass of Value, so it's |
| 79 | // safe to use the ValueType to determine whether you can cast from |
| 80 | // Value* to (Implementing Class)*. Also, a Value object never changes |
| 81 | // its type after construction. |
| 82 | ValueType GetType() const { return type_; } |
| 83 | |
| 84 | // Returns true if the current object represents a given type. |
| 85 | bool IsType(ValueType type) const { return type == type_; } |
| 86 | |
| 87 | // These methods allow the convenient retrieval of settings. |
| 88 | // If the current setting object can be converted into the given type, |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 89 | // the value is returned through the |out_value| parameter and true is |
| 90 | // returned; otherwise, false is returned and |out_value| is unchanged. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 91 | virtual bool GetAsBoolean(bool* out_value) const; |
| 92 | virtual bool GetAsInteger(int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 93 | virtual bool GetAsDouble(double* out_value) const; |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 94 | virtual bool GetAsString(std::string* out_value) const; |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 95 | virtual bool GetAsString(string16* out_value) const; |
scottbyer@google.com | 673b876 | 2010-12-07 09:35:29 +0900 | [diff] [blame] | 96 | virtual bool GetAsList(ListValue** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 97 | |
| 98 | // This creates a deep copy of the entire Value tree, and returns a pointer |
| 99 | // 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] | 100 | // |
| 101 | // Subclasses return their own type directly in their overrides; |
| 102 | // this works because C++ supports covariant return types. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 103 | virtual Value* DeepCopy() const; |
| 104 | |
| 105 | // Compares if two Value objects have equal contents. |
| 106 | virtual bool Equals(const Value* other) const; |
| 107 | |
bauerb@chromium.org | fbd55e1 | 2010-12-07 03:13:43 +0900 | [diff] [blame] | 108 | // Compares if two Value objects have equal contents. Can handle NULLs. |
| 109 | // NULLs are considered equal but different from Value::CreateNullValue(). |
| 110 | static bool Equals(const Value* a, const Value* b); |
| 111 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 112 | protected: |
| 113 | // This isn't safe for end-users (they should use the Create*Value() |
| 114 | // static methods above), but it's useful for subclasses. |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 115 | explicit Value(ValueType type); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 116 | |
| 117 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 118 | Value(); |
| 119 | |
| 120 | ValueType type_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(Value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | // FundamentalValue represents the simple fundamental types of values. |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 126 | class BASE_API FundamentalValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 127 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 128 | explicit FundamentalValue(bool in_value); |
| 129 | explicit FundamentalValue(int in_value); |
| 130 | explicit FundamentalValue(double in_value); |
erg@google.com | 2d9b43e | 2010-12-09 03:06:44 +0900 | [diff] [blame] | 131 | virtual ~FundamentalValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 132 | |
| 133 | // Subclassed methods |
| 134 | virtual bool GetAsBoolean(bool* out_value) const; |
| 135 | virtual bool GetAsInteger(int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 136 | virtual bool GetAsDouble(double* out_value) const; |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 137 | virtual FundamentalValue* DeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 138 | virtual bool Equals(const Value* other) const; |
| 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 | }; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 146 | |
| 147 | DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 148 | }; |
| 149 | |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 150 | class BASE_API StringValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 151 | public: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 152 | // Initializes a StringValue with a UTF-8 narrow character string. |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 153 | explicit StringValue(const std::string& in_value); |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 154 | |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 155 | // Initializes a StringValue with a string16. |
| 156 | explicit StringValue(const string16& in_value); |
viettrungluu@chromium.org | 31b80ba | 2010-08-04 00:42:58 +0900 | [diff] [blame] | 157 | |
erg@google.com | 2d9b43e | 2010-12-09 03:06:44 +0900 | [diff] [blame] | 158 | virtual ~StringValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 159 | |
| 160 | // Subclassed methods |
erg@google.com | 2d9b43e | 2010-12-09 03:06:44 +0900 | [diff] [blame] | 161 | virtual bool GetAsString(std::string* out_value) const; |
| 162 | virtual bool GetAsString(string16* out_value) const; |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 163 | virtual StringValue* DeepCopy() const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 164 | virtual bool Equals(const Value* other) const; |
| 165 | |
| 166 | private: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 167 | std::string value_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 168 | |
| 169 | DISALLOW_COPY_AND_ASSIGN(StringValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 170 | }; |
| 171 | |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 172 | class BASE_API BinaryValue: public Value { |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 173 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 174 | virtual ~BinaryValue(); |
| 175 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 176 | // Creates a Value to represent a binary buffer. The new object takes |
| 177 | // ownership of the pointer passed in, if successful. |
| 178 | // Returns NULL if buffer is NULL. |
| 179 | static BinaryValue* Create(char* buffer, size_t size); |
| 180 | |
| 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. |
| 184 | // Returns NULL if buffer is NULL. |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 185 | static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 186 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 187 | size_t GetSize() const { return size_; } |
| 188 | char* GetBuffer() { return buffer_; } |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 189 | const char* GetBuffer() const { return buffer_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 190 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 191 | // Overridden from Value: |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 192 | virtual BinaryValue* DeepCopy() const; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 193 | virtual bool Equals(const Value* other) const; |
| 194 | |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 195 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 196 | // Constructor is private so that only objects with valid buffer pointers |
| 197 | // and size values can be created. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 198 | BinaryValue(char* buffer, size_t size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 199 | |
| 200 | char* buffer_; |
| 201 | size_t size_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 202 | |
| 203 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 204 | }; |
| 205 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame] | 206 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 207 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 208 | // are |std::string|s and should be UTF-8 encoded. |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 209 | class BASE_API DictionaryValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 210 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 211 | DictionaryValue(); |
erg@google.com | 2d9b43e | 2010-12-09 03:06:44 +0900 | [diff] [blame] | 212 | virtual ~DictionaryValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 213 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 214 | // 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] | 215 | bool HasKey(const std::string& key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 216 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 217 | // Returns the number of Values in this dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 218 | size_t size() const { return dictionary_.size(); } |
| 219 | |
| 220 | // Returns whether the dictionary is empty. |
| 221 | bool empty() const { return dictionary_.empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 222 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 223 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 224 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 225 | |
| 226 | // Sets the Value associated with the given path starting from this object. |
| 227 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 228 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 229 | // within a key, but there are no other restrictions on keys. |
| 230 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 231 | // a DictionaryValue, a new DictionaryValue will be created and attached |
| 232 | // to the path in that location. |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 233 | // Note that the dictionary takes ownership of the value referenced by |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 234 | // |in_value|, and therefore |in_value| must be non-NULL. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 235 | void Set(const std::string& path, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 236 | |
| 237 | // Convenience forms of Set(). These methods will replace any existing |
| 238 | // value at that path, even if it has a different type. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 239 | void SetBoolean(const std::string& path, bool in_value); |
| 240 | void SetInteger(const std::string& path, int in_value); |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 241 | void SetDouble(const std::string& path, double in_value); |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 242 | void SetString(const std::string& path, const std::string& in_value); |
viettrungluu@chromium.org | b7b6aa0 | 2010-08-05 01:58:12 +0900 | [diff] [blame] | 243 | void SetString(const std::string& path, const string16& in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 244 | |
| 245 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 246 | // be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 247 | void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 248 | |
| 249 | // Gets the Value associated with the given path starting from this object. |
| 250 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 251 | // into the next DictionaryValue down. If the path can be resolved |
| 252 | // 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] | 253 | // through the |out_value| parameter, and the function will return true. |
| 254 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 255 | // Note that the dictionary always owns the value that's returned. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 256 | bool Get(const std::string& path, Value** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 257 | |
| 258 | // These are convenience forms of Get(). The value will be retrieved |
| 259 | // and the return value will be true if the path is valid and the value at |
| 260 | // the end of the path can be returned in the form specified. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 261 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 262 | bool GetInteger(const std::string& path, int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 263 | bool GetDouble(const std::string& path, double* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 264 | bool GetString(const std::string& path, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 265 | bool GetString(const std::string& path, string16* out_value) const; |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 266 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 267 | bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
| 268 | bool GetDictionary(const std::string& path, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 269 | DictionaryValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 270 | bool GetList(const std::string& path, ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 271 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 272 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 273 | // be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 274 | bool GetWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 275 | Value** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 276 | bool GetIntegerWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 277 | int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 278 | bool GetDoubleWithoutPathExpansion(const std::string& key, |
jam@chromium.org | 5bd5392 | 2010-10-01 16:28:25 +0900 | [diff] [blame] | 279 | double* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 280 | bool GetStringWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 281 | std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 282 | bool GetStringWithoutPathExpansion(const std::string& key, |
| 283 | string16* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 284 | bool GetDictionaryWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 285 | DictionaryValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 286 | bool GetListWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 287 | ListValue** out_value) const; |
| 288 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 289 | // Removes the Value with the specified path from this dictionary (or one |
| 290 | // of its child dictionaries, if the path is more than just a local key). |
| 291 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 292 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 293 | // be deleted. This method returns true if |path| is a valid path; otherwise |
| 294 | // it will return false and the DictionaryValue object will be unchanged. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 295 | bool Remove(const std::string& path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 296 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 297 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 298 | // to be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 299 | bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 300 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 301 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 302 | // the copy. This never returns NULL, even if |this| itself is empty. |
| 303 | DictionaryValue* DeepCopyWithoutEmptyChildren(); |
| 304 | |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 305 | // Merge a given dictionary into this dictionary. This is done recursively, |
| 306 | // i.e. any subdictionaries will be merged as well. In case of key collisions, |
| 307 | // the passed in dictionary takes precedence and data already present will be |
| 308 | // replaced. |
| 309 | void MergeDictionary(const DictionaryValue* dictionary); |
| 310 | |
akalin@chromium.org | 71cd130 | 2011-05-10 18:23:39 +0900 | [diff] [blame] | 311 | // Swaps contents with the |other| dictionary. |
| 312 | void Swap(DictionaryValue* other) { |
| 313 | dictionary_.swap(other->dictionary_); |
| 314 | } |
| 315 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 316 | // This class provides an iterator for the keys in the dictionary. |
| 317 | // It can't be used to modify the dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 318 | // |
| 319 | // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 320 | // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 321 | // keys have '.'s in them. |
darin@chromium.org | 020e0b1 | 2011-04-30 03:08:21 +0900 | [diff] [blame] | 322 | class key_iterator |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 323 | : private std::iterator<std::input_iterator_tag, const std::string> { |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 324 | public: |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 325 | explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
erg@google.com | bf6ce9f | 2010-01-27 08:08:02 +0900 | [diff] [blame] | 326 | key_iterator operator++() { |
| 327 | ++itr_; |
| 328 | return *this; |
| 329 | } |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 330 | const std::string& operator*() { return itr_->first; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 331 | bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 332 | bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 333 | |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 334 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 335 | ValueMap::const_iterator itr_; |
| 336 | }; |
| 337 | |
| 338 | key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 339 | key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 340 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 341 | // Overridden from Value: |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 342 | virtual DictionaryValue* DeepCopy() const; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 343 | virtual bool Equals(const Value* other) const; |
| 344 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 345 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 346 | ValueMap dictionary_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 347 | |
| 348 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | // This type of Value represents a list of other Value values. |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 352 | class BASE_API ListValue : public Value { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 353 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 354 | typedef ValueVector::iterator iterator; |
| 355 | typedef ValueVector::const_iterator const_iterator; |
| 356 | |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 357 | ListValue(); |
hans@chromium.org | 78b7593 | 2011-05-25 18:08:19 +0900 | [diff] [blame] | 358 | virtual ~ListValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 359 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 360 | // Clears the contents of this ListValue |
| 361 | void Clear(); |
| 362 | |
| 363 | // Returns the number of Values in this list. |
| 364 | size_t GetSize() const { return list_.size(); } |
| 365 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 366 | // Returns whether the list is empty. |
| 367 | bool empty() const { return list_.empty(); } |
| 368 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 369 | // Sets the list item at the given index to be the Value specified by |
| 370 | // the value given. If the index beyond the current end of the list, null |
| 371 | // Values will be used to pad out the list. |
| 372 | // Returns true if successful, or false if the index was negative or |
| 373 | // the value is a null pointer. |
| 374 | bool Set(size_t index, Value* in_value); |
| 375 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 376 | // 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] | 377 | // only if the index falls within the current list range. |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 378 | // Note that the list always owns the Value passed out via |out_value|. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 379 | bool Get(size_t index, Value** out_value) const; |
| 380 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 381 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 382 | // only if the index is valid and the Value at that index can be returned |
| 383 | // in the specified form. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 384 | bool GetBoolean(size_t index, bool* out_value) const; |
| 385 | bool GetInteger(size_t index, int* out_value) const; |
arv@chromium.org | 13413eb | 2011-02-01 10:02:07 +0900 | [diff] [blame] | 386 | bool GetDouble(size_t index, double* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 387 | bool GetString(size_t index, std::string* out_value) const; |
viettrungluu@chromium.org | 58767ef | 2010-08-05 04:35:33 +0900 | [diff] [blame] | 388 | bool GetString(size_t index, string16* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 389 | bool GetBinary(size_t index, BinaryValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 390 | bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 391 | bool GetList(size_t index, ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 392 | |
| 393 | // Removes the Value with the specified index from this list. |
| 394 | // 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] | 395 | // 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] | 396 | // be deleted. This method returns true if |index| is valid; otherwise |
| 397 | // it will return false and the ListValue object will be unchanged. |
| 398 | bool Remove(size_t index, Value** out_value); |
| 399 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 400 | // Removes the first instance of |value| found in the list, if any, and |
| 401 | // deletes it. Returns the index that it was located at (-1 for not present). |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 402 | int Remove(const Value& value); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 403 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 404 | // Appends a Value to the end of the list. |
| 405 | void Append(Value* in_value); |
| 406 | |
markusheintz@chromium.org | 7862187 | 2011-03-18 22:56:38 +0900 | [diff] [blame] | 407 | // Appends a Value if it's not already present. Takes ownership of the |
| 408 | // |in_value|. Returns true if successful, or false if the value was already |
| 409 | // 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] | 410 | bool AppendIfNotPresent(Value* in_value); |
| 411 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 412 | // Insert a Value at index. |
| 413 | // Returns true if successful, or false if the index was out of range. |
| 414 | bool Insert(size_t index, Value* in_value); |
| 415 | |
brettw@chromium.org | 2031cd8 | 2010-08-20 03:05:56 +0900 | [diff] [blame] | 416 | // Swaps contents with the |other| list. |
| 417 | void Swap(ListValue* other) { |
| 418 | list_.swap(other->list_); |
| 419 | } |
| 420 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 421 | // Iteration |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 422 | ListValue::iterator begin() { return list_.begin(); } |
| 423 | ListValue::iterator end() { return list_.end(); } |
| 424 | |
| 425 | ListValue::const_iterator begin() const { return list_.begin(); } |
| 426 | ListValue::const_iterator end() const { return list_.end(); } |
| 427 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 428 | // Overridden from Value: |
| 429 | virtual bool GetAsList(ListValue** out_value); |
akalin@chromium.org | 810f92f | 2011-01-18 11:16:59 +0900 | [diff] [blame] | 430 | virtual ListValue* DeepCopy() const; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 431 | virtual bool Equals(const Value* other) const; |
| 432 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 433 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 434 | ValueVector list_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 435 | |
| 436 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | // This interface is implemented by classes that know how to serialize and |
| 440 | // deserialize Value objects. |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 441 | class BASE_API ValueSerializer { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 442 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 443 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 444 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 445 | virtual bool Serialize(const Value& root) = 0; |
| 446 | |
| 447 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 448 | // 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] | 449 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 450 | // error_code will be set with the underlying error. |
| 451 | // If |error_message| is non-null, it will be filled in with a formatted |
| 452 | // error message including the location of the error if appropriate. |
| 453 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 454 | }; |
| 455 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 456 | #endif // BASE_VALUES_H_ |