1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/prefs/pref_value_map.h"
#include <limits.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/strings/string_piece.h"
#include "base/values.h"
PrefValueMap::PrefValueMap() {}
PrefValueMap::~PrefValueMap() {}
bool PrefValueMap::GetValue(base::StringPiece key,
const base::Value** value) const {
auto it = prefs_.find(key);
if (it == prefs_.end())
return false;
if (value)
*value = &it->second;
return true;
}
bool PrefValueMap::GetValue(base::StringPiece key, base::Value** value) {
auto it = prefs_.find(key);
if (it == prefs_.end())
return false;
if (value)
*value = &it->second;
return true;
}
bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
base::Value& existing_value = prefs_[key];
if (value == existing_value)
return false;
existing_value = std::move(value);
return true;
}
bool PrefValueMap::RemoveValue(const std::string& key) {
return prefs_.erase(key) != 0;
}
void PrefValueMap::Clear() {
prefs_.clear();
}
void PrefValueMap::ClearWithPrefix(const std::string& prefix) {
Map::iterator low = prefs_.lower_bound(prefix);
// Appending maximum possible character so that there will be no string with
// prefix |prefix| that we may miss.
Map::iterator high = prefs_.upper_bound(prefix + char(CHAR_MAX));
prefs_.erase(low, high);
}
void PrefValueMap::Swap(PrefValueMap* other) {
prefs_.swap(other->prefs_);
}
PrefValueMap::iterator PrefValueMap::begin() {
return prefs_.begin();
}
PrefValueMap::iterator PrefValueMap::end() {
return prefs_.end();
}
PrefValueMap::const_iterator PrefValueMap::begin() const {
return prefs_.begin();
}
PrefValueMap::const_iterator PrefValueMap::end() const {
return prefs_.end();
}
bool PrefValueMap::empty() const {
return prefs_.empty();
}
bool PrefValueMap::GetBoolean(const std::string& key, bool* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_bool()) {
*value = stored_value->GetBool();
return true;
}
return false;
}
void PrefValueMap::SetBoolean(const std::string& key, bool value) {
SetValue(key, base::Value(value));
}
bool PrefValueMap::GetString(const std::string& key, std::string* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_string()) {
*value = stored_value->GetString();
return true;
}
return false;
}
void PrefValueMap::SetString(const std::string& key, const std::string& value) {
SetValue(key, base::Value(value));
}
bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
const base::Value* stored_value = nullptr;
if (GetValue(key, &stored_value) && stored_value->is_int()) {
*value = stored_value->GetInt();
return true;
}
return false;
}
void PrefValueMap::SetInteger(const std::string& key, const int value) {
SetValue(key, base::Value(value));
}
void PrefValueMap::SetDouble(const std::string& key, const double value) {
SetValue(key, base::Value(value));
}
void PrefValueMap::GetDifferingKeys(
const PrefValueMap* other,
std::vector<std::string>* differing_keys) const {
differing_keys->clear();
// Put everything into ordered maps.
std::map<std::string, const base::Value*> this_prefs;
std::map<std::string, const base::Value*> other_prefs;
for (const auto& pair : prefs_)
this_prefs.emplace(pair.first, &pair.second);
for (const auto& pair : other->prefs_)
other_prefs.emplace(pair.first, &pair.second);
// Walk over the maps in lockstep, adding everything that is different.
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 != *other_pref->second)
differing_keys->push_back(this_pref->first);
++this_pref;
++other_pref;
} else if (diff < 0) {
differing_keys->push_back(this_pref->first);
++this_pref;
} else if (diff > 0) {
differing_keys->push_back(other_pref->first);
++other_pref;
}
}
// Add the remaining entries.
for (; this_pref != this_prefs.end(); ++this_pref)
differing_keys->push_back(this_pref->first);
for (; other_pref != other_prefs.end(); ++other_pref)
differing_keys->push_back(other_pref->first);
}
base::Value::Dict PrefValueMap::AsDict() const {
base::Value::Dict dictionary;
for (const auto& value : prefs_)
dictionary.SetByDottedPath(value.first, value.second.Clone());
return dictionary;
}
|