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 EventReference = 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, bool) -> 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  auto IsIntegratedModule(ModuleIdentifier) -> bool;
80 
81  void ListenEvent(ModuleIdentifier, EventIdentifier);
82 
83  void TriggerEvent(EventReference);
84 
85  auto SearchEvent(EventTriggerIdentifier) -> std::optional<EventReference>;
86 
87  auto GetModuleListening(ModuleIdentifier) -> QList<EventIdentifier>;
88 
89  void ActiveModule(ModuleIdentifier);
90 
91  void DeactiveModule(ModuleIdentifier);
92 
93  auto GetTaskRunner(ModuleIdentifier) -> std::optional<TaskRunnerPtr>;
94 
95  auto UpsertRTValue(Namespace, Key, std::any) -> bool;
96 
97  auto RetrieveRTValue(Namespace, Key) -> std::optional<std::any>;
98 
99  auto ListenRTPublish(QObject*, Namespace, Key, LPCallback) -> bool;
100 
101  auto ListRTChildKeys(const QString&, const QString&) -> std::vector<Key>;
102 
103  auto GRT() -> GlobalRegisterTable*;
104 
105  private:
106  class Impl;
107  SecureUniquePtr<Impl> p_;
108 };
109 
110 template <typename T, typename... Args>
111 void RegisterModule(Args&&... args) {
112  ModuleManager::GetInstance().RegisterModule(
113  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...));
114 }
115 
116 template <typename T, typename... Args>
117 void RegisterAndActivateModule(Args&&... args) {
118  auto& manager = ModuleManager::GetInstance();
119  auto module =
120  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...);
121  manager.RegisterModule(module);
122  manager.ActiveModule(module->GetModuleIdentifier());
123 }
124 
125 template <typename... Args>
126 void TriggerEvent(const EventIdentifier& event_id, Args&&... args,
127  Event::EventCallback e_cb = nullptr) {
128  ModuleManager::GetInstance().TriggerEvent(
129  std::move(MakeEvent(event_id, std::forward<Args>(args)..., e_cb)));
130 }
131 
138 auto GPGFRONTEND_CORE_EXPORT IsModuleActivate(ModuleIdentifier) -> bool;
139 
149 auto GPGFRONTEND_CORE_EXPORT UpsertRTValue(const QString& namespace_,
150  const QString& key,
151  const std::any& value) -> bool;
152 
159 auto GPGFRONTEND_CORE_EXPORT ListenRTPublishEvent(QObject*, Namespace, Key,
160  LPCallback) -> bool;
161 
169 auto GPGFRONTEND_CORE_EXPORT ListRTChildKeys(const QString& namespace_,
170  const QString& key)
171  -> std::vector<Key>;
172 
173 template <typename T>
174 auto RetrieveRTValueTyped(const QString& namespace_, const QString& key)
175  -> std::optional<T> {
176  auto any_value =
177  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
178  if (any_value && any_value->type() == typeid(T)) {
179  return std::any_cast<T>(*any_value);
180  }
181  return std::nullopt;
182 }
183 
184 template <typename T>
185 auto RetrieveRTValueTypedOrDefault(const QString& namespace_,
186  const QString& key, const T& defaultValue)
187  -> T {
188  auto any_value =
189  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
190  if (any_value && any_value->type() == typeid(T)) {
191  return std::any_cast<T>(*any_value);
192  }
193  return defaultValue;
194 }
195 
196 } // namespace GpgFrontend::Module
Definition: GlobalRegisterTable.h:43
Definition: ModuleManager.cpp:47
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:228
auto ListRTChildKeys(const QString &namespace_, const QString &key) -> std::vector< Key >
Definition: ModuleManager.cpp:239
auto IsModuleActivate(ModuleIdentifier id) -> bool
Definition: ModuleManager.cpp:224
auto ListenRTPublishEvent(QObject *o, Namespace n, Key k, LPCallback c) -> bool
Definition: ModuleManager.cpp:234
Definition: ModuleManager.h:39