license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | |
| 24 | #include <iterator> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "base/basictypes.h" |
| 30 | |
| 31 | class Value; |
| 32 | class FundamentalValue; |
| 33 | class StringValue; |
| 34 | class BinaryValue; |
| 35 | class DictionaryValue; |
| 36 | class ListValue; |
| 37 | |
| 38 | typedef std::vector<Value*> ValueVector; |
| 39 | typedef std::map<std::wstring, Value*> ValueMap; |
| 40 | |
| 41 | // The Value class is the base class for Values. A Value can be |
| 42 | // instantiated via the Create*Value() factory methods, or by directly |
| 43 | // creating instances of the subclasses. |
| 44 | class Value { |
| 45 | public: |
| 46 | virtual ~Value(); |
| 47 | |
| 48 | // Convenience methods for creating Value objects for various |
| 49 | // kinds of values without thinking about which class implements them. |
| 50 | // These can always be expected to return a valid Value*. |
| 51 | static Value* CreateNullValue(); |
| 52 | static Value* CreateBooleanValue(bool in_value); |
| 53 | static Value* CreateIntegerValue(int in_value); |
| 54 | static Value* CreateRealValue(double in_value); |
| 55 | static Value* CreateStringValue(const std::wstring& in_value); |
| 56 | |
| 57 | // This one can return NULL if the input isn't valid. If the return value |
| 58 | // is non-null, the new object has taken ownership of the buffer pointer. |
| 59 | static BinaryValue* CreateBinaryValue(char* buffer, size_t size); |
| 60 | |
| 61 | typedef enum { |
| 62 | TYPE_NULL = 0, |
| 63 | TYPE_BOOLEAN, |
| 64 | TYPE_INTEGER, |
| 65 | TYPE_REAL, |
| 66 | TYPE_STRING, |
| 67 | TYPE_BINARY, |
| 68 | TYPE_DICTIONARY, |
| 69 | TYPE_LIST |
| 70 | } ValueType; |
| 71 | |
| 72 | // Returns the type of the value stored by the current Value object. |
| 73 | // Each type will be implemented by only one subclass of Value, so it's |
| 74 | // safe to use the ValueType to determine whether you can cast from |
| 75 | // Value* to (Implementing Class)*. Also, a Value object never changes |
| 76 | // its type after construction. |
| 77 | ValueType GetType() const { return type_; } |
| 78 | |
| 79 | // Returns true if the current object represents a given type. |
| 80 | bool IsType(ValueType type) const { return type == type_; } |
| 81 | |
| 82 | // These methods allow the convenient retrieval of settings. |
| 83 | // If the current setting object can be converted into the given type, |
| 84 | // the value is returned through the "value" parameter and true is returned; |
| 85 | // otherwise, false is returned and "value" is unchanged. |
| 86 | virtual bool GetAsBoolean(bool* out_value) const; |
| 87 | virtual bool GetAsInteger(int* out_value) const; |
| 88 | virtual bool GetAsReal(double* out_value) const; |
| 89 | virtual bool GetAsString(std::wstring* out_value) const; |
| 90 | |
| 91 | // This creates a deep copy of the entire Value tree, and returns a pointer |
| 92 | // to the copy. The caller gets ownership of the copy, of course. |
| 93 | virtual Value* DeepCopy() const; |
| 94 | |
| 95 | // Compares if two Value objects have equal contents. |
| 96 | virtual bool Equals(const Value* other) const; |
| 97 | |
| 98 | protected: |
| 99 | // This isn't safe for end-users (they should use the Create*Value() |
| 100 | // static methods above), but it's useful for subclasses. |
| 101 | Value(ValueType type) : type_(type) {} |
| 102 | |
| 103 | private: |
| 104 | DISALLOW_EVIL_CONSTRUCTORS(Value); |
| 105 | Value(); |
| 106 | |
| 107 | ValueType type_; |
| 108 | }; |
| 109 | |
| 110 | // FundamentalValue represents the simple fundamental types of values. |
| 111 | class FundamentalValue : public Value { |
| 112 | public: |
| 113 | FundamentalValue(bool in_value) |
| 114 | : Value(TYPE_BOOLEAN), boolean_value_(in_value) {} |
| 115 | FundamentalValue(int in_value) |
| 116 | : Value(TYPE_INTEGER), integer_value_(in_value) {} |
| 117 | FundamentalValue(double in_value) |
| 118 | : Value(TYPE_REAL), real_value_(in_value) {} |
| 119 | ~FundamentalValue(); |
| 120 | |
| 121 | // Subclassed methods |
| 122 | virtual bool GetAsBoolean(bool* out_value) const; |
| 123 | virtual bool GetAsInteger(int* out_value) const; |
| 124 | virtual bool GetAsReal(double* out_value) const; |
| 125 | virtual Value* DeepCopy() const; |
| 126 | virtual bool Equals(const Value* other) const; |
| 127 | |
| 128 | private: |
| 129 | DISALLOW_EVIL_CONSTRUCTORS(FundamentalValue); |
| 130 | |
| 131 | union { |
| 132 | bool boolean_value_; |
| 133 | int integer_value_; |
| 134 | double real_value_; |
| 135 | }; |
| 136 | }; |
| 137 | |
| 138 | class StringValue : public Value { |
| 139 | public: |
| 140 | StringValue(const std::wstring& in_value) |
| 141 | : Value(TYPE_STRING), value_(in_value) {} |
| 142 | ~StringValue(); |
| 143 | |
| 144 | // Subclassed methods |
| 145 | bool GetAsString(std::wstring* out_value) const; |
| 146 | Value* DeepCopy() const; |
| 147 | virtual bool Equals(const Value* other) const; |
| 148 | |
| 149 | private: |
| 150 | DISALLOW_EVIL_CONSTRUCTORS(StringValue); |
| 151 | |
| 152 | std::wstring value_; |
| 153 | }; |
| 154 | |
| 155 | class BinaryValue: public Value { |
| 156 | public: |
| 157 | // Creates a Value to represent a binary buffer. The new object takes |
| 158 | // ownership of the pointer passed in, if successful. |
| 159 | // Returns NULL if buffer is NULL. |
| 160 | static BinaryValue* Create(char* buffer, size_t size); |
| 161 | |
| 162 | // For situations where you want to keep ownership of your buffer, this |
| 163 | // factory method creates a new BinaryValue by copying the contents of the |
| 164 | // buffer that's passed in. |
| 165 | // Returns NULL if buffer is NULL. |
| 166 | static BinaryValue* CreateWithCopiedBuffer(char* buffer, size_t size); |
| 167 | |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 168 | ~BinaryValue(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | |
| 170 | // Subclassed methods |
| 171 | Value* DeepCopy() const; |
| 172 | virtual bool Equals(const Value* other) const; |
| 173 | |
| 174 | size_t GetSize() const { return size_; } |
| 175 | char* GetBuffer() { return buffer_; } |
| 176 | |
| 177 | private: |
| 178 | DISALLOW_EVIL_CONSTRUCTORS(BinaryValue); |
| 179 | |
| 180 | // Constructor is private so that only objects with valid buffer pointers |
| 181 | // and size values can be created. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 182 | BinaryValue(char* buffer, size_t size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | |
| 184 | char* buffer_; |
| 185 | size_t size_; |
| 186 | }; |
| 187 | |
| 188 | class DictionaryValue : public Value { |
| 189 | public: |
| 190 | DictionaryValue() : Value(TYPE_DICTIONARY) {} |
| 191 | ~DictionaryValue(); |
| 192 | |
| 193 | // Subclassed methods |
| 194 | Value* DeepCopy() const; |
| 195 | virtual bool Equals(const Value* other) const; |
| 196 | |
| 197 | // Returns true if the current dictionary has a value for the given key. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 198 | bool HasKey(const std::wstring& key); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | |
| 200 | // Clears any current contents of this dictionary. |
[email protected] | af5ed4a | 2008-08-04 13:56:25 | [diff] [blame] | 201 | void Clear(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | |
| 203 | // Sets the Value associated with the given path starting from this object. |
| 204 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 205 | // into the next DictionaryValue down. Obviously, "." can't be used |
| 206 | // within a key, but there are no other restrictions on keys. |
| 207 | // If the key at any step of the way doesn't exist, or exists but isn't |
| 208 | // a DictionaryValue, a new DictionaryValue will be created and attached |
| 209 | // to the path in that location. |
| 210 | // Note that the dictionary takes ownership of the value |
| 211 | // referenced by in_value. |
| 212 | bool Set(const std::wstring& path, Value* in_value); |
| 213 | |
| 214 | // Convenience forms of Set(). These methods will replace any existing |
| 215 | // value at that path, even if it has a different type. |
| 216 | bool SetBoolean(const std::wstring& path, bool in_value); |
| 217 | bool SetInteger(const std::wstring& path, int in_value); |
| 218 | bool SetReal(const std::wstring& path, double in_value); |
| 219 | bool SetString(const std::wstring& path, const std::wstring& in_value); |
| 220 | |
| 221 | // Gets the Value associated with the given path starting from this object. |
| 222 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
| 223 | // into the next DictionaryValue down. If the path can be resolved |
| 224 | // successfully, the value for the last key in the path will be returned |
| 225 | // through the "value" parameter, and the function will return true. |
| 226 | // Otherwise, it will return false and "value" will be untouched. |
| 227 | // Note that the dictionary always owns the value that's returned. |
| 228 | bool Get(const std::wstring& path, Value** out_value) const; |
| 229 | |
| 230 | // These are convenience forms of Get(). The value will be retrieved |
| 231 | // and the return value will be true if the path is valid and the value at |
| 232 | // the end of the path can be returned in the form specified. |
| 233 | bool GetBoolean(const std::wstring& path, bool* out_value) const; |
| 234 | bool GetInteger(const std::wstring& path, int* out_value) const; |
| 235 | bool GetReal(const std::wstring& path, double* out_value) const; |
| 236 | bool GetString(const std::wstring& path, std::wstring* out_value) const; |
| 237 | bool GetBinary(const std::wstring& path, BinaryValue** out_value) const; |
| 238 | bool GetDictionary(const std::wstring& path, |
| 239 | DictionaryValue** out_value) const; |
| 240 | bool GetList(const std::wstring& path, ListValue** out_value) const; |
| 241 | |
| 242 | // Removes the Value with the specified path from this dictionary (or one |
| 243 | // of its child dictionaries, if the path is more than just a local key). |
| 244 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 245 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 246 | // be deleted. This method returns true if |path| is a valid path; otherwise |
| 247 | // it will return false and the DictionaryValue object will be unchanged. |
| 248 | bool Remove(const std::wstring& path, Value** out_value); |
| 249 | |
| 250 | // This class provides an iterator for the keys in the dictionary. |
| 251 | // It can't be used to modify the dictionary. |
| 252 | class key_iterator |
| 253 | : private std::iterator<std::input_iterator_tag, const std::wstring> { |
| 254 | public: |
| 255 | key_iterator(ValueMap::const_iterator itr) { itr_ = itr; } |
| 256 | key_iterator operator++() { ++itr_; return *this; } |
| 257 | const std::wstring& operator*() { return itr_->first; } |
| 258 | bool operator!=(const key_iterator& other) { return itr_ != other.itr_; } |
| 259 | bool operator==(const key_iterator& other) { return itr_ == other.itr_; } |
| 260 | |
| 261 | private: |
| 262 | ValueMap::const_iterator itr_; |
| 263 | }; |
| 264 | |
| 265 | key_iterator begin_keys() const { return key_iterator(dictionary_.begin()); } |
| 266 | key_iterator end_keys() const { return key_iterator(dictionary_.end()); } |
| 267 | |
| 268 | private: |
| 269 | DISALLOW_EVIL_CONSTRUCTORS(DictionaryValue); |
| 270 | |
| 271 | // Associates the value |in_value| with the |key|. This method should be |
| 272 | // used instead of "dictionary_[key] = foo" so that any previous value can |
| 273 | // be properly deleted. |
| 274 | void SetInCurrentNode(const std::wstring& key, Value* in_value); |
| 275 | |
| 276 | ValueMap dictionary_; |
| 277 | }; |
| 278 | |
| 279 | // This type of Value represents a list of other Value values. |
| 280 | // TODO(jhughes): Flesh this out. |
| 281 | class ListValue : public Value { |
| 282 | public: |
| 283 | ListValue() : Value(TYPE_LIST) {} |
| 284 | ~ListValue(); |
| 285 | |
| 286 | // Subclassed methods |
| 287 | Value* DeepCopy() const; |
| 288 | virtual bool Equals(const Value* other) const; |
| 289 | |
| 290 | // Clears the contents of this ListValue |
| 291 | void Clear(); |
| 292 | |
| 293 | // Returns the number of Values in this list. |
| 294 | size_t GetSize() const { return list_.size(); } |
| 295 | |
| 296 | // Sets the list item at the given index to be the Value specified by |
| 297 | // the value given. If the index beyond the current end of the list, null |
| 298 | // Values will be used to pad out the list. |
| 299 | // Returns true if successful, or false if the index was negative or |
| 300 | // the value is a null pointer. |
| 301 | bool Set(size_t index, Value* in_value); |
| 302 | |
| 303 | // Gets the Value at the given index. Modifies value (and returns true) |
| 304 | // only if the index falls within the current list range. |
| 305 | // Note that the list always owns the Value passed out via out_value. |
| 306 | bool Get(size_t index, Value** out_value) const; |
| 307 | |
| 308 | // Convenience forms of Get(). Modifies value (and returns true) only if |
| 309 | // the index is valid and the Value at that index can be returned in |
| 310 | // the specified form. |
| 311 | bool GetDictionary(size_t index, DictionaryValue** out_value) const; |
| 312 | |
| 313 | // Removes the Value with the specified index from this list. |
| 314 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
| 315 | // passed out via out_value. If |out_value| is NULL, the removed value will |
| 316 | // be deleted. This method returns true if |index| is valid; otherwise |
| 317 | // it will return false and the ListValue object will be unchanged. |
| 318 | bool Remove(size_t index, Value** out_value); |
| 319 | |
| 320 | // Appends a Value to the end of the list. |
| 321 | void Append(Value* in_value); |
| 322 | |
| 323 | // Iteration |
| 324 | typedef ValueVector::iterator iterator; |
| 325 | typedef ValueVector::const_iterator const_iterator; |
| 326 | |
| 327 | ListValue::iterator begin() { return list_.begin(); } |
| 328 | ListValue::iterator end() { return list_.end(); } |
| 329 | |
| 330 | ListValue::const_iterator begin() const { return list_.begin(); } |
| 331 | ListValue::const_iterator end() const { return list_.end(); } |
| 332 | |
| 333 | ListValue::iterator Erase(iterator item) { |
| 334 | return list_.erase(item); |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | DISALLOW_EVIL_CONSTRUCTORS(ListValue); |
| 339 | |
| 340 | ValueVector list_; |
| 341 | }; |
| 342 | |
| 343 | // This interface is implemented by classes that know how to serialize and |
| 344 | // deserialize Value objects. |
| 345 | class ValueSerializer { |
| 346 | public: |
[email protected] | abb9d0c | 2008-08-06 15:46:59 | [diff] [blame] | 347 | virtual ~ValueSerializer() {} |
| 348 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 349 | virtual bool Serialize(const Value& root) = 0; |
| 350 | |
| 351 | // This method deserializes the subclass-specific format into a Value object. |
| 352 | // The method should return true if and only if the root parameter is set |
| 353 | // to a complete Value representation of the serialized form. If the |
| 354 | // return value is true, the caller takes ownership of the objects pointed |
| 355 | // to by root. If the return value is false, root should be unchanged. |
| 356 | virtual bool Deserialize(Value** root) = 0; |
| 357 | }; |
| 358 | |
[email protected] | 101d542 | 2008-09-26 20:22:42 | [diff] [blame^] | 359 | #endif // BASE_VALUES_H_ |