michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // This file specifies a recursive data storage class called Value intended for michael@0: // storing settings and other persistable data. michael@0: // michael@0: // A Value represents something that can be stored in JSON or passed to/from michael@0: // JavaScript. As such, it is NOT a generalized variant type, since only the michael@0: // types supported by JavaScript/JSON are supported. michael@0: // michael@0: // IN PARTICULAR this means that there is no support for int64 or unsigned michael@0: // numbers. Writing JSON with such types would violate the spec. If you need michael@0: // something like this, either use a double or make a string value containing michael@0: // the number you want. michael@0: michael@0: #ifndef BASE_VALUES_H_ michael@0: #define BASE_VALUES_H_ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/compiler_specific.h" michael@0: #include "base/memory/scoped_ptr.h" michael@0: #include "base/strings/string16.h" michael@0: michael@0: // This file declares "using base::Value", etc. at the bottom, so that michael@0: // current code can use these classes without the base namespace. In michael@0: // new code, please always use base::Value, etc. or add your own michael@0: // "using" declaration. michael@0: // http://crbug.com/88666 michael@0: namespace base { michael@0: michael@0: class DictionaryValue; michael@0: class FundamentalValue; michael@0: class ListValue; michael@0: class StringValue; michael@0: class Value; michael@0: michael@0: typedef std::vector ValueVector; michael@0: typedef std::map ValueMap; michael@0: michael@0: // The Value class is the base class for Values. A Value can be instantiated michael@0: // via the Create*Value() factory methods, or by directly creating instances of michael@0: // the subclasses. michael@0: // michael@0: // See the file-level comment above for more information. michael@0: class BASE_EXPORT Value { michael@0: public: michael@0: enum Type { michael@0: TYPE_NULL = 0, michael@0: TYPE_BOOLEAN, michael@0: TYPE_INTEGER, michael@0: TYPE_DOUBLE, michael@0: TYPE_STRING, michael@0: TYPE_BINARY, michael@0: TYPE_DICTIONARY, michael@0: TYPE_LIST michael@0: // Note: Do not add more types. See the file-level comment above for why. michael@0: }; michael@0: michael@0: virtual ~Value(); michael@0: michael@0: static Value* CreateNullValue(); michael@0: // DEPRECATED: Do not use the following 5 functions. Instead, use michael@0: // new FundamentalValue or new StringValue. michael@0: static FundamentalValue* CreateBooleanValue(bool in_value); michael@0: static FundamentalValue* CreateIntegerValue(int in_value); michael@0: static FundamentalValue* CreateDoubleValue(double in_value); michael@0: static StringValue* CreateStringValue(const std::string& in_value); michael@0: static StringValue* CreateStringValue(const string16& in_value); michael@0: michael@0: // Returns the type of the value stored by the current Value object. michael@0: // Each type will be implemented by only one subclass of Value, so it's michael@0: // safe to use the Type to determine whether you can cast from michael@0: // Value* to (Implementing Class)*. Also, a Value object never changes michael@0: // its type after construction. michael@0: Type GetType() const { return type_; } michael@0: michael@0: // Returns true if the current object represents a given type. michael@0: bool IsType(Type type) const { return type == type_; } michael@0: michael@0: // These methods allow the convenient retrieval of the contents of the Value. michael@0: // If the current object can be converted into the given type, the value is michael@0: // returned through the |out_value| parameter and true is returned; michael@0: // otherwise, false is returned and |out_value| is unchanged. michael@0: virtual bool GetAsBoolean(bool* out_value) const; michael@0: virtual bool GetAsInteger(int* out_value) const; michael@0: virtual bool GetAsDouble(double* out_value) const; michael@0: virtual bool GetAsString(std::string* out_value) const; michael@0: virtual bool GetAsString(string16* out_value) const; michael@0: virtual bool GetAsList(ListValue** out_value); michael@0: virtual bool GetAsList(const ListValue** out_value) const; michael@0: virtual bool GetAsDictionary(DictionaryValue** out_value); michael@0: virtual bool GetAsDictionary(const DictionaryValue** out_value) const; michael@0: // Note: Do not add more types. See the file-level comment above for why. michael@0: michael@0: // This creates a deep copy of the entire Value tree, and returns a pointer michael@0: // to the copy. The caller gets ownership of the copy, of course. michael@0: // michael@0: // Subclasses return their own type directly in their overrides; michael@0: // this works because C++ supports covariant return types. michael@0: virtual Value* DeepCopy() const; michael@0: michael@0: // Compares if two Value objects have equal contents. michael@0: virtual bool Equals(const Value* other) const; michael@0: michael@0: // Compares if two Value objects have equal contents. Can handle NULLs. michael@0: // NULLs are considered equal but different from Value::CreateNullValue(). michael@0: static bool Equals(const Value* a, const Value* b); michael@0: michael@0: protected: michael@0: // These aren't safe for end-users, but they are useful for subclasses. michael@0: explicit Value(Type type); michael@0: Value(const Value& that); michael@0: Value& operator=(const Value& that); michael@0: michael@0: private: michael@0: Type type_; michael@0: }; michael@0: michael@0: // FundamentalValue represents the simple fundamental types of values. michael@0: class BASE_EXPORT FundamentalValue : public Value { michael@0: public: michael@0: explicit FundamentalValue(bool in_value); michael@0: explicit FundamentalValue(int in_value); michael@0: explicit FundamentalValue(double in_value); michael@0: virtual ~FundamentalValue(); michael@0: michael@0: // Overridden from Value: michael@0: virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; michael@0: virtual bool GetAsInteger(int* out_value) const OVERRIDE; michael@0: virtual bool GetAsDouble(double* out_value) const OVERRIDE; michael@0: virtual FundamentalValue* DeepCopy() const OVERRIDE; michael@0: virtual bool Equals(const Value* other) const OVERRIDE; michael@0: michael@0: private: michael@0: union { michael@0: bool boolean_value_; michael@0: int integer_value_; michael@0: double double_value_; michael@0: }; michael@0: }; michael@0: michael@0: class BASE_EXPORT StringValue : public Value { michael@0: public: michael@0: // Initializes a StringValue with a UTF-8 narrow character string. michael@0: explicit StringValue(const std::string& in_value); michael@0: michael@0: // Initializes a StringValue with a string16. michael@0: explicit StringValue(const string16& in_value); michael@0: michael@0: virtual ~StringValue(); michael@0: michael@0: // Overridden from Value: michael@0: virtual bool GetAsString(std::string* out_value) const OVERRIDE; michael@0: virtual bool GetAsString(string16* out_value) const OVERRIDE; michael@0: virtual StringValue* DeepCopy() const OVERRIDE; michael@0: virtual bool Equals(const Value* other) const OVERRIDE; michael@0: michael@0: private: michael@0: std::string value_; michael@0: }; michael@0: michael@0: class BASE_EXPORT BinaryValue: public Value { michael@0: public: michael@0: // Creates a BinaryValue with a null buffer and size of 0. michael@0: BinaryValue(); michael@0: michael@0: // Creates a BinaryValue, taking ownership of the bytes pointed to by michael@0: // |buffer|. michael@0: BinaryValue(scoped_ptr buffer, size_t size); michael@0: michael@0: virtual ~BinaryValue(); michael@0: michael@0: // For situations where you want to keep ownership of your buffer, this michael@0: // factory method creates a new BinaryValue by copying the contents of the michael@0: // buffer that's passed in. michael@0: static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); michael@0: michael@0: size_t GetSize() const { return size_; } michael@0: michael@0: // May return NULL. michael@0: char* GetBuffer() { return buffer_.get(); } michael@0: const char* GetBuffer() const { return buffer_.get(); } michael@0: michael@0: // Overridden from Value: michael@0: virtual BinaryValue* DeepCopy() const OVERRIDE; michael@0: virtual bool Equals(const Value* other) const OVERRIDE; michael@0: michael@0: private: michael@0: scoped_ptr buffer_; michael@0: size_t size_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(BinaryValue); michael@0: }; michael@0: michael@0: // DictionaryValue provides a key-value dictionary with (optional) "path" michael@0: // parsing for recursive access; see the comment at the top of the file. Keys michael@0: // are |std::string|s and should be UTF-8 encoded. michael@0: class BASE_EXPORT DictionaryValue : public Value { michael@0: public: michael@0: DictionaryValue(); michael@0: virtual ~DictionaryValue(); michael@0: michael@0: // Overridden from Value: michael@0: virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE; michael@0: virtual bool GetAsDictionary( michael@0: const DictionaryValue** out_value) const OVERRIDE; michael@0: michael@0: // Returns true if the current dictionary has a value for the given key. michael@0: bool HasKey(const std::string& key) const; michael@0: michael@0: // Returns the number of Values in this dictionary. michael@0: size_t size() const { return dictionary_.size(); } michael@0: michael@0: // Returns whether the dictionary is empty. michael@0: bool empty() const { return dictionary_.empty(); } michael@0: michael@0: // Clears any current contents of this dictionary. michael@0: void Clear(); michael@0: michael@0: // Sets the Value associated with the given path starting from this object. michael@0: // A path has the form "" or "..[...]", where "." indexes michael@0: // into the next DictionaryValue down. Obviously, "." can't be used michael@0: // within a key, but there are no other restrictions on keys. michael@0: // If the key at any step of the way doesn't exist, or exists but isn't michael@0: // a DictionaryValue, a new DictionaryValue will be created and attached michael@0: // to the path in that location. michael@0: // Note that the dictionary takes ownership of the value referenced by michael@0: // |in_value|, and therefore |in_value| must be non-NULL. michael@0: void Set(const std::string& path, Value* in_value); michael@0: michael@0: // Convenience forms of Set(). These methods will replace any existing michael@0: // value at that path, even if it has a different type. michael@0: void SetBoolean(const std::string& path, bool in_value); michael@0: void SetInteger(const std::string& path, int in_value); michael@0: void SetDouble(const std::string& path, double in_value); michael@0: void SetString(const std::string& path, const std::string& in_value); michael@0: void SetString(const std::string& path, const string16& in_value); michael@0: michael@0: // Like Set(), but without special treatment of '.'. This allows e.g. URLs to michael@0: // be used as paths. michael@0: void SetWithoutPathExpansion(const std::string& key, Value* in_value); michael@0: michael@0: // Convenience forms of SetWithoutPathExpansion(). michael@0: void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); michael@0: void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); michael@0: void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); michael@0: void SetStringWithoutPathExpansion(const std::string& path, michael@0: const std::string& in_value); michael@0: void SetStringWithoutPathExpansion(const std::string& path, michael@0: const string16& in_value); michael@0: michael@0: // Gets the Value associated with the given path starting from this object. michael@0: // A path has the form "" or "..[...]", where "." indexes michael@0: // into the next DictionaryValue down. If the path can be resolved michael@0: // successfully, the value for the last key in the path will be returned michael@0: // through the |out_value| parameter, and the function will return true. michael@0: // Otherwise, it will return false and |out_value| will be untouched. michael@0: // Note that the dictionary always owns the value that's returned. michael@0: bool Get(const std::string& path, const Value** out_value) const; michael@0: bool Get(const std::string& path, Value** out_value); michael@0: michael@0: // These are convenience forms of Get(). The value will be retrieved michael@0: // and the return value will be true if the path is valid and the value at michael@0: // the end of the path can be returned in the form specified. michael@0: bool GetBoolean(const std::string& path, bool* out_value) const; michael@0: bool GetInteger(const std::string& path, int* out_value) const; michael@0: bool GetDouble(const std::string& path, double* out_value) const; michael@0: bool GetString(const std::string& path, std::string* out_value) const; michael@0: bool GetString(const std::string& path, string16* out_value) const; michael@0: bool GetStringASCII(const std::string& path, std::string* out_value) const; michael@0: bool GetBinary(const std::string& path, const BinaryValue** out_value) const; michael@0: bool GetBinary(const std::string& path, BinaryValue** out_value); michael@0: bool GetDictionary(const std::string& path, michael@0: const DictionaryValue** out_value) const; michael@0: bool GetDictionary(const std::string& path, DictionaryValue** out_value); michael@0: bool GetList(const std::string& path, const ListValue** out_value) const; michael@0: bool GetList(const std::string& path, ListValue** out_value); michael@0: michael@0: // Like Get(), but without special treatment of '.'. This allows e.g. URLs to michael@0: // be used as paths. michael@0: bool GetWithoutPathExpansion(const std::string& key, michael@0: const Value** out_value) const; michael@0: bool GetWithoutPathExpansion(const std::string& key, Value** out_value); michael@0: bool GetBooleanWithoutPathExpansion(const std::string& key, michael@0: bool* out_value) const; michael@0: bool GetIntegerWithoutPathExpansion(const std::string& key, michael@0: int* out_value) const; michael@0: bool GetDoubleWithoutPathExpansion(const std::string& key, michael@0: double* out_value) const; michael@0: bool GetStringWithoutPathExpansion(const std::string& key, michael@0: std::string* out_value) const; michael@0: bool GetStringWithoutPathExpansion(const std::string& key, michael@0: string16* out_value) const; michael@0: bool GetDictionaryWithoutPathExpansion( michael@0: const std::string& key, michael@0: const DictionaryValue** out_value) const; michael@0: bool GetDictionaryWithoutPathExpansion(const std::string& key, michael@0: DictionaryValue** out_value); michael@0: bool GetListWithoutPathExpansion(const std::string& key, michael@0: const ListValue** out_value) const; michael@0: bool GetListWithoutPathExpansion(const std::string& key, michael@0: ListValue** out_value); michael@0: michael@0: // Removes the Value with the specified path from this dictionary (or one michael@0: // of its child dictionaries, if the path is more than just a local key). michael@0: // If |out_value| is non-NULL, the removed Value will be passed out via michael@0: // |out_value|. If |out_value| is NULL, the removed value will be deleted. michael@0: // This method returns true if |path| is a valid path; otherwise it will michael@0: // return false and the DictionaryValue object will be unchanged. michael@0: virtual bool Remove(const std::string& path, scoped_ptr* out_value); michael@0: michael@0: // Like Remove(), but without special treatment of '.'. This allows e.g. URLs michael@0: // to be used as paths. michael@0: virtual bool RemoveWithoutPathExpansion(const std::string& key, michael@0: scoped_ptr* out_value); michael@0: michael@0: // Makes a copy of |this| but doesn't include empty dictionaries and lists in michael@0: // the copy. This never returns NULL, even if |this| itself is empty. michael@0: DictionaryValue* DeepCopyWithoutEmptyChildren() const; michael@0: michael@0: // Merge |dictionary| into this dictionary. This is done recursively, i.e. any michael@0: // sub-dictionaries will be merged as well. In case of key collisions, the michael@0: // passed in dictionary takes precedence and data already present will be michael@0: // replaced. Values within |dictionary| are deep-copied, so |dictionary| may michael@0: // be freed any time after this call. michael@0: void MergeDictionary(const DictionaryValue* dictionary); michael@0: michael@0: // Swaps contents with the |other| dictionary. michael@0: virtual void Swap(DictionaryValue* other); michael@0: michael@0: // This class provides an iterator over both keys and values in the michael@0: // dictionary. It can't be used to modify the dictionary. michael@0: class BASE_EXPORT Iterator { michael@0: public: michael@0: explicit Iterator(const DictionaryValue& target); michael@0: michael@0: bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } michael@0: void Advance() { ++it_; } michael@0: michael@0: const std::string& key() const { return it_->first; } michael@0: const Value& value() const { return *it_->second; } michael@0: michael@0: private: michael@0: const DictionaryValue& target_; michael@0: ValueMap::const_iterator it_; michael@0: }; michael@0: michael@0: // Overridden from Value: michael@0: virtual DictionaryValue* DeepCopy() const OVERRIDE; michael@0: virtual bool Equals(const Value* other) const OVERRIDE; michael@0: michael@0: private: michael@0: ValueMap dictionary_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(DictionaryValue); michael@0: }; michael@0: michael@0: // This type of Value represents a list of other Value values. michael@0: class BASE_EXPORT ListValue : public Value { michael@0: public: michael@0: typedef ValueVector::iterator iterator; michael@0: typedef ValueVector::const_iterator const_iterator; michael@0: michael@0: ListValue(); michael@0: virtual ~ListValue(); michael@0: michael@0: // Clears the contents of this ListValue michael@0: void Clear(); michael@0: michael@0: // Returns the number of Values in this list. michael@0: size_t GetSize() const { return list_.size(); } michael@0: michael@0: // Returns whether the list is empty. michael@0: bool empty() const { return list_.empty(); } michael@0: michael@0: // Sets the list item at the given index to be the Value specified by michael@0: // the value given. If the index beyond the current end of the list, null michael@0: // Values will be used to pad out the list. michael@0: // Returns true if successful, or false if the index was negative or michael@0: // the value is a null pointer. michael@0: bool Set(size_t index, Value* in_value); michael@0: michael@0: // Gets the Value at the given index. Modifies |out_value| (and returns true) michael@0: // only if the index falls within the current list range. michael@0: // Note that the list always owns the Value passed out via |out_value|. michael@0: bool Get(size_t index, const Value** out_value) const; michael@0: bool Get(size_t index, Value** out_value); michael@0: michael@0: // Convenience forms of Get(). Modifies |out_value| (and returns true) michael@0: // only if the index is valid and the Value at that index can be returned michael@0: // in the specified form. michael@0: bool GetBoolean(size_t index, bool* out_value) const; michael@0: bool GetInteger(size_t index, int* out_value) const; michael@0: bool GetDouble(size_t index, double* out_value) const; michael@0: bool GetString(size_t index, std::string* out_value) const; michael@0: bool GetString(size_t index, string16* out_value) const; michael@0: bool GetBinary(size_t index, const BinaryValue** out_value) const; michael@0: bool GetBinary(size_t index, BinaryValue** out_value); michael@0: bool GetDictionary(size_t index, const DictionaryValue** out_value) const; michael@0: bool GetDictionary(size_t index, DictionaryValue** out_value); michael@0: bool GetList(size_t index, const ListValue** out_value) const; michael@0: bool GetList(size_t index, ListValue** out_value); michael@0: michael@0: // Removes the Value with the specified index from this list. michael@0: // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be michael@0: // passed out via |out_value|. If |out_value| is NULL, the removed value will michael@0: // be deleted. This method returns true if |index| is valid; otherwise michael@0: // it will return false and the ListValue object will be unchanged. michael@0: virtual bool Remove(size_t index, scoped_ptr* out_value); michael@0: michael@0: // Removes the first instance of |value| found in the list, if any, and michael@0: // deletes it. |index| is the location where |value| was found. Returns false michael@0: // if not found. michael@0: bool Remove(const Value& value, size_t* index); michael@0: michael@0: // Removes the element at |iter|. If |out_value| is NULL, the value will be michael@0: // deleted, otherwise ownership of the value is passed back to the caller. michael@0: // Returns an iterator pointing to the location of the element that michael@0: // followed the erased element. michael@0: iterator Erase(iterator iter, scoped_ptr* out_value); michael@0: michael@0: // Appends a Value to the end of the list. michael@0: void Append(Value* in_value); michael@0: michael@0: // Convenience forms of Append. michael@0: void AppendBoolean(bool in_value); michael@0: void AppendInteger(int in_value); michael@0: void AppendDouble(double in_value); michael@0: void AppendString(const std::string& in_value); michael@0: void AppendString(const string16& in_value); michael@0: void AppendStrings(const std::vector& in_values); michael@0: void AppendStrings(const std::vector& in_values); michael@0: michael@0: // Appends a Value if it's not already present. Takes ownership of the michael@0: // |in_value|. Returns true if successful, or false if the value was already michael@0: // present. If the value was already present the |in_value| is deleted. michael@0: bool AppendIfNotPresent(Value* in_value); michael@0: michael@0: // Insert a Value at index. michael@0: // Returns true if successful, or false if the index was out of range. michael@0: bool Insert(size_t index, Value* in_value); michael@0: michael@0: // Searches for the first instance of |value| in the list using the Equals michael@0: // method of the Value type. michael@0: // Returns a const_iterator to the found item or to end() if none exists. michael@0: const_iterator Find(const Value& value) const; michael@0: michael@0: // Swaps contents with the |other| list. michael@0: virtual void Swap(ListValue* other); michael@0: michael@0: // Iteration. michael@0: iterator begin() { return list_.begin(); } michael@0: iterator end() { return list_.end(); } michael@0: michael@0: const_iterator begin() const { return list_.begin(); } michael@0: const_iterator end() const { return list_.end(); } michael@0: michael@0: // Overridden from Value: michael@0: virtual bool GetAsList(ListValue** out_value) OVERRIDE; michael@0: virtual bool GetAsList(const ListValue** out_value) const OVERRIDE; michael@0: virtual ListValue* DeepCopy() const OVERRIDE; michael@0: virtual bool Equals(const Value* other) const OVERRIDE; michael@0: michael@0: private: michael@0: ValueVector list_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ListValue); michael@0: }; michael@0: michael@0: // This interface is implemented by classes that know how to serialize and michael@0: // deserialize Value objects. michael@0: class BASE_EXPORT ValueSerializer { michael@0: public: michael@0: virtual ~ValueSerializer(); michael@0: michael@0: virtual bool Serialize(const Value& root) = 0; michael@0: michael@0: // This method deserializes the subclass-specific format into a Value object. michael@0: // If the return value is non-NULL, the caller takes ownership of returned michael@0: // Value. If the return value is NULL, and if error_code is non-NULL, michael@0: // error_code will be set with the underlying error. michael@0: // If |error_message| is non-null, it will be filled in with a formatted michael@0: // error message including the location of the error if appropriate. michael@0: virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; michael@0: }; michael@0: michael@0: // Stream operator so Values can be used in assertion statements. In order that michael@0: // gtest uses this operator to print readable output on test failures, we must michael@0: // override each specific type. Otherwise, the default template implementation michael@0: // is preferred over an upcast. michael@0: BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); michael@0: michael@0: BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, michael@0: const FundamentalValue& value) { michael@0: return out << static_cast(value); michael@0: } michael@0: michael@0: BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, michael@0: const StringValue& value) { michael@0: return out << static_cast(value); michael@0: } michael@0: michael@0: BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, michael@0: const DictionaryValue& value) { michael@0: return out << static_cast(value); michael@0: } michael@0: michael@0: BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, michael@0: const ListValue& value) { michael@0: return out << static_cast(value); michael@0: } michael@0: michael@0: } // namespace base michael@0: michael@0: // http://crbug.com/88666 michael@0: using base::DictionaryValue; michael@0: using base::ListValue; michael@0: using base::StringValue; michael@0: using base::Value; michael@0: michael@0: #endif // BASE_VALUES_H_