aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/basic
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/function/basic')
-rw-r--r--src/core/function/basic/ChannelObject.cpp59
-rw-r--r--src/core/function/basic/ChannelObject.h98
-rw-r--r--src/core/function/basic/GpgFunctionObject.cpp105
-rw-r--r--src/core/function/basic/GpgFunctionObject.h194
-rw-r--r--src/core/function/basic/SingletonStorage.cpp133
-rw-r--r--src/core/function/basic/SingletonStorage.h90
-rw-r--r--src/core/function/basic/SingletonStorageCollection.cpp124
-rw-r--r--src/core/function/basic/SingletonStorageCollection.h79
8 files changed, 882 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..18449ddb
--- /dev/null
+++ b/src/core/function/basic/ChannelObject.cpp
@@ -0,0 +1,59 @@
+/**
+ * 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"
+
+#include <iostream>
+
+namespace GpgFrontend {
+
+ChannelObject::ChannelObject() noexcept = default;
+
+ChannelObject::ChannelObject(int channel, QString type)
+ : channel_(channel), type_(std::move(type)) {}
+
+#ifdef DEBUG
+ChannelObject::~ChannelObject() noexcept {
+ // using iostream instead of spdlog bacause at this time spdlog may have
+ // already been destroyed.
+ QTextStream(stdout) << "releasing channel object: " << this->type_
+ << Qt::endl;
+}
+#else
+ChannelObject::~ChannelObject() noexcept = default;
+#endif
+
+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..27be55c4
--- /dev/null
+++ b/src/core/function/basic/ChannelObject.h
@@ -0,0 +1,98 @@
+/**
+ * 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/GpgConstants.h"
+#include "core/function/SecureMemoryAllocator.h"
+namespace GpgFrontend {
+
+/**
+ * @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 Destroy the Channel Object object
+ *
+ */
+ virtual ~ChannelObject() noexcept;
+
+ /**
+ * @brief Construct a new Channel Object object
+ *
+ * @param channel
+ */
+ explicit ChannelObject(int channel, QString type);
+
+ /**
+ * @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
+ QString type_;
+};
+
+template <typename Derived>
+auto ConvertToChannelObjectPtr(
+ std::unique_ptr<Derived, SecureObjectDeleter<Derived>> derivedPtr)
+ -> std::unique_ptr<ChannelObject, SecureObjectDeleter<ChannelObject>> {
+ static_assert(std::is_base_of_v<ChannelObject, Derived>,
+ "Derived must be a subclass of ChannelObject");
+
+ ChannelObject* base_ptr = derivedPtr.release();
+ return std::unique_ptr<ChannelObject, SecureObjectDeleter<ChannelObject>>(
+ base_ptr);
+}
+
+} // 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..e9e444f1
--- /dev/null
+++ b/src/core/function/basic/GpgFunctionObject.cpp
@@ -0,0 +1,105 @@
+/**
+ * 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"
+
+#include <map>
+#include <mutex>
+#include <typeinfo>
+
+#include "core/function/SecureMemoryAllocator.h"
+#include "core/function/basic/ChannelObject.h"
+
+struct FunctionObjectTypeLockInfo {
+ std::map<int, std::mutex> channel_lock_map;
+ std::mutex type_lock;
+};
+
+std::mutex g_function_object_mutex_map_lock;
+std::map<size_t, FunctionObjectTypeLockInfo> g_function_object_mutex_map;
+
+namespace GpgFrontend {
+auto GetGlobalFunctionObjectChannelLock(const std::type_info& type, int channel)
+ -> std::mutex& {
+ std::lock_guard<std::mutex> lock_guard(g_function_object_mutex_map_lock);
+ auto& channel_map = g_function_object_mutex_map[type.hash_code()];
+ return channel_map.channel_lock_map[channel];
+}
+
+auto GetGlobalFunctionObjectTypeLock(const std::type_info& type)
+ -> std::mutex& {
+ std::lock_guard<std::mutex> lock_guard(g_function_object_mutex_map_lock);
+ auto& channel_map = g_function_object_mutex_map[type.hash_code()];
+ return channel_map.type_lock;
+}
+
+/**
+ * @brief Get the Instance object
+ *
+ * @param channel
+ * @return T&
+ */
+auto GetChannelObjectInstance(const std::type_info& type, int channel)
+ -> ChannelObject* {
+ GF_DEFAULT_LOG_TRACE("try to get instance of type: {} at channel: {}",
+ type.name(), channel);
+
+ // lock this channel
+ std::lock_guard<std::mutex> guard(
+ GetGlobalFunctionObjectChannelLock(type, channel));
+
+ auto* p_storage =
+ SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(type);
+ GF_DEFAULT_LOG_TRACE("get singleton storage result, p_storage: {}",
+ static_cast<void*>(p_storage));
+
+ auto* p_pbj =
+ static_cast<ChannelObject*>(p_storage->FindObjectInChannel(channel));
+ GF_DEFAULT_LOG_TRACE("find channel object result, channel {}, p_pbj: {}",
+ channel, static_cast<void*>(p_pbj));
+
+ return p_pbj;
+}
+
+auto CreateChannelObjectInstance(const std::type_info& type, int channel,
+ SecureUniquePtr<ChannelObject> channel_object)
+ -> ChannelObject* {
+ // lock this channel
+ std::lock_guard<std::mutex> guard(
+ GetGlobalFunctionObjectChannelLock(type, channel));
+
+ auto* p_storage =
+ SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(type);
+ GF_DEFAULT_LOG_TRACE("create channel object, channel {}, type: {}", channel,
+ type.name());
+
+ // do create object of this channel
+ return p_storage->SetObjectInChannel(channel, std::move(channel_object));
+}
+
+} // namespace GpgFrontend \ 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..1ea352b6
--- /dev/null
+++ b/src/core/function/basic/GpgFunctionObject.h
@@ -0,0 +1,194 @@
+/**
+ * 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 <mutex>
+#include <stdexcept>
+
+#include "core/GpgFrontendCoreExport.h"
+#include "core/function/basic/ChannelObject.h"
+#include "core/function/basic/SingletonStorage.h"
+#include "core/function/basic/SingletonStorageCollection.h"
+#include "core/utils/MemoryUtils.h"
+
+namespace GpgFrontend {
+
+auto GPGFRONTEND_CORE_EXPORT GetChannelObjectInstance(
+ const std::type_info& type, int channel) -> ChannelObject*;
+
+auto GPGFRONTEND_CORE_EXPORT CreateChannelObjectInstance(
+ const std::type_info& type, int channel,
+ SecureUniquePtr<ChannelObject> channel_object) -> ChannelObject*;
+
+auto GPGFRONTEND_CORE_EXPORT
+GetGlobalFunctionObjectTypeLock(const std::type_info& type) -> std::mutex&;
+
+/**
+ * @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_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
+ "T not derived from SingletonFunctionObject<T>");
+
+ const auto& type = typeid(T);
+ std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
+ auto* channel_object = GetChannelObjectInstance(type, channel);
+ if (channel_object == nullptr) {
+ channel_object = CreateChannelObjectInstance(
+ type, channel,
+ ConvertToChannelObjectPtr(SecureCreateUniqueObject<T>(channel)));
+ }
+ return *static_cast<T*>(channel_object);
+ }
+
+ /**
+ * @brief Create a Instance object
+ *
+ * @param channel
+ * @param factory
+ * @return T&
+ */
+ static auto CreateInstance(
+ int channel, const std::function<ChannelObjectPtr(void)>& factory) -> T& {
+ static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
+ "T not derived from SingletonFunctionObject<T>");
+
+ const auto& type = typeid(T);
+ std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
+ return *static_cast<T*>(
+ CreateChannelObjectInstance(type, channel, factory()));
+ }
+
+ /**
+ * @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, typeid(T).name()) {}
+
+ /**
+ * @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..eab71e0f
--- /dev/null
+++ b/src/core/function/basic/SingletonStorage.cpp
@@ -0,0 +1,133 @@
+/**
+ * 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"
+#include "utils/MemoryUtils.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()) {
+ GF_DEFAULT_LOG_TRACE("cannot find channel object, channel: {}",
+ channel);
+ 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, ChannelObjectPtr p_obj)
+ -> GpgFrontend::ChannelObject* {
+ GF_DEFAULT_LOG_TRACE(
+ "set channel object, type: {} in channel: {}, address: {}",
+ typeid(p_obj.get()).name(), channel, static_cast<void*>(p_obj.get()));
+
+ assert(p_obj != nullptr);
+ if (p_obj == nullptr) {
+ GF_DEFAULT_LOG_ERROR(
+ "cannot set a nullptr as a channel obejct of channel: {}", channel);
+ return nullptr;
+ }
+
+ p_obj->SetChannel(channel);
+ auto* raw_obj = p_obj.get();
+
+ {
+ GF_DEFAULT_LOG_TRACE(
+ "register channel object to instances map, "
+ "channel: {}, address: {}",
+ channel, static_cast<void*>(p_obj.get()));
+ std::unique_lock<std::shared_mutex> lock(instances_mutex_);
+ instances_map_[channel] = std::move(p_obj);
+ }
+
+ GF_DEFAULT_LOG_TRACE(
+ "set channel: {} success, current channel object address: {}", channel,
+ static_cast<void*>(raw_obj));
+ return raw_obj;
+ }
+
+ private:
+ std::shared_mutex instances_mutex_; ///< mutex for _instances_map
+ std::map<int, ChannelObjectPtr>
+ instances_map_; ///< map of singleton instances
+};
+
+SingletonStorage::SingletonStorage() noexcept
+ : p_(SecureCreateUniqueObject<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, ChannelObjectPtr 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..0ef47443
--- /dev/null
+++ b/src/core/function/basic/SingletonStorage.h
@@ -0,0 +1,90 @@
+/**
+ * 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/SecureMemoryAllocator.h"
+
+namespace GpgFrontend {
+
+class ChannelObject;
+
+using ChannelObjectPtr = SecureUniquePtr<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, ChannelObjectPtr p_obj)
+ -> ChannelObject*;
+
+ private:
+ class Impl;
+ SecureUniquePtr<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..c22b5242
--- /dev/null
+++ b/src/core/function/basic/SingletonStorageCollection.cpp
@@ -0,0 +1,124 @@
+/**
+ * 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 <memory>
+#include <shared_mutex>
+
+#include "core/function/SecureMemoryAllocator.h"
+#include "core/function/basic/SingletonStorage.h"
+#include "core/utils/MemoryUtils.h"
+
+namespace GpgFrontend {
+
+SecureUniquePtr<SingletonStorageCollection> global_instance = nullptr;
+
+class SingletonStorageCollection::Impl {
+ public:
+ /**
+ * @brief Get the Instance object
+ *
+ * @return SingletonStorageCollection*
+ */
+ static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* {
+ if (force_refresh || global_instance == nullptr) {
+ global_instance = SecureCreateUniqueObject<SingletonStorageCollection>();
+ GF_DEFAULT_LOG_TRACE(
+ "a new global singleton storage collection created, address: {}",
+ static_cast<void*>(global_instance.get()));
+ }
+ return global_instance.get();
+ }
+
+ /**
+ * @brief Get the Instance object
+ *
+ * @return SingletonStorageCollection*
+ */
+ static void Destroy() { global_instance = nullptr; }
+
+ /**
+ * @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()) {
+ auto storage = SecureCreateUniqueObject<SingletonStorage>();
+ GF_DEFAULT_LOG_TRACE(
+ "hash: {} created, singleton storage address: {} type_name: {}",
+ hash, static_cast<void*>(storage.get()), type_id.name());
+
+ {
+ std::unique_lock<std::shared_mutex> lock(storages_mutex_);
+ storages_map_.insert({hash, std::move(storage)});
+ }
+ continue;
+ }
+ return ins_it->second.get();
+ }
+ }
+
+ private:
+ std::shared_mutex storages_mutex_; ///< mutex for storages_map_
+ std::map<size_t, SingletonStoragePtr> storages_map_;
+};
+
+SingletonStorageCollection::SingletonStorageCollection() noexcept
+ : p_(SecureCreateUniqueObject<Impl>()) {}
+
+SingletonStorageCollection::~SingletonStorageCollection() = default;
+
+auto GpgFrontend::SingletonStorageCollection::GetInstance(bool force_refresh)
+ -> GpgFrontend::SingletonStorageCollection* {
+ return Impl::GetInstance(force_refresh);
+}
+
+void SingletonStorageCollection::Destroy() {
+ GF_DEFAULT_LOG_TRACE(
+ "global singleton storage collection is about to destroy, address: {}",
+ static_cast<void*>(global_instance.get()));
+ return SingletonStorageCollection::Impl::Destroy();
+}
+
+auto SingletonStorageCollection::GetSingletonStorage(
+ const std::type_info& type_id) -> GpgFrontend::SingletonStorage* {
+ return p_->GetSingletonStorage(type_id);
+}
+
+} // 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..38ced83b
--- /dev/null
+++ b/src/core/function/basic/SingletonStorageCollection.h
@@ -0,0 +1,79 @@
+/**
+ * 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/SecureMemoryAllocator.h"
+
+namespace GpgFrontend {
+class SingletonStorage;
+
+using SingletonStoragePtr =
+ std::unique_ptr<SingletonStorage, SecureObjectDeleter<SingletonStorage>>;
+
+class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection {
+ public:
+ /**
+ * @brief
+ *
+ */
+ SingletonStorageCollection() noexcept;
+
+ /**
+ * @brief
+ *
+ */
+ ~SingletonStorageCollection();
+
+ /**
+ * @brief Get the Instance object
+ *
+ * @return SingletonStorageCollection*
+ */
+ static auto GetInstance(bool force_refresh) -> SingletonStorageCollection*;
+
+ /**
+ * @brief
+ *
+ */
+ static void Destroy();
+
+ /**
+ * @brief Get the Singleton Storage object
+ *
+ * @param singleton_function_object
+ * @return SingletonStorage*
+ */
+ auto GetSingletonStorage(const std::type_info&) -> SingletonStorage*;
+
+ private:
+ class Impl;
+ SecureUniquePtr<Impl> p_;
+};
+
+} // namespace GpgFrontend \ No newline at end of file