values.h revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file specifies a recursive data storage class called Value intended for
67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// storing settings and other persistable data.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// A Value represents something that can be stored in JSON or passed to/from
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// JavaScript. As such, it is NOT a generalized variant type, since only the
107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// types supported by JavaScript/JSON are supported.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// IN PARTICULAR this means that there is no support for int64 or unsigned
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// numbers. Writing JSON with such types would violate the spec. If you need
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// something like this, either use a double or make a string value containing
157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// the number you want.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_VALUES_H_
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_VALUES_H_
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iterator>
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file declares "using base::Value", etc. at the bottom, so that
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// current code can use these classes without the base namespace. In
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// new code, please always use base::Value, etc. or add your own
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "using" declaration.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://crbug.com/88666
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BinaryValue;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FundamentalValue;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListValue;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringValue;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<Value*> ValueVector;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::map<std::string, Value*> ValueMap;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Value class is the base class for Values. A Value can be instantiated
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// via the Create*Value() factory methods, or by directly creating instances of
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the subclasses.
517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch//
527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// See the file-level comment above for more information.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT Value {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Type {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_NULL = 0,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_BOOLEAN,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_INTEGER,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_DOUBLE,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_STRING,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_BINARY,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_DICTIONARY,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_LIST
647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Note: Do not add more types. See the file-level comment above for why.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Value();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Value* CreateNullValue();
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // DEPRECATED: Do not use the following 5 functions. Instead, use
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // new FundamentalValue or new StringValue.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FundamentalValue* CreateBooleanValue(bool in_value);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FundamentalValue* CreateIntegerValue(int in_value);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static FundamentalValue* CreateDoubleValue(double in_value);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StringValue* CreateStringValue(const std::string& in_value);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static StringValue* CreateStringValue(const string16& in_value);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type of the value stored by the current Value object.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Each type will be implemented by only one subclass of Value, so it's
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // safe to use the Type to determine whether you can cast from
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value* to (Implementing Class)*.  Also, a Value object never changes
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // its type after construction.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type GetType() const { return type_; }
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the current object represents a given type.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsType(Type type) const { return type == type_; }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // These methods allow the convenient retrieval of the contents of the Value.
897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If the current object can be converted into the given type, the value is
907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // returned through the |out_value| parameter and true is returned;
917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // otherwise, false is returned and |out_value| is unchanged.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsBoolean(bool* out_value) const;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsInteger(int* out_value) const;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDouble(double* out_value) const;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(std::string* out_value) const;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(string16* out_value) const;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(ListValue** out_value);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(const ListValue** out_value) const;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(DictionaryValue** out_value);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
1017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Note: Do not add more types. See the file-level comment above for why.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This creates a deep copy of the entire Value tree, and returns a pointer
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the copy.  The caller gets ownership of the copy, of course.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Subclasses return their own type directly in their overrides;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this works because C++ supports covariant return types.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Value* DeepCopy() const;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compares if two Value objects have equal contents.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compares if two Value objects have equal contents. Can handle NULLs.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULLs are considered equal but different from Value::CreateNullValue().
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool Equals(const Value* a, const Value* b);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These aren't safe for end-users, but they are useful for subclasses.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit Value(Type type);
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Value(const Value& that);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Value& operator=(const Value& that);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type type_;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FundamentalValue represents the simple fundamental types of values.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT FundamentalValue : public Value {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(bool in_value);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(int in_value);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(double in_value);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FundamentalValue();
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsBoolean(bool* out_value) const OVERRIDE;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsInteger(int* out_value) const OVERRIDE;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDouble(double* out_value) const OVERRIDE;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual FundamentalValue* DeepCopy() const OVERRIDE;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  union {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool boolean_value_;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int integer_value_;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    double double_value_;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT StringValue : public Value {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes a StringValue with a UTF-8 narrow character string.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit StringValue(const std::string& in_value);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes a StringValue with a string16.
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit StringValue(const string16& in_value);
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~StringValue();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(std::string* out_value) const OVERRIDE;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(string16* out_value) const OVERRIDE;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual StringValue* DeepCopy() const OVERRIDE;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string value_;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT BinaryValue: public Value {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a BinaryValue with a null buffer and size of 0.
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BinaryValue();
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a BinaryValue, taking ownership of the bytes pointed to by
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |buffer|.
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BinaryValue(scoped_ptr<char[]> buffer, size_t size);
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~BinaryValue();
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For situations where you want to keep ownership of your buffer, this
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // factory method creates a new BinaryValue by copying the contents of the
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buffer that's passed in.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetSize() const { return size_; }
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // May return NULL.
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  char* GetBuffer() { return buffer_.get(); }
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const char* GetBuffer() const { return buffer_.get(); }
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual BinaryValue* DeepCopy() const OVERRIDE;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<char[]> buffer_;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t size_;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BinaryValue);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DictionaryValue provides a key-value dictionary with (optional) "path"
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// parsing for recursive access; see the comment at the top of the file. Keys
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are |std::string|s and should be UTF-8 encoded.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT DictionaryValue : public Value {
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue();
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DictionaryValue();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const DictionaryValue** out_value) const OVERRIDE;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the current dictionary has a value for the given key.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasKey(const std::string& key) const;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of Values in this dictionary.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t size() const { return dictionary_.size(); }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether the dictionary is empty.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const { return dictionary_.empty(); }
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears any current contents of this dictionary.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the Value associated with the given path starting from this object.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the next DictionaryValue down.  Obviously, "." can't be used
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // within a key, but there are no other restrictions on keys.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the key at any step of the way doesn't exist, or exists but isn't
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a DictionaryValue, a new DictionaryValue will be created and attached
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the path in that location.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the dictionary takes ownership of the value referenced by
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |in_value|, and therefore |in_value| must be non-NULL.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Set(const std::string& path, Value* in_value);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Set().  These methods will replace any existing
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value at that path, even if it has a different type.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBoolean(const std::string& path, bool in_value);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetInteger(const std::string& path, int in_value);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDouble(const std::string& path, double in_value);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetString(const std::string& path, const std::string& in_value);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetString(const std::string& path, const string16& in_value);
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Set(), but without special treatment of '.'.  This allows e.g. URLs to
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be used as paths.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetWithoutPathExpansion(const std::string& key, Value* in_value);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of SetWithoutPathExpansion().
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetIntegerWithoutPathExpansion(const std::string& path, int in_value);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDoubleWithoutPathExpansion(const std::string& path, double in_value);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetStringWithoutPathExpansion(const std::string& path,
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const std::string& in_value);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetStringWithoutPathExpansion(const std::string& path,
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const string16& in_value);
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the Value associated with the given path starting from this object.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the next DictionaryValue down.  If the path can be resolved
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // successfully, the value for the last key in the path will be returned
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // through the |out_value| parameter, and the function will return true.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise, it will return false and |out_value| will be untouched.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the dictionary always owns the value that's returned.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(const std::string& path, const Value** out_value) const;
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(const std::string& path, Value** out_value);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are convenience forms of Get().  The value will be retrieved
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and the return value will be true if the path is valid and the value at
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the end of the path can be returned in the form specified.
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBoolean(const std::string& path, bool* out_value) const;
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetInteger(const std::string& path, int* out_value) const;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDouble(const std::string& path, double* out_value) const;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(const std::string& path, std::string* out_value) const;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(const std::string& path, string16* out_value) const;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringASCII(const std::string& path, std::string* out_value) const;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(const std::string& path, BinaryValue** out_value);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(const std::string& path,
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     const DictionaryValue** out_value) const;
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(const std::string& path, DictionaryValue** out_value);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(const std::string& path, const ListValue** out_value) const;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(const std::string& path, ListValue** out_value);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Get(), but without special treatment of '.'.  This allows e.g. URLs to
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be used as paths.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetWithoutPathExpansion(const std::string& key,
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const Value** out_value) const;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBooleanWithoutPathExpansion(const std::string& key,
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      bool* out_value) const;
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetIntegerWithoutPathExpansion(const std::string& key,
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int* out_value) const;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDoubleWithoutPathExpansion(const std::string& key,
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     double* out_value) const;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringWithoutPathExpansion(const std::string& key,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     std::string* out_value) const;
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringWithoutPathExpansion(const std::string& key,
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     string16* out_value) const;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionaryWithoutPathExpansion(
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& key,
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const DictionaryValue** out_value) const;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionaryWithoutPathExpansion(const std::string& key,
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         DictionaryValue** out_value);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetListWithoutPathExpansion(const std::string& key,
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const ListValue** out_value) const;
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetListWithoutPathExpansion(const std::string& key,
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   ListValue** out_value);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the Value with the specified path from this dictionary (or one
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of its child dictionaries, if the path is more than just a local key).
3143240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // If |out_value| is non-NULL, the removed Value will be passed out via
3153240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // |out_value|.  If |out_value| is NULL, the removed value will be deleted.
3163240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // This method returns true if |path| is a valid path; otherwise it will
3173240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // return false and the DictionaryValue object will be unchanged.
3183240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value);
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Remove(), but without special treatment of '.'.  This allows e.g. URLs
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to be used as paths.
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RemoveWithoutPathExpansion(const std::string& key,
3233240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                                          scoped_ptr<Value>* out_value);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes a copy of |this| but doesn't include empty dictionaries and lists in
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the copy.  This never returns NULL, even if |this| itself is empty.
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* DeepCopyWithoutEmptyChildren();
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sub-dictionaries will be merged as well. In case of key collisions, the
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // passed in dictionary takes precedence and data already present will be
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be freed any time after this call.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MergeDictionary(const DictionaryValue* dictionary);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Swaps contents with the |other| dictionary.
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Swap(DictionaryValue* other);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This class provides an iterator over both keys and values in the
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dictionary.  It can't be used to modify the dictionary.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class BASE_EXPORT Iterator {
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit Iterator(const DictionaryValue& target);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Advance() { ++it_; }
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& key() const { return it_->first; }
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Value& value() const { return *it_->second; }
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DictionaryValue& target_;
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ValueMap::const_iterator it_;
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DictionaryValue* DeepCopy() const OVERRIDE;
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ValueMap dictionary_;
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This type of Value represents a list of other Value values.
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT ListValue : public Value {
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef ValueVector::iterator iterator;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef ValueVector::const_iterator const_iterator;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListValue();
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ListValue();
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears the contents of this ListValue
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of Values in this list.
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetSize() const { return list_.size(); }
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether the list is empty.
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const { return list_.empty(); }
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the list item at the given index to be the Value specified by
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the value given.  If the index beyond the current end of the list, null
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Values will be used to pad out the list.
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if successful, or false if the index was negative or
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the value is a null pointer.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Set(size_t index, Value* in_value);
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the Value at the given index.  Modifies |out_value| (and returns true)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only if the index falls within the current list range.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the list always owns the Value passed out via |out_value|.
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(size_t index, const Value** out_value) const;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(size_t index, Value** out_value);
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Get().  Modifies |out_value| (and returns true)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only if the index is valid and the Value at that index can be returned
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the specified form.
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBoolean(size_t index, bool* out_value) const;
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetInteger(size_t index, int* out_value) const;
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDouble(size_t index, double* out_value) const;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(size_t index, std::string* out_value) const;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(size_t index, string16* out_value) const;
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(size_t index, const BinaryValue** out_value) const;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(size_t index, BinaryValue** out_value);
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(size_t index, DictionaryValue** out_value);
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(size_t index, const ListValue** out_value) const;
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(size_t index, ListValue** out_value);
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the Value with the specified index from this list.
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // passed out via |out_value|.  If |out_value| is NULL, the removed value will
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be deleted.  This method returns true if |index| is valid; otherwise
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it will return false and the ListValue object will be unchanged.
4173240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual bool Remove(size_t index, scoped_ptr<Value>* out_value);
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the first instance of |value| found in the list, if any, and
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deletes it. |index| is the location where |value| was found. Returns false
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if not found.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Remove(const Value& value, size_t* index);
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the element at |iter|. If |out_value| is NULL, the value will be
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deleted, otherwise ownership of the value is passed back to the caller.
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns an iterator pointing to the location of the element that
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // followed the erased element.
4283240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  iterator Erase(iterator iter, scoped_ptr<Value>* out_value);
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends a Value to the end of the list.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Append(Value* in_value);
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Append.
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendBoolean(bool in_value);
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendInteger(int in_value);
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendDouble(double in_value);
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendString(const std::string& in_value);
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendString(const string16& in_value);
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendStrings(const std::vector<std::string>& in_values);
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendStrings(const std::vector<string16>& in_values);
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends a Value if it's not already present. Takes ownership of the
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |in_value|. Returns true if successful, or false if the value was already
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // present. If the value was already present the |in_value| is deleted.
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AppendIfNotPresent(Value* in_value);
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Insert a Value at index.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if successful, or false if the index was out of range.
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Insert(size_t index, Value* in_value);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Searches for the first instance of |value| in the list using the Equals
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // method of the Value type.
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a const_iterator to the found item or to end() if none exists.
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator Find(const Value& value) const;
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Swaps contents with the |other| list.
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Swap(ListValue* other);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Iteration.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  iterator begin() { return list_.begin(); }
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  iterator end() { return list_.end(); }
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator begin() const { return list_.begin(); }
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator end() const { return list_.end(); }
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(ListValue** out_value) OVERRIDE;
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ListValue* DeepCopy() const OVERRIDE;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ValueVector list_;
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ListValue);
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This interface is implemented by classes that know how to serialize and
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deserialize Value objects.
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT ValueSerializer {
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ValueSerializer();
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Serialize(const Value& root) = 0;
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method deserializes the subclass-specific format into a Value object.
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the return value is non-NULL, the caller takes ownership of returned
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value. If the return value is NULL, and if error_code is non-NULL,
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error_code will be set with the underlying error.
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |error_message| is non-null, it will be filled in with a formatted
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error message including the location of the error if appropriate.
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Stream operator so Values can be used in assertion statements.  In order that
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// gtest uses this operator to print readable output on test failures, we must
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// override each specific type. Otherwise, the default template implementation
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// is preferred over an upcast.
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const FundamentalValue& value) {
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const StringValue& value) {
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const DictionaryValue& value) {
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const ListValue& value) {
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://crbug.com/88666
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::DictionaryValue;
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::ListValue;
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::StringValue;
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::Value;
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_VALUES_H_
530