pref_value_map.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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 CHROME_BROWSER_PREFS_PREF_VALUE_MAP_H_
6#define CHROME_BROWSER_PREFS_PREF_VALUE_MAP_H_
7#pragma once
8
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14
15class Value;
16
17// A generic string to value map used by the PrefStore implementations.
18class PrefValueMap {
19 public:
20  typedef std::map<std::string, Value*>::iterator iterator;
21  typedef std::map<std::string, Value*>::const_iterator const_iterator;
22
23  PrefValueMap();
24  virtual ~PrefValueMap();
25
26  // Gets the value for |key| and stores it in |value|. Ownership remains with
27  // the map. Returns true if a value is present. If not, |value| is not
28  // touched.
29  bool GetValue(const std::string& key, Value** value) const;
30
31  // Sets a new |value| for |key|. Takes ownership of |value|, which must be
32  // non-NULL. Returns true if the value changed.
33  bool SetValue(const std::string& key, Value* value);
34
35  // Removes the value for |key| from the map. Returns true if a value was
36  // removed.
37  bool RemoveValue(const std::string& key);
38
39  // Clears the map.
40  void Clear();
41
42  iterator begin();
43  iterator end();
44  const_iterator begin() const;
45  const_iterator end() const;
46
47  // Gets a boolean value for |key| and stores it in |value|. Returns true if
48  // the value was found and of the proper type.
49  bool GetBoolean(const std::string& key, bool* value) const;
50
51  // Gets a string value for |key| and stores it in |value|. Returns true if
52  // the value was found and of the proper type.
53  bool GetString(const std::string& key, std::string* value) const;
54
55  // Sets the value for |key| to the string |value|.
56  void SetString(const std::string& key, const std::string& value);
57
58  // Compares this value map against |other| and stores all key names that have
59  // different values in |differing_keys|. This includes keys that are present
60  // only in one of the maps.
61  void GetDifferingKeys(const PrefValueMap* other,
62                        std::vector<std::string>* differing_keys) const;
63
64 private:
65  typedef std::map<std::string, Value*> Map;
66
67  Map prefs_;
68
69  DISALLOW_COPY_AND_ASSIGN(PrefValueMap);
70};
71
72#endif  // CHROME_BROWSER_PREFS_PREF_VALUE_MAP_H_
73