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/GlobalRegisterTable.cpp | |
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/GlobalRegisterTable.cpp')
-rw-r--r-- | src/core/module/GlobalRegisterTable.cpp | 31 |
1 files changed, 29 insertions, 2 deletions
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 |