GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
ModuleManager.h
1 
29 #pragma once
30 
31 #include <mutex>
32 #include <vector>
33 
34 #include "core/function/SecureMemoryAllocator.h"
35 #include "core/function/basic/GpgFunctionObject.h"
36 #include "core/module/Event.h"
37 #include "core/utils/MemoryUtils.h"
38 
40 class TaskRunner;
41 }
42 
43 namespace GpgFrontend::Module {
44 
45 using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>;
46 
47 class Event;
48 class Module;
49 class GlobalModuleContext;
50 class ModuleManager;
51 class GlobalRegisterTable;
52 
53 using EventRefrernce = std::shared_ptr<Event>;
54 using ModuleIdentifier = QString;
55 using ModulePtr = std::shared_ptr<Module>;
56 using ModuleMangerPtr = std::shared_ptr<ModuleManager>;
57 using GMCPtr = std::shared_ptr<GlobalModuleContext>;
58 using Namespace = QString;
59 using Key = QString;
60 using LPCallback = std::function<void(Namespace, Key, int, std::any)>;
61 
62 class GPGFRONTEND_CORE_EXPORT ModuleManager
63  : public SingletonFunctionObject<ModuleManager> {
64  public:
65  explicit ModuleManager(int channel);
66 
67  virtual ~ModuleManager() override;
68 
69  auto LoadModule(QString) -> void;
70 
71  auto SearchModule(ModuleIdentifier) -> ModulePtr;
72 
73  auto ListAllRegisteredModuleID() -> QList<ModuleIdentifier>;
74 
75  void RegisterModule(ModulePtr);
76 
77  auto IsModuleActivated(ModuleIdentifier) -> bool;
78 
79  void ListenEvent(ModuleIdentifier, EventIdentifier);
80 
81  void TriggerEvent(EventRefrernce);
82 
83  auto SearchEvent(EventTriggerIdentifier) -> std::optional<EventRefrernce>;
84 
85  auto GetModuleListening(ModuleIdentifier) -> QList<EventIdentifier>;
86 
87  void ActiveModule(ModuleIdentifier);
88 
89  void DeactiveModule(ModuleIdentifier);
90 
91  auto GetTaskRunner(ModuleIdentifier) -> std::optional<TaskRunnerPtr>;
92 
93  auto UpsertRTValue(Namespace, Key, std::any) -> bool;
94 
95  auto RetrieveRTValue(Namespace, Key) -> std::optional<std::any>;
96 
97  auto ListenRTPublish(QObject*, Namespace, Key, LPCallback) -> bool;
98 
99  auto ListRTChildKeys(const QString&, const QString&) -> std::vector<Key>;
100 
101  auto GRT() -> GlobalRegisterTable*;
102 
103  private:
104  class Impl;
105  SecureUniquePtr<Impl> p_;
106 };
107 
108 template <typename T, typename... Args>
109 void RegisterModule(Args&&... args) {
110  ModuleManager::GetInstance().RegisterModule(
111  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...));
112 }
113 
114 template <typename T, typename... Args>
115 void RegisterAndActivateModule(Args&&... args) {
116  auto& manager = ModuleManager::GetInstance();
117  auto module =
118  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...);
119  manager.RegisterModule(module);
120  manager.ActiveModule(module->GetModuleIdentifier());
121 }
122 
123 template <typename... Args>
124 void TriggerEvent(const EventIdentifier& event_id, Args&&... args,
125  Event::EventCallback e_cb = nullptr) {
126  ModuleManager::GetInstance().TriggerEvent(
127  std::move(MakeEvent(event_id, std::forward<Args>(args)..., e_cb)));
128 }
129 
136 auto GPGFRONTEND_CORE_EXPORT IsModuleActivate(ModuleIdentifier) -> bool;
137 
147 auto GPGFRONTEND_CORE_EXPORT UpsertRTValue(const QString& namespace_,
148  const QString& key,
149  const std::any& value) -> bool;
150 
157 auto GPGFRONTEND_CORE_EXPORT ListenRTPublishEvent(QObject*, Namespace, Key,
158  LPCallback) -> bool;
159 
167 auto GPGFRONTEND_CORE_EXPORT ListRTChildKeys(const QString& namespace_,
168  const QString& key)
169  -> std::vector<Key>;
170 
171 template <typename T>
172 auto RetrieveRTValueTyped(const QString& namespace_, const QString& key)
173  -> std::optional<T> {
174  auto any_value =
175  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
176  if (any_value && any_value->type() == typeid(T)) {
177  return std::any_cast<T>(*any_value);
178  }
179  return std::nullopt;
180 }
181 
182 template <typename T>
183 auto RetrieveRTValueTypedOrDefault(const QString& namespace_,
184  const QString& key, const T& defaultValue)
185  -> T {
186  auto any_value =
187  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
188  if (any_value && any_value->type() == typeid(T)) {
189  return std::any_cast<T>(*any_value);
190  }
191  return defaultValue;
192 }
193 
194 } // namespace GpgFrontend::Module
Definition: GlobalRegisterTable.h:43
Definition: ModuleManager.cpp:48
Definition: ModuleManager.h:63
Definition: GpgFunctionObject.h:57
static auto GetInstance(int channel=GpgFrontend::kGpgFrontendDefaultChannel) -> ModuleManager &
Get the Instance object.
Definition: GpgFunctionObject.h:79
Definition: TaskRunner.h:37
Definition: Event.cpp:33
auto UpsertRTValue(const QString &namespace_, const QString &key, const std::any &value) -> bool
Definition: ModuleManager.cpp:212
auto ListRTChildKeys(const QString &namespace_, const QString &key) -> std::vector< Key >
Definition: ModuleManager.cpp:223
auto IsModuleActivate(ModuleIdentifier id) -> bool
Definition: ModuleManager.cpp:208
auto ListenRTPublishEvent(QObject *o, Namespace n, Key k, LPCallback c) -> bool
Definition: ModuleManager.cpp:218
Definition: ModuleManager.h:39