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 #include "base/prefs/pref_value_map.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
12 PrefValueMap::PrefValueMap() {}
14 PrefValueMap::~PrefValueMap() {
18 bool PrefValueMap::GetValue(const std::string
& key
,
19 const base::Value
** value
) const {
20 const Map::const_iterator entry
= prefs_
.find(key
);
21 if (entry
!= prefs_
.end()) {
23 *value
= entry
->second
;
30 bool PrefValueMap::GetValue(const std::string
& key
, base::Value
** value
) {
31 const Map::const_iterator entry
= prefs_
.find(key
);
32 if (entry
!= prefs_
.end()) {
34 *value
= entry
->second
;
41 bool PrefValueMap::SetValue(const std::string
& key
, base::Value
* value
) {
43 scoped_ptr
<base::Value
> value_ptr(value
);
44 const Map::iterator entry
= prefs_
.find(key
);
45 if (entry
!= prefs_
.end()) {
46 if (base::Value::Equals(entry
->second
, value
))
49 entry
->second
= value_ptr
.release();
51 prefs_
[key
] = value_ptr
.release();
57 bool PrefValueMap::RemoveValue(const std::string
& key
) {
58 const Map::iterator entry
= prefs_
.find(key
);
59 if (entry
!= prefs_
.end()) {
68 void PrefValueMap::Clear() {
69 STLDeleteValues(&prefs_
);
72 void PrefValueMap::Swap(PrefValueMap
* other
) {
73 prefs_
.swap(other
->prefs_
);
76 PrefValueMap::iterator
PrefValueMap::begin() {
77 return prefs_
.begin();
80 PrefValueMap::iterator
PrefValueMap::end() {
84 PrefValueMap::const_iterator
PrefValueMap::begin() const {
85 return prefs_
.begin();
88 PrefValueMap::const_iterator
PrefValueMap::end() const {
92 bool PrefValueMap::GetBoolean(const std::string
& key
,
94 const base::Value
* stored_value
= NULL
;
95 return GetValue(key
, &stored_value
) && stored_value
->GetAsBoolean(value
);
98 void PrefValueMap::SetBoolean(const std::string
& key
, bool value
) {
99 SetValue(key
, new base::FundamentalValue(value
));
102 bool PrefValueMap::GetString(const std::string
& key
,
103 std::string
* value
) const {
104 const base::Value
* stored_value
= NULL
;
105 return GetValue(key
, &stored_value
) && stored_value
->GetAsString(value
);
108 void PrefValueMap::SetString(const std::string
& key
,
109 const std::string
& value
) {
110 SetValue(key
, new base::StringValue(value
));
113 bool PrefValueMap::GetInteger(const std::string
& key
, int* value
) const {
114 const base::Value
* stored_value
= NULL
;
115 return GetValue(key
, &stored_value
) && stored_value
->GetAsInteger(value
);
118 void PrefValueMap::SetInteger(const std::string
& key
, const int value
) {
119 SetValue(key
, new base::FundamentalValue(value
));
122 void PrefValueMap::SetDouble(const std::string
& key
, const double value
) {
123 SetValue(key
, new base::FundamentalValue(value
));
126 void PrefValueMap::GetDifferingKeys(
127 const PrefValueMap
* other
,
128 std::vector
<std::string
>* differing_keys
) const {
129 differing_keys
->clear();
131 // Walk over the maps in lockstep, adding everything that is different.
132 Map::const_iterator
this_pref(prefs_
.begin());
133 Map::const_iterator
other_pref(other
->prefs_
.begin());
134 while (this_pref
!= prefs_
.end() && other_pref
!= other
->prefs_
.end()) {
135 const int diff
= this_pref
->first
.compare(other_pref
->first
);
137 if (!this_pref
->second
->Equals(other_pref
->second
))
138 differing_keys
->push_back(this_pref
->first
);
141 } else if (diff
< 0) {
142 differing_keys
->push_back(this_pref
->first
);
144 } else if (diff
> 0) {
145 differing_keys
->push_back(other_pref
->first
);
150 // Add the remaining entries.
151 for ( ; this_pref
!= prefs_
.end(); ++this_pref
)
152 differing_keys
->push_back(this_pref
->first
);
153 for ( ; other_pref
!= other
->prefs_
.end(); ++other_pref
)
154 differing_keys
->push_back(other_pref
->first
);