• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 class Value;
16 
17 // A generic string to value map used by the PrefStore implementations.
18 class 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, const Value** value) const;
30   bool GetValue(const std::string& key, Value** value);
31 
32   // Sets a new |value| for |key|. Takes ownership of |value|, which must be
33   // non-NULL. Returns true if the value changed.
34   bool SetValue(const std::string& key, Value* value);
35 
36   // Removes the value for |key| from the map. Returns true if a value was
37   // removed.
38   bool RemoveValue(const std::string& key);
39 
40   // Clears the map.
41   void Clear();
42 
43   iterator begin();
44   iterator end();
45   const_iterator begin() const;
46   const_iterator end() const;
47 
48   // Gets a boolean value for |key| and stores it in |value|. Returns true if
49   // the value was found and of the proper type.
50   bool GetBoolean(const std::string& key, bool* value) const;
51 
52   // Gets a string value for |key| and stores it in |value|. Returns true if
53   // the value was found and of the proper type.
54   bool GetString(const std::string& key, std::string* value) const;
55 
56   // Sets the value for |key| to the string |value|.
57   void SetString(const std::string& key, const std::string& value);
58 
59   // Compares this value map against |other| and stores all key names that have
60   // different values in |differing_keys|. This includes keys that are present
61   // only in one of the maps.
62   void GetDifferingKeys(const PrefValueMap* other,
63                         std::vector<std::string>* differing_keys) const;
64 
65  private:
66   typedef std::map<std::string, Value*> Map;
67 
68   Map prefs_;
69 
70   DISALLOW_COPY_AND_ASSIGN(PrefValueMap);
71 };
72 
73 #endif  // CHROME_BROWSER_PREFS_PREF_VALUE_MAP_H_
74