[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 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 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 21 | #ifndef BASE_VALUES_H_ |
| 22 | #define BASE_VALUES_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 23 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | |
| 25 | #include <iterator> |
| 26 | #include <map> |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 27 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include "base/basictypes.h" |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 31 | #include "base/string16.h" |
| 32 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 42 | typedef std::map<std::string, Value*> ValueMap; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 58 | static Value* CreateStringValue(const std::string& in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 59 | static Value* CreateStringValue(const std::wstring& in_value); |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 60 | static Value* CreateStringValueFromUTF16(const string16& in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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, |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [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 | d7cae12 | 2008-07-26 21:49:38 | [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; |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 94 | virtual bool GetAsString(std::string* out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | virtual bool GetAsString(std::wstring* out_value) const; |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 96 | virtual bool GetAsUTF16(string16* out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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. |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 108 | explicit Value(ValueType type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | |
| 110 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | Value(); |
| 112 | |
| 113 | ValueType type_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(Value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // FundamentalValue represents the simple fundamental types of values. |
| 119 | class FundamentalValue : public Value { |
| 120 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 121 | explicit FundamentalValue(bool in_value); |
| 122 | explicit FundamentalValue(int in_value); |
| 123 | explicit FundamentalValue(double in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | union { |
| 135 | bool boolean_value_; |
| 136 | int integer_value_; |
| 137 | double real_value_; |
| 138 | }; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 139 | |
| 140 | DISALLOW_COPY_AND_ASSIGN(FundamentalValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | class StringValue : public Value { |
| 144 | public: |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 145 | // Initializes a StringValue with a UTF-8 narrow character string. |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 146 | explicit StringValue(const std::string& in_value); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 147 | |
| 148 | // Initializes a StringValue with a wide character string. |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 149 | explicit StringValue(const std::wstring& in_value); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 150 | |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | ~StringValue(); |
| 157 | |
| 158 | // Subclassed methods |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 159 | bool GetAsString(std::string* out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | bool GetAsString(std::wstring* out_value) const; |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 161 | bool GetAsUTF16(string16* out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | Value* DeepCopy() const; |
| 163 | virtual bool Equals(const Value* other) const; |
| 164 | |
| 165 | private: |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 166 | std::string value_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 167 | |
| 168 | DISALLOW_COPY_AND_ASSIGN(StringValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class BinaryValue: public Value { |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 172 | public: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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. |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 182 | static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 184 | ~BinaryValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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_; } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 192 | const char* GetBuffer() const { return buffer_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 193 | |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 194 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | // Constructor is private so that only objects with valid buffer pointers |
| 196 | // and size values can be created. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 197 | BinaryValue(char* buffer, size_t size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | |
| 199 | char* buffer_; |
| 200 | size_t size_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 201 | |
| 202 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | }; |
| 204 | |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 205 | // TODO(viettrungluu): Things marked DEPRECATED will be removed. crbug.com/23581 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | class DictionaryValue : public Value { |
| 207 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 208 | DictionaryValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | ~DictionaryValue(); |
| 210 | |
| 211 | // Subclassed methods |
| 212 | Value* DeepCopy() const; |
| 213 | virtual bool Equals(const Value* other) const; |
| 214 | |
| 215 | // Returns true if the current dictionary has a value for the given key. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 216 | bool HasKey(const std::string& key) const; |
| 217 | /*DEPRECATED*/bool HasKeyASCII(const std::string& key) const; |
| 218 | /*DEPRECATED*/bool HasKey(const std::wstring& key) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 219 | |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 220 | // Returns the number of Values in this dictionary. |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [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(); } |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 225 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | // Clears any current contents of this dictionary. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 227 | void Clear(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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. |
[email protected] | fb804132c | 2009-04-15 00:17:53 | [diff] [blame] | 236 | // Note that the dictionary takes ownership of the value referenced by |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 237 | // |in_value|, and therefore |in_value| must be non-NULL. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 238 | void Set(const std::string& path, Value* in_value); |
| 239 | /*DEPRECATED*/void Set(const std::wstring& path, Value* in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 240 | |
| 241 | // Convenience forms of Set(). These methods will replace any existing |
| 242 | // value at that path, even if it has a different type. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 243 | void SetBoolean(const std::string& path, bool in_value); |
| 244 | void SetInteger(const std::string& path, int in_value); |
| 245 | void SetReal(const std::string& path, double in_value); |
| 246 | void SetString(const std::string& path, const std::string& in_value); |
| 247 | void SetStringFromUTF16(const std::string& path, const string16& in_value); |
| 248 | /*DEPRECATED*/void SetBoolean(const std::wstring& path, bool in_value); |
| 249 | /*DEPRECATED*/void SetInteger(const std::wstring& path, int in_value); |
| 250 | /*DEPRECATED*/void SetReal(const std::wstring& path, double in_value); |
| 251 | /*DEPRECATED*/void SetString(const std::wstring& path, |
| 252 | const std::string& in_value); |
| 253 | /*DEPRECATED*/void SetString(const std::wstring& path, |
| 254 | const std::wstring& in_value); |
| 255 | /*DEPRECATED*/void SetStringFromUTF16(const std::wstring& path, |
| 256 | const string16& in_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 257 | |
| 258 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
| 259 | // be used as paths. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 260 | void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
| 261 | /*DEPRECATED*/void SetWithoutPathExpansion(const std::wstring& key, |
| 262 | Value* in_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 263 | |
| 264 | // Gets the Value associated with the given path starting from this object. |
| 265 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 266 | // into the next DictionaryValue down. If the path can be resolved |
| 267 | // successfully, the value for the last key in the path will be returned |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 268 | // through the |out_value| parameter, and the function will return true. |
| 269 | // Otherwise, it will return false and |out_value| will be untouched. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 270 | // Note that the dictionary always owns the value that's returned. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 271 | bool Get(const std::string& path, Value** out_value) const; |
| 272 | /*DEPRECATED*/bool Get(const std::wstring& path, Value** out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 273 | |
| 274 | // These are convenience forms of Get(). The value will be retrieved |
| 275 | // and the return value will be true if the path is valid and the value at |
| 276 | // the end of the path can be returned in the form specified. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 277 | bool GetBoolean(const std::string& path, bool* out_value) const; |
| 278 | bool GetInteger(const std::string& path, int* out_value) const; |
| 279 | bool GetReal(const std::string& path, double* out_value) const; |
| 280 | bool GetString(const std::string& path, std::string* out_value) const; |
| 281 | bool GetStringAsUTF16(const std::string& path, string16* out_value) const; |
[email protected] | 9430e4b | 2010-02-19 13:32:16 | [diff] [blame] | 282 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 283 | bool GetBinary(const std::string& path, BinaryValue** out_value) const; |
| 284 | bool GetDictionary(const std::string& path, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 285 | DictionaryValue** out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 286 | bool GetList(const std::string& path, ListValue** out_value) const; |
| 287 | /*DEPRECATED*/bool GetBoolean(const std::wstring& path, |
| 288 | bool* out_value) const; |
| 289 | /*DEPRECATED*/bool GetInteger(const std::wstring& path, int* out_value) const; |
| 290 | /*DEPRECATED*/bool GetReal(const std::wstring& path, double* out_value) const; |
| 291 | // Use |GetStringAsUTF16()| instead: |
| 292 | /*DEPRECATED*/bool GetString(const std::string& path, |
| 293 | string16* out_value) const; |
| 294 | /*DEPRECATED*/bool GetString(const std::wstring& path, |
| 295 | std::string* out_value) const; |
| 296 | /*DEPRECATED*/bool GetString(const std::wstring& path, |
| 297 | std::wstring* out_value) const; |
| 298 | /*DEPRECATED*/bool GetStringAsUTF16(const std::wstring& path, |
| 299 | string16* out_value) const; |
| 300 | /*DEPRECATED*/bool GetBinary(const std::wstring& path, |
| 301 | BinaryValue** out_value) const; |
| 302 | /*DEPRECATED*/bool GetDictionary(const std::wstring& path, |
| 303 | DictionaryValue** out_value) const; |
| 304 | /*DEPRECATED*/bool GetList(const std::wstring& path, |
| 305 | ListValue** out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 306 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 307 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
| 308 | // be used as paths. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 309 | bool GetWithoutPathExpansion(const std::string& key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 310 | Value** out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 311 | bool GetIntegerWithoutPathExpansion(const std::string& key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 312 | int* out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 313 | bool GetStringWithoutPathExpansion(const std::string& key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 314 | std::string* out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 315 | bool GetStringAsUTF16WithoutPathExpansion(const std::string& key, |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 316 | string16* out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 317 | bool GetDictionaryWithoutPathExpansion(const std::string& key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 318 | DictionaryValue** out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 319 | bool GetListWithoutPathExpansion(const std::string& key, |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 320 | ListValue** out_value) const; |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 321 | /*DEPRECATED*/bool GetWithoutPathExpansion(const std::wstring& key, |
| 322 | Value** out_value) const; |
| 323 | /*DEPRECATED*/bool GetIntegerWithoutPathExpansion(const std::wstring& key, |
| 324 | int* out_value) const; |
| 325 | /*DEPRECATED*/bool GetStringWithoutPathExpansion( |
| 326 | const std::wstring& key, std::string* out_value) const; |
| 327 | /*DEPRECATED*/bool GetStringWithoutPathExpansion( |
| 328 | const std::wstring& key, std::wstring* out_value) const; |
| 329 | /*DEPRECATED*/bool GetStringAsUTF16WithoutPathExpansion( |
| 330 | const std::wstring& key, string16* out_value) const; |
| 331 | /*DEPRECATED*/bool GetDictionaryWithoutPathExpansion( |
| 332 | const std::wstring& key, DictionaryValue** out_value) const; |
| 333 | /*DEPRECATED*/bool GetListWithoutPathExpansion(const std::wstring& key, |
| 334 | ListValue** out_value) const; |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 335 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 336 | // Removes the Value with the specified path from this dictionary (or one |
| 337 | // of its child dictionaries, if the path is more than just a local key). |
| 338 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 339 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 340 | // be deleted. This method returns true if |path| is a valid path; otherwise |
| 341 | // it will return false and the DictionaryValue object will be unchanged. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 342 | bool Remove(const std::string& path, Value** out_value); |
| 343 | /*DEPRECATED*/bool Remove(const std::wstring& path, Value** out_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 345 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
| 346 | // to be used as paths. |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 347 | bool RemoveWithoutPathExpansion(const std::string& key, Value** out_value); |
| 348 | /*DEPRECATED*/bool RemoveWithoutPathExpansion(const std::wstring& key, |
| 349 | Value** out_value); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 350 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 351 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
| 352 | // the copy. This never returns NULL, even if |this| itself is empty. |
| 353 | DictionaryValue* DeepCopyWithoutEmptyChildren(); |
| 354 | |
[email protected] | c378cca | 2010-05-14 13:17:40 | [diff] [blame] | 355 | // Merge a given dictionary into this dictionary. This is done recursively, |
| 356 | // i.e. any subdictionaries will be merged as well. In case of key collisions, |
| 357 | // the passed in dictionary takes precedence and data already present will be |
| 358 | // replaced. |
| 359 | void MergeDictionary(const DictionaryValue* dictionary); |
| 360 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 361 | // This class provides an iterator for the keys in the dictionary. |
| 362 | // It can't be used to modify the dictionary. |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 363 | // |
| 364 | // YOU SHOULD ALWAYS USE THE XXXWithoutPathExpansion() APIs WITH THESE, NOT |
| 365 | // THE NORMAL XXX() APIs. This makes sure things will work correctly if any |
| 366 | // keys have '.'s in them. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 367 | class key_iterator |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 368 | : private std::iterator<std::input_iterator_tag, const std::string> { |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 369 | public: |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 370 | explicit key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 371 | key_iterator operator++() { |
| 372 | ++itr_; |
| 373 | return *this; |
| 374 | } |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 375 | const std::string& operator*() { return itr_->first; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 376 | bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 377 | bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 378 | |
[email protected] | 03c5e86 | 2009-02-17 22:50:14 | [diff] [blame] | 379 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 380 | ValueMap::const_iterator itr_; |
| 381 | }; |
| 382 | |
| 383 | key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 384 | key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 385 | |
| 386 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 387 | ValueMap dictionary_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 388 | |
| 389 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | // This type of Value represents a list of other Value values. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 393 | class ListValue : public Value { |
| 394 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 395 | ListValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 396 | ~ListValue(); |
| 397 | |
| 398 | // Subclassed methods |
| 399 | Value* DeepCopy() const; |
| 400 | virtual bool Equals(const Value* other) const; |
| 401 | |
| 402 | // Clears the contents of this ListValue |
| 403 | void Clear(); |
| 404 | |
| 405 | // Returns the number of Values in this list. |
| 406 | size_t GetSize() const { return list_.size(); } |
| 407 | |
[email protected] | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 408 | // Returns whether the list is empty. |
| 409 | bool empty() const { return list_.empty(); } |
| 410 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 411 | // Sets the list item at the given index to be the Value specified by |
| 412 | // the value given. If the index beyond the current end of the list, null |
| 413 | // Values will be used to pad out the list. |
| 414 | // Returns true if successful, or false if the index was negative or |
| 415 | // the value is a null pointer. |
| 416 | bool Set(size_t index, Value* in_value); |
| 417 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 418 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 419 | // only if the index falls within the current list range. |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 420 | // Note that the list always owns the Value passed out via |out_value|. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 421 | bool Get(size_t index, Value** out_value) const; |
| 422 | |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 423 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
| 424 | // only if the index is valid and the Value at that index can be returned |
| 425 | // in the specified form. |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 426 | bool GetBoolean(size_t index, bool* out_value) const; |
| 427 | bool GetInteger(size_t index, int* out_value) const; |
| 428 | bool GetReal(size_t index, double* out_value) const; |
| 429 | bool GetString(size_t index, std::string* out_value) const; |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 430 | bool GetString(size_t index, std::wstring* out_value) const; |
[email protected] | 9101ef1e | 2010-01-15 20:09:03 | [diff] [blame] | 431 | bool GetStringAsUTF16(size_t index, string16* out_value) const; |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 432 | bool GetBinary(size_t index, BinaryValue** out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 433 | bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 434 | bool GetList(size_t index, ListValue** out_value) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 435 | |
| 436 | // Removes the Value with the specified index from this list. |
| 437 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
[email protected] | b930d13 | 2009-01-05 18:37:51 | [diff] [blame] | 438 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 439 | // be deleted. This method returns true if |index| is valid; otherwise |
| 440 | // it will return false and the ListValue object will be unchanged. |
| 441 | bool Remove(size_t index, Value** out_value); |
| 442 | |
[email protected] | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 443 | // Removes the first instance of |value| found in the list, if any, and |
| 444 | // deletes it. Returns the index that it was located at (-1 for not present). |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 445 | int Remove(const Value& value); |
[email protected] | e7f5c6f | 2009-05-09 00:33:04 | [diff] [blame] | 446 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 447 | // Appends a Value to the end of the list. |
| 448 | void Append(Value* in_value); |
| 449 | |
[email protected] | 84064220 | 2010-04-12 21:48:10 | [diff] [blame] | 450 | // Appends a Value if it's not already present. |
| 451 | // Returns true if successful, or false if the value was already present. |
| 452 | bool AppendIfNotPresent(Value* in_value); |
| 453 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 454 | // Insert a Value at index. |
| 455 | // Returns true if successful, or false if the index was out of range. |
| 456 | bool Insert(size_t index, Value* in_value); |
| 457 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 458 | // Iteration |
| 459 | typedef ValueVector::iterator iterator; |
| 460 | typedef ValueVector::const_iterator const_iterator; |
| 461 | |
| 462 | ListValue::iterator begin() { return list_.begin(); } |
| 463 | ListValue::iterator end() { return list_.end(); } |
| 464 | |
| 465 | ListValue::const_iterator begin() const { return list_.begin(); } |
| 466 | ListValue::const_iterator end() const { return list_.end(); } |
| 467 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 468 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 469 | ValueVector list_; |
[email protected] | 7867cd0 | 2009-09-14 16:56:12 | [diff] [blame] | 470 | |
| 471 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | // This interface is implemented by classes that know how to serialize and |
| 475 | // deserialize Value objects. |
| 476 | class ValueSerializer { |
| 477 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 478 | virtual ~ValueSerializer(); |
[email protected] | abb9d0c | 2008-08-06 15:46:59 | [diff] [blame] | 479 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 480 | virtual bool Serialize(const Value& root) = 0; |
| 481 | |
| 482 | // This method deserializes the subclass-specific format into a Value object. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 483 | // If the return value is non-NULL, the caller takes ownership of returned |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 484 | // Value. If the return value is NULL, and if error_code is non-NULL, |
| 485 | // error_code will be set with the underlying error. |
| 486 | // If |error_message| is non-null, it will be filled in with a formatted |
| 487 | // error message including the location of the error if appropriate. |
| 488 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 489 | }; |
| 490 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame] | 491 | #endif // BASE_VALUES_H_ |