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 | |
| 5 | // This file specifies a recursive data storage class called Value |
| 6 | // intended for storing setting and other persistable data. |
| 7 | // It includes the ability to specify (recursive) lists and dictionaries, so |
| 8 | // it's fairly expressive. However, the API is optimized for the common case, |
| 9 | // namely storing a hierarchical tree of simple values. Given a |
| 10 | // DictionaryValue root, you can easily do things like: |
| 11 | // |
| 12 | // root->SetString(L"global.pages.homepage", L"http://goateleporter.com"); |
| 13 | // std::wstring homepage = L"http://google.com"; // default/fallback value |
| 14 | // root->GetString(L"global.pages.homepage", &homepage); |
| 15 | // |
| 16 | // where "global" and "pages" are also DictionaryValues, and "homepage" |
| 17 | // is a string setting. If some elements of the path didn't exist yet, |
| 18 | // the SetString() method would create the missing elements and attach them |
| 19 | // to root before attaching the homepage value. |
| 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; |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 42 | typedef std::map<std::wstring, 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 | |
| 205 | class DictionaryValue : public Value { |
| 206 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 207 | DictionaryValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 208 | ~DictionaryValue(); |
| 209 | |
| 210 | // Subclassed methods |
| 211 | Value* DeepCopy() const; |
| 212 | virtual bool Equals(const Value* other) const; |
| 213 | |
| 214 | // Returns true if the current dictionary has a value for the given key. |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 215 | bool HasKeyASCII(const std::string& key) const; |
| 216 | // Deprecated version of the above. TODO: add a string16 version for Unicode. |
| 217 | // http://code.google.com/p/chromium/issues/detail?id=23581 |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 218 | bool HasKey(const std::wstring& key) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 219 | |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 220 | // Returns the number of Values in this dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 221 | size_t size() const { return dictionary_.size(); } |
| 222 | |
| 223 | // Returns whether the dictionary is empty. |
| 224 | bool empty() const { return dictionary_.empty(); } |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 225 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 226 | // Clears any current contents of this dictionary. |
deanm@google.com | 522c383 | 2008-08-04 22:56:25 +0900 | [diff] [blame] | 227 | void Clear(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 228 | |
| 229 | // Sets the Value associated with the given path starting from this object. |
| 230 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 231 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 232 | // within a key, but there are no other restrictions on keys. |
| 233 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 234 | // a DictionaryValue, a new DictionaryValue will be created and attached |
| 235 | // to the path in that location. |
jcampan@chromium.org | 9b0f7be | 2009-04-15 09:17:53 +0900 | [diff] [blame] | 236 | // Note that the dictionary takes ownership of the value referenced by |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 237 | // |in_value|, and therefore |in_value| must be non-NULL. |
| 238 | void Set(const std::wstring& path, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 239 | |
| 240 | // Convenience forms of Set(). These methods will replace any existing |
| 241 | // value at that path, even if it has a different type. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 242 | void SetBoolean(const std::wstring& path, bool in_value); |
| 243 | void SetInteger(const std::wstring& path, int in_value); |
| 244 | void SetReal(const std::wstring& path, double in_value); |
| 245 | void SetString(const std::wstring& path, const std::string& in_value); |
| 246 | void SetString(const std::wstring& path, const std::wstring& in_value); |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 247 | void SetStringFromUTF16(const std::wstring& path, const string16& in_value); |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 248 | |
| 249 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 250 | // be used as paths. |
| 251 | void SetWithoutPathExpansion(const std::wstring& key, Value* in_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 252 | |
| 253 | // Gets the Value associated with the given path starting from this object. |
| 254 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 255 | // into the next DictionaryValue down. If the path can be resolved |
| 256 | // 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] | 257 | // through the |out_value| parameter, and the function will return true. |
| 258 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 259 | // Note that the dictionary always owns the value that's returned. |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 260 | bool Get(const std::wstring& path, Value** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 261 | |
| 262 | // These are convenience forms of Get(). The value will be retrieved |
| 263 | // and the return value will be true if the path is valid and the value at |
| 264 | // the end of the path can be returned in the form specified. |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 265 | bool GetBoolean(const std::wstring& path, bool* out_value) const; |
| 266 | bool GetInteger(const std::wstring& path, int* out_value) const; |
| 267 | bool GetReal(const std::wstring& path, double* out_value) const; |
evan@chromium.org | d9ab8de | 2010-02-19 22:32:16 +0900 | [diff] [blame] | 268 | bool GetString(const std::string& path, string16* out_value) const; |
| 269 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
| 270 | // TODO: deprecate wstring accessors. |
| 271 | // http://code.google.com/p/chromium/issues/detail?id=23581 |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 272 | bool GetString(const std::wstring& path, std::string* out_value) const; |
| 273 | bool GetString(const std::wstring& path, std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 274 | bool GetStringAsUTF16(const std::wstring& path, string16* out_value) const; |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 275 | bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; |
| 276 | bool GetDictionary(const std::wstring& path, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 277 | DictionaryValue** out_value) const; |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 278 | bool GetList(const std::wstring& path, ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 279 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 280 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 281 | // be used as paths. |
| 282 | bool GetWithoutPathExpansion(const std::wstring& key, |
| 283 | Value** out_value) const; |
| 284 | bool GetIntegerWithoutPathExpansion(const std::wstring& path, |
| 285 | int* out_value) const; |
| 286 | bool GetStringWithoutPathExpansion(const std::wstring& path, |
| 287 | std::string* out_value) const; |
| 288 | bool GetStringWithoutPathExpansion(const std::wstring& path, |
| 289 | std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 290 | bool GetStringAsUTF16WithoutPathExpansion(const std::wstring& path, |
| 291 | string16* out_value) const; |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 292 | bool GetDictionaryWithoutPathExpansion(const std::wstring& path, |
| 293 | DictionaryValue** out_value) const; |
| 294 | bool GetListWithoutPathExpansion(const std::wstring& path, |
| 295 | ListValue** out_value) const; |
| 296 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 297 | // Removes the Value with the specified path from this dictionary (or one |
| 298 | // of its child dictionaries, if the path is more than just a local key). |
| 299 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 300 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 301 | // be deleted. This method returns true if |path| is a valid path; otherwise |
| 302 | // it will return false and the DictionaryValue object will be unchanged. |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 303 | bool Remove(const std::wstring& path, Value** out_value); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 304 | |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 305 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 306 | // to be used as paths. |
| 307 | bool RemoveWithoutPathExpansion(const std::wstring& key, Value** out_value); |
| 308 | |
tony@chromium.org | e4948ab | 2009-12-02 09:20:32 +0900 | [diff] [blame] | 309 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 310 | // the copy. This never returns NULL, even if |this| itself is empty. |
| 311 | DictionaryValue* DeepCopyWithoutEmptyChildren(); |
| 312 | |
mnissler@chromium.org | 17cee0e | 2010-05-14 22:17:40 +0900 | [diff] [blame] | 313 | // Merge a given dictionary into this dictionary. This is done recursively, |
| 314 | // i.e. any subdictionaries will be merged as well. In case of key collisions, |
| 315 | // the passed in dictionary takes precedence and data already present will be |
| 316 | // replaced. |
| 317 | void MergeDictionary(const DictionaryValue* dictionary); |
| 318 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 319 | // This class provides an iterator for the keys in the dictionary. |
| 320 | // It can't be used to modify the dictionary. |
pkasting@chromium.org | 36515db | 2009-11-26 05:47:52 +0900 | [diff] [blame] | 321 | // |
| 322 | // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 323 | // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 324 | // keys have '.'s in them. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 325 | class key_iterator |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 326 | : private std::iterator<std::input_iterator_tag, const std::wstring> { |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 327 | public: |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 328 | explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
erg@google.com | bf6ce9f | 2010-01-27 08:08:02 +0900 | [diff] [blame] | 329 | key_iterator operator++() { |
| 330 | ++itr_; |
| 331 | return *this; |
| 332 | } |
nsylvain@chromium.org | 1242667 | 2009-03-04 07:59:43 +0900 | [diff] [blame] | 333 | const std::wstring& operator*() { return itr_->first; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 334 | bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 335 | bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 336 | |
jar@chromium.org | af39a37 | 2009-02-18 07:50:14 +0900 | [diff] [blame] | 337 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 338 | ValueMap::const_iterator itr_; |
| 339 | }; |
| 340 | |
| 341 | key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 342 | key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 343 | |
| 344 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 345 | ValueMap dictionary_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 346 | |
| 347 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | // This type of Value represents a list of other Value values. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 351 | class ListValue : public Value { |
| 352 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 353 | ListValue(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 354 | ~ListValue(); |
| 355 | |
| 356 | // Subclassed methods |
| 357 | Value* DeepCopy() const; |
| 358 | virtual bool Equals(const Value* other) const; |
| 359 | |
| 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; |
| 386 | bool GetReal(size_t index, double* out_value) const; |
| 387 | bool GetString(size_t index, std::string* out_value) const; |
estade@chromium.org | 8f158c3 | 2009-12-18 11:39:16 +0900 | [diff] [blame] | 388 | bool GetString(size_t index, std::wstring* out_value) const; |
munjal@chromium.org | 3b2d3a4 | 2010-01-16 05:09:03 +0900 | [diff] [blame] | 389 | bool GetStringAsUTF16(size_t index, string16* out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 390 | bool GetBinary(size_t index, BinaryValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 391 | bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
aa@chromium.org | c93b02f | 2009-01-21 06:05:32 +0900 | [diff] [blame] | 392 | bool GetList(size_t index, ListValue** out_value) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 393 | |
| 394 | // Removes the Value with the specified index from this list. |
| 395 | // 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] | 396 | // 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] | 397 | // be deleted. This method returns true if |index| is valid; otherwise |
| 398 | // it will return false and the ListValue object will be unchanged. |
| 399 | bool Remove(size_t index, Value** out_value); |
| 400 | |
pkasting@chromium.org | dfe6a69 | 2009-12-01 04:59:11 +0900 | [diff] [blame] | 401 | // Removes the first instance of |value| found in the list, if any, and |
| 402 | // 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] | 403 | int Remove(const Value& value); |
pkasting@chromium.org | 727139c | 2009-05-09 09:33:04 +0900 | [diff] [blame] | 404 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 405 | // Appends a Value to the end of the list. |
| 406 | void Append(Value* in_value); |
| 407 | |
zork@chromium.org | b5f742b | 2010-04-13 06:48:10 +0900 | [diff] [blame] | 408 | // Appends a Value if it's not already present. |
| 409 | // Returns true if successful, or false if the value was already present. |
| 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 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 416 | // Iteration |
| 417 | typedef ValueVector::iterator iterator; |
| 418 | typedef ValueVector::const_iterator const_iterator; |
| 419 | |
| 420 | ListValue::iterator begin() { return list_.begin(); } |
| 421 | ListValue::iterator end() { return list_.end(); } |
| 422 | |
| 423 | ListValue::const_iterator begin() const { return list_.begin(); } |
| 424 | ListValue::const_iterator end() const { return list_.end(); } |
| 425 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 426 | private: |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 427 | ValueVector list_; |
jcampan@chromium.org | d110567 | 2009-09-15 01:56:12 +0900 | [diff] [blame] | 428 | |
| 429 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | // This interface is implemented by classes that know how to serialize and |
| 433 | // deserialize Value objects. |
| 434 | class ValueSerializer { |
| 435 | public: |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 436 | virtual ~ValueSerializer(); |
mmentovai@google.com | 4a5b627 | 2008-08-07 00:46:59 +0900 | [diff] [blame] | 437 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 438 | virtual bool Serialize(const Value& root) = 0; |
| 439 | |
| 440 | // This method deserializes the subclass-specific format into a Value object. |
aa@chromium.org | ca9c79e | 2008-12-30 04:59:08 +0900 | [diff] [blame] | 441 | // 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] | 442 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 443 | // error_code will be set with the underlying error. |
| 444 | // If |error_message| is non-null, it will be filled in with a formatted |
| 445 | // error message including the location of the error if appropriate. |
| 446 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 447 | }; |
| 448 | |
darin@chromium.org | 01f1070 | 2008-09-27 05:22:42 +0900 | [diff] [blame] | 449 | #endif // BASE_VALUES_H_ |