1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_PREFS_PREF_VALUE_MAP_H_
6#define BASE_PREFS_PREF_VALUE_MAP_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/prefs/base_prefs_export.h"
14
15namespace base {
16class Value;
17}
18
19// A generic string to value map used by the PrefStore implementations.
20class BASE_PREFS_EXPORT PrefValueMap {
21 public:
22  typedef std::map<std::string, base::Value*>::iterator iterator;
23  typedef std::map<std::string, base::Value*>::const_iterator const_iterator;
24
25  PrefValueMap();
26  virtual ~PrefValueMap();
27
28  // Gets the value for |key| and stores it in |value|. Ownership remains with
29  // the map. Returns true if a value is present. If not, |value| is not
30  // touched.
31  bool GetValue(const std::string& key, const base::Value** value) const;
32  bool GetValue(const std::string& key, base::Value** value);
33
34  // Sets a new |value| for |key|. Takes ownership of |value|, which must be
35  // non-NULL. Returns true if the value changed.
36  bool SetValue(const std::string& key, base::Value* value);
37
38  // Removes the value for |key| from the map. Returns true if a value was
39  // removed.
40  bool RemoveValue(const std::string& key);
41
42  // Clears the map.
43  void Clear();
44
45  // Swaps the contents of two maps.
46  void Swap(PrefValueMap* other);
47
48  iterator begin();
49  iterator end();
50  const_iterator begin() const;
51  const_iterator end() const;
52
53  // Gets a boolean value for |key| and stores it in |value|. Returns true if
54  // the value was found and of the proper type.
55  bool GetBoolean(const std::string& key, bool* value) const;
56
57  // Sets the value for |key| to the boolean |value|.
58  void SetBoolean(const std::string& key, bool value);
59
60  // Gets a string value for |key| and stores it in |value|. Returns true if
61  // the value was found and of the proper type.
62  bool GetString(const std::string& key, std::string* value) const;
63
64  // Sets the value for |key| to the string |value|.
65  void SetString(const std::string& key, const std::string& value);
66
67  // Gets an int value for |key| and stores it in |value|. Returns true if
68  // the value was found and of the proper type.
69  bool GetInteger(const std::string& key, int* value) const;
70
71  // Sets the value for |key| to the int |value|.
72  void SetInteger(const std::string& key, const int value);
73
74  // Sets the value for |key| to the double |value|.
75  void SetDouble(const std::string& key, const double value);
76
77  // Compares this value map against |other| and stores all key names that have
78  // different values in |differing_keys|. This includes keys that are present
79  // only in one of the maps.
80  void GetDifferingKeys(const PrefValueMap* other,
81                        std::vector<std::string>* differing_keys) const;
82
83 private:
84  typedef std::map<std::string, base::Value*> Map;
85
86  Map prefs_;
87
88  DISALLOW_COPY_AND_ASSIGN(PrefValueMap);
89};
90
91#endif  // BASE_PREFS_PREF_VALUE_MAP_H_
92