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 #include <stdexcept>
33 
34 #include "core/GpgFrontendCoreExport.h"
35 #include "core/function/basic/ChannelObject.h"
36 #include "core/function/basic/SingletonStorage.h"
37 #include "core/function/basic/SingletonStorageCollection.h"
38 #include "core/utils/MemoryUtils.h"
39 
40 namespace GpgFrontend {
41 
42 auto GPGFRONTEND_CORE_EXPORT GetChannelObjectInstance(
43  const std::type_info& type, int channel) -> ChannelObject*;
44 
45 auto GPGFRONTEND_CORE_EXPORT CreateChannelObjectInstance(
46  const std::type_info& type, int channel,
47  SecureUniquePtr<ChannelObject> channel_object) -> ChannelObject*;
48 
49 auto GPGFRONTEND_CORE_EXPORT
50 GetGlobalFunctionObjectTypeLock(const std::type_info& type) -> std::mutex&;
51 
57 template <typename T>
59  public:
65 
72  -> SingletonFunctionObject& = delete;
73 
80  static auto GetInstance(int channel = GpgFrontend::kGpgFrontendDefaultChannel)
81  -> T& {
82  static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
83  "T not derived from SingletonFunctionObject<T>");
84 
85  const auto& type = typeid(T);
86  std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
87  auto* channel_object = GetChannelObjectInstance(type, channel);
88  if (channel_object == nullptr) {
89  channel_object = CreateChannelObjectInstance(
90  type, channel,
91  ConvertToChannelObjectPtr(SecureCreateUniqueObject<T>(channel)));
92  }
93  return *static_cast<T*>(channel_object);
94  }
95 
103  static auto CreateInstance(
104  int channel, const std::function<ChannelObjectPtr(void)>& factory) -> T& {
105  static_assert(std::is_base_of_v<SingletonFunctionObject<T>, T>,
106  "T not derived from SingletonFunctionObject<T>");
107 
108  const auto& type = typeid(T);
109  std::lock_guard<std::mutex> guard(GetGlobalFunctionObjectTypeLock(type));
110  return *static_cast<T*>(
111  CreateChannelObjectInstance(type, channel, factory()));
112  }
113 
120  static void ReleaseChannel(int channel) {
122  ->GetSingletonStorage(typeid(T))
123  ->ReleaseChannel(channel);
124  }
125 
131  static auto GetDefaultChannel() -> int {
133  }
134 
140  [[nodiscard]] auto GetChannel() const -> int {
141  return ChannelObject::GetChannel();
142  }
143 
149  static auto GetAllChannelId() -> std::vector<int> {
151  ->GetSingletonStorage(typeid(T))
152  ->GetAllChannelId();
153  }
154 
159  SingletonFunctionObject(T&&) = delete;
160 
165  SingletonFunctionObject(const T&) = delete;
166 
171  void operator=(const T&) = delete;
172 
173  protected:
179 
185  explicit SingletonFunctionObject(int channel)
186  : ChannelObject(channel, typeid(T).name()) {}
187 
192  virtual ~SingletonFunctionObject() = default;
193 };
194 } // 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:58
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:185
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:140
auto operator=(const SingletonFunctionObject< T > &) -> SingletonFunctionObject &=delete
prohibit copy
static auto GetAllChannelId() -> std::vector< int >
Get all the channel ids.
Definition: GpgFunctionObject.h:149
SingletonFunctionObject(T &&)=delete
Construct a new Singleton Function Object object.
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:120
static auto GetInstance(int channel=GpgFrontend::kGpgFrontendDefaultChannel) -> T &
Get the Instance object.
Definition: GpgFunctionObject.h:80
static auto GetDefaultChannel() -> int
Get the Default Channel object.
Definition: GpgFunctionObject.h:131
static auto CreateInstance(int channel, const std::function< ChannelObjectPtr(void)> &factory) -> T &
Create a Instance object.
Definition: GpgFunctionObject.h:103
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