diff options
author | Saturneric <[email protected]> | 2023-10-24 13:22:13 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2023-10-24 13:42:16 +0000 |
commit | fa2e87a48acbc32650ca9db073b991729dfba622 (patch) | |
tree | 1076f6b2eac737d0559cd78e70b44975c789980e /src/core/module | |
parent | fix: solve build issues on macOS (diff) | |
download | GpgFrontend-fa2e87a48acbc32650ca9db073b991729dfba622.tar.gz GpgFrontend-fa2e87a48acbc32650ca9db073b991729dfba622.zip |
feat: use module instead of integrated code at version checking task
Diffstat (limited to 'src/core/module')
-rw-r--r-- | src/core/module/GlobalModuleContext.cpp | 12 | ||||
-rw-r--r-- | src/core/module/GlobalModuleContext.h | 2 | ||||
-rw-r--r-- | src/core/module/GlobalRegisterTable.cpp | 31 | ||||
-rw-r--r-- | src/core/module/GlobalRegisterTable.h | 12 | ||||
-rw-r--r-- | src/core/module/Module.cpp | 1 | ||||
-rw-r--r-- | src/core/module/Module.h | 1 | ||||
-rw-r--r-- | src/core/module/ModuleManager.cpp | 24 | ||||
-rw-r--r-- | src/core/module/ModuleManager.h | 32 |
8 files changed, 105 insertions, 10 deletions
diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 919c744a..1e6d87b1 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -270,6 +270,10 @@ class GlobalModuleContext::Impl { return true; } + bool IsModuleExists(ModuleIdentifier id) const { + return search_module_register_table(id).has_value(); + } + private: struct ModuleRegisterInfo { int channel; @@ -305,15 +309,13 @@ class GlobalModuleContext::Impl { // Function to search for a module in the register table. std::optional<ModuleRegisterInfoPtr> search_module_register_table( - ModuleIdentifier identifier) { + ModuleIdentifier identifier) const { auto it = module_register_table_.find(identifier); if (it == module_register_table_.end()) { return std::nullopt; } return it->second; } - - std::list<ModuleIdentifier>& search_module_events_table(ModuleIdentifier); }; // Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. @@ -368,4 +370,8 @@ int GlobalModuleContext::GetDefaultChannel(ModuleRawPtr _) { return p_->GetDefaultChannel(_); } +bool GlobalModuleContext::IsModuleExists(ModuleIdentifier id) { + return p_->IsModuleExists(id); +} + } // namespace GpgFrontend::Module diff --git a/src/core/module/GlobalModuleContext.h b/src/core/module/GlobalModuleContext.h index e48f186e..8ef1f644 100644 --- a/src/core/module/GlobalModuleContext.h +++ b/src/core/module/GlobalModuleContext.h @@ -77,6 +77,8 @@ class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject { bool TriggerEvent(EventRefrernce); + bool IsModuleExists(ModuleIdentifier); + private: class Impl; std::unique_ptr<Impl> p_; diff --git a/src/core/module/GlobalRegisterTable.cpp b/src/core/module/GlobalRegisterTable.cpp index 1346d9ab..a4910f9f 100644 --- a/src/core/module/GlobalRegisterTable.cpp +++ b/src/core/module/GlobalRegisterTable.cpp @@ -34,6 +34,8 @@ #include <shared_mutex> #include <unordered_map> +#include "spdlog/spdlog.h" + namespace GpgFrontend::Module { class GlobalRegisterTable::Impl { @@ -46,16 +48,22 @@ class GlobalRegisterTable::Impl { Value(std::any v) : value(v), type(v.type()) {} }; - Impl() : global_register_table_() {} + Impl(GlobalRegisterTable* parent) + : parent_(parent), global_register_table_() {} bool PublishKV(Namespace n, Key k, std::any v) { std::unique_lock lock(lock_); + SPDLOG_DEBUG("publishing kv to rt, n: {}, k: {}, v type: {}", n, k, + v.type().name()); + auto& sub_table = global_register_table_.emplace(n, SubTable{}).first->second; auto sub_it = sub_table.find(k); if (sub_it == sub_table.end()) { sub_it = sub_table.emplace(k, std::make_unique<Value>(Value{v})).first; + SPDLOG_DEBUG("new kv in rt, created n: {}, k: {}, v type: {}", n, k, + v.type().name()); } else { if (sub_it->second->type != v.type()) { return false; @@ -64,11 +72,14 @@ class GlobalRegisterTable::Impl { } sub_it->second->version++; + emit parent_->SignalPublish(n, k, sub_it->second->version); return true; } std::optional<std::any> LookupKV(Namespace n, Key k) { std::shared_lock lock(lock_); + SPDLOG_DEBUG("looking up kv in rt, n: {}, k: {}", n, k); + auto it = global_register_table_.find(n); if (it == global_register_table_.end()) return std::nullopt; @@ -79,15 +90,26 @@ class GlobalRegisterTable::Impl { : std::nullopt; } + bool ListenPublish(QObject* o, Namespace n, Key k, LPCallback c) { + if (o == nullptr) return false; + return QObject::connect(parent_, &GlobalRegisterTable::SignalPublish, o, + [n, k, c](Namespace pn, Key pk, int v) { + if (pn == n && pk == k) { + c(pn, pk, v); + } + }) == nullptr; + } + private: using SubTable = std::unordered_map<Key, std::unique_ptr<Value>>; using Table = std::map<Namespace, SubTable>; std::shared_mutex lock_; + GlobalRegisterTable* parent_; Table global_register_table_; }; -GlobalRegisterTable::GlobalRegisterTable() : p_(std::make_unique<Impl>()) {} +GlobalRegisterTable::GlobalRegisterTable() : p_(std::make_unique<Impl>(this)) {} GlobalRegisterTable::~GlobalRegisterTable() = default; @@ -99,4 +121,9 @@ std::optional<std::any> GlobalRegisterTable::LookupKV(Namespace n, Key v) { return p_->LookupKV(n, v); } +bool GlobalRegisterTable::ListenPublish(QObject* o, Namespace n, Key k, + LPCallback c) { + return p_->ListenPublish(o, n, k, c); +} + } // namespace GpgFrontend::Module
\ No newline at end of file diff --git a/src/core/module/GlobalRegisterTable.h b/src/core/module/GlobalRegisterTable.h index f900559e..5991efb5 100644 --- a/src/core/module/GlobalRegisterTable.h +++ b/src/core/module/GlobalRegisterTable.h @@ -29,16 +29,17 @@ #pragma once #include <any> +#include <functional> #include <optional> -#include "core/GpgFrontendCore.h" - namespace GpgFrontend::Module { using Namespace = std::string; using Key = std::string; +using LPCallback = std::function<void(Namespace, Key, int)>; -class GlobalRegisterTable { +class GlobalRegisterTable : public QObject { + Q_OBJECT public: GlobalRegisterTable(); @@ -48,6 +49,11 @@ class GlobalRegisterTable { std::optional<std::any> LookupKV(Namespace, Key); + bool ListenPublish(QObject *, Namespace, Key, LPCallback); + + signals: + void SignalPublish(Namespace, Key, int); + private: class Impl; std::unique_ptr<Impl> p_; diff --git a/src/core/module/Module.cpp b/src/core/module/Module.cpp index d66773ff..12de004a 100644 --- a/src/core/module/Module.cpp +++ b/src/core/module/Module.cpp @@ -29,6 +29,7 @@ #include "Module.h" #include <boost/format.hpp> +#include <string> #include "core/module/GlobalModuleContext.h" diff --git a/src/core/module/Module.h b/src/core/module/Module.h index d2f6aa3b..da87f88e 100644 --- a/src/core/module/Module.h +++ b/src/core/module/Module.h @@ -29,7 +29,6 @@ #pragma once #include "core/module/Event.h" -#include "core/thread/Task.h" #include "core/thread/TaskRunner.h" namespace GpgFrontend::Module { diff --git a/src/core/module/ModuleManager.cpp b/src/core/module/ModuleManager.cpp index 0bf9e972..20f9ab28 100644 --- a/src/core/module/ModuleManager.cpp +++ b/src/core/module/ModuleManager.cpp @@ -28,11 +28,12 @@ #include "ModuleManager.h" -#include <memory> +#include <boost/format.hpp> #include "core/module/GlobalModuleContext.h" #include "core/module/GlobalRegisterTable.h" #include "core/module/Module.h" +#include "core/thread/Task.h" #include "core/thread/TaskRunner.h" namespace GpgFrontend::Module { @@ -88,6 +89,10 @@ class ModuleManager::Impl { return grt_->LookupKV(n, k); } + bool ListenPublish(QObject* o, Namespace n, Key k, LPCallback c) { + return grt_->ListenPublish(o, n, k, c); + } + private: static ModuleMangerPtr global_module_manager_; TaskRunnerPtr task_runner_; @@ -95,6 +100,12 @@ class ModuleManager::Impl { GRTPtr grt_; }; +bool UpsertRTValueTyped(const std::string& namespace_, const std::string& key, + const std::any& value) { + return ModuleManager::GetInstance()->UpsertRTValue(namespace_, key, + std::any(value)); +} + ModuleManager::ModuleManager() : p_(std::make_unique<Impl>()) {} ModuleManager::~ModuleManager() = default; @@ -129,4 +140,15 @@ std::optional<std::any> ModuleManager::RetrieveRTValue(Namespace n, Key k) { return p_->RetrieveRTValue(n, k); } +bool ModuleManager::ListenPublish(QObject* o, Namespace n, Key k, + LPCallback c) { + return p_->ListenPublish(o, n, k, c); +} + +ModuleIdentifier GetRealModuleIdentifier(const ModuleIdentifier& id) { + // WARNING: when YOU need to CHANGE this line, YOU SHOULD change the same code + // in Module.cpp as well. + return (boost::format("__module_%1%") % id).str(); +} + } // namespace GpgFrontend::Module
\ No newline at end of file diff --git a/src/core/module/ModuleManager.h b/src/core/module/ModuleManager.h index 66fa9db6..586627b6 100644 --- a/src/core/module/ModuleManager.h +++ b/src/core/module/ModuleManager.h @@ -50,6 +50,10 @@ using ModuleMangerPtr = std::shared_ptr<ModuleManager>; using GMCPtr = std::shared_ptr<GlobalModuleContext>; using Namespace = std::string; using Key = std::string; +using LPCallback = std::function<void(Namespace, Key, int)>; + +ModuleIdentifier GPGFRONTEND_CORE_EXPORT +GetRealModuleIdentifier(const ModuleIdentifier& id); class GPGFRONTEND_CORE_EXPORT ModuleManager : public QObject { Q_OBJECT @@ -72,6 +76,8 @@ class GPGFRONTEND_CORE_EXPORT ModuleManager : public QObject { std::optional<std::any> RetrieveRTValue(Namespace, Key); + bool ListenPublish(QObject*, Namespace, Key, LPCallback); + private: class Impl; std::unique_ptr<Impl> p_; @@ -100,4 +106,30 @@ void TriggerEvent(const EventIdentifier& event_id, Args&&... args) { std::move(MakeEvent(event_id, std::forward<Args>(args)...))); } +bool GPGFRONTEND_CORE_EXPORT UpsertRTValueTyped(const std::string& namespace_, + const std::string& key, + const std::any& value); + +template <typename T> +std::optional<T> RetrieveRTValueTyped(const std::string& namespace_, + const std::string& key) { + auto any_value = + ModuleManager::GetInstance()->RetrieveRTValue(namespace_, key); + if (any_value && any_value->type() == typeid(T)) { + return std::any_cast<T>(*any_value); + } + return std::nullopt; +} + +template <typename T> +T RetrieveRTValueTypedOrDefault(const std::string& namespace_, + const std::string& key, const T& defaultValue) { + auto any_value = + ModuleManager::GetInstance()->RetrieveRTValue(namespace_, key); + if (any_value && any_value->type() == typeid(T)) { + return std::any_cast<T>(*any_value); + } + return defaultValue; +} + } // namespace GpgFrontend::Module
\ No newline at end of file |