Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // This file specifies a recursive data storage class called Value intended for |
michael@0 | 6 | // storing settings and other persistable data. |
michael@0 | 7 | // |
michael@0 | 8 | // A Value represents something that can be stored in JSON or passed to/from |
michael@0 | 9 | // JavaScript. As such, it is NOT a generalized variant type, since only the |
michael@0 | 10 | // types supported by JavaScript/JSON are supported. |
michael@0 | 11 | // |
michael@0 | 12 | // IN PARTICULAR this means that there is no support for int64 or unsigned |
michael@0 | 13 | // numbers. Writing JSON with such types would violate the spec. If you need |
michael@0 | 14 | // something like this, either use a double or make a string value containing |
michael@0 | 15 | // the number you want. |
michael@0 | 16 | |
michael@0 | 17 | #ifndef BASE_VALUES_H_ |
michael@0 | 18 | #define BASE_VALUES_H_ |
michael@0 | 19 | |
michael@0 | 20 | #include <stddef.h> |
michael@0 | 21 | |
michael@0 | 22 | #include <iosfwd> |
michael@0 | 23 | #include <map> |
michael@0 | 24 | #include <string> |
michael@0 | 25 | #include <utility> |
michael@0 | 26 | #include <vector> |
michael@0 | 27 | |
michael@0 | 28 | #include "base/base_export.h" |
michael@0 | 29 | #include "base/basictypes.h" |
michael@0 | 30 | #include "base/compiler_specific.h" |
michael@0 | 31 | #include "base/memory/scoped_ptr.h" |
michael@0 | 32 | #include "base/strings/string16.h" |
michael@0 | 33 | |
michael@0 | 34 | // This file declares "using base::Value", etc. at the bottom, so that |
michael@0 | 35 | // current code can use these classes without the base namespace. In |
michael@0 | 36 | // new code, please always use base::Value, etc. or add your own |
michael@0 | 37 | // "using" declaration. |
michael@0 | 38 | // http://crbug.com/88666 |
michael@0 | 39 | namespace base { |
michael@0 | 40 | |
michael@0 | 41 | class DictionaryValue; |
michael@0 | 42 | class FundamentalValue; |
michael@0 | 43 | class ListValue; |
michael@0 | 44 | class StringValue; |
michael@0 | 45 | class Value; |
michael@0 | 46 | |
michael@0 | 47 | typedef std::vector<Value*> ValueVector; |
michael@0 | 48 | typedef std::map<std::string, Value*> ValueMap; |
michael@0 | 49 | |
michael@0 | 50 | // The Value class is the base class for Values. A Value can be instantiated |
michael@0 | 51 | // via the Create*Value() factory methods, or by directly creating instances of |
michael@0 | 52 | // the subclasses. |
michael@0 | 53 | // |
michael@0 | 54 | // See the file-level comment above for more information. |
michael@0 | 55 | class BASE_EXPORT Value { |
michael@0 | 56 | public: |
michael@0 | 57 | enum Type { |
michael@0 | 58 | TYPE_NULL = 0, |
michael@0 | 59 | TYPE_BOOLEAN, |
michael@0 | 60 | TYPE_INTEGER, |
michael@0 | 61 | TYPE_DOUBLE, |
michael@0 | 62 | TYPE_STRING, |
michael@0 | 63 | TYPE_BINARY, |
michael@0 | 64 | TYPE_DICTIONARY, |
michael@0 | 65 | TYPE_LIST |
michael@0 | 66 | // Note: Do not add more types. See the file-level comment above for why. |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | virtual ~Value(); |
michael@0 | 70 | |
michael@0 | 71 | static Value* CreateNullValue(); |
michael@0 | 72 | // DEPRECATED: Do not use the following 5 functions. Instead, use |
michael@0 | 73 | // new FundamentalValue or new StringValue. |
michael@0 | 74 | static FundamentalValue* CreateBooleanValue(bool in_value); |
michael@0 | 75 | static FundamentalValue* CreateIntegerValue(int in_value); |
michael@0 | 76 | static FundamentalValue* CreateDoubleValue(double in_value); |
michael@0 | 77 | static StringValue* CreateStringValue(const std::string& in_value); |
michael@0 | 78 | static StringValue* CreateStringValue(const string16& in_value); |
michael@0 | 79 | |
michael@0 | 80 | // Returns the type of the value stored by the current Value object. |
michael@0 | 81 | // Each type will be implemented by only one subclass of Value, so it's |
michael@0 | 82 | // safe to use the Type to determine whether you can cast from |
michael@0 | 83 | // Value* to (Implementing Class)*. Also, a Value object never changes |
michael@0 | 84 | // its type after construction. |
michael@0 | 85 | Type GetType() const { return type_; } |
michael@0 | 86 | |
michael@0 | 87 | // Returns true if the current object represents a given type. |
michael@0 | 88 | bool IsType(Type type) const { return type == type_; } |
michael@0 | 89 | |
michael@0 | 90 | // These methods allow the convenient retrieval of the contents of the Value. |
michael@0 | 91 | // If the current object can be converted into the given type, the value is |
michael@0 | 92 | // returned through the |out_value| parameter and true is returned; |
michael@0 | 93 | // otherwise, false is returned and |out_value| is unchanged. |
michael@0 | 94 | virtual bool GetAsBoolean(bool* out_value) const; |
michael@0 | 95 | virtual bool GetAsInteger(int* out_value) const; |
michael@0 | 96 | virtual bool GetAsDouble(double* out_value) const; |
michael@0 | 97 | virtual bool GetAsString(std::string* out_value) const; |
michael@0 | 98 | virtual bool GetAsString(string16* out_value) const; |
michael@0 | 99 | virtual bool GetAsList(ListValue** out_value); |
michael@0 | 100 | virtual bool GetAsList(const ListValue** out_value) const; |
michael@0 | 101 | virtual bool GetAsDictionary(DictionaryValue** out_value); |
michael@0 | 102 | virtual bool GetAsDictionary(const DictionaryValue** out_value) const; |
michael@0 | 103 | // Note: Do not add more types. See the file-level comment above for why. |
michael@0 | 104 | |
michael@0 | 105 | // This creates a deep copy of the entire Value tree, and returns a pointer |
michael@0 | 106 | // to the copy. The caller gets ownership of the copy, of course. |
michael@0 | 107 | // |
michael@0 | 108 | // Subclasses return their own type directly in their overrides; |
michael@0 | 109 | // this works because C++ supports covariant return types. |
michael@0 | 110 | virtual Value* DeepCopy() const; |
michael@0 | 111 | |
michael@0 | 112 | // Compares if two Value objects have equal contents. |
michael@0 | 113 | virtual bool Equals(const Value* other) const; |
michael@0 | 114 | |
michael@0 | 115 | // Compares if two Value objects have equal contents. Can handle NULLs. |
michael@0 | 116 | // NULLs are considered equal but different from Value::CreateNullValue(). |
michael@0 | 117 | static bool Equals(const Value* a, const Value* b); |
michael@0 | 118 | |
michael@0 | 119 | protected: |
michael@0 | 120 | // These aren't safe for end-users, but they are useful for subclasses. |
michael@0 | 121 | explicit Value(Type type); |
michael@0 | 122 | Value(const Value& that); |
michael@0 | 123 | Value& operator=(const Value& that); |
michael@0 | 124 | |
michael@0 | 125 | private: |
michael@0 | 126 | Type type_; |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | // FundamentalValue represents the simple fundamental types of values. |
michael@0 | 130 | class BASE_EXPORT FundamentalValue : public Value { |
michael@0 | 131 | public: |
michael@0 | 132 | explicit FundamentalValue(bool in_value); |
michael@0 | 133 | explicit FundamentalValue(int in_value); |
michael@0 | 134 | explicit FundamentalValue(double in_value); |
michael@0 | 135 | virtual ~FundamentalValue(); |
michael@0 | 136 | |
michael@0 | 137 | // Overridden from Value: |
michael@0 | 138 | virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; |
michael@0 | 139 | virtual bool GetAsInteger(int* out_value) const OVERRIDE; |
michael@0 | 140 | virtual bool GetAsDouble(double* out_value) const OVERRIDE; |
michael@0 | 141 | virtual FundamentalValue* DeepCopy() const OVERRIDE; |
michael@0 | 142 | virtual bool Equals(const Value* other) const OVERRIDE; |
michael@0 | 143 | |
michael@0 | 144 | private: |
michael@0 | 145 | union { |
michael@0 | 146 | bool boolean_value_; |
michael@0 | 147 | int integer_value_; |
michael@0 | 148 | double double_value_; |
michael@0 | 149 | }; |
michael@0 | 150 | }; |
michael@0 | 151 | |
michael@0 | 152 | class BASE_EXPORT StringValue : public Value { |
michael@0 | 153 | public: |
michael@0 | 154 | // Initializes a StringValue with a UTF-8 narrow character string. |
michael@0 | 155 | explicit StringValue(const std::string& in_value); |
michael@0 | 156 | |
michael@0 | 157 | // Initializes a StringValue with a string16. |
michael@0 | 158 | explicit StringValue(const string16& in_value); |
michael@0 | 159 | |
michael@0 | 160 | virtual ~StringValue(); |
michael@0 | 161 | |
michael@0 | 162 | // Overridden from Value: |
michael@0 | 163 | virtual bool GetAsString(std::string* out_value) const OVERRIDE; |
michael@0 | 164 | virtual bool GetAsString(string16* out_value) const OVERRIDE; |
michael@0 | 165 | virtual StringValue* DeepCopy() const OVERRIDE; |
michael@0 | 166 | virtual bool Equals(const Value* other) const OVERRIDE; |
michael@0 | 167 | |
michael@0 | 168 | private: |
michael@0 | 169 | std::string value_; |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | class BASE_EXPORT BinaryValue: public Value { |
michael@0 | 173 | public: |
michael@0 | 174 | // Creates a BinaryValue with a null buffer and size of 0. |
michael@0 | 175 | BinaryValue(); |
michael@0 | 176 | |
michael@0 | 177 | // Creates a BinaryValue, taking ownership of the bytes pointed to by |
michael@0 | 178 | // |buffer|. |
michael@0 | 179 | BinaryValue(scoped_ptr<char[]> buffer, size_t size); |
michael@0 | 180 | |
michael@0 | 181 | virtual ~BinaryValue(); |
michael@0 | 182 | |
michael@0 | 183 | // For situations where you want to keep ownership of your buffer, this |
michael@0 | 184 | // factory method creates a new BinaryValue by copying the contents of the |
michael@0 | 185 | // buffer that's passed in. |
michael@0 | 186 | static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
michael@0 | 187 | |
michael@0 | 188 | size_t GetSize() const { return size_; } |
michael@0 | 189 | |
michael@0 | 190 | // May return NULL. |
michael@0 | 191 | char* GetBuffer() { return buffer_.get(); } |
michael@0 | 192 | const char* GetBuffer() const { return buffer_.get(); } |
michael@0 | 193 | |
michael@0 | 194 | // Overridden from Value: |
michael@0 | 195 | virtual BinaryValue* DeepCopy() const OVERRIDE; |
michael@0 | 196 | virtual bool Equals(const Value* other) const OVERRIDE; |
michael@0 | 197 | |
michael@0 | 198 | private: |
michael@0 | 199 | scoped_ptr<char[]> buffer_; |
michael@0 | 200 | size_t size_; |
michael@0 | 201 | |
michael@0 | 202 | DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | // DictionaryValue provides a key-value dictionary with (optional) "path" |
michael@0 | 206 | // parsing for recursive access; see the comment at the top of the file. Keys |
michael@0 | 207 | // are |std::string|s and should be UTF-8 encoded. |
michael@0 | 208 | class BASE_EXPORT DictionaryValue : public Value { |
michael@0 | 209 | public: |
michael@0 | 210 | DictionaryValue(); |
michael@0 | 211 | virtual ~DictionaryValue(); |
michael@0 | 212 | |
michael@0 | 213 | // Overridden from Value: |
michael@0 | 214 | virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE; |
michael@0 | 215 | virtual bool GetAsDictionary( |
michael@0 | 216 | const DictionaryValue** out_value) const OVERRIDE; |
michael@0 | 217 | |
michael@0 | 218 | // Returns true if the current dictionary has a value for the given key. |
michael@0 | 219 | bool HasKey(const std::string& key) const; |
michael@0 | 220 | |
michael@0 | 221 | // Returns the number of Values in this dictionary. |
michael@0 | 222 | size_t size() const { return dictionary_.size(); } |
michael@0 | 223 | |
michael@0 | 224 | // Returns whether the dictionary is empty. |
michael@0 | 225 | bool empty() const { return dictionary_.empty(); } |
michael@0 | 226 | |
michael@0 | 227 | // Clears any current contents of this dictionary. |
michael@0 | 228 | void Clear(); |
michael@0 | 229 | |
michael@0 | 230 | // Sets the Value associated with the given path starting from this object. |
michael@0 | 231 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
michael@0 | 232 | // into the next DictionaryValue down. Obviously, "." can't be used |
michael@0 | 233 | // within a key, but there are no other restrictions on keys. |
michael@0 | 234 | // If the key at any step of the way doesn't exist, or exists but isn't |
michael@0 | 235 | // a DictionaryValue, a new DictionaryValue will be created and attached |
michael@0 | 236 | // to the path in that location. |
michael@0 | 237 | // Note that the dictionary takes ownership of the value referenced by |
michael@0 | 238 | // |in_value|, and therefore |in_value| must be non-NULL. |
michael@0 | 239 | void Set(const std::string& path, Value* in_value); |
michael@0 | 240 | |
michael@0 | 241 | // Convenience forms of Set(). These methods will replace any existing |
michael@0 | 242 | // value at that path, even if it has a different type. |
michael@0 | 243 | void SetBoolean(const std::string& path, bool in_value); |
michael@0 | 244 | void SetInteger(const std::string& path, int in_value); |
michael@0 | 245 | void SetDouble(const std::string& path, double in_value); |
michael@0 | 246 | void SetString(const std::string& path, const std::string& in_value); |
michael@0 | 247 | void SetString(const std::string& path, const string16& in_value); |
michael@0 | 248 | |
michael@0 | 249 | // Like Set(), but without special treatment of '.'. This allows e.g. URLs to |
michael@0 | 250 | // be used as paths. |
michael@0 | 251 | void SetWithoutPathExpansion(const std::string& key, Value* in_value); |
michael@0 | 252 | |
michael@0 | 253 | // Convenience forms of SetWithoutPathExpansion(). |
michael@0 | 254 | void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); |
michael@0 | 255 | void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); |
michael@0 | 256 | void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); |
michael@0 | 257 | void SetStringWithoutPathExpansion(const std::string& path, |
michael@0 | 258 | const std::string& in_value); |
michael@0 | 259 | void SetStringWithoutPathExpansion(const std::string& path, |
michael@0 | 260 | const string16& in_value); |
michael@0 | 261 | |
michael@0 | 262 | // Gets the Value associated with the given path starting from this object. |
michael@0 | 263 | // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes |
michael@0 | 264 | // into the next DictionaryValue down. If the path can be resolved |
michael@0 | 265 | // successfully, the value for the last key in the path will be returned |
michael@0 | 266 | // through the |out_value| parameter, and the function will return true. |
michael@0 | 267 | // Otherwise, it will return false and |out_value| will be untouched. |
michael@0 | 268 | // Note that the dictionary always owns the value that's returned. |
michael@0 | 269 | bool Get(const std::string& path, const Value** out_value) const; |
michael@0 | 270 | bool Get(const std::string& path, Value** out_value); |
michael@0 | 271 | |
michael@0 | 272 | // These are convenience forms of Get(). The value will be retrieved |
michael@0 | 273 | // and the return value will be true if the path is valid and the value at |
michael@0 | 274 | // the end of the path can be returned in the form specified. |
michael@0 | 275 | bool GetBoolean(const std::string& path, bool* out_value) const; |
michael@0 | 276 | bool GetInteger(const std::string& path, int* out_value) const; |
michael@0 | 277 | bool GetDouble(const std::string& path, double* out_value) const; |
michael@0 | 278 | bool GetString(const std::string& path, std::string* out_value) const; |
michael@0 | 279 | bool GetString(const std::string& path, string16* out_value) const; |
michael@0 | 280 | bool GetStringASCII(const std::string& path, std::string* out_value) const; |
michael@0 | 281 | bool GetBinary(const std::string& path, const BinaryValue** out_value) const; |
michael@0 | 282 | bool GetBinary(const std::string& path, BinaryValue** out_value); |
michael@0 | 283 | bool GetDictionary(const std::string& path, |
michael@0 | 284 | const DictionaryValue** out_value) const; |
michael@0 | 285 | bool GetDictionary(const std::string& path, DictionaryValue** out_value); |
michael@0 | 286 | bool GetList(const std::string& path, const ListValue** out_value) const; |
michael@0 | 287 | bool GetList(const std::string& path, ListValue** out_value); |
michael@0 | 288 | |
michael@0 | 289 | // Like Get(), but without special treatment of '.'. This allows e.g. URLs to |
michael@0 | 290 | // be used as paths. |
michael@0 | 291 | bool GetWithoutPathExpansion(const std::string& key, |
michael@0 | 292 | const Value** out_value) const; |
michael@0 | 293 | bool GetWithoutPathExpansion(const std::string& key, Value** out_value); |
michael@0 | 294 | bool GetBooleanWithoutPathExpansion(const std::string& key, |
michael@0 | 295 | bool* out_value) const; |
michael@0 | 296 | bool GetIntegerWithoutPathExpansion(const std::string& key, |
michael@0 | 297 | int* out_value) const; |
michael@0 | 298 | bool GetDoubleWithoutPathExpansion(const std::string& key, |
michael@0 | 299 | double* out_value) const; |
michael@0 | 300 | bool GetStringWithoutPathExpansion(const std::string& key, |
michael@0 | 301 | std::string* out_value) const; |
michael@0 | 302 | bool GetStringWithoutPathExpansion(const std::string& key, |
michael@0 | 303 | string16* out_value) const; |
michael@0 | 304 | bool GetDictionaryWithoutPathExpansion( |
michael@0 | 305 | const std::string& key, |
michael@0 | 306 | const DictionaryValue** out_value) const; |
michael@0 | 307 | bool GetDictionaryWithoutPathExpansion(const std::string& key, |
michael@0 | 308 | DictionaryValue** out_value); |
michael@0 | 309 | bool GetListWithoutPathExpansion(const std::string& key, |
michael@0 | 310 | const ListValue** out_value) const; |
michael@0 | 311 | bool GetListWithoutPathExpansion(const std::string& key, |
michael@0 | 312 | ListValue** out_value); |
michael@0 | 313 | |
michael@0 | 314 | // Removes the Value with the specified path from this dictionary (or one |
michael@0 | 315 | // of its child dictionaries, if the path is more than just a local key). |
michael@0 | 316 | // If |out_value| is non-NULL, the removed Value will be passed out via |
michael@0 | 317 | // |out_value|. If |out_value| is NULL, the removed value will be deleted. |
michael@0 | 318 | // This method returns true if |path| is a valid path; otherwise it will |
michael@0 | 319 | // return false and the DictionaryValue object will be unchanged. |
michael@0 | 320 | virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value); |
michael@0 | 321 | |
michael@0 | 322 | // Like Remove(), but without special treatment of '.'. This allows e.g. URLs |
michael@0 | 323 | // to be used as paths. |
michael@0 | 324 | virtual bool RemoveWithoutPathExpansion(const std::string& key, |
michael@0 | 325 | scoped_ptr<Value>* out_value); |
michael@0 | 326 | |
michael@0 | 327 | // Makes a copy of |this| but doesn't include empty dictionaries and lists in |
michael@0 | 328 | // the copy. This never returns NULL, even if |this| itself is empty. |
michael@0 | 329 | DictionaryValue* DeepCopyWithoutEmptyChildren() const; |
michael@0 | 330 | |
michael@0 | 331 | // Merge |dictionary| into this dictionary. This is done recursively, i.e. any |
michael@0 | 332 | // sub-dictionaries will be merged as well. In case of key collisions, the |
michael@0 | 333 | // passed in dictionary takes precedence and data already present will be |
michael@0 | 334 | // replaced. Values within |dictionary| are deep-copied, so |dictionary| may |
michael@0 | 335 | // be freed any time after this call. |
michael@0 | 336 | void MergeDictionary(const DictionaryValue* dictionary); |
michael@0 | 337 | |
michael@0 | 338 | // Swaps contents with the |other| dictionary. |
michael@0 | 339 | virtual void Swap(DictionaryValue* other); |
michael@0 | 340 | |
michael@0 | 341 | // This class provides an iterator over both keys and values in the |
michael@0 | 342 | // dictionary. It can't be used to modify the dictionary. |
michael@0 | 343 | class BASE_EXPORT Iterator { |
michael@0 | 344 | public: |
michael@0 | 345 | explicit Iterator(const DictionaryValue& target); |
michael@0 | 346 | |
michael@0 | 347 | bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } |
michael@0 | 348 | void Advance() { ++it_; } |
michael@0 | 349 | |
michael@0 | 350 | const std::string& key() const { return it_->first; } |
michael@0 | 351 | const Value& value() const { return *it_->second; } |
michael@0 | 352 | |
michael@0 | 353 | private: |
michael@0 | 354 | const DictionaryValue& target_; |
michael@0 | 355 | ValueMap::const_iterator it_; |
michael@0 | 356 | }; |
michael@0 | 357 | |
michael@0 | 358 | // Overridden from Value: |
michael@0 | 359 | virtual DictionaryValue* DeepCopy() const OVERRIDE; |
michael@0 | 360 | virtual bool Equals(const Value* other) const OVERRIDE; |
michael@0 | 361 | |
michael@0 | 362 | private: |
michael@0 | 363 | ValueMap dictionary_; |
michael@0 | 364 | |
michael@0 | 365 | DISALLOW_COPY_AND_ASSIGN(DictionaryValue); |
michael@0 | 366 | }; |
michael@0 | 367 | |
michael@0 | 368 | // This type of Value represents a list of other Value values. |
michael@0 | 369 | class BASE_EXPORT ListValue : public Value { |
michael@0 | 370 | public: |
michael@0 | 371 | typedef ValueVector::iterator iterator; |
michael@0 | 372 | typedef ValueVector::const_iterator const_iterator; |
michael@0 | 373 | |
michael@0 | 374 | ListValue(); |
michael@0 | 375 | virtual ~ListValue(); |
michael@0 | 376 | |
michael@0 | 377 | // Clears the contents of this ListValue |
michael@0 | 378 | void Clear(); |
michael@0 | 379 | |
michael@0 | 380 | // Returns the number of Values in this list. |
michael@0 | 381 | size_t GetSize() const { return list_.size(); } |
michael@0 | 382 | |
michael@0 | 383 | // Returns whether the list is empty. |
michael@0 | 384 | bool empty() const { return list_.empty(); } |
michael@0 | 385 | |
michael@0 | 386 | // Sets the list item at the given index to be the Value specified by |
michael@0 | 387 | // the value given. If the index beyond the current end of the list, null |
michael@0 | 388 | // Values will be used to pad out the list. |
michael@0 | 389 | // Returns true if successful, or false if the index was negative or |
michael@0 | 390 | // the value is a null pointer. |
michael@0 | 391 | bool Set(size_t index, Value* in_value); |
michael@0 | 392 | |
michael@0 | 393 | // Gets the Value at the given index. Modifies |out_value| (and returns true) |
michael@0 | 394 | // only if the index falls within the current list range. |
michael@0 | 395 | // Note that the list always owns the Value passed out via |out_value|. |
michael@0 | 396 | bool Get(size_t index, const Value** out_value) const; |
michael@0 | 397 | bool Get(size_t index, Value** out_value); |
michael@0 | 398 | |
michael@0 | 399 | // Convenience forms of Get(). Modifies |out_value| (and returns true) |
michael@0 | 400 | // only if the index is valid and the Value at that index can be returned |
michael@0 | 401 | // in the specified form. |
michael@0 | 402 | bool GetBoolean(size_t index, bool* out_value) const; |
michael@0 | 403 | bool GetInteger(size_t index, int* out_value) const; |
michael@0 | 404 | bool GetDouble(size_t index, double* out_value) const; |
michael@0 | 405 | bool GetString(size_t index, std::string* out_value) const; |
michael@0 | 406 | bool GetString(size_t index, string16* out_value) const; |
michael@0 | 407 | bool GetBinary(size_t index, const BinaryValue** out_value) const; |
michael@0 | 408 | bool GetBinary(size_t index, BinaryValue** out_value); |
michael@0 | 409 | bool GetDictionary(size_t index, const DictionaryValue** out_value) const; |
michael@0 | 410 | bool GetDictionary(size_t index, DictionaryValue** out_value); |
michael@0 | 411 | bool GetList(size_t index, const ListValue** out_value) const; |
michael@0 | 412 | bool GetList(size_t index, ListValue** out_value); |
michael@0 | 413 | |
michael@0 | 414 | // Removes the Value with the specified index from this list. |
michael@0 | 415 | // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be |
michael@0 | 416 | // passed out via |out_value|. If |out_value| is NULL, the removed value will |
michael@0 | 417 | // be deleted. This method returns true if |index| is valid; otherwise |
michael@0 | 418 | // it will return false and the ListValue object will be unchanged. |
michael@0 | 419 | virtual bool Remove(size_t index, scoped_ptr<Value>* out_value); |
michael@0 | 420 | |
michael@0 | 421 | // Removes the first instance of |value| found in the list, if any, and |
michael@0 | 422 | // deletes it. |index| is the location where |value| was found. Returns false |
michael@0 | 423 | // if not found. |
michael@0 | 424 | bool Remove(const Value& value, size_t* index); |
michael@0 | 425 | |
michael@0 | 426 | // Removes the element at |iter|. If |out_value| is NULL, the value will be |
michael@0 | 427 | // deleted, otherwise ownership of the value is passed back to the caller. |
michael@0 | 428 | // Returns an iterator pointing to the location of the element that |
michael@0 | 429 | // followed the erased element. |
michael@0 | 430 | iterator Erase(iterator iter, scoped_ptr<Value>* out_value); |
michael@0 | 431 | |
michael@0 | 432 | // Appends a Value to the end of the list. |
michael@0 | 433 | void Append(Value* in_value); |
michael@0 | 434 | |
michael@0 | 435 | // Convenience forms of Append. |
michael@0 | 436 | void AppendBoolean(bool in_value); |
michael@0 | 437 | void AppendInteger(int in_value); |
michael@0 | 438 | void AppendDouble(double in_value); |
michael@0 | 439 | void AppendString(const std::string& in_value); |
michael@0 | 440 | void AppendString(const string16& in_value); |
michael@0 | 441 | void AppendStrings(const std::vector<std::string>& in_values); |
michael@0 | 442 | void AppendStrings(const std::vector<string16>& in_values); |
michael@0 | 443 | |
michael@0 | 444 | // Appends a Value if it's not already present. Takes ownership of the |
michael@0 | 445 | // |in_value|. Returns true if successful, or false if the value was already |
michael@0 | 446 | // present. If the value was already present the |in_value| is deleted. |
michael@0 | 447 | bool AppendIfNotPresent(Value* in_value); |
michael@0 | 448 | |
michael@0 | 449 | // Insert a Value at index. |
michael@0 | 450 | // Returns true if successful, or false if the index was out of range. |
michael@0 | 451 | bool Insert(size_t index, Value* in_value); |
michael@0 | 452 | |
michael@0 | 453 | // Searches for the first instance of |value| in the list using the Equals |
michael@0 | 454 | // method of the Value type. |
michael@0 | 455 | // Returns a const_iterator to the found item or to end() if none exists. |
michael@0 | 456 | const_iterator Find(const Value& value) const; |
michael@0 | 457 | |
michael@0 | 458 | // Swaps contents with the |other| list. |
michael@0 | 459 | virtual void Swap(ListValue* other); |
michael@0 | 460 | |
michael@0 | 461 | // Iteration. |
michael@0 | 462 | iterator begin() { return list_.begin(); } |
michael@0 | 463 | iterator end() { return list_.end(); } |
michael@0 | 464 | |
michael@0 | 465 | const_iterator begin() const { return list_.begin(); } |
michael@0 | 466 | const_iterator end() const { return list_.end(); } |
michael@0 | 467 | |
michael@0 | 468 | // Overridden from Value: |
michael@0 | 469 | virtual bool GetAsList(ListValue** out_value) OVERRIDE; |
michael@0 | 470 | virtual bool GetAsList(const ListValue** out_value) const OVERRIDE; |
michael@0 | 471 | virtual ListValue* DeepCopy() const OVERRIDE; |
michael@0 | 472 | virtual bool Equals(const Value* other) const OVERRIDE; |
michael@0 | 473 | |
michael@0 | 474 | private: |
michael@0 | 475 | ValueVector list_; |
michael@0 | 476 | |
michael@0 | 477 | DISALLOW_COPY_AND_ASSIGN(ListValue); |
michael@0 | 478 | }; |
michael@0 | 479 | |
michael@0 | 480 | // This interface is implemented by classes that know how to serialize and |
michael@0 | 481 | // deserialize Value objects. |
michael@0 | 482 | class BASE_EXPORT ValueSerializer { |
michael@0 | 483 | public: |
michael@0 | 484 | virtual ~ValueSerializer(); |
michael@0 | 485 | |
michael@0 | 486 | virtual bool Serialize(const Value& root) = 0; |
michael@0 | 487 | |
michael@0 | 488 | // This method deserializes the subclass-specific format into a Value object. |
michael@0 | 489 | // If the return value is non-NULL, the caller takes ownership of returned |
michael@0 | 490 | // Value. If the return value is NULL, and if error_code is non-NULL, |
michael@0 | 491 | // error_code will be set with the underlying error. |
michael@0 | 492 | // If |error_message| is non-null, it will be filled in with a formatted |
michael@0 | 493 | // error message including the location of the error if appropriate. |
michael@0 | 494 | virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; |
michael@0 | 495 | }; |
michael@0 | 496 | |
michael@0 | 497 | // Stream operator so Values can be used in assertion statements. In order that |
michael@0 | 498 | // gtest uses this operator to print readable output on test failures, we must |
michael@0 | 499 | // override each specific type. Otherwise, the default template implementation |
michael@0 | 500 | // is preferred over an upcast. |
michael@0 | 501 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); |
michael@0 | 502 | |
michael@0 | 503 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
michael@0 | 504 | const FundamentalValue& value) { |
michael@0 | 505 | return out << static_cast<const Value&>(value); |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
michael@0 | 509 | const StringValue& value) { |
michael@0 | 510 | return out << static_cast<const Value&>(value); |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
michael@0 | 514 | const DictionaryValue& value) { |
michael@0 | 515 | return out << static_cast<const Value&>(value); |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, |
michael@0 | 519 | const ListValue& value) { |
michael@0 | 520 | return out << static_cast<const Value&>(value); |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | } // namespace base |
michael@0 | 524 | |
michael@0 | 525 | // http://crbug.com/88666 |
michael@0 | 526 | using base::DictionaryValue; |
michael@0 | 527 | using base::ListValue; |
michael@0 | 528 | using base::StringValue; |
michael@0 | 529 | using base::Value; |
michael@0 | 530 | |
michael@0 | 531 | #endif // BASE_VALUES_H_ |