From a22d2245486826784feb405a8f8a4ef896a7b855 Mon Sep 17 00:00:00 2001 From: Eugene Kurzberg Date: Wed, 6 Jul 2016 14:10:16 +0300 Subject: [PATCH] Implemented generic CallbackManager and System Settings KeyManager to provide possibility to add several callbacks for each System Settings key. Change-Id: I0909b74d4674155604f0be05f470b9a02a5bb23b Signed-off-by: Eugene Kurzberg --- lib-apps-common/inc/System/Settings.h | 42 ++++++++++++++ lib-apps-common/inc/Utils/CallbackManager.h | 73 ++++++++++++++++++++++++ lib-apps-common/inc/Utils/CallbackManagerImpl.h | 57 +++++++++++++++++++ lib-apps-common/src/System/Settings.cpp | 76 +++++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 lib-apps-common/inc/System/Settings.h create mode 100644 lib-apps-common/inc/Utils/CallbackManager.h create mode 100644 lib-apps-common/inc/Utils/CallbackManagerImpl.h create mode 100644 lib-apps-common/src/System/Settings.cpp diff --git a/lib-apps-common/inc/System/Settings.h b/lib-apps-common/inc/System/Settings.h new file mode 100644 index 0000000..2d70ec3 --- /dev/null +++ b/lib-apps-common/inc/System/Settings.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef SYSTEM_SETTINGS_H +#define SYSTEM_SETTINGS_H + +#include +#include "Utils/CallbackManager.h" + +namespace System +{ + namespace Settings + { + typedef Utils::CallbackManager CallbackManager; + + /** + * @see system_settings_set_changed_cb() + */ + EXPORT_API void addCallback(system_settings_key_e key, CallbackManager::Callback callback); + + /** + * @see system_settings_unset_changed_cb() + */ + EXPORT_API void removeCallback(system_settings_key_e key, CallbackManager::Callback callback); + }; +} + +#endif /* SYSTEM_SETTINGS_H */ diff --git a/lib-apps-common/inc/Utils/CallbackManager.h b/lib-apps-common/inc/Utils/CallbackManager.h new file mode 100644 index 0000000..d5093e2 --- /dev/null +++ b/lib-apps-common/inc/Utils/CallbackManager.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef UTILS_CALLBACK_MANAGER_H +#define UTILS_CALLBACK_MANAGER_H + +#include + +namespace Utils +{ + /** + * @brief Provides possibility to store and invoke several callbacks at once. + * @param[in] Args Callback arguments types + */ + template + class CallbackManager + { + public: + /** + * @brief Callback prototype. + */ + typedef void(*CallbackType)(void *, Args...); + + /** + * @brief Callback and data pair. + */ + typedef std::pair Callback; + + /** + * @return Whether there is no added callbacks. + */ + bool isEmpty() const; + + /** + * @brief Add new callback. + * @param[in] callback Callback and data pair. + */ + void addCallback(Callback callback); + + /** + * @brief Remove added callback. + * @param[in] callback Callback and data pair. + */ + void removeCallback(Callback callback); + + /** + * @brief Invoke all added callbacks. + */ + void invokeCallbacks(Args... args); + + private: + typedef std::vector Callbacks; + Callbacks m_Callbacks; + }; +} + +#include "Utils/CallbackManagerImpl.h" + +#endif /* UTILS_CALLBACK_MANAGER_H */ diff --git a/lib-apps-common/inc/Utils/CallbackManagerImpl.h b/lib-apps-common/inc/Utils/CallbackManagerImpl.h new file mode 100644 index 0000000..a401b24 --- /dev/null +++ b/lib-apps-common/inc/Utils/CallbackManagerImpl.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef UTILS_CALLBACK_MANAGER_IMPL_H +#define UTILS_CALLBACK_MANAGER_IMPL_H + +#include "Utils/CallbackManager.h" +#include + +namespace Utils +{ + template + bool CallbackManager::isEmpty() const + { + return m_Callbacks.empty(); + } + + template + void CallbackManager::addCallback(Callback callback) + { + m_Callbacks.push_back(callback); + } + + template + void CallbackManager::removeCallback(Callback callback) + { + auto it = std::find(m_Callbacks.begin(), m_Callbacks.end(), callback); + if (it != m_Callbacks.end()) { + m_Callbacks.erase(it); + } + } + + template + void CallbackManager::invokeCallbacks(Args... args) + { + /* FIXME: This might fail if removeCallback() or addCallback() is called during invocation */ + for (auto &&callback : m_Callbacks) { + callback.first(callback.second, args...); + } + } +} + +#endif /* UTILS_CALLBACK_MANAGER_IMPL_H */ diff --git a/lib-apps-common/src/System/Settings.cpp b/lib-apps-common/src/System/Settings.cpp new file mode 100644 index 0000000..deb0948 --- /dev/null +++ b/lib-apps-common/src/System/Settings.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "System/Settings.h" +#include "Utils/Callback.h" + +#include + +using namespace System::Settings; + +namespace +{ + class KeyManager : public CallbackManager + { + public: + explicit KeyManager(system_settings_key_e key) + : m_Key(key) + { + system_settings_set_changed_cb(m_Key, + makeCallbackWithLastParam(&KeyManager::invokeCallbacks), this); + } + + ~KeyManager() + { + system_settings_unset_changed_cb(m_Key); + } + + bool operator==(system_settings_key_e key) const + { + return m_Key == key; + } + + private: + system_settings_key_e m_Key; + }; + + std::list keyManagers; +} + +void System::Settings::addCallback(system_settings_key_e key, KeyManager::Callback callback) +{ + auto manager = std::find(keyManagers.begin(), keyManagers.end(), key); + if (manager == keyManagers.end()) { + keyManagers.emplace_back(key); + manager = --keyManagers.end(); + } + + manager->addCallback(callback); +} + +void System::Settings::removeCallback(system_settings_key_e key, KeyManager::Callback callback) +{ + auto manager = std::find(keyManagers.begin(), keyManagers.end(), key); + if (manager == keyManagers.end()) { + return; + } + + manager->removeCallback(callback); + if (manager->isEmpty()) { + keyManagers.erase(manager); + } +} -- 2.7.4