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)
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <stddef.h>
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <iosfwd>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <utility>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FundamentalValue;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListValue;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringValue;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<Value*> ValueVector;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::map<std::string, Value*> ValueMap;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Value class is the base class for Values. A Value can be instantiated
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// via the Create*Value() factory methods, or by directly creating instances of
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the subclasses.
487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch//
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// See the file-level comment above for more information.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT Value {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Type {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_NULL = 0,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_BOOLEAN,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_INTEGER,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_DOUBLE,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_STRING,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_BINARY,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_DICTIONARY,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TYPE_LIST
617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Note: Do not add more types. See the file-level comment above for why.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Value();
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Value* CreateNullValue();
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the type of the value stored by the current Value object.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Each type will be implemented by only one subclass of Value, so it's
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // safe to use the Type to determine whether you can cast from
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value* to (Implementing Class)*.  Also, a Value object never changes
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // its type after construction.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type GetType() const { return type_; }
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the current object represents a given type.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsType(Type type) const { return type == type_; }
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // These methods allow the convenient retrieval of the contents of the Value.
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If the current object can be converted into the given type, the value is
807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // returned through the |out_value| parameter and true is returned;
817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // otherwise, false is returned and |out_value| is unchanged.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsBoolean(bool* out_value) const;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsInteger(int* out_value) const;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDouble(double* out_value) const;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(std::string* out_value) const;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(string16* out_value) const;
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool GetAsString(const StringValue** out_value) const;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(ListValue** out_value);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(const ListValue** out_value) const;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(DictionaryValue** out_value);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Note: Do not add more types. See the file-level comment above for why.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This creates a deep copy of the entire Value tree, and returns a pointer
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the copy.  The caller gets ownership of the copy, of course.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Subclasses return their own type directly in their overrides;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this works because C++ supports covariant return types.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Value* DeepCopy() const;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compares if two Value objects have equal contents.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compares if two Value objects have equal contents. Can handle NULLs.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULLs are considered equal but different from Value::CreateNullValue().
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool Equals(const Value* a, const Value* b);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These aren't safe for end-users, but they are useful for subclasses.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit Value(Type type);
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Value(const Value& that);
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Value& operator=(const Value& that);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Type type_;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FundamentalValue represents the simple fundamental types of values.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT FundamentalValue : public Value {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(bool in_value);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(int in_value);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FundamentalValue(double in_value);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FundamentalValue();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsBoolean(bool* out_value) const OVERRIDE;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsInteger(int* out_value) const OVERRIDE;
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // doubles.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDouble(double* out_value) const OVERRIDE;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual FundamentalValue* DeepCopy() const OVERRIDE;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  union {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool boolean_value_;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int integer_value_;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    double double_value_;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT StringValue : public Value {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes a StringValue with a UTF-8 narrow character string.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit StringValue(const std::string& in_value);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes a StringValue with a string16.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit StringValue(const string16& in_value);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~StringValue();
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns |value_| as a pointer or reference.
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string* GetString();
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& GetString() const;
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(std::string* out_value) const OVERRIDE;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsString(string16* out_value) const OVERRIDE;
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool GetAsString(const StringValue** out_value) const OVERRIDE;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual StringValue* DeepCopy() const OVERRIDE;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string value_;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT BinaryValue: public Value {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a BinaryValue with a null buffer and size of 0.
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BinaryValue();
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates a BinaryValue, taking ownership of the bytes pointed to by
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |buffer|.
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BinaryValue(scoped_ptr<char[]> buffer, size_t size);
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~BinaryValue();
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For situations where you want to keep ownership of your buffer, this
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // factory method creates a new BinaryValue by copying the contents of the
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buffer that's passed in.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetSize() const { return size_; }
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // May return NULL.
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  char* GetBuffer() { return buffer_.get(); }
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const char* GetBuffer() const { return buffer_.get(); }
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual BinaryValue* DeepCopy() const OVERRIDE;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<char[]> buffer_;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t size_;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BinaryValue);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DictionaryValue provides a key-value dictionary with (optional) "path"
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// parsing for recursive access; see the comment at the top of the file. Keys
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are |std::string|s and should be UTF-8 encoded.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT DictionaryValue : public Value {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DictionaryValue();
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(DictionaryValue** out_value) OVERRIDE;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsDictionary(
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const DictionaryValue** out_value) const OVERRIDE;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the current dictionary has a value for the given key.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasKey(const std::string& key) const;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of Values in this dictionary.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t size() const { return dictionary_.size(); }
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether the dictionary is empty.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const { return dictionary_.empty(); }
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears any current contents of this dictionary.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the Value associated with the given path starting from this object.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the next DictionaryValue down.  Obviously, "." can't be used
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // within a key, but there are no other restrictions on keys.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the key at any step of the way doesn't exist, or exists but isn't
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a DictionaryValue, a new DictionaryValue will be created and attached
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the path in that location.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the dictionary takes ownership of the value referenced by
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |in_value|, and therefore |in_value| must be non-NULL.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Set(const std::string& path, Value* in_value);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Set().  These methods will replace any existing
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value at that path, even if it has a different type.
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBoolean(const std::string& path, bool in_value);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetInteger(const std::string& path, int in_value);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDouble(const std::string& path, double in_value);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetString(const std::string& path, const std::string& in_value);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetString(const std::string& path, const string16& in_value);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Set(), but without special treatment of '.'.  This allows e.g. URLs to
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be used as paths.
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetWithoutPathExpansion(const std::string& key, Value* in_value);
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of SetWithoutPathExpansion().
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetIntegerWithoutPathExpansion(const std::string& path, int in_value);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDoubleWithoutPathExpansion(const std::string& path, double in_value);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetStringWithoutPathExpansion(const std::string& path,
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const std::string& in_value);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetStringWithoutPathExpansion(const std::string& path,
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const string16& in_value);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the Value associated with the given path starting from this object.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into the next DictionaryValue down.  If the path can be resolved
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // successfully, the value for the last key in the path will be returned
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // through the |out_value| parameter, and the function will return true.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise, it will return false and |out_value| will be untouched.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the dictionary always owns the value that's returned.
265a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |out_value| is optional and will only be set if non-NULL.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(const std::string& path, const Value** out_value) const;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(const std::string& path, Value** out_value);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are convenience forms of Get().  The value will be retrieved
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and the return value will be true if the path is valid and the value at
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the end of the path can be returned in the form specified.
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |out_value| is optional and will only be set if non-NULL.
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;
275a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
276a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // doubles.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDouble(const std::string& path, double* out_value) const;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(const std::string& path, std::string* out_value) const;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(const std::string& path, string16* out_value) const;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringASCII(const std::string& path, std::string* out_value) const;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(const std::string& path, BinaryValue** out_value);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(const std::string& path,
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     const DictionaryValue** out_value) const;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(const std::string& path, DictionaryValue** out_value);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(const std::string& path, const ListValue** out_value) const;
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(const std::string& path, ListValue** out_value);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Get(), but without special treatment of '.'.  This allows e.g. URLs to
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be used as paths.
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetWithoutPathExpansion(const std::string& key,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const Value** out_value) const;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBooleanWithoutPathExpansion(const std::string& key,
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      bool* out_value) const;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetIntegerWithoutPathExpansion(const std::string& key,
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int* out_value) const;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDoubleWithoutPathExpansion(const std::string& key,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     double* out_value) const;
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringWithoutPathExpansion(const std::string& key,
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     std::string* out_value) const;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetStringWithoutPathExpansion(const std::string& key,
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     string16* out_value) const;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionaryWithoutPathExpansion(
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& key,
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const DictionaryValue** out_value) const;
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionaryWithoutPathExpansion(const std::string& key,
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         DictionaryValue** out_value);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetListWithoutPathExpansion(const std::string& key,
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const ListValue** out_value) const;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetListWithoutPathExpansion(const std::string& key,
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   ListValue** out_value);
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the Value with the specified path from this dictionary (or one
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of its child dictionaries, if the path is more than just a local key).
3163240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // If |out_value| is non-NULL, the removed Value will be passed out via
3173240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // |out_value|.  If |out_value| is NULL, the removed value will be deleted.
3183240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // This method returns true if |path| is a valid path; otherwise it will
3193240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // return false and the DictionaryValue object will be unchanged.
3203240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual bool Remove(const std::string& path, scoped_ptr<Value>* out_value);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Like Remove(), but without special treatment of '.'.  This allows e.g. URLs
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to be used as paths.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RemoveWithoutPathExpansion(const std::string& key,
3253240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                                          scoped_ptr<Value>* out_value);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Removes a path, clearing out all dictionaries on |path| that remain empty
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // after removing the value at |path|.
329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool RemovePath(const std::string& path,
330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                          scoped_ptr<Value>* out_value);
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes a copy of |this| but doesn't include empty dictionaries and lists in
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the copy.  This never returns NULL, even if |this| itself is empty.
33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DictionaryValue* DeepCopyWithoutEmptyChildren() const;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sub-dictionaries will be merged as well. In case of key collisions, the
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // passed in dictionary takes precedence and data already present will be
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be freed any time after this call.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MergeDictionary(const DictionaryValue* dictionary);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Swaps contents with the |other| dictionary.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Swap(DictionaryValue* other);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This class provides an iterator over both keys and values in the
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dictionary.  It can't be used to modify the dictionary.
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class BASE_EXPORT Iterator {
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit Iterator(const DictionaryValue& target);
351a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ~Iterator();
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Advance() { ++it_; }
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& key() const { return it_->first; }
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Value& value() const { return *it_->second; }
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DictionaryValue& target_;
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ValueMap::const_iterator it_;
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DictionaryValue* DeepCopy() const OVERRIDE;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ValueMap dictionary_;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This type of Value represents a list of other Value values.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT ListValue : public Value {
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef ValueVector::iterator iterator;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef ValueVector::const_iterator const_iterator;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListValue();
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ListValue();
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears the contents of this ListValue
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of Values in this list.
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetSize() const { return list_.size(); }
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether the list is empty.
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const { return list_.empty(); }
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the list item at the given index to be the Value specified by
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the value given.  If the index beyond the current end of the list, null
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Values will be used to pad out the list.
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if successful, or false if the index was negative or
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the value is a null pointer.
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Set(size_t index, Value* in_value);
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the Value at the given index.  Modifies |out_value| (and returns true)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only if the index falls within the current list range.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that the list always owns the Value passed out via |out_value|.
402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |out_value| is optional and will only be set if non-NULL.
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(size_t index, const Value** out_value) const;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Get(size_t index, Value** out_value);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Get().  Modifies |out_value| (and returns true)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only if the index is valid and the Value at that index can be returned
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the specified form.
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |out_value| is optional and will only be set if non-NULL.
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBoolean(size_t index, bool* out_value) const;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetInteger(size_t index, int* out_value) const;
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // doubles.
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDouble(size_t index, double* out_value) const;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(size_t index, std::string* out_value) const;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetString(size_t index, string16* out_value) const;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(size_t index, const BinaryValue** out_value) const;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetBinary(size_t index, BinaryValue** out_value);
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetDictionary(size_t index, DictionaryValue** out_value);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(size_t index, const ListValue** out_value) const;
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetList(size_t index, ListValue** out_value);
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the Value with the specified index from this list.
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // passed out via |out_value|.  If |out_value| is NULL, the removed value will
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be deleted.  This method returns true if |index| is valid; otherwise
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it will return false and the ListValue object will be unchanged.
4293240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  virtual bool Remove(size_t index, scoped_ptr<Value>* out_value);
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the first instance of |value| found in the list, if any, and
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deletes it. |index| is the location where |value| was found. Returns false
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if not found.
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Remove(const Value& value, size_t* index);
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the element at |iter|. If |out_value| is NULL, the value will be
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deleted, otherwise ownership of the value is passed back to the caller.
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns an iterator pointing to the location of the element that
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // followed the erased element.
4403240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  iterator Erase(iterator iter, scoped_ptr<Value>* out_value);
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends a Value to the end of the list.
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Append(Value* in_value);
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience forms of Append.
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendBoolean(bool in_value);
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendInteger(int in_value);
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendDouble(double in_value);
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendString(const std::string& in_value);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendString(const string16& in_value);
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendStrings(const std::vector<std::string>& in_values);
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendStrings(const std::vector<string16>& in_values);
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Appends a Value if it's not already present. Takes ownership of the
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |in_value|. Returns true if successful, or false if the value was already
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // present. If the value was already present the |in_value| is deleted.
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AppendIfNotPresent(Value* in_value);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Insert a Value at index.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if successful, or false if the index was out of range.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Insert(size_t index, Value* in_value);
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Searches for the first instance of |value| in the list using the Equals
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // method of the Value type.
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a const_iterator to the found item or to end() if none exists.
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator Find(const Value& value) const;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Swaps contents with the |other| list.
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Swap(ListValue* other);
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Iteration.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  iterator begin() { return list_.begin(); }
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  iterator end() { return list_.end(); }
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator begin() const { return list_.begin(); }
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator end() const { return list_.end(); }
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Value:
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(ListValue** out_value) OVERRIDE;
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAsList(const ListValue** out_value) const OVERRIDE;
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ListValue* DeepCopy() const OVERRIDE;
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Equals(const Value* other) const OVERRIDE;
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ValueVector list_;
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ListValue);
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This interface is implemented by classes that know how to serialize and
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deserialize Value objects.
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT ValueSerializer {
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ValueSerializer();
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Serialize(const Value& root) = 0;
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method deserializes the subclass-specific format into a Value object.
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the return value is non-NULL, the caller takes ownership of returned
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Value. If the return value is NULL, and if error_code is non-NULL,
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error_code will be set with the underlying error.
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |error_message| is non-null, it will be filled in with a formatted
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error message including the location of the error if appropriate.
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Value* Deserialize(int* error_code, std::string* error_str) = 0;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Stream operator so Values can be used in assertion statements.  In order that
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// gtest uses this operator to print readable output on test failures, we must
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// override each specific type. Otherwise, the default template implementation
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// is preferred over an upcast.
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const FundamentalValue& value) {
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const StringValue& value) {
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const DictionaryValue& value) {
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                            const ListValue& value) {
530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return out << static_cast<const Value&>(value);
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_VALUES_H_
536