Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5810)

Unified Diff: base/prefs/pref_value_map.cc

Issue 1124763003: Update from https://crrev.com/327068 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: update nacl, buildtools, fix display_change_notifier_unittest Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/prefs/pref_value_map.cc
diff --git a/base/prefs/pref_value_map.cc b/base/prefs/pref_value_map.cc
index cafc1d8be5b5200801d0899d89f5bfaa472196db..5f2dc506b69c017f3c190614269047ccd30eccb1 100644
--- a/base/prefs/pref_value_map.cc
+++ b/base/prefs/pref_value_map.cc
@@ -4,6 +4,8 @@
#include "base/prefs/pref_value_map.h"
+#include <map>
+
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
@@ -18,51 +20,49 @@ PrefValueMap::~PrefValueMap() {
bool PrefValueMap::GetValue(const std::string& key,
const base::Value** value) const {
const Map::const_iterator entry = prefs_.find(key);
- if (entry != prefs_.end()) {
- if (value)
- *value = entry->second;
- return true;
- }
+ if (entry == prefs_.end())
+ return false;
- return false;
+ if (value)
+ *value = entry->second;
+ return true;
}
bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
const Map::const_iterator entry = prefs_.find(key);
- if (entry != prefs_.end()) {
- if (value)
- *value = entry->second;
- return true;
- }
+ if (entry == prefs_.end())
+ return false;
- return false;
+ if (value)
+ *value = entry->second;
+ return true;
}
bool PrefValueMap::SetValue(const std::string& key, base::Value* value) {
DCHECK(value);
+ auto result = prefs_.insert(std::make_pair(key, value));
+ if (result.second)
+ return true;
+
scoped_ptr<base::Value> value_ptr(value);
- const Map::iterator entry = prefs_.find(key);
- if (entry != prefs_.end()) {
- if (base::Value::Equals(entry->second, value))
- return false;
- delete entry->second;
- entry->second = value_ptr.release();
- } else {
- prefs_[key] = value_ptr.release();
- }
+ const Map::iterator& entry = result.first;
+ if (base::Value::Equals(entry->second, value))
+ return false;
+
+ delete entry->second;
+ entry->second = value_ptr.release();
return true;
}
bool PrefValueMap::RemoveValue(const std::string& key) {
const Map::iterator entry = prefs_.find(key);
- if (entry != prefs_.end()) {
- delete entry->second;
- prefs_.erase(entry);
- return true;
- }
+ if (entry == prefs_.end())
+ return false;
- return false;
+ delete entry->second;
+ prefs_.erase(entry);
+ return true;
}
void PrefValueMap::Clear() {
@@ -91,7 +91,7 @@ PrefValueMap::const_iterator PrefValueMap::end() const {
bool PrefValueMap::GetBoolean(const std::string& key,
bool* value) const {
- const base::Value* stored_value = NULL;
+ const base::Value* stored_value = nullptr;
return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
}
@@ -101,7 +101,7 @@ void PrefValueMap::SetBoolean(const std::string& key, bool value) {
bool PrefValueMap::GetString(const std::string& key,
std::string* value) const {
- const base::Value* stored_value = NULL;
+ const base::Value* stored_value = nullptr;
return GetValue(key, &stored_value) && stored_value->GetAsString(value);
}
@@ -111,7 +111,7 @@ void PrefValueMap::SetString(const std::string& key,
}
bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
- const base::Value* stored_value = NULL;
+ const base::Value* stored_value = nullptr;
return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
}
@@ -128,10 +128,15 @@ void PrefValueMap::GetDifferingKeys(
std::vector<std::string>* differing_keys) const {
differing_keys->clear();
+ // Put everything into ordered maps.
+ std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end());
+ std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(),
+ other->prefs_.end());
+
// Walk over the maps in lockstep, adding everything that is different.
- Map::const_iterator this_pref(prefs_.begin());
- Map::const_iterator other_pref(other->prefs_.begin());
- while (this_pref != prefs_.end() && other_pref != other->prefs_.end()) {
+ auto this_pref(this_prefs.begin());
+ auto other_pref(other_prefs.begin());
+ while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
const int diff = this_pref->first.compare(other_pref->first);
if (diff == 0) {
if (!this_pref->second->Equals(other_pref->second))
@@ -148,8 +153,8 @@ void PrefValueMap::GetDifferingKeys(
}
// Add the remaining entries.
- for ( ; this_pref != prefs_.end(); ++this_pref)
+ for ( ; this_pref != this_prefs.end(); ++this_pref)
differing_keys->push_back(this_pref->first);
- for ( ; other_pref != other->prefs_.end(); ++other_pref)
+ for ( ; other_pref != other_prefs.end(); ++other_pref)
differing_keys->push_back(other_pref->first);
}
« no previous file with comments | « base/prefs/pref_value_map.h ('k') | base/process/kill.h » ('j') | mojo/public/tools/BUILD.gn » ('J')

Powered by Google App Engine
This is Rietveld 408576698