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.
5 #ifndef BASE_PREFS_PREF_VALUE_MAP_H_
6 #define BASE_PREFS_PREF_VALUE_MAP_H_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/prefs/base_prefs_export.h"
19 // A generic string to value map used by the PrefStore implementations.
20 class BASE_PREFS_EXPORT PrefValueMap
{
22 using Map
= base::hash_map
<std::string
, base::Value
*>;
23 using iterator
= Map::iterator
;
24 using const_iterator
= Map::const_iterator
;
27 virtual ~PrefValueMap();
29 // Gets the value for |key| and stores it in |value|. Ownership remains with
30 // the map. Returns true if a value is present. If not, |value| is not
32 bool GetValue(const std::string
& key
, const base::Value
** value
) const;
33 bool GetValue(const std::string
& key
, base::Value
** value
);
35 // Sets a new |value| for |key|. Takes ownership of |value|, which must be
36 // non-NULL. Returns true if the value changed.
37 bool SetValue(const std::string
& key
, base::Value
* value
);
39 // Removes the value for |key| from the map. Returns true if a value was
41 bool RemoveValue(const std::string
& key
);
46 // Swaps the contents of two maps.
47 void Swap(PrefValueMap
* other
);
51 const_iterator
begin() const;
52 const_iterator
end() const;
54 // Gets a boolean value for |key| and stores it in |value|. Returns true if
55 // the value was found and of the proper type.
56 bool GetBoolean(const std::string
& key
, bool* value
) const;
58 // Sets the value for |key| to the boolean |value|.
59 void SetBoolean(const std::string
& key
, bool value
);
61 // Gets a string value for |key| and stores it in |value|. Returns true if
62 // the value was found and of the proper type.
63 bool GetString(const std::string
& key
, std::string
* value
) const;
65 // Sets the value for |key| to the string |value|.
66 void SetString(const std::string
& key
, const std::string
& value
);
68 // Gets an int value for |key| and stores it in |value|. Returns true if
69 // the value was found and of the proper type.
70 bool GetInteger(const std::string
& key
, int* value
) const;
72 // Sets the value for |key| to the int |value|.
73 void SetInteger(const std::string
& key
, const int value
);
75 // Sets the value for |key| to the double |value|.
76 void SetDouble(const std::string
& key
, const double value
);
78 // Compares this value map against |other| and stores all key names that have
79 // different values in |differing_keys|. This includes keys that are present
80 // only in one of the maps.
81 void GetDifferingKeys(const PrefValueMap
* other
,
82 std::vector
<std::string
>* differing_keys
) const;
87 DISALLOW_COPY_AND_ASSIGN(PrefValueMap
);
90 #endif // BASE_PREFS_PREF_VALUE_MAP_H_