values.h revision 36040ed30c39d2106a2cd5ec033e98b71302a744
1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This file specifies a recursive data storage class called Value intended for
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// storing settings and other persistable data.
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// A Value represents something that can be stored in JSON or passed to/from
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// JavaScript. As such, it is NOT a generalized variant type, since only the
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// types supported by JavaScript/JSON are supported.
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
120d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko// IN PARTICULAR this means that there is no support for int64_t or unsigned
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// numbers. Writing JSON with such types would violate the spec. If you need
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// something like this, either use a double or make a string value containing
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the number you want.
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#ifndef BASE_VALUES_H_
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#define BASE_VALUES_H_
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <stddef.h>
210d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include <stdint.h>
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <iosfwd>
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <map>
2594ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez#include <memory>
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <string>
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <utility>
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <vector>
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/base_export.h"
31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/compiler_specific.h"
320d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include "base/macros.h"
3336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#include "base/memory/manual_constructor.h"
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string16.h"
350d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko#include "base/strings/string_piece.h"
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass DictionaryValue;
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass ListValue;
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass Value;
4236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abeusing BinaryValue = Value;
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// The Value class is the base class for Values. A Value can be instantiated
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// via the Create*Value() factory methods, or by directly creating instances of
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the subclasses.
47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//
48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// See the file-level comment above for more information.
49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT Value {
50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
5136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using DictStorage = std::map<std::string, std::unique_ptr<Value>>;
5236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using ListStorage = std::vector<std::unique_ptr<Value>>;
5336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
5436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  enum class Type {
5536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    NONE = 0,
5636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    BOOLEAN,
5736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    INTEGER,
5836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DOUBLE,
5936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    STRING,
6036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    BINARY,
6136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DICTIONARY,
6236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    LIST
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // Note: Do not add more types. See the file-level comment above for why.
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
66e5b2c6fa6f923f3a2f66346c2f169d9f0fceb3dcLuis Hector Chavez  static std::unique_ptr<Value> CreateNullValue();
670601274935e7f632eb0d6ce0fd223b744349d20bJay Civelli
6836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // For situations where you want to keep ownership of your buffer, this
6936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // factory method creates a new BinaryValue by copying the contents of the
7036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // buffer that's passed in.
7136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead.
7236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // TODO(crbug.com/646113): Delete this and migrate callsites.
7336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer,
7436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                                             size_t size);
7536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
7636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value(const Value& that);
7736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value(Value&& that);
7836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value();  // A null value.
7936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(Type type);
8036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(bool in_bool);
8136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(int in_int);
8236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(double in_double);
8336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
8436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(const char*) and Value(const char16*) are required despite
8536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(const std::string&) and Value(const string16&) because otherwise the
8636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // compiler will choose the Value(bool) constructor for these arguments.
8736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(std::string&&) allow for efficient move construction.
8836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Value(StringPiece) exists due to many callsites passing StringPieces as
8936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // arguments.
9036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const char* in_string);
9136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const std::string& in_string);
9236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(std::string&& in_string);
9336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const char16* in_string);
9436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const string16& in_string);
9536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(StringPiece in_string);
9636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
9736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(const std::vector<char>& in_blob);
9836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  explicit Value(std::vector<char>&& in_blob);
9936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value& operator=(const Value& that);
10136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value& operator=(Value&& that);
10236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  ~Value();
10436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
10536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Returns the name for a given |type|.
10636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  static const char* GetTypeName(Type type);
10736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the type of the value stored by the current Value object.
109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Each type will be implemented by only one subclass of Value, so it's
110b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // safe to use the Type to determine whether you can cast from
111b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Value* to (Implementing Class)*.  Also, a Value object never changes
112b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // its type after construction.
11336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Type GetType() const { return type_; }  // DEPRECATED, use type().
11436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Type type() const { return type_; }
115b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
116b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if the current object represents a given type.
117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool IsType(Type type) const { return type == type_; }
11836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_bool() const { return type() == Type::BOOLEAN; }
11936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_int() const { return type() == Type::INTEGER; }
12036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_double() const { return type() == Type::DOUBLE; }
12136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_string() const { return type() == Type::STRING; }
12236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_blob() const { return type() == Type::BINARY; }
12336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_dict() const { return type() == Type::DICTIONARY; }
12436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool is_list() const { return type() == Type::LIST; }
12536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
12636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // These will all fatally assert if the type doesn't match.
12736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBool() const;
12836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  int GetInt() const;
12936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  double GetDouble() const;  // Implicitly converts from int if necessary.
13036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const std::string& GetString() const;
13136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const std::vector<char>& GetBlob() const;
13236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
13336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t GetSize() const;         // DEPRECATED, use GetBlob().size() instead.
13436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const char* GetBuffer() const;  // DEPRECATED, use GetBlob().data() instead.
135b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
136b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // These methods allow the convenient retrieval of the contents of the Value.
137b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the current object can be converted into the given type, the value is
138b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // returned through the |out_value| parameter and true is returned;
139b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // otherwise, false is returned and |out_value| is unchanged.
14036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsBoolean(bool* out_value) const;
14136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsInteger(int* out_value) const;
14236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDouble(double* out_value) const;
14336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(std::string* out_value) const;
14436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(string16* out_value) const;
14536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(const Value** out_value) const;
14636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsString(StringPiece* out_value) const;
14736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsBinary(const BinaryValue** out_value) const;
14836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // ListValue::From is the equivalent for std::unique_ptr conversions.
14936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsList(ListValue** out_value);
15036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsList(const ListValue** out_value) const;
15136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
15236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDictionary(DictionaryValue** out_value);
15336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetAsDictionary(const DictionaryValue** out_value) const;
154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note: Do not add more types. See the file-level comment above for why.
155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This creates a deep copy of the entire Value tree, and returns a pointer
15736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // to the copy. The caller gets ownership of the copy, of course.
158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Subclasses return their own type directly in their overrides;
159b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // this works because C++ supports covariant return types.
16036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  Value* DeepCopy() const;
161b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove the above.
16294ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<Value> CreateDeepCopy() const;
163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
164b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Compares if two Value objects have equal contents.
16536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Equals(const Value* other) const;
166b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
167b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Compares if two Value objects have equal contents. Can handle NULLs.
168b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // NULLs are considered equal but different from Value::CreateNullValue().
169b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  static bool Equals(const Value* a, const Value* b);
170b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
171b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat protected:
17236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // TODO(crbug.com/646113): Make these private once DictionaryValue and
17336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // ListValue are properly inlined.
174b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  Type type_;
175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
176b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  union {
17736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    bool bool_value_;
17836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    int int_value_;
179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    double double_value_;
18036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::string> string_value_;
18136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::vector<char>> binary_value_;
18236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // For current gcc and clang sizeof(DictStorage) = 48, which would result
18336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it
18436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC.
18536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_;
18636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    ManualConstructor<ListStorage> list_;
187b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
188b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat private:
19036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyFundamentalValue(const Value& that);
19136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyConstructFrom(const Value& that);
19236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalMoveConstructFrom(Value&& that);
19336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCopyAssignFromSameType(const Value& that);
19436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalMoveAssignFromSameType(Value&& that);
19536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void InternalCleanup();
196b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
197b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
198b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// DictionaryValue provides a key-value dictionary with (optional) "path"
199b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// parsing for recursive access; see the comment at the top of the file. Keys
200b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// are |std::string|s and should be UTF-8 encoded.
201b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT DictionaryValue : public Value {
202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
2030d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  // Returns |value| if it is a dictionary, nullptr otherwise.
20494ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
2050d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko
206b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  DictionaryValue();
207b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
208b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if the current dictionary has a value for the given key.
20936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool HasKey(StringPiece key) const;
210b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
211b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the number of Values in this dictionary.
21236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t size() const { return (*dict_ptr_)->size(); }
213b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
214b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns whether the dictionary is empty.
21536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool empty() const { return (*dict_ptr_)->empty(); }
216b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
217b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Clears any current contents of this dictionary.
218b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Clear();
219b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Sets the Value associated with the given path starting from this object.
221b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
222b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // into the next DictionaryValue down.  Obviously, "." can't be used
223b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // within a key, but there are no other restrictions on keys.
224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the key at any step of the way doesn't exist, or exists but isn't
225b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // a DictionaryValue, a new DictionaryValue will be created and attached
226b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // to the path in that location. |in_value| must be non-null.
22736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Set(StringPiece path, std::unique_ptr<Value> in_value);
228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
22936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Set(StringPiece path, Value* in_value);
230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Set().  These methods will replace any existing
232b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // value at that path, even if it has a different type.
23336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetBoolean(StringPiece path, bool in_value);
23436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetInteger(StringPiece path, int in_value);
23536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetDouble(StringPiece path, double in_value);
23636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetString(StringPiece path, StringPiece in_value);
23736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetString(StringPiece path, const string16& in_value);
238b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
239b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Set(), but without special treatment of '.'.  This allows e.g. URLs to
240b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be used as paths.
24136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetWithoutPathExpansion(StringPiece key,
24294ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez                               std::unique_ptr<Value> in_value);
243b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
24436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetWithoutPathExpansion(StringPiece key, Value* in_value);
245b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
246b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of SetWithoutPathExpansion().
24736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
24836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
24936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
25036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
25136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void SetStringWithoutPathExpansion(StringPiece path,
252b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     const string16& in_value);
253b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
254b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Gets the Value associated with the given path starting from this object.
255b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
256b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // into the next DictionaryValue down.  If the path can be resolved
257b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // successfully, the value for the last key in the path will be returned
258b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // through the |out_value| parameter, and the function will return true.
259b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Otherwise, it will return false and |out_value| will be untouched.
260b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note that the dictionary always owns the value that's returned.
261b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
2620d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool Get(StringPiece path, const Value** out_value) const;
2630d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool Get(StringPiece path, Value** out_value);
264b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
265b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // These are convenience forms of Get().  The value will be retrieved
266b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // and the return value will be true if the path is valid and the value at
267b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the end of the path can be returned in the form specified.
268b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
26936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBoolean(StringPiece path, bool* out_value) const;
27036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetInteger(StringPiece path, int* out_value) const;
27136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
272b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // doubles.
27336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDouble(StringPiece path, double* out_value) const;
27436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetString(StringPiece path, std::string* out_value) const;
27536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetString(StringPiece path, string16* out_value) const;
27636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringASCII(StringPiece path, std::string* out_value) const;
27736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBinary(StringPiece path, const BinaryValue** out_value) const;
27836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBinary(StringPiece path, BinaryValue** out_value);
2790d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool GetDictionary(StringPiece path,
280b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                     const DictionaryValue** out_value) const;
2810d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  bool GetDictionary(StringPiece path, DictionaryValue** out_value);
28236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetList(StringPiece path, const ListValue** out_value) const;
28336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetList(StringPiece path, ListValue** out_value);
284b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
285b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Get(), but without special treatment of '.'.  This allows e.g. URLs to
286b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be used as paths.
28736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
28836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
28936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
29036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
29136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
29236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringWithoutPathExpansion(StringPiece key,
293b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     std::string* out_value) const;
29436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetStringWithoutPathExpansion(StringPiece key,
295b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                     string16* out_value) const;
296b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionaryWithoutPathExpansion(
29736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe      StringPiece key,
298b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      const DictionaryValue** out_value) const;
29936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetDictionaryWithoutPathExpansion(StringPiece key,
300b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                         DictionaryValue** out_value);
30136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetListWithoutPathExpansion(StringPiece key,
302b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                   const ListValue** out_value) const;
30336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
304b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
305b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the Value with the specified path from this dictionary (or one
306b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // of its child dictionaries, if the path is more than just a local key).
307b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |out_value| is non-NULL, the removed Value will be passed out via
308b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value|.  If |out_value| is NULL, the removed value will be deleted.
309b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This method returns true if |path| is a valid path; otherwise it will
310b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // return false and the DictionaryValue object will be unchanged.
31136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
312b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
313b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Like Remove(), but without special treatment of '.'.  This allows e.g. URLs
314b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // to be used as paths.
31536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool RemoveWithoutPathExpansion(StringPiece key,
31636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                  std::unique_ptr<Value>* out_value);
317b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
318b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes a path, clearing out all dictionaries on |path| that remain empty
319b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // after removing the value at |path|.
32036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
321b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
322b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Makes a copy of |this| but doesn't include empty dictionaries and lists in
323b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the copy.  This never returns NULL, even if |this| itself is empty.
32494ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
325b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
326b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Merge |dictionary| into this dictionary. This is done recursively, i.e. any
327b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // sub-dictionaries will be merged as well. In case of key collisions, the
328b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // passed in dictionary takes precedence and data already present will be
329b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
330b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be freed any time after this call.
331b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void MergeDictionary(const DictionaryValue* dictionary);
332b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
333b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Swaps contents with the |other| dictionary.
33436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Swap(DictionaryValue* other);
335b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
336b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This class provides an iterator over both keys and values in the
337b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // dictionary.  It can't be used to modify the dictionary.
338b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  class BASE_EXPORT Iterator {
339b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat   public:
340b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    explicit Iterator(const DictionaryValue& target);
34145779228f8c9e40851cfd23f727e2bd8ffdd4714Alex Vakulenko    Iterator(const Iterator& other);
342b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    ~Iterator();
343b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
34436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    bool IsAtEnd() const { return it_ == (*target_.dict_ptr_)->end(); }
345b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    void Advance() { ++it_; }
346b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
347b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const std::string& key() const { return it_->first; }
348b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const Value& value() const { return *it_->second; }
349b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
350b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat   private:
351b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    const DictionaryValue& target_;
35236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe    DictStorage::const_iterator it_;
353b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  };
354b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
35536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  DictionaryValue* DeepCopy() const;
356b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove the above.
35794ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
358b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
359b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
360b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This type of Value represents a list of other Value values.
361b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ListValue : public Value {
362b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
36336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using const_iterator = ListStorage::const_iterator;
36436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  using iterator = ListStorage::iterator;
365b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
3660d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko  // Returns |value| if it is a list, nullptr otherwise.
36794ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
3680d205d712abd16eeed2f5d5b1052a367d23a223fAlex Vakulenko
369b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  ListValue();
370b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
371b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Clears the contents of this ListValue
372b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Clear();
373b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
374b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns the number of Values in this list.
37536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  size_t GetSize() const { return list_->size(); }
376b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
377b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns whether the list is empty.
37836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool empty() const { return list_->empty(); }
379b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
380b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Sets the list item at the given index to be the Value specified by
381b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the value given.  If the index beyond the current end of the list, null
382b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Values will be used to pad out the list.
383b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if successful, or false if the index was negative or
384b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // the value is a null pointer.
385b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Set(size_t index, Value* in_value);
386b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of the above. TODO(estade): remove the above.
38794ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  bool Set(size_t index, std::unique_ptr<Value> in_value);
388b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
389b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Gets the Value at the given index.  Modifies |out_value| (and returns true)
390b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // only if the index falls within the current list range.
391b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Note that the list always owns the Value passed out via |out_value|.
392b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
393b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Get(size_t index, const Value** out_value) const;
394b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Get(size_t index, Value** out_value);
395b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
396b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Get().  Modifies |out_value| (and returns true)
397b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // only if the index is valid and the Value at that index can be returned
398b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // in the specified form.
399b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // |out_value| is optional and will only be set if non-NULL.
400b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBoolean(size_t index, bool* out_value) const;
401b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetInteger(size_t index, int* out_value) const;
40236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
403b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // doubles.
404b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDouble(size_t index, double* out_value) const;
405b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetString(size_t index, std::string* out_value) const;
406b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetString(size_t index, string16* out_value) const;
407b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBinary(size_t index, const BinaryValue** out_value) const;
408b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetBinary(size_t index, BinaryValue** out_value);
409b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
410b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetDictionary(size_t index, DictionaryValue** out_value);
411b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetList(size_t index, const ListValue** out_value) const;
412b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool GetList(size_t index, ListValue** out_value);
413b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
414b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the Value with the specified index from this list.
415b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
416b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // passed out via |out_value|.  If |out_value| is NULL, the removed value will
417b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // be deleted.  This method returns true if |index| is valid; otherwise
418b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // it will return false and the ListValue object will be unchanged.
41936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Remove(size_t index, std::unique_ptr<Value>* out_value);
420b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
421b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the first instance of |value| found in the list, if any, and
422b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // deletes it. |index| is the location where |value| was found. Returns false
423b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // if not found.
424b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool Remove(const Value& value, size_t* index);
425b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
426b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Removes the element at |iter|. If |out_value| is NULL, the value will be
427b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // deleted, otherwise ownership of the value is passed back to the caller.
428b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns an iterator pointing to the location of the element that
429b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // followed the erased element.
43094ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
431b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
432b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Appends a Value to the end of the list.
43394ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  void Append(std::unique_ptr<Value> in_value);
43436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#if !defined(OS_LINUX)
435b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Deprecated version of the above. TODO(estade): remove.
436b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void Append(Value* in_value);
43736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe#endif
438b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
439b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Convenience forms of Append.
440b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendBoolean(bool in_value);
441b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendInteger(int in_value);
442b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendDouble(double in_value);
44336040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void AppendString(StringPiece in_value);
444b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendString(const string16& in_value);
445b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendStrings(const std::vector<std::string>& in_values);
446b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  void AppendStrings(const std::vector<string16>& in_values);
447b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
44836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // Appends a Value if it's not already present. Returns true if successful,
44936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  // or false if the value was already
45036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
451b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
452b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Insert a Value at index.
453b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns true if successful, or false if the index was out of range.
45436040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  bool Insert(size_t index, std::unique_ptr<Value> in_value);
455b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
456b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Searches for the first instance of |value| in the list using the Equals
457b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // method of the Value type.
458b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Returns a const_iterator to the found item or to end() if none exists.
459b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  const_iterator Find(const Value& value) const;
460b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
461b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Swaps contents with the |other| list.
46236040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  void Swap(ListValue* other);
463b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
464b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Iteration.
46536040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  iterator begin() { return list_->begin(); }
46636040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  iterator end() { return list_->end(); }
467b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
46836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const_iterator begin() const { return list_->begin(); }
46936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  const_iterator end() const { return list_->end(); }
470b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
47136040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe  ListValue* DeepCopy() const;
472b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
47394ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  std::unique_ptr<ListValue> CreateDeepCopy() const;
474b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
475b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
476b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This interface is implemented by classes that know how to serialize
477b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Value objects.
478b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ValueSerializer {
479b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
480b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual ~ValueSerializer();
481b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
482b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual bool Serialize(const Value& root) = 0;
483b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
484b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
485b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// This interface is implemented by classes that know how to deserialize Value
486b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// objects.
487b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratclass BASE_EXPORT ValueDeserializer {
488b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat public:
489b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  virtual ~ValueDeserializer();
490b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
491b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // This method deserializes the subclass-specific format into a Value object.
492b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If the return value is non-NULL, the caller takes ownership of returned
493b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Value. If the return value is NULL, and if error_code is non-NULL,
494b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // error_code will be set with the underlying error.
495b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If |error_message| is non-null, it will be filled in with a formatted
496b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // error message including the location of the error if appropriate.
49794ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez  virtual std::unique_ptr<Value> Deserialize(int* error_code,
49894ffa55491333f3dcc701befd0d2652922916d99Luis Hector Chavez                                             std::string* error_str) = 0;
499b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat};
500b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
501b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Stream operator so Values can be used in assertion statements.  In order that
502b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// gtest uses this operator to print readable output on test failures, we must
503b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// override each specific type. Otherwise, the default template implementation
504b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// is preferred over an upcast.
505b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
506b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
507b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
508b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                            const DictionaryValue& value) {
509b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return out << static_cast<const Value&>(value);
510b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
511b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
512b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratBASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
513b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                                            const ListValue& value) {
514b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return out << static_cast<const Value&>(value);
515b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
516b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
51736040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe// Stream operator so that enum class Types can be used in log statements.
51836040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko AbeBASE_EXPORT std::ostream& operator<<(std::ostream& out,
51936040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe                                     const Value::Type& type);
52036040ed30c39d2106a2cd5ec033e98b71302a744Hidehiko Abe
521b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace base
522b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
523b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // BASE_VALUES_H_
524