diff options
-rw-r--r-- | src/settings.cpp | 45 | ||||
-rw-r--r-- | src/settings.h | 31 |
2 files changed, 44 insertions, 32 deletions
diff --git a/src/settings.cpp b/src/settings.cpp index 56afa6133..91ea9c58b 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<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); + + if (it_cbks != m_callbacks.end()) { + SettingsCallbackList &cbks = it_cbks->second; - std::vector<std::pair<setting_changed_callback, void*> >::iterator position = - std::find(vector.begin(), vector.end(), std::make_pair(cbf, userdata)); + SettingsCallbackList::iterator position = + std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata)); - if (position != vector.end()) - vector.erase(position); + 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); } } diff --git a/src/settings.h b/src/settings.h index 80d41fd79..0af861a58 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<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); -private: + 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_callback_mutex; - mutable Mutex m_callbackMutex; - mutable Mutex m_mutex; // All methods that access m_settings/m_defaults directly should lock this. + // All methods that access m_settings/m_defaults directly should lock this. + mutable Mutex m_mutex; }; |