diff --git a/src/settings.cpp b/src/settings.cpp index 56afa613..91ea9c58 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -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 > >::iterator iterToVector = m_callbacks.find(name); - if (iterToVector != m_callbacks.end()) - { - std::vector > &vector = iterToVector->second; + MutexAutoLock lock(m_callback_mutex); + SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name); - std::vector >::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 > >::iterator iterToVector = m_callbacks.find(name); - if (iterToVector != m_callbacks.end()) - { - std::vector >::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); } } diff --git a/src/settings.h b/src/settings.h index 80d41fd7..0af861a5 100644 --- a/src/settings.h +++ b/src/settings.h @@ -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 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 m_settings; std::map m_defaults; - std::map > > 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; };