skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
| 30 | #include "base/basictypes.h" |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 31 | #include "base/string16.h" |
| 32 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 33 | |
| 34 | class Value; |
| 35 | class FundamentalValue; |
| 36 | class StringValue; |
| 37 | class BinaryValue; |
| 38 | class DictionaryValue; |
| 39 | class ListValue; |
| 40 | |
| 41 | typedef std::vector<Value*> ValueVector; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 42 | typedef std::map<std::string, Value*> ValueMap; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | |
| 44 | // The Value class is the base class for Values. A Value can be |
| 45 | // instantiated via the Create*Value() factory methods, or by directly |
| 46 | // creating instances of the subclasses. |
| 47 | class Value { |
| 48 | public: |
| 49 | virtual ~Value(); |
| 50 | |
| 51 | // Convenience methods for creating Value objects for various |
| 52 | // kinds of values without thinking about which class implements them. |
| 53 | // These can always be expected to return a valid Value*. |
| 54 | static Value* CreateNullValue(); |
| 55 | static Value* CreateBooleanValue(bool in_value); |
| 56 | static Value* CreateIntegerValue(int in_value); |
| 57 | static Value* CreateRealValue(double in_value); |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 58 | static Value* CreateStringValue(const std::string& in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 59 | static Value* CreateStringValue(const std::wstring& in_value); |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 60 | static Value* CreateStringValueFromUTF16(const string16& in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 61 | |
| 62 | // This one can return NULL if the input isn't valid. If the return value |
| 63 | // is non-null, the new object has taken ownership of the buffer pointer. |
| 64 | static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
| 65 | |
| 66 | typedef enum { |
| 67 | TYPE_NULL = 0, |
| 68 | TYPE_BOOLEAN, |
| 69 | TYPE_INTEGER, |
| 70 | TYPE_REAL, |
| 71 | TYPE_STRING, |
| 72 | TYPE_BINARY, |
| 73 | TYPE_DICTIONARY, |
| 74 | TYPE_LIST |
| 75 | } ValueType; |
| 76 | |
| 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; |
| 93 | virtual bool GetAsReal(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; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 95 | virtual bool GetAsString(std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 96 | virtual bool GetAsUTF16(string16* out_value) const; |
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. |
| 100 | virtual Value* DeepCopy() const; |
| 101 | |
| 102 | // Compares if two Value objects have equal contents. |
| 103 | virtual bool Equals(const Value* other) const; |
| 104 | |
| 105 | protected: |
| 106 | // This isn't safe for end-users (they should use the Create*Value() |
| 107 | // static methods above), but it's useful for subclasses. |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 108 | explicit Value(ValueType type); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 109 | |
| 110 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 111 | Value(); |
| 112 | |
| 113 | ValueType type_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(Value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // FundamentalValue represents the simple fundamental types of values. |
| 119 | class FundamentalValue : public Value { |
| 120 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 121 | explicit FundamentalValue(bool in_value); |
| 122 | explicit FundamentalValue(int in_value); |
| 123 | explicit FundamentalValue(double in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 124 | ~FundamentalValue(); |
| 125 | |
| 126 | // Subclassed methods |
| 127 | virtual bool GetAsBoolean(bool* out_value) const; |
| 128 | virtual bool GetAsInteger(int* out_value) const; |
| 129 | virtual bool GetAsReal(double* out_value) const; |
| 130 | virtual Value* DeepCopy() const; |
| 131 | virtual bool Equals(const Value* other) const; |
| 132 | |
| 133 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 134 | union { |
| 135 | bool boolean_value_; |
| 136 | int integer_value_; |
| 137 | double real_value_; |
| 138 | }; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 139 | |
| 140 | DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | class StringValue : public Value { |
| 144 | public: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 145 | // Initializes a StringValue with a UTF-8 narrow character string. |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 146 | explicit StringValue(const std::string& in_value); |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 147 | |
| 148 | // Initializes a StringValue with a wide character string. |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 149 | explicit StringValue(const std::wstring& in_value); |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 150 | |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 151 | #if !defined(WCHAR_T_IS_UTF16) |
| 152 | // Initializes a StringValue with a string16. |
| 153 | explicit StringValue(const string16& in_value); |
| 154 | #endif |
| 155 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 156 | ~StringValue(); |
| 157 | |
| 158 | // Subclassed methods |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 159 | bool GetAsString(std::string* out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 160 | bool GetAsString(std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 161 | bool GetAsUTF16(string16* out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 162 | Value* DeepCopy() const; |
| 163 | virtual bool Equals(const Value* other) const; |
| 164 | |
| 165 | private: |
scherkus@chromium.org | 2b923e7 | 2008-12-11 10:23:17 +0900 | [diff] [blame] | 166 | std::string value_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 167 | |
| 168 | DISALLOW_COPY_AND_ASSIGN(StringValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class BinaryValue: public Value { |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 172 | public: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 173 | // Creates a Value to represent a binary buffer. The new object takes |
| 174 | // ownership of the pointer passed in, if successful. |
| 175 | // Returns NULL if buffer is NULL. |
| 176 | static BinaryValue* Create(char* buffer, size_t size); |
| 177 | |
| 178 | // For situations where you want to keep ownership of your buffer, this |
| 179 | // factory method creates a new BinaryValue by copying the contents of the |
| 180 | // buffer that's passed in. |
| 181 | // Returns NULL if buffer is NULL. |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 182 | static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 183 | |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 184 | ~BinaryValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 185 | |
| 186 | // Subclassed methods |
| 187 | Value* DeepCopy() const; |
| 188 | virtual bool Equals(const Value* other) const; |
| 189 | |
| 190 | size_t GetSize() const { return size_; } |
| 191 | char* GetBuffer() { return buffer_; } |
mpcomplete@chromium.org | 554d431 | 2009-10-07 03:15:58 +0900 | [diff] [blame] | 192 | const char* GetBuffer() const { return buffer_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 193 | |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 194 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 195 | // Constructor is private so that only objects with valid buffer pointers |
| 196 | // and size values can be created. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 197 | BinaryValue(char* buffer, size_t size); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 198 | |
| 199 | char* buffer_; |
| 200 | size_t size_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 201 | |
| 202 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 203 | }; |
| 204 | |
viettrungluu@chromium.org | 496dec2 | 2010-07-31 13:56:14 +0900 | [diff] [blame^] | 205 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
| 206 | // parsing for recursive access; see the comment at the top of the file. Keys |
| 207 | // are |std::string|s and should be UTF-8 encoded. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 208 | // TODO(viettrungluu): Things marked DEPRECATED will be removed. crbug.com/23581 |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 209 | class DictionaryValue : public Value { |
| 210 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 211 | DictionaryValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 212 | ~DictionaryValue(); |
| 213 | |
| 214 | // Subclassed methods |
| 215 | Value* DeepCopy() const; |
| 216 | virtual bool Equals(const Value* other) const; |
| 217 | |
| 218 | // 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] | 219 | bool HasKey(const std::string& key) const; |
| 220 | /*DEPRECATED*/bool HasKeyASCII(const std::string& key) const; |
| 221 | /*DEPRECATED*/bool HasKey(const std::wstring& key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 222 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 223 | // Returns the number of Values in this dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 224 | size_t size() const { return dictionary_.size(); } |
| 225 | |
| 226 | // Returns whether the dictionary is empty. |
| 227 | bool empty() const { return dictionary_.empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 228 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 229 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 230 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 231 | |
| 232 | // Sets the Value associated with the given path starting from this object. |
| 233 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 234 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 235 | // within a key, but there are no other restrictions on keys. |
| 236 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 237 | // a DictionaryValue, a new DictionaryValue will be created and attached |
| 238 | // to the path in that location. |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 239 | // Note that the dictionary takes ownership of the value referenced by |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 240 | // |in_value|, and therefore |in_value| must be non-NULL. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 241 | void Set(const std::string& path, Value* in_value); |
| 242 | /*DEPRECATED*/void Set(const std::wstring& path, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 243 | |
| 244 | // Convenience forms of Set(). These methods will replace any existing |
| 245 | // value at that path, even if it has a different type. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 246 | void SetBoolean(const std::string& path, bool in_value); |
| 247 | void SetInteger(const std::string& path, int in_value); |
| 248 | void SetReal(const std::string& path, double in_value); |
| 249 | void SetString(const std::string& path, const std::string& in_value); |
| 250 | void SetStringFromUTF16(const std::string& path, const string16& in_value); |
| 251 | /*DEPRECATED*/void SetBoolean(const std::wstring& path, bool in_value); |
| 252 | /*DEPRECATED*/void SetInteger(const std::wstring& path, int in_value); |
| 253 | /*DEPRECATED*/void SetReal(const std::wstring& path, double in_value); |
| 254 | /*DEPRECATED*/void SetString(const std::wstring& path, |
| 255 | const std::string& in_value); |
| 256 | /*DEPRECATED*/void SetString(const std::wstring& path, |
| 257 | const std::wstring& in_value); |
| 258 | /*DEPRECATED*/void SetStringFromUTF16(const std::wstring& path, |
| 259 | const string16& in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 260 | |
| 261 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 262 | // be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 263 | void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
| 264 | /*DEPRECATED*/void SetWithoutPathExpansion(const std::wstring& key, |
| 265 | Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 266 | |
| 267 | // Gets the Value associated with the given path starting from this object. |
| 268 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 269 | // into the next DictionaryValue down. If the path can be resolved |
| 270 | // 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] | 271 | // through the |out_value| parameter, and the function will return true. |
| 272 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 273 | // Note that the dictionary always owns the value that's returned. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 274 | bool Get(const std::string& path, Value** out_value) const; |
| 275 | /*DEPRECATED*/bool Get(const std::wstring& path, Value** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 276 | |
| 277 | // These are convenience forms of Get(). The value will be retrieved |
| 278 | // and the return value will be true if the path is valid and the value at |
| 279 | // 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] | 280 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 281 | bool GetInteger(const std::string& path, int* out_value) const; |
| 282 | bool GetReal(const std::string& path, double* out_value) const; |
| 283 | bool GetString(const std::string& path, std::string* out_value) const; |
| 284 | bool GetStringAsUTF16(const std::string& path, string16* out_value) const; |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 285 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 286 | bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
| 287 | bool GetDictionary(const std::string& path, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 288 | DictionaryValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 289 | bool GetList(const std::string& path, ListValue** out_value) const; |
| 290 | /*DEPRECATED*/bool GetBoolean(const std::wstring& path, |
| 291 | bool* out_value) const; |
| 292 | /*DEPRECATED*/bool GetInteger(const std::wstring& path, int* out_value) const; |
| 293 | /*DEPRECATED*/bool GetReal(const std::wstring& path, double* out_value) const; |
| 294 | // Use |GetStringAsUTF16()| instead: |
| 295 | /*DEPRECATED*/bool GetString(const std::string& path, |
| 296 | string16* out_value) const; |
| 297 | /*DEPRECATED*/bool GetString(const std::wstring& path, |
| 298 | std::string* out_value) const; |
| 299 | /*DEPRECATED*/bool GetString(const std::wstring& path, |
| 300 | std::wstring* out_value) const; |
| 301 | /*DEPRECATED*/bool GetStringAsUTF16(const std::wstring& path, |
| 302 | string16* out_value) const; |
| 303 | /*DEPRECATED*/bool GetBinary(const std::wstring& path, |
| 304 | BinaryValue** out_value) const; |
| 305 | /*DEPRECATED*/bool GetDictionary(const std::wstring& path, |
| 306 | DictionaryValue** out_value) const; |
| 307 | /*DEPRECATED*/bool GetList(const std::wstring& path, |
| 308 | ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 309 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 310 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 311 | // be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 312 | bool GetWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 313 | Value** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 314 | bool GetIntegerWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 315 | int* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 316 | bool GetStringWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 317 | std::string* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 318 | bool GetStringAsUTF16WithoutPathExpansion(const std::string& key, |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 319 | string16* out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 320 | bool GetDictionaryWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 321 | DictionaryValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 322 | bool GetListWithoutPathExpansion(const std::string& key, |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 323 | ListValue** out_value) const; |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 324 | /*DEPRECATED*/bool GetWithoutPathExpansion(const std::wstring& key, |
| 325 | Value** out_value) const; |
| 326 | /*DEPRECATED*/bool GetIntegerWithoutPathExpansion(const std::wstring& key, |
| 327 | int* out_value) const; |
| 328 | /*DEPRECATED*/bool GetStringWithoutPathExpansion( |
| 329 | const std::wstring& key, std::string* out_value) const; |
| 330 | /*DEPRECATED*/bool GetStringWithoutPathExpansion( |
| 331 | const std::wstring& key, std::wstring* out_value) const; |
| 332 | /*DEPRECATED*/bool GetStringAsUTF16WithoutPathExpansion( |
| 333 | const std::wstring& key, string16* out_value) const; |
| 334 | /*DEPRECATED*/bool GetDictionaryWithoutPathExpansion( |
| 335 | const std::wstring& key, DictionaryValue** out_value) const; |
| 336 | /*DEPRECATED*/bool GetListWithoutPathExpansion(const std::wstring& key, |
| 337 | ListValue** out_value) const; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 338 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 339 | // Removes the Value with the specified path from this dictionary (or one |
| 340 | // of its child dictionaries, if the path is more than just a local key). |
| 341 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 342 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 343 | // be deleted. This method returns true if |path| is a valid path; otherwise |
| 344 | // it will return false and the DictionaryValue object will be unchanged. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 345 | bool Remove(const std::string& path, Value** out_value); |
| 346 | /*DEPRECATED*/bool Remove(const std::wstring& path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 347 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 348 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 349 | // to be used as paths. |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 350 | bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value); |
| 351 | /*DEPRECATED*/bool RemoveWithoutPathExpansion(const std::wstring& key, |
| 352 | Value** out_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 353 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 354 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 355 | // the copy. This never returns NULL, even if |this| itself is empty. |
| 356 | DictionaryValue* DeepCopyWithoutEmptyChildren(); |
| 357 | |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 358 | // Merge a given dictionary into this dictionary. This is done recursively, |
| 359 | // i.e. any subdictionaries will be merged as well. In case of key collisions, |
| 360 | // the passed in dictionary takes precedence and data already present will be |
| 361 | // replaced. |
| 362 | void MergeDictionary(const DictionaryValue* dictionary); |
| 363 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 364 | // This class provides an iterator for the keys in the dictionary. |
| 365 | // It can't be used to modify the dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 366 | // |
| 367 | // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 368 | // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 369 | // keys have '.'s in them. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 370 | class key_iterator |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 371 | : private std::iterator<std::input_iterator_tag, const std::string> { |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 372 | public: |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 373 | explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
erg@google.com | bf6ce9f | 2010-01-27 08:08:02 +0900 | [diff] [blame] | 374 | key_iterator operator++() { |
| 375 | ++itr_; |
| 376 | return *this; |
| 377 | } |
viettrungluu@chromium.org | 178423d | 2010-07-31 04:47:47 +0900 | [diff] [blame] | 378 | const std::string& operator*() { return itr_->first; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 379 | bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 380 | bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 381 | |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 382 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 383 | ValueMap::const_iterator itr_; |
| 384 | }; |
| 385 | |
| 386 | key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 387 | key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 388 | |
| 389 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 390 | ValueMap dictionary_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 391 | |
| 392 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | // This type of Value represents a list of other Value values. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 396 | class ListValue : public Value { |
| 397 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 398 | ListValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 399 | ~ListValue(); |
| 400 | |
| 401 | // Subclassed methods |
| 402 | Value* DeepCopy() const; |
| 403 | virtual bool Equals(const Value* other) const; |
| 404 | |
| 405 | // Clears the contents of this ListValue |
| 406 | void Clear(); |
| 407 | |
| 408 | // Returns the number of Values in this list. |
| 409 | size_t GetSize() const { return list_.size(); } |
| 410 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 411 | // Returns whether the list is empty. |
| 412 | bool empty() const { return list_.empty(); } |
| 413 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 414 | // Sets the list item at the given index to be the Value specified by |
| 415 | // the value given. If the index beyond the current end of the list, null |
| 416 | // Values will be used to pad out the list. |
| 417 | // Returns true if successful, or false if the index was negative or |
| 418 | // the value is a null pointer. |
| 419 | bool Set(size_t index, Value* in_value); |
| 420 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 421 | // 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] | 422 | // only if the index falls within the current list range. |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 423 | // 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] | 424 | bool Get(size_t index, Value** out_value) const; |
| 425 | |
skerner@chromium.org | 53f71f0 | 2010-04-09 04:06:15 +0900 | [diff] [blame] | 426 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 427 | // only if the index is valid and the Value at that index can be returned |
| 428 | // in the specified form. |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 429 | bool GetBoolean(size_t index, bool* out_value) const; |
| 430 | bool GetInteger(size_t index, int* out_value) const; |
| 431 | bool GetReal(size_t index, double* out_value) const; |
| 432 | bool GetString(size_t index, std::string* out_value) const; |
estade@chromium.org | 8f158c3 | 2009-12-18 11:39:16 +0900 | [diff] [blame] | 433 | bool GetString(size_t index, std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 434 | bool GetStringAsUTF16(size_t index, string16* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 435 | bool GetBinary(size_t index, BinaryValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 436 | bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 437 | bool GetList(size_t index, ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 438 | |
| 439 | // Removes the Value with the specified index from this list. |
| 440 | // 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] | 441 | // 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] | 442 | // be deleted. This method returns true if |index| is valid; otherwise |
| 443 | // it will return false and the ListValue object will be unchanged. |
| 444 | bool Remove(size_t index, Value** out_value); |
| 445 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 446 | // Removes the first instance of |value| found in the list, if any, and |
| 447 | // 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] | 448 | int Remove(const Value& value); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 449 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 450 | // Appends a Value to the end of the list. |
| 451 | void Append(Value* in_value); |
| 452 | |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 453 | // Appends a Value if it's not already present. |
| 454 | // Returns true if successful, or false if the value was already present. |
| 455 | bool AppendIfNotPresent(Value* in_value); |
| 456 | |
erikkay@chromium.org | 9034a23 | 2009-08-29 05:26:05 +0900 | [diff] [blame] | 457 | // Insert a Value at index. |
| 458 | // Returns true if successful, or false if the index was out of range. |
| 459 | bool Insert(size_t index, Value* in_value); |
| 460 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 461 | // Iteration |
| 462 | typedef ValueVector::iterator iterator; |
| 463 | typedef ValueVector::const_iterator const_iterator; |
| 464 | |
| 465 | ListValue::iterator begin() { return list_.begin(); } |
| 466 | ListValue::iterator end() { return list_.end(); } |
| 467 | |
| 468 | ListValue::const_iterator begin() const { return list_.begin(); } |
| 469 | ListValue::const_iterator end() const { return list_.end(); } |
| 470 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 471 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 472 | ValueVector list_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 473 | |
| 474 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | // This interface is implemented by classes that know how to serialize and |
| 478 | // deserialize Value objects. |
| 479 | class ValueSerializer { |
| 480 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 481 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 482 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 483 | virtual bool Serialize(const Value& root) = 0; |
| 484 | |
| 485 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 486 | // 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] | 487 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 488 | // error_code will be set with the underlying error. |
| 489 | // If |error_message| is non-null, it will be filled in with a formatted |
| 490 | // error message including the location of the error if appropriate. |
| 491 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 492 | }; |
| 493 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 494 | #endif // BASE_VALUES_H_ |