OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/prefs/pref_value_map.h" | 5 #include "base/prefs/pref_value_map.h" |
6 | 6 |
| 7 #include <map> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
9 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 | 13 |
12 PrefValueMap::PrefValueMap() {} | 14 PrefValueMap::PrefValueMap() {} |
13 | 15 |
14 PrefValueMap::~PrefValueMap() { | 16 PrefValueMap::~PrefValueMap() { |
15 Clear(); | 17 Clear(); |
16 } | 18 } |
17 | 19 |
18 bool PrefValueMap::GetValue(const std::string& key, | 20 bool PrefValueMap::GetValue(const std::string& key, |
19 const base::Value** value) const { | 21 const base::Value** value) const { |
20 const Map::const_iterator entry = prefs_.find(key); | 22 const Map::const_iterator entry = prefs_.find(key); |
21 if (entry != prefs_.end()) { | 23 if (entry == prefs_.end()) |
22 if (value) | 24 return false; |
23 *value = entry->second; | |
24 return true; | |
25 } | |
26 | 25 |
27 return false; | 26 if (value) |
| 27 *value = entry->second; |
| 28 return true; |
28 } | 29 } |
29 | 30 |
30 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { | 31 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { |
31 const Map::const_iterator entry = prefs_.find(key); | 32 const Map::const_iterator entry = prefs_.find(key); |
32 if (entry != prefs_.end()) { | 33 if (entry == prefs_.end()) |
33 if (value) | 34 return false; |
34 *value = entry->second; | |
35 return true; | |
36 } | |
37 | 35 |
38 return false; | 36 if (value) |
| 37 *value = entry->second; |
| 38 return true; |
39 } | 39 } |
40 | 40 |
41 bool PrefValueMap::SetValue(const std::string& key, base::Value* value) { | 41 bool PrefValueMap::SetValue(const std::string& key, base::Value* value) { |
42 DCHECK(value); | 42 DCHECK(value); |
| 43 auto result = prefs_.insert(std::make_pair(key, value)); |
| 44 if (result.second) |
| 45 return true; |
| 46 |
43 scoped_ptr<base::Value> value_ptr(value); | 47 scoped_ptr<base::Value> value_ptr(value); |
44 const Map::iterator entry = prefs_.find(key); | 48 const Map::iterator& entry = result.first; |
45 if (entry != prefs_.end()) { | 49 if (base::Value::Equals(entry->second, value)) |
46 if (base::Value::Equals(entry->second, value)) | 50 return false; |
47 return false; | 51 |
48 delete entry->second; | 52 delete entry->second; |
49 entry->second = value_ptr.release(); | 53 entry->second = value_ptr.release(); |
50 } else { | |
51 prefs_[key] = value_ptr.release(); | |
52 } | |
53 | 54 |
54 return true; | 55 return true; |
55 } | 56 } |
56 | 57 |
57 bool PrefValueMap::RemoveValue(const std::string& key) { | 58 bool PrefValueMap::RemoveValue(const std::string& key) { |
58 const Map::iterator entry = prefs_.find(key); | 59 const Map::iterator entry = prefs_.find(key); |
59 if (entry != prefs_.end()) { | 60 if (entry == prefs_.end()) |
60 delete entry->second; | 61 return false; |
61 prefs_.erase(entry); | |
62 return true; | |
63 } | |
64 | 62 |
65 return false; | 63 delete entry->second; |
| 64 prefs_.erase(entry); |
| 65 return true; |
66 } | 66 } |
67 | 67 |
68 void PrefValueMap::Clear() { | 68 void PrefValueMap::Clear() { |
69 STLDeleteValues(&prefs_); | 69 STLDeleteValues(&prefs_); |
70 } | 70 } |
71 | 71 |
72 void PrefValueMap::Swap(PrefValueMap* other) { | 72 void PrefValueMap::Swap(PrefValueMap* other) { |
73 prefs_.swap(other->prefs_); | 73 prefs_.swap(other->prefs_); |
74 } | 74 } |
75 | 75 |
76 PrefValueMap::iterator PrefValueMap::begin() { | 76 PrefValueMap::iterator PrefValueMap::begin() { |
77 return prefs_.begin(); | 77 return prefs_.begin(); |
78 } | 78 } |
79 | 79 |
80 PrefValueMap::iterator PrefValueMap::end() { | 80 PrefValueMap::iterator PrefValueMap::end() { |
81 return prefs_.end(); | 81 return prefs_.end(); |
82 } | 82 } |
83 | 83 |
84 PrefValueMap::const_iterator PrefValueMap::begin() const { | 84 PrefValueMap::const_iterator PrefValueMap::begin() const { |
85 return prefs_.begin(); | 85 return prefs_.begin(); |
86 } | 86 } |
87 | 87 |
88 PrefValueMap::const_iterator PrefValueMap::end() const { | 88 PrefValueMap::const_iterator PrefValueMap::end() const { |
89 return prefs_.end(); | 89 return prefs_.end(); |
90 } | 90 } |
91 | 91 |
92 bool PrefValueMap::GetBoolean(const std::string& key, | 92 bool PrefValueMap::GetBoolean(const std::string& key, |
93 bool* value) const { | 93 bool* value) const { |
94 const base::Value* stored_value = NULL; | 94 const base::Value* stored_value = nullptr; |
95 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); | 95 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); |
96 } | 96 } |
97 | 97 |
98 void PrefValueMap::SetBoolean(const std::string& key, bool value) { | 98 void PrefValueMap::SetBoolean(const std::string& key, bool value) { |
99 SetValue(key, new base::FundamentalValue(value)); | 99 SetValue(key, new base::FundamentalValue(value)); |
100 } | 100 } |
101 | 101 |
102 bool PrefValueMap::GetString(const std::string& key, | 102 bool PrefValueMap::GetString(const std::string& key, |
103 std::string* value) const { | 103 std::string* value) const { |
104 const base::Value* stored_value = NULL; | 104 const base::Value* stored_value = nullptr; |
105 return GetValue(key, &stored_value) && stored_value->GetAsString(value); | 105 return GetValue(key, &stored_value) && stored_value->GetAsString(value); |
106 } | 106 } |
107 | 107 |
108 void PrefValueMap::SetString(const std::string& key, | 108 void PrefValueMap::SetString(const std::string& key, |
109 const std::string& value) { | 109 const std::string& value) { |
110 SetValue(key, new base::StringValue(value)); | 110 SetValue(key, new base::StringValue(value)); |
111 } | 111 } |
112 | 112 |
113 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { | 113 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { |
114 const base::Value* stored_value = NULL; | 114 const base::Value* stored_value = nullptr; |
115 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); | 115 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); |
116 } | 116 } |
117 | 117 |
118 void PrefValueMap::SetInteger(const std::string& key, const int value) { | 118 void PrefValueMap::SetInteger(const std::string& key, const int value) { |
119 SetValue(key, new base::FundamentalValue(value)); | 119 SetValue(key, new base::FundamentalValue(value)); |
120 } | 120 } |
121 | 121 |
122 void PrefValueMap::SetDouble(const std::string& key, const double value) { | 122 void PrefValueMap::SetDouble(const std::string& key, const double value) { |
123 SetValue(key, new base::FundamentalValue(value)); | 123 SetValue(key, new base::FundamentalValue(value)); |
124 } | 124 } |
125 | 125 |
126 void PrefValueMap::GetDifferingKeys( | 126 void PrefValueMap::GetDifferingKeys( |
127 const PrefValueMap* other, | 127 const PrefValueMap* other, |
128 std::vector<std::string>* differing_keys) const { | 128 std::vector<std::string>* differing_keys) const { |
129 differing_keys->clear(); | 129 differing_keys->clear(); |
130 | 130 |
| 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()); |
| 135 |
131 // Walk over the maps in lockstep, adding everything that is different. | 136 // Walk over the maps in lockstep, adding everything that is different. |
132 Map::const_iterator this_pref(prefs_.begin()); | 137 auto this_pref(this_prefs.begin()); |
133 Map::const_iterator other_pref(other->prefs_.begin()); | 138 auto other_pref(other_prefs.begin()); |
134 while (this_pref != prefs_.end() && other_pref != other->prefs_.end()) { | 139 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) { |
135 const int diff = this_pref->first.compare(other_pref->first); | 140 const int diff = this_pref->first.compare(other_pref->first); |
136 if (diff == 0) { | 141 if (diff == 0) { |
137 if (!this_pref->second->Equals(other_pref->second)) | 142 if (!this_pref->second->Equals(other_pref->second)) |
138 differing_keys->push_back(this_pref->first); | 143 differing_keys->push_back(this_pref->first); |
139 ++this_pref; | 144 ++this_pref; |
140 ++other_pref; | 145 ++other_pref; |
141 } else if (diff < 0) { | 146 } else if (diff < 0) { |
142 differing_keys->push_back(this_pref->first); | 147 differing_keys->push_back(this_pref->first); |
143 ++this_pref; | 148 ++this_pref; |
144 } else if (diff > 0) { | 149 } else if (diff > 0) { |
145 differing_keys->push_back(other_pref->first); | 150 differing_keys->push_back(other_pref->first); |
146 ++other_pref; | 151 ++other_pref; |
147 } | 152 } |
148 } | 153 } |
149 | 154 |
150 // Add the remaining entries. | 155 // Add the remaining entries. |
151 for ( ; this_pref != prefs_.end(); ++this_pref) | 156 for ( ; this_pref != this_prefs.end(); ++this_pref) |
152 differing_keys->push_back(this_pref->first); | 157 differing_keys->push_back(this_pref->first); |
153 for ( ; other_pref != other->prefs_.end(); ++other_pref) | 158 for ( ; other_pref != other_prefs.end(); ++other_pref) |
154 differing_keys->push_back(other_pref->first); | 159 differing_keys->push_back(other_pref->first); |
155 } | 160 } |
OLD | NEW |