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 
52 using EventRefrernce = std::shared_ptr<Event>;
53 using ModuleIdentifier = QString;
54 using ModulePtr = std::shared_ptr<Module>;
55 using ModuleMangerPtr = std::shared_ptr<ModuleManager>;
56 using GMCPtr = std::shared_ptr<GlobalModuleContext>;
57 using Namespace = QString;
58 using Key = QString;
59 using LPCallback = std::function<void(Namespace, Key, int, std::any)>;
60 
61 class GPGFRONTEND_CORE_EXPORT ModuleManager
62  : public SingletonFunctionObject<ModuleManager> {
63  public:
64  explicit ModuleManager(int channel);
65 
66  virtual ~ModuleManager() override;
67 
68  void RegisterModule(ModulePtr);
69 
70  auto IsModuleActivated(ModuleIdentifier) -> bool;
71 
72  void TriggerEvent(EventRefrernce);
73 
74  void ActiveModule(ModuleIdentifier);
75 
76  void DeactiveModule(ModuleIdentifier);
77 
78  auto GetTaskRunner(ModuleIdentifier) -> std::optional<TaskRunnerPtr>;
79 
80  auto UpsertRTValue(Namespace, Key, std::any) -> bool;
81 
82  auto RetrieveRTValue(Namespace, Key) -> std::optional<std::any>;
83 
84  auto ListenRTPublish(QObject*, Namespace, Key, LPCallback) -> bool;
85 
86  auto ListRTChildKeys(const QString&, const QString&) -> std::vector<Key>;
87 
88  private:
89  class Impl;
90  SecureUniquePtr<Impl> p_;
91 };
92 
93 template <typename T, typename... Args>
94 void RegisterModule(Args&&... args) {
95  ModuleManager::GetInstance().RegisterModule(
96  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...));
97 }
98 
99 template <typename T, typename... Args>
100 void RegisterAndActivateModule(Args&&... args) {
101  auto& manager = ModuleManager::GetInstance();
102  auto module =
103  GpgFrontend::SecureCreateSharedObject<T>(std::forward<Args>(args)...);
104  manager.RegisterModule(module);
105  manager.ActiveModule(module->GetModuleIdentifier());
106 }
107 
108 template <typename... Args>
109 void TriggerEvent(const EventIdentifier& event_id, Args&&... args,
110  Event::EventCallback e_cb = nullptr) {
111  ModuleManager::GetInstance().TriggerEvent(
112  std::move(MakeEvent(event_id, std::forward<Args>(args)..., e_cb)));
113 }
114 
121 auto GPGFRONTEND_CORE_EXPORT IsModuleAcivate(ModuleIdentifier) -> bool;
122 
132 auto GPGFRONTEND_CORE_EXPORT UpsertRTValue(const QString& namespace_,
133  const QString& key,
134  const std::any& value) -> bool;
135 
142 auto GPGFRONTEND_CORE_EXPORT ListenRTPublishEvent(QObject*, Namespace, Key,
143  LPCallback) -> bool;
144 
152 auto GPGFRONTEND_CORE_EXPORT ListRTChildKeys(const QString& namespace_,
153  const QString& key)
154  -> std::vector<Key>;
155 
156 template <typename T>
157 auto RetrieveRTValueTyped(const QString& namespace_, const QString& key)
158  -> std::optional<T> {
159  auto any_value =
160  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
161  if (any_value && any_value->type() == typeid(T)) {
162  return std::any_cast<T>(*any_value);
163  }
164  return std::nullopt;
165 }
166 
167 template <typename T>
168 auto RetrieveRTValueTypedOrDefault(const QString& namespace_,
169  const QString& key, const T& defaultValue)
170  -> T {
171  auto any_value =
172  ModuleManager::GetInstance().RetrieveRTValue(namespace_, key);
173  if (any_value && any_value->type() == typeid(T)) {
174  return std::any_cast<T>(*any_value);
175  }
176  return defaultValue;
177 }
178 
179 } // namespace GpgFrontend::Module
Definition: ModuleManager.cpp:45
Definition: ModuleManager.h:62
Definition: GpgFunctionObject.h:58
static auto GetInstance(int channel=GpgFrontend::kGpgFrontendDefaultChannel) -> ModuleManager &
Get the Instance object.
Definition: GpgFunctionObject.h:80
Definition: TaskRunner.h:37
Definition: Event.cpp:31
auto UpsertRTValue(const QString &namespace_, const QString &key, const std::any &value) -> bool
Definition: ModuleManager.cpp:122
auto ListRTChildKeys(const QString &namespace_, const QString &key) -> std::vector< Key >
Definition: ModuleManager.cpp:133
auto IsModuleAcivate(ModuleIdentifier id) -> bool
Definition: ModuleManager.cpp:118
auto ListenRTPublishEvent(QObject *o, Namespace n, Key k, LPCallback c) -> bool
Definition: ModuleManager.cpp:128
Definition: ModuleManager.h:39