values.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// Use of this source code is governed by a BSD-style license that can be 3664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// found in the LICENSE file. 4664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 5664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// This file specifies a recursive data storage class called Value intended for 6664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// storing setting and other persistable data. It includes the ability to 7664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// specify (recursive) lists and dictionaries, so it's fairly expressive. 8664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// However, the API is optimized for the common case, namely storing a 9664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// hierarchical tree of simple values. Given a DictionaryValue root, you can 10664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// easily do things like: 11664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// 12664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// root->SetString("global.pages.homepage", "http://goateleporter.com"); 13664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// std::string homepage = "http://google.com"; // default/fallback value 14664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// root->GetString("global.pages.homepage", &homepage); 15664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// 16664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// where "global" and "pages" are also DictionaryValues, and "homepage" is a 17664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// string setting. If some elements of the path didn't exist yet, the 18664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// SetString() method would create the missing elements and attach them to root 19664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// before attaching the homepage value. 20664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 21664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#ifndef BASE_VALUES_H_ 22664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#define BASE_VALUES_H_ 23664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 24664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include <iterator> 25664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include <map> 26664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include <string> 27664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include <vector> 28664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 29664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include "base/base_export.h" 30664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include "base/basictypes.h" 31664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include "base/compiler_specific.h" 32664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include "base/memory/scoped_ptr.h" 33664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis#include "base/string16.h" 34664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 35664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// This file declares "using base::Value", etc. at the bottom, so that 36664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// current code can use these classes without the base namespace. In 37664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// new code, please always use base::Value, etc. or add your own 38664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// "using" declaration. 39664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// http://crbug.com/88666 40664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisnamespace base { 41664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 42664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass BinaryValue; 43664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass DictionaryValue; 44664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass FundamentalValue; 45664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass ListValue; 46664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass StringValue; 47664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass Value; 48664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 49664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennistypedef std::vector<Value*> ValueVector; 50664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennistypedef std::map<std::string, Value*> ValueMap; 51664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 52664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// The Value class is the base class for Values. A Value can be instantiated 53664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// via the Create*Value() factory methods, or by directly creating instances of 54664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// the subclasses. 55664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass BASE_EXPORT Value { 56664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis public: 57664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis enum Type { 58664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_NULL = 0, 59664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_BOOLEAN, 60664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_INTEGER, 61664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_DOUBLE, 62664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_STRING, 63664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_BINARY, 64664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_DICTIONARY, 65664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis TYPE_LIST 66664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis }; 67664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 68664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual ~Value(); 69664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 70664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static Value* CreateNullValue(); 71664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // DEPRECATED: Do not use the following 5 functions. Instead, use 72664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // new FundamentalValue or new StringValue. 73664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static FundamentalValue* CreateBooleanValue(bool in_value); 74664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static FundamentalValue* CreateIntegerValue(int in_value); 75664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static FundamentalValue* CreateDoubleValue(double in_value); 76664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static StringValue* CreateStringValue(const std::string& in_value); 77664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static StringValue* CreateStringValue(const string16& in_value); 78664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 79664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Returns the type of the value stored by the current Value object. 80664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Each type will be implemented by only one subclass of Value, so it's 81664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // safe to use the Type to determine whether you can cast from 82664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Value* to (Implementing Class)*. Also, a Value object never changes 83664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // its type after construction. 84664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis Type GetType() const { return type_; } 85664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 86664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Returns true if the current object represents a given type. 87664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis bool IsType(Type type) const { return type == type_; } 88664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 89664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // These methods allow the convenient retrieval of settings. 90664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // If the current setting object can be converted into the given type, 91664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // the value is returned through the |out_value| parameter and true is 92664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // returned; otherwise, false is returned and |out_value| is unchanged. 93664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsBoolean(bool* out_value) const; 94664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsInteger(int* out_value) const; 95664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsDouble(double* out_value) const; 96664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsString(std::string* out_value) const; 97664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsString(string16* out_value) const; 98664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsList(ListValue** out_value); 99664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsList(const ListValue** out_value) const; 100664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsDictionary(DictionaryValue** out_value); 101664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsDictionary(const DictionaryValue** out_value) const; 102664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 103664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // This creates a deep copy of the entire Value tree, and returns a pointer 104664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // to the copy. The caller gets ownership of the copy, of course. 105664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // 106664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Subclasses return their own type directly in their overrides; 107664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // this works because C++ supports covariant return types. 108664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual Value* DeepCopy() const; 109664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 110664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Compares if two Value objects have equal contents. 111664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool Equals(const Value* other) const; 112664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 113664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Compares if two Value objects have equal contents. Can handle NULLs. 114664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // NULLs are considered equal but different from Value::CreateNullValue(). 115664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static bool Equals(const Value* a, const Value* b); 116664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 117664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis protected: 118664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // These aren't safe for end-users, but they are useful for subclasses. 119664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit Value(Type type); 120664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis Value(const Value& that); 121664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis Value& operator=(const Value& that); 122664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 123664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis private: 124664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis Type type_; 125664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis}; 126664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 127664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// FundamentalValue represents the simple fundamental types of values. 128664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass BASE_EXPORT FundamentalValue : public Value { 129664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis public: 130664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit FundamentalValue(bool in_value); 131664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit FundamentalValue(int in_value); 132664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit FundamentalValue(double in_value); 133664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual ~FundamentalValue(); 134664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 135664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Overridden from Value: 13666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis virtual bool GetAsBoolean(bool* out_value) const OVERRIDE; 137664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsInteger(int* out_value) const OVERRIDE; 138664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsDouble(double* out_value) const OVERRIDE; 139664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual FundamentalValue* DeepCopy() const OVERRIDE; 140664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool Equals(const Value* other) const OVERRIDE; 14166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 142664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis private: 143664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis union { 144664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis bool boolean_value_; 145664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis int integer_value_; 146664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis double double_value_; 14766a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis }; 148664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis}; 149664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 150664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass BASE_EXPORT StringValue : public Value { 151664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis public: 152664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Initializes a StringValue with a UTF-8 narrow character string. 153664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit StringValue(const std::string& in_value); 154664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 155664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Initializes a StringValue with a string16. 156664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis explicit StringValue(const string16& in_value); 157664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 158664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual ~StringValue(); 159664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 160664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Overridden from Value: 161664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsString(std::string* out_value) const OVERRIDE; 162664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool GetAsString(string16* out_value) const OVERRIDE; 163664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual StringValue* DeepCopy() const OVERRIDE; 164664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool Equals(const Value* other) const OVERRIDE; 165664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 166664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis private: 167664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis std::string value_; 168664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis}; 169664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 170664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennisclass BASE_EXPORT BinaryValue: public Value { 171664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis public: 172664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Creates a BinaryValue with a null buffer and size of 0. 173664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis BinaryValue(); 174664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 175664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Creates a BinaryValue, taking ownership of the bytes pointed to by 176664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // |buffer|. 177664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis BinaryValue(scoped_ptr<char[]> buffer, size_t size); 178664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 1799c39c34a196b50aaaa2dd897b7844e2e0508e522Siva Velusamy virtual ~BinaryValue(); 180664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 18166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // For situations where you want to keep ownership of your buffer, this 182664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // factory method creates a new BinaryValue by copying the contents of the 183664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // buffer that's passed in. 184664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); 185664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 186664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis size_t GetSize() const { return size_; } 187664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 188664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // May return NULL. 189664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis char* GetBuffer() { return buffer_.get(); } 190664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis const char* GetBuffer() const { return buffer_.get(); } 191664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 192664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Overridden from Value: 193664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual BinaryValue* DeepCopy() const OVERRIDE; 194664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual bool Equals(const Value* other) const OVERRIDE; 195664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 196664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis private: 197664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis scoped_ptr<char[]> buffer_; 198664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis size_t size_; 199664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 200664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis DISALLOW_COPY_AND_ASSIGN(BinaryValue); 201664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis}; 202664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 2039c39c34a196b50aaaa2dd897b7844e2e0508e522Siva Velusamy// DictionaryValue provides a key-value dictionary with (optional) "path" 204664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// parsing for recursive access; see the comment at the top of the file. Keys 205664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis// are |std::string|s and should be UTF-8 encoded. 20666a37686207944273ced825e0e8b6b6375f8c3deJamie Gennisclass BASE_EXPORT DictionaryValue : public Value { 207664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis public: 208664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis DictionaryValue(); 209664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis virtual ~DictionaryValue(); 210664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 2119c39c34a196b50aaaa2dd897b7844e2e0508e522Siva Velusamy // Overridden from Value: 2129c39c34a196b50aaaa2dd897b7844e2e0508e522Siva Velusamy virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE; 2139c39c34a196b50aaaa2dd897b7844e2e0508e522Siva Velusamy virtual bool GetAsDictionary( 214664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis const DictionaryValue** out_value) const OVERRIDE; 215664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 216664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Returns true if the current dictionary has a value for the given key. 217664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis bool HasKey(const std::string& key) const; 21866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 219664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Returns the number of Values in this dictionary. 22066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis size_t size() const { return dictionary_.size(); } 221664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 222664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Returns whether the dictionary is empty. 223664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis bool empty() const { return dictionary_.empty(); } 22466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 225664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Clears any current contents of this dictionary. 226664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void Clear(); 227664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 228664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Sets the Value associated with the given path starting from this object. 229664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes 23066a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // into the next DictionaryValue down. Obviously, "." can't be used 23166a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // within a key, but there are no other restrictions on keys. 23266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // If the key at any step of the way doesn't exist, or exists but isn't 23366a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // a DictionaryValue, a new DictionaryValue will be created and attached 23466a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // to the path in that location. 235664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Note that the dictionary takes ownership of the value referenced by 236664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // |in_value|, and therefore |in_value| must be non-NULL. 237664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void Set(const std::string& path, Value* in_value); 23866a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis 23966a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis // Convenience forms of Set(). These methods will replace any existing 240664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // value at that path, even if it has a different type. 241664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void SetBoolean(const std::string& path, bool in_value); 24266a37686207944273ced825e0e8b6b6375f8c3deJamie Gennis void SetInteger(const std::string& path, int in_value); 243664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void SetDouble(const std::string& path, double in_value); 244664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void SetString(const std::string& path, const std::string& in_value); 245664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void SetString(const std::string& path, const string16& in_value); 246664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 247664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Like Set(), but without special treatment of '.'. This allows e.g. URLs to 248664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // be used as paths. 249664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis void SetWithoutPathExpansion(const std::string& key, Value* in_value); 250664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis 251664f21bcaf14044e5e9b09cb7beb8724d18fb851Jamie Gennis // Convenience forms of SetWithoutPathExpansion(). 252 void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value); 253 void SetIntegerWithoutPathExpansion(const std::string& path, int in_value); 254 void SetDoubleWithoutPathExpansion(const std::string& path, double in_value); 255 void SetStringWithoutPathExpansion(const std::string& path, 256 const std::string& in_value); 257 void SetStringWithoutPathExpansion(const std::string& path, 258 const string16& in_value); 259 260 // Gets the Value associated with the given path starting from this object. 261 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes 262 // into the next DictionaryValue down. If the path can be resolved 263 // successfully, the value for the last key in the path will be returned 264 // through the |out_value| parameter, and the function will return true. 265 // Otherwise, it will return false and |out_value| will be untouched. 266 // Note that the dictionary always owns the value that's returned. 267 bool Get(const std::string& path, const Value** out_value) const; 268 bool Get(const std::string& path, Value** out_value); 269 270 // These are convenience forms of Get(). The value will be retrieved 271 // and the return value will be true if the path is valid and the value at 272 // the end of the path can be returned in the form specified. 273 bool GetBoolean(const std::string& path, bool* out_value) const; 274 bool GetInteger(const std::string& path, int* out_value) const; 275 bool GetDouble(const std::string& path, double* out_value) const; 276 bool GetString(const std::string& path, std::string* out_value) const; 277 bool GetString(const std::string& path, string16* out_value) const; 278 bool GetStringASCII(const std::string& path, std::string* out_value) const; 279 bool GetBinary(const std::string& path, const BinaryValue** out_value) const; 280 bool GetBinary(const std::string& path, BinaryValue** out_value); 281 bool GetDictionary(const std::string& path, 282 const DictionaryValue** out_value) const; 283 bool GetDictionary(const std::string& path, DictionaryValue** out_value); 284 bool GetList(const std::string& path, const ListValue** out_value) const; 285 bool GetList(const std::string& path, ListValue** out_value); 286 287 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to 288 // be used as paths. 289 bool GetWithoutPathExpansion(const std::string& key, 290 const Value** out_value) const; 291 bool GetWithoutPathExpansion(const std::string& key, Value** out_value); 292 bool GetBooleanWithoutPathExpansion(const std::string& key, 293 bool* out_value) const; 294 bool GetIntegerWithoutPathExpansion(const std::string& key, 295 int* out_value) const; 296 bool GetDoubleWithoutPathExpansion(const std::string& key, 297 double* out_value) const; 298 bool GetStringWithoutPathExpansion(const std::string& key, 299 std::string* out_value) const; 300 bool GetStringWithoutPathExpansion(const std::string& key, 301 string16* out_value) const; 302 bool GetDictionaryWithoutPathExpansion( 303 const std::string& key, 304 const DictionaryValue** out_value) const; 305 bool GetDictionaryWithoutPathExpansion(const std::string& key, 306 DictionaryValue** out_value); 307 bool GetListWithoutPathExpansion(const std::string& key, 308 const ListValue** out_value) const; 309 bool GetListWithoutPathExpansion(const std::string& key, 310 ListValue** out_value); 311 312 // Removes the Value with the specified path from this dictionary (or one 313 // of its child dictionaries, if the path is more than just a local key). 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 |path| is a valid path; otherwise 317 // it will return false and the DictionaryValue object will be unchanged. 318 virtual bool Remove(const std::string& path, Value** out_value); 319 320 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs 321 // to be used as paths. 322 virtual bool RemoveWithoutPathExpansion(const std::string& key, 323 Value** out_value); 324 325 // Makes a copy of |this| but doesn't include empty dictionaries and lists in 326 // the copy. This never returns NULL, even if |this| itself is empty. 327 DictionaryValue* DeepCopyWithoutEmptyChildren(); 328 329 // Merge |dictionary| into this dictionary. This is done recursively, i.e. any 330 // sub-dictionaries will be merged as well. In case of key collisions, the 331 // passed in dictionary takes precedence and data already present will be 332 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may 333 // be freed any time after this call. 334 void MergeDictionary(const DictionaryValue* dictionary); 335 336 // Swaps contents with the |other| dictionary. 337 virtual void Swap(DictionaryValue* other); 338 339 // This class provides an iterator over both keys and values in the 340 // dictionary. It can't be used to modify the dictionary. 341 class BASE_EXPORT Iterator { 342 public: 343 explicit Iterator(const DictionaryValue& target); 344 345 // DEPRECATED: use !IsAtEnd() instead. 346 bool HasNext() const { return it_ != target_.dictionary_.end(); } 347 348 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } 349 void Advance() { ++it_; } 350 351 const std::string& key() const { return it_->first; } 352 const Value& value() const { return *it_->second; } 353 354 private: 355 const DictionaryValue& target_; 356 ValueMap::const_iterator it_; 357 }; 358 359 // Overridden from Value: 360 virtual DictionaryValue* DeepCopy() const OVERRIDE; 361 virtual bool Equals(const Value* other) const OVERRIDE; 362 363 private: 364 ValueMap dictionary_; 365 366 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); 367}; 368 369// This type of Value represents a list of other Value values. 370class BASE_EXPORT ListValue : public Value { 371 public: 372 typedef ValueVector::iterator iterator; 373 typedef ValueVector::const_iterator const_iterator; 374 375 ListValue(); 376 virtual ~ListValue(); 377 378 // Clears the contents of this ListValue 379 void Clear(); 380 381 // Returns the number of Values in this list. 382 size_t GetSize() const { return list_.size(); } 383 384 // Returns whether the list is empty. 385 bool empty() const { return list_.empty(); } 386 387 // Sets the list item at the given index to be the Value specified by 388 // the value given. If the index beyond the current end of the list, null 389 // Values will be used to pad out the list. 390 // Returns true if successful, or false if the index was negative or 391 // the value is a null pointer. 392 bool Set(size_t index, Value* in_value); 393 394 // Gets the Value at the given index. Modifies |out_value| (and returns true) 395 // only if the index falls within the current list range. 396 // Note that the list always owns the Value passed out via |out_value|. 397 bool Get(size_t index, const Value** out_value) const; 398 bool Get(size_t index, Value** out_value); 399 400 // Convenience forms of Get(). Modifies |out_value| (and returns true) 401 // only if the index is valid and the Value at that index can be returned 402 // in the specified form. 403 bool GetBoolean(size_t index, bool* out_value) const; 404 bool GetInteger(size_t index, int* out_value) const; 405 bool GetDouble(size_t index, double* out_value) const; 406 bool GetString(size_t index, std::string* out_value) const; 407 bool GetString(size_t index, string16* out_value) const; 408 bool GetBinary(size_t index, const BinaryValue** out_value) const; 409 bool GetBinary(size_t index, BinaryValue** out_value); 410 bool GetDictionary(size_t index, const DictionaryValue** out_value) const; 411 bool GetDictionary(size_t index, DictionaryValue** out_value); 412 bool GetList(size_t index, const ListValue** out_value) const; 413 bool GetList(size_t index, ListValue** out_value); 414 415 // Removes the Value with the specified index from this list. 416 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be 417 // passed out via |out_value|. If |out_value| is NULL, the removed value will 418 // be deleted. This method returns true if |index| is valid; otherwise 419 // it will return false and the ListValue object will be unchanged. 420 virtual bool Remove(size_t index, Value** out_value); 421 422 // Removes the first instance of |value| found in the list, if any, and 423 // deletes it. |index| is the location where |value| was found. Returns false 424 // if not found. 425 bool Remove(const Value& value, size_t* index); 426 427 // Removes the element at |iter|. If |out_value| is NULL, the value will be 428 // deleted, otherwise ownership of the value is passed back to the caller. 429 // Returns an iterator pointing to the location of the element that 430 // followed the erased element. 431 iterator Erase(iterator iter, Value** out_value); 432 433 // Appends a Value to the end of the list. 434 void Append(Value* in_value); 435 436 // Convenience forms of Append. 437 void AppendBoolean(bool in_value); 438 void AppendInteger(int in_value); 439 void AppendDouble(double in_value); 440 void AppendString(const std::string& in_value); 441 void AppendString(const string16& in_value); 442 void AppendStrings(const std::vector<std::string>& in_values); 443 void AppendStrings(const std::vector<string16>& in_values); 444 445 // Appends a Value if it's not already present. Takes ownership of the 446 // |in_value|. Returns true if successful, or false if the value was already 447 // present. If the value was already present the |in_value| is deleted. 448 bool AppendIfNotPresent(Value* in_value); 449 450 // Insert a Value at index. 451 // Returns true if successful, or false if the index was out of range. 452 bool Insert(size_t index, Value* in_value); 453 454 // Searches for the first instance of |value| in the list using the Equals 455 // method of the Value type. 456 // Returns a const_iterator to the found item or to end() if none exists. 457 const_iterator Find(const Value& value) const; 458 459 // Swaps contents with the |other| list. 460 virtual void Swap(ListValue* other); 461 462 // Iteration. 463 iterator begin() { return list_.begin(); } 464 iterator end() { return list_.end(); } 465 466 const_iterator begin() const { return list_.begin(); } 467 const_iterator end() const { return list_.end(); } 468 469 // Overridden from Value: 470 virtual bool GetAsList(ListValue** out_value) OVERRIDE; 471 virtual bool GetAsList(const ListValue** out_value) const OVERRIDE; 472 virtual ListValue* DeepCopy() const OVERRIDE; 473 virtual bool Equals(const Value* other) const OVERRIDE; 474 475 private: 476 ValueVector list_; 477 478 DISALLOW_COPY_AND_ASSIGN(ListValue); 479}; 480 481// This interface is implemented by classes that know how to serialize and 482// deserialize Value objects. 483class BASE_EXPORT ValueSerializer { 484 public: 485 virtual ~ValueSerializer(); 486 487 virtual bool Serialize(const Value& root) = 0; 488 489 // This method deserializes the subclass-specific format into a Value object. 490 // If the return value is non-NULL, the caller takes ownership of returned 491 // Value. If the return value is NULL, and if error_code is non-NULL, 492 // error_code will be set with the underlying error. 493 // If |error_message| is non-null, it will be filled in with a formatted 494 // error message including the location of the error if appropriate. 495 virtual Value* Deserialize(int* error_code, std::string* error_str) = 0; 496}; 497 498// Stream operator so Values can be used in assertion statements. 499BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); 500 501} // namespace base 502 503// http://crbug.com/88666 504using base::DictionaryValue; 505using base::ListValue; 506using base::StringValue; 507using base::Value; 508 509#endif // BASE_VALUES_H_ 510