Settings: Clean up settings changed callback code

This commit is contained in:
kwolekr 2016-06-11 00:10:25 -04:00
parent 817fea6330
commit 33b874fea3
2 changed files with 44 additions and 32 deletions

@ -985,39 +985,38 @@ void Settings::clearDefaultsNoLock()
}
void Settings::registerChangedCallback(std::string name,
setting_changed_callback cbf, void *userdata)
void Settings::registerChangedCallback(const std::string &name,
SettingsChangedCallback cbf, void *userdata)
{
MutexAutoLock lock(m_callbackMutex);
MutexAutoLock lock(m_callback_mutex);
m_callbacks[name].push_back(std::make_pair(cbf, userdata));
}
void Settings::deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata)
void Settings::deregisterChangedCallback(const std::string &name,
SettingsChangedCallback cbf, void *userdata)
{
MutexAutoLock lock(m_callbackMutex);
std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
if (iterToVector != m_callbacks.end())
{
std::vector<std::pair<setting_changed_callback, void*> > &vector = iterToVector->second;
MutexAutoLock lock(m_callback_mutex);
SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
std::vector<std::pair<setting_changed_callback, void*> >::iterator position =
std::find(vector.begin(), vector.end(), std::make_pair(cbf, userdata));
if (it_cbks != m_callbacks.end()) {
SettingsCallbackList &cbks = it_cbks->second;
if (position != vector.end())
vector.erase(position);
SettingsCallbackList::iterator position =
std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
if (position != cbks.end())
cbks.erase(position);
}
}
void Settings::doCallbacks(const std::string name)
void Settings::doCallbacks(const std::string &name) const
{
MutexAutoLock lock(m_callbackMutex);
std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
if (iterToVector != m_callbacks.end())
{
std::vector<std::pair<setting_changed_callback, void*> >::iterator iter;
for (iter = iterToVector->second.begin(); iter != iterToVector->second.end(); ++iter)
{
(iter->first)(name, iter->second);
}
MutexAutoLock lock(m_callback_mutex);
SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
if (it_cbks != m_callbacks.end()) {
SettingsCallbackList::const_iterator it;
for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
(it->first)(name, it->second);
}
}

@ -35,8 +35,17 @@ struct NoiseParams;
extern Settings *g_settings;
extern std::string g_settings_path;
/** function type to register a changed callback */
typedef void (*setting_changed_callback)(const std::string &name, void *data);
// Type for a settings changed callback function
typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
typedef std::vector<
std::pair<
SettingsChangedCallback,
void *
>
> SettingsCallbackList;
typedef std::map<std::string, SettingsCallbackList> SettingsCallbackMap;
enum ValueType {
VALUETYPE_STRING,
@ -209,24 +218,28 @@ public:
void clearDefaults();
void updateValue(const Settings &other, const std::string &name);
void update(const Settings &other);
void registerChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
void deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
void registerChangedCallback(const std::string &name,
SettingsChangedCallback cbf, void *userdata = NULL);
void deregisterChangedCallback(const std::string &name,
SettingsChangedCallback cbf, void *userdata = NULL);
private:
void updateNoLock(const Settings &other);
void clearNoLock();
void clearDefaultsNoLock();
void doCallbacks(std::string name);
void doCallbacks(const std::string &name) const;
std::map<std::string, SettingsEntry> m_settings;
std::map<std::string, SettingsEntry> m_defaults;
std::map<std::string, std::vector<std::pair<setting_changed_callback,void*> > > m_callbacks;
SettingsCallbackMap m_callbacks;
mutable Mutex m_callbackMutex;
mutable Mutex m_mutex; // All methods that access m_settings/m_defaults directly should lock this.
mutable Mutex m_callback_mutex;
// All methods that access m_settings/m_defaults directly should lock this.
mutable Mutex m_mutex;
};