36 static const char kListenerKey[] =
"SettingManager";
40 class SettingManager::SettingData :
public Shareable {
44 void SettingListener(SettingBase* setting);
51 const std::string& key,
56 const std::string& key);
63 void UnregisterSettingLocked(SettingBase* setting);
67 typedef std::map<std::string, std::vector<std::string> > GroupMap;
71 struct SettingGroupInfo {
72 typedef std::map<std::string, SettingBase::ListenerInfo> ListenerMap;
73 std::set<SettingBase*> settings;
74 ListenerMap listeners;
77 typedef std::map<std::string, SettingGroupInfo> SettingGroupMap;
79 typedef base::SharedPtr<SettingData> SettingDataPtr;
82 ~SettingData()
override {}
86 GroupMap setting_groups_;
87 SettingGroupMap groups_;
98 void SettingManager::SettingData::SettingListener(SettingBase* setting) {
99 SettingMap::iterator it = settings_.find(setting->GetName());
100 DCHECK(it != settings_.end());
102 const std::vector<std::string>& group_names =
103 setting_groups_[setting->GetName()];
104 const size_t count = group_names.size();
105 for (
size_t i = 0; i < count; ++i) {
106 const SettingGroupInfo& group_info = groups_[group_names[i]];
107 for (SettingGroupInfo::ListenerMap::const_iterator it =
108 group_info.listeners.begin(); it != group_info.listeners.end();
110 if (it->second.enabled)
111 it->second.listener(setting);
115 SettingBase* SettingManager::SettingData::GetSetting(
const std::string&
name) {
116 SettingMap::const_iterator it = settings_.find(name);
117 return it == settings_.end() ? NULL : it->second;
120 void SettingManager::SettingData::RegisterSetting(SettingBase* setting) {
122 const std::string& name = setting->GetName();
128 SettingMap::const_iterator it = settings_.find(name);
129 if (it != settings_.end()) {
130 LOG(
WARNING) <<
"Duplicate setting named '" << name
131 <<
"' registered in SettingManager";
133 UnregisterSettingLocked(setting);
135 std::vector<std::string> group_names =
SplitString(name,
"/");
138 if (group_names.size() > 1)
139 group_names.pop_back();
142 const size_t count = group_names.size();
143 for (
size_t i = 0; i < count; ++i) {
145 group_names[i] = group_names[i - 1U] +
"/" + group_names[i];
146 groups_[group_names[i]].settings.insert(setting);
150 settings_[
name] = setting;
151 setting_groups_[
name] = group_names;
155 setting->RegisterListener(
157 std::bind(&SettingData::SettingListener,
this, std::placeholders::_1));
160 setting->data_ref_ =
this;
164 void SettingManager::SettingData::UnregisterSetting(SettingBase* setting) {
168 UnregisterSettingLocked(setting);
171 void SettingManager::SettingData::UnregisterSettingLocked(
172 SettingBase* setting) {
174 SettingMap::iterator it = settings_.find(setting->GetName());
175 if (it != settings_.end() && it->second == setting) {
176 const std::vector<std::string>& group_names =
177 setting_groups_[setting->GetName()];
180 const size_t count = group_names.size();
181 for (
size_t i = 0; i < count; ++i)
182 groups_[group_names[i]].settings.erase(it->second);
186 setting->UnregisterListener(kListenerKey);
189 void SettingManager::SettingData::RegisterGroupListener(
190 const std::string& group,
191 const std::string& key,
193 SettingGroupMap::iterator it = groups_.find(group);
194 if (it != groups_.end())
195 it->second.listeners[key] = SettingBase::ListenerInfo(listener,
true);
198 void SettingManager::SettingData::EnableGroupListener(
const std::string& group,
199 const std::string& key,
201 SettingGroupMap::iterator it = groups_.find(group);
202 if (it != groups_.end())
203 it->second.listeners[key].enabled = enable;
206 void SettingManager::SettingData::UnregisterGroupListener(
207 const std::string& group,
208 const std::string& key) {
209 SettingGroupMap::iterator it = groups_.find(group);
210 if (it != groups_.end())
211 it->second.listeners.erase(key);
222 SettingManager::SettingManager() : data_(new SettingData) {
225 SettingManager::~SettingManager() {
228 SettingBase* SettingManager::GetSetting(
const std::string& name) {
229 return GetInstance()->data_->GetSetting(name);
233 return GetInstance()->data_->GetAllSettings();
237 GetInstance()->data_->RegisterSetting(setting);
244 SettingData* data =
static_cast<SettingData*
>(setting->data_ref_.Get());
245 data->UnregisterSetting(setting);
248 void SettingManager::RegisterGroupListener(
249 const std::string& group,
250 const std::string& key,
252 GetInstance()->data_->RegisterGroupListener(group, key, listener);
255 void SettingManager::EnableGroupListener(
const std::string& group,
256 const std::string& key,
258 GetInstance()->data_->EnableGroupListener(group, key, enable);
261 void SettingManager::UnregisterGroupListener(
const std::string& group,
262 const std::string& key) {
263 GetInstance()->data_->UnregisterGroupListener(group, key);
#define ION_DECLARE_SAFE_STATIC_POINTER(type, variable)
Declare a static non-array pointer and calls a default constructor.
static void RegisterGroupListener(const std::string &group, const std::string &key, const SettingBase::Listener &listener)
Adds a function that will be called when any setting in the passed group changes. ...
Base class for Setting, which encapsulates the name of the setting and any functors that should be ca...
GenericLockGuard< port::Mutex > LockGuard
Convenient typedefs for ion::port::Mutex.
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
std::vector< std::string > ION_API SplitString(const std::string &str, const std::string &delimiters)
Splits a string into a vector of substrings, given a set of delimiter characters (expressed as a stri...
static void RegisterSetting(SettingBase *setting)
Adds the setting to the manager and its groups.
static SettingBase * GetSetting(const std::string &name)
Returns the setting with the passed name.
std::map< std::string, SettingBase * > SettingMap
Maps a setting's name to setting itself and its groups.
static void EnableGroupListener(const std::string &group, const std::string &key, bool enable)
Enables or disables the group listener identified by key.
std::function< void(SettingBase *setting)> Listener
A function that is called when the value changes.
Copyright 2016 Google Inc.
static const SettingMap & GetAllSettings()
Returns all settings keyed by their names.
SettingManager tracks all existing SettingBase instances, and allows callers to obtain a map of all s...
static void UnregisterSetting(SettingBase *setting)
Removes the setting from the manager and its groups.
static void UnregisterGroupListener(const std::string &group, const std::string &key)
Removes the group listener identified by key.
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.