diff options
Diffstat (limited to 'src/core/function/basic')
-rw-r--r-- | src/core/function/basic/ChannelObject.cpp | 45 | ||||
-rw-r--r-- | src/core/function/basic/ChannelObject.h | 80 | ||||
-rw-r--r-- | src/core/function/basic/GpgFunctionObject.cpp | 29 | ||||
-rw-r--r-- | src/core/function/basic/GpgFunctionObject.h | 209 | ||||
-rw-r--r-- | src/core/function/basic/SingletonStorage.cpp | 118 | ||||
-rw-r--r-- | src/core/function/basic/SingletonStorage.h | 86 | ||||
-rw-r--r-- | src/core/function/basic/SingletonStorageCollection.cpp | 104 | ||||
-rw-r--r-- | src/core/function/basic/SingletonStorageCollection.h | 69 |
8 files changed, 740 insertions, 0 deletions
diff --git a/src/core/function/basic/ChannelObject.cpp b/src/core/function/basic/ChannelObject.cpp new file mode 100644 index 00000000..9485a278 --- /dev/null +++ b/src/core/function/basic/ChannelObject.cpp @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "ChannelObject.h" + +namespace GpgFrontend { + +ChannelObject::ChannelObject() noexcept = default; + +ChannelObject::ChannelObject(int channel) : channel_(channel) {} + +void ChannelObject::SetChannel(int channel) { this->channel_ = channel; } + +auto ChannelObject::GetChannel() const -> int { return channel_; } + +auto ChannelObject::GetDefaultChannel() -> int { + return kGpgFrontendDefaultChannel; +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/ChannelObject.h b/src/core/function/basic/ChannelObject.h new file mode 100644 index 00000000..9d247e6a --- /dev/null +++ b/src/core/function/basic/ChannelObject.h @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +static constexpr int kGpgFrontendDefaultChannel = + 0; ///< the default channel id + +/** + * @brief object which in channel system is called "channel" + * + */ +class GPGFRONTEND_CORE_EXPORT ChannelObject { + public: + /** + * @brief Construct a new Default Channel Object object + * + */ + ChannelObject() noexcept; + + /** + * @brief Construct a new Channel Object object + * + * @param channel + */ + explicit ChannelObject(int channel); + + /** + * @brief Get the Default Channel object + * + * @return int + */ + static auto GetDefaultChannel() -> int; + + /** + * @brief Get the Channel object + * + * @return int + */ + [[nodiscard]] auto GetChannel() const -> int; + + /** + * @brief Set the Channel object + * + * @param channel + */ + void SetChannel(int channel); + + private: + int channel_ = kGpgFrontendDefaultChannel; ///< The channel id +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/GpgFunctionObject.cpp b/src/core/function/basic/GpgFunctionObject.cpp new file mode 100644 index 00000000..242621d2 --- /dev/null +++ b/src/core/function/basic/GpgFunctionObject.cpp @@ -0,0 +1,29 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GpgFunctionObject.h"
\ No newline at end of file diff --git a/src/core/function/basic/GpgFunctionObject.h b/src/core/function/basic/GpgFunctionObject.h new file mode 100644 index 00000000..59bc5715 --- /dev/null +++ b/src/core/function/basic/GpgFunctionObject.h @@ -0,0 +1,209 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +#include "core/function/basic/ChannelObject.h" +#include "core/function/basic/SingletonStorage.h" +#include "core/function/basic/SingletonStorageCollection.h" + +namespace GpgFrontend { + +/** + * @brief + * + * @tparam T + */ +template <typename T> +class SingletonFunctionObject : public ChannelObject { + public: + /** + * @brief prohibit copy + * + */ + SingletonFunctionObject(const SingletonFunctionObject<T>&) = delete; + + /** + * @brief prohibit copy + * + * @return SingletonFunctionObject& + */ + auto operator=(const SingletonFunctionObject<T>&) + -> SingletonFunctionObject& = delete; + + /** + * @brief Get the Instance object + * + * @param channel + * @return T& + */ + static auto GetInstance(int channel = GpgFrontend::kGpgfrontendDefaultChannel) + -> T& { + static std::mutex g_channel_mutex_map_lock; + static std::map<int, std::mutex> g_channel_mutex_map; + + { + std::lock_guard<std::mutex> guard(g_channel_mutex_map_lock); + if (g_channel_mutex_map.find(channel) == g_channel_mutex_map.end()) { + g_channel_mutex_map[channel]; + } + } + + static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value, + "T not derived from SingletonFunctionObject<T>"); + + auto* p_storage = + SingletonStorageCollection::GetInstance(false)->GetSingletonStorage( + typeid(T)); + auto* p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel)); + + if (p_pbj == nullptr) { + // lock this channel + std::lock_guard<std::mutex> guard(g_channel_mutex_map[channel]); + + // double check + if (p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel)); + p_pbj != nullptr) { + return *p_pbj; + } + + // do create object of this channel + auto new_obj = std::unique_ptr<ChannelObject>(new T(channel)); + return *static_cast<T*>( + p_storage->SetObjectInChannel(channel, std::move(new_obj))); + } + return *p_pbj; + } + + /** + * @brief Create a Instance object + * + * @param channel + * @param factory + * @return T& + */ + static auto CreateInstance( + int channel, + const std::function<std::unique_ptr<ChannelObject>(void)>& factory) + -> T& { + static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value, + "T not derived from SingletonFunctionObject<T>"); + + auto* p_storage = + SingletonStorageCollection::GetInstance(false)->GetSingletonStorage( + typeid(T)); + + auto p_pbj = static_cast<T*>(p_storage->FindObjectInChannel(channel)); + + if (p_pbj == nullptr) { + return *static_cast<T*>( + p_storage->SetObjectInChannel(channel, factory())); + } + return *p_pbj; + } + + /** + * @brief + * + * @param channel + * @return T& + */ + static void ReleaseChannel(int channel) { + SingletonStorageCollection::GetInstance(false) + ->GetSingletonStorage(typeid(T)) + ->ReleaseChannel(channel); + } + + /** + * @brief Get the Default Channel object + * + * @return int + */ + static auto GetDefaultChannel() -> int { + return ChannelObject::GetDefaultChannel(); + } + + /** + * @brief Get the Channel object + * + * @return int + */ + [[nodiscard]] auto GetChannel() const -> int { + return ChannelObject::GetChannel(); + } + + /** + * @brief Get all the channel ids + * + * @return std::vector<int> + */ + static auto GetAllChannelId() -> std::vector<int> { + return SingletonStorageCollection::GetInstance(false) + ->GetSingletonStorage(typeid(T)) + ->GetAllChannelId(); + } + + /** + * @brief Construct a new Singleton Function Object object + * + */ + SingletonFunctionObject(T&&) = delete; + + /** + * @brief Construct a new Singleton Function Object object + * + */ + SingletonFunctionObject(const T&) = delete; + + /** + * @brief + * + */ + void operator=(const T&) = delete; + + protected: + /** + * @brief Construct a new Singleton Function Object object + * + */ + SingletonFunctionObject() = default; + + /** + * @brief Construct a new Singleton Function Object object + * + * @param channel + */ + explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {} + + /** + * @brief Destroy the Singleton Function Object object + * + */ + virtual ~SingletonFunctionObject() = default; +}; +} // namespace GpgFrontend diff --git a/src/core/function/basic/SingletonStorage.cpp b/src/core/function/basic/SingletonStorage.cpp new file mode 100644 index 00000000..567d3572 --- /dev/null +++ b/src/core/function/basic/SingletonStorage.cpp @@ -0,0 +1,118 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SingletonStorage.h" + +#include <shared_mutex> + +#include "core/function/basic/ChannelObject.h" + +namespace GpgFrontend { + +class SingletonStorage::Impl { + public: + void ReleaseChannel(int channel) { + decltype(instances_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(instances_mutex_); + ins_it = instances_map_.find(channel); + } + if (ins_it != instances_map_.end()) instances_map_.erase(ins_it); + } + + auto FindObjectInChannel(int channel) -> GpgFrontend::ChannelObject* { + // read instances_map_ + decltype(instances_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(instances_mutex_); + ins_it = instances_map_.find(channel); + if (ins_it == instances_map_.end()) { + return nullptr; + } + return ins_it->second.get(); + } + } + + auto GetAllChannelId() -> std::vector<int> { + std::vector<int> channels; + channels.reserve(instances_map_.size()); + for (const auto& [key, value] : instances_map_) { + channels.push_back(key); + } + return channels; + } + + auto SetObjectInChannel(int channel, std::unique_ptr<ChannelObject> p_obj) + -> GpgFrontend::ChannelObject* { + { + SPDLOG_TRACE("set channel: {} instance address: {}", channel, + static_cast<void*>(&instances_map_)); + + assert(p_obj != nullptr); + if (p_obj == nullptr) return nullptr; + + auto* raw_obj = p_obj.get(); + p_obj->SetChannel(channel); + { + std::unique_lock<std::shared_mutex> lock(instances_mutex_); + instances_map_.insert({channel, std::move(p_obj)}); + } + return raw_obj; + } + } + + private: + std::shared_mutex instances_mutex_; ///< mutex for _instances_map + std::map<int, std::unique_ptr<ChannelObject>> + instances_map_; ///< map of singleton instances +}; + +SingletonStorage::SingletonStorage() noexcept : p_(std::make_unique<Impl>()) {} + +SingletonStorage::~SingletonStorage() = default; + +void SingletonStorage::ReleaseChannel(int channel) { + p_->ReleaseChannel(channel); +} + +auto SingletonStorage::FindObjectInChannel(int channel) + -> GpgFrontend::ChannelObject* { + return p_->FindObjectInChannel(channel); +} + +auto SingletonStorage::GetAllChannelId() -> std::vector<int> { + return p_->GetAllChannelId(); +} + +auto SingletonStorage::SetObjectInChannel(int channel, + std::unique_ptr<ChannelObject> p_obj) + -> GpgFrontend::ChannelObject* { + return p_->SetObjectInChannel(channel, std::move(p_obj)); +} + +}; // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorage.h b/src/core/function/basic/SingletonStorage.h new file mode 100644 index 00000000..04fb2f07 --- /dev/null +++ b/src/core/function/basic/SingletonStorage.h @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +class ChannelObject; + +class GPGFRONTEND_CORE_EXPORT SingletonStorage { + public: + /** + * @brief + * + */ + SingletonStorage() noexcept; + + /** + * @brief + * + */ + ~SingletonStorage(); + + /** + * @brief + * + * @param channel + */ + void ReleaseChannel(int channel); + + /** + * @brief + * + * @param channel + * @return T* + */ + auto FindObjectInChannel(int channel) -> ChannelObject*; + + /** + * @brief Get all the channel ids + * + * @return std::vector<int> + */ + auto GetAllChannelId() -> std::vector<int>; + + /** + * @brief Set a new object in channel object + * + * @param channel + * @param p_obj + * @return T* + */ + auto SetObjectInChannel(int channel, std::unique_ptr<ChannelObject> p_obj) + -> ChannelObject*; + + private: + class Impl; + std::unique_ptr<Impl> p_; +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp new file mode 100644 index 00000000..a5eb1dc0 --- /dev/null +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -0,0 +1,104 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SingletonStorageCollection.h" + +#include <shared_mutex> + +#include "core/function/basic/SingletonStorage.h" + +namespace GpgFrontend { + +class SingletonStorageCollection::Impl { + public: + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { + static SingletonStorageCollection* instance = nullptr; + + if (force_refresh || instance == nullptr) { + instance = new SingletonStorageCollection(); + SPDLOG_DEBUG("new single storage collection created: {}", + static_cast<void*>(instance)); + } + + return instance; + } + + /** + * @brief Get the Singleton Storage object + * + * @param singleton_function_object + * @return SingletonStorage* + */ + auto GetSingletonStorage(const std::type_info& type_id) -> SingletonStorage* { + const auto hash = type_id.hash_code(); + + while (true) { + decltype(storages_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(storages_mutex_); + ins_it = storages_map_.find(hash); + } + if (ins_it == storages_map_.end()) { + { + std::unique_lock<std::shared_mutex> lock(storages_mutex_); + storages_map_.insert({hash, std::make_unique<SingletonStorage>()}); + } + SPDLOG_TRACE("hash: {} created, storage address: {} type_name: {}", + hash, static_cast<void*>(&storages_map_), type_id.name()); + continue; + } + return ins_it->second.get(); + } + } + + private: + std::shared_mutex storages_mutex_; ///< mutex for storages_map_ + std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_; +}; + +SingletonStorageCollection::SingletonStorageCollection() noexcept + : p_(std::make_unique<Impl>()) {} + +SingletonStorageCollection::~SingletonStorageCollection() = default; + +auto SingletonStorageCollection::GetSingletonStorage( + const std::type_info& type_id) -> GpgFrontend::SingletonStorage* { + return p_->GetSingletonStorage(type_id); +} + +auto GpgFrontend::SingletonStorageCollection::GetInstance( + bool force_refresh = false) -> GpgFrontend::SingletonStorageCollection* { + return Impl::GetInstance(force_refresh); +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorageCollection.h b/src/core/function/basic/SingletonStorageCollection.h new file mode 100644 index 00000000..16a72fd4 --- /dev/null +++ b/src/core/function/basic/SingletonStorageCollection.h @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +class SingletonStorage; + +class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection { + public: + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static auto GetInstance(bool force_refresh) -> SingletonStorageCollection*; + + /** + * @brief Get the Singleton Storage object + * + * @param singleton_function_object + * @return SingletonStorage* + */ + auto GetSingletonStorage(const std::type_info&) -> SingletonStorage*; + + private: + class Impl; + std::unique_ptr<Impl> p_; + + /** + * @brief + * + */ + SingletonStorageCollection() noexcept; + + /** + * @brief + * + */ + ~SingletonStorageCollection(); +}; + +} // namespace GpgFrontend
\ No newline at end of file |