GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
GpgFunctionObject.h
1 
29 #pragma once
30 
31 #include <mutex>
32 
33 #include "core/GpgFrontendCoreExport.h"
34 #include "core/function/basic/ChannelObject.h"
35 #include "core/function/basic/SingletonStorage.h"
36 #include "core/function/basic/SingletonStorageCollection.h"
37 #include "core/utils/MemoryUtils.h"
38 
39 namespace GpgFrontend {
40 
41 auto GPGFRONTEND_CORE_EXPORT GetChannelObjectInstance(
42  const std::type_info& type, int channel) -> ChannelObject*;
43 
44 auto GPGFRONTEND_CORE_EXPORT CreateChannelObjectInstance(
45  const std::type_info& type, int channel,
46  SecureUniquePtr<ChannelObject> channel_object) -> ChannelObject*;
47 
48 auto GPGFRONTEND_CORE_EXPORT
49 GetGlobalFunctionObjectTypeLock(const std::type_info& type) -> std::mutex&;
50 
56 template <typename T>
58  public:
64 
71  -> SingletonFunctionObject& = delete;
72 
79  static auto GetInstance(int channel = GpgFrontend::kGpgFrontendDefaultChannel)
80  -> T& {
81  static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
82  "T not derived from SingletonFunctionObject<T>");
83 
84  const auto& type = typeid(T);
85  std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
86  auto* channel_object = GetChannelObjectInstance(type, channel);
87  if (channel_object == nullptr) {
88  channel_object = CreateChannelObjectInstance(
89  type, channel,
90  ConvertToChannelObjectPtr(SecureCreateUniqueObject<T>(channel)));
91  }
92  return *static_cast<T*>(channel_object);
93  }
94 
102  static auto CreateInstance(
103  int channel, const std::function<ChannelObjectPtr(void)>& factory) -> T& {
104  static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
105  "T not derived from SingletonFunctionObject<T>");
106 
107  const auto& type = typeid(T);
108  std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
109  return *static_cast<T*>(
110  CreateChannelObjectInstance(type, channel, factory()));
111  }
112 
119  static void ReleaseChannel(int channel) {
121  ->GetSingletonStorage(typeid(T))
122  ->ReleaseChannel(channel);
123  }
124 
130  static auto GetDefaultChannel() -> int {
132  }
133 
139  [[nodiscard]] auto GetChannel() const -> int {
140  return ChannelObject::GetChannel();
141  }
142 
148  static auto GetAllChannelId() -> std::vector<int> {
150  ->GetSingletonStorage(typeid(T))
151  ->GetAllChannelId();
152  }
153 
158  SingletonFunctionObject(T&&) = delete;
159 
164  SingletonFunctionObject(const T&) = delete;
165 
170  void operator=(const T&) = delete;
171 
172  protected:
178 
184  explicit SingletonFunctionObject(int channel)
185  : ChannelObject(channel, typeid(T).name()) {}
186 
191  virtual ~SingletonFunctionObject() = default;
192 };
193 } // namespace GpgFrontend
object which in channel system is called "channel"
Definition: ChannelObject.h:39
auto GetChannel() const -> int
Get the Channel object.
Definition: ChannelObject.cpp:53
static auto GetDefaultChannel() -> int
Get the Default Channel object.
Definition: ChannelObject.cpp:55
Definition: GpgFunctionObject.h:57
SingletonFunctionObject()=default
Construct a new Singleton Function Object object.
SingletonFunctionObject(const SingletonFunctionObject< T > &)=delete
prohibit copy
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:184
SingletonFunctionObject(const T &)=delete
Construct a new Singleton Function Object object.
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
auto GetChannel() const -> int
Get the Channel object.
Definition: GpgFunctionObject.h:139
auto operator=(const SingletonFunctionObject< T > &) -> SingletonFunctionObject &=delete
prohibit copy
static auto GetAllChannelId() -> std::vector< int >
Get all the channel ids.
Definition: GpgFunctionObject.h:148
SingletonFunctionObject(T &&)=delete
Construct a new Singleton Function Object object.
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:119
static auto GetInstance(int channel=GpgFrontend::kGpgFrontendDefaultChannel) -> T &
Get the Instance object.
Definition: GpgFunctionObject.h:79
static auto GetDefaultChannel() -> int
Get the Default Channel object.
Definition: GpgFunctionObject.h:130
static auto CreateInstance(int channel, const std::function< ChannelObjectPtr(void)> &factory) -> T &
Create a Instance object.
Definition: GpgFunctionObject.h:102
static auto GetInstance(bool force_refresh) -> SingletonStorageCollection *
Get the Instance object.
Definition: SingletonStorageCollection.cpp:107
Definition: app.cpp:39
auto GetChannelObjectInstance(const std::type_info &type, int channel) -> ChannelObject *
Get the Instance object.
Definition: GpgFunctionObject.cpp:67