Remove extra semicolon from autofill_messages.h.
[chromium-blink-merge.git] / base / prefs / pref_value_map.cc
blob5f2dc506b69c017f3c190614269047ccd30eccb1
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 <map>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "base/values.h"
14 PrefValueMap::PrefValueMap() {}
16 PrefValueMap::~PrefValueMap() {
17 Clear();
20 bool PrefValueMap::GetValue(const std::string& key,
21 const base::Value** value) const {
22 const Map::const_iterator entry = prefs_.find(key);
23 if (entry == prefs_.end())
24 return false;
26 if (value)
27 *value = entry->second;
28 return true;
31 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
32 const Map::const_iterator entry = prefs_.find(key);
33 if (entry == prefs_.end())
34 return false;
36 if (value)
37 *value = entry->second;
38 return true;
41 bool PrefValueMap::SetValue(const std::string& key, base::Value* value) {
42 DCHECK(value);
43 auto result = prefs_.insert(std::make_pair(key, value));
44 if (result.second)
45 return true;
47 scoped_ptr<base::Value> value_ptr(value);
48 const Map::iterator& entry = result.first;
49 if (base::Value::Equals(entry->second, value))
50 return false;
52 delete entry->second;
53 entry->second = value_ptr.release();
55 return true;
58 bool PrefValueMap::RemoveValue(const std::string& key) {
59 const Map::iterator entry = prefs_.find(key);
60 if (entry == prefs_.end())
61 return false;
63 delete entry->second;
64 prefs_.erase(entry);
65 return true;
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() {
81 return prefs_.end();
84 PrefValueMap::const_iterator PrefValueMap::begin() const {
85 return prefs_.begin();
88 PrefValueMap::const_iterator PrefValueMap::end() const {
89 return prefs_.end();
92 bool PrefValueMap::GetBoolean(const std::string& key,
93 bool* value) const {
94 const base::Value* stored_value = nullptr;
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 = nullptr;
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 = nullptr;
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 // Put everything into ordered maps.
132 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end());
133 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(),
134 other->prefs_.end());
136 // Walk over the maps in lockstep, adding everything that is different.
137 auto this_pref(this_prefs.begin());
138 auto other_pref(other_prefs.begin());
139 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
140 const int diff = this_pref->first.compare(other_pref->first);
141 if (diff == 0) {
142 if (!this_pref->second->Equals(other_pref->second))
143 differing_keys->push_back(this_pref->first);
144 ++this_pref;
145 ++other_pref;
146 } else if (diff < 0) {
147 differing_keys->push_back(this_pref->first);
148 ++this_pref;
149 } else if (diff > 0) {
150 differing_keys->push_back(other_pref->first);
151 ++other_pref;
155 // Add the remaining entries.
156 for ( ; this_pref != this_prefs.end(); ++this_pref)
157 differing_keys->push_back(this_pref->first);
158 for ( ; other_pref != other_prefs.end(); ++other_pref)
159 differing_keys->push_back(other_pref->first);