GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
GpgFunctionObject.h
1 
29 #ifndef GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
30 #define GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
31 
32 #include "GpgConstants.h"
33 
34 namespace GpgFrontend {
35 
40 class GPGFRONTEND_CORE_EXPORT ChannelObject {
41  public:
46  ChannelObject() noexcept;
47 
53  ChannelObject(int channel);
54 
60  static int GetDefaultChannel();
61 
67  [[nodiscard]] int GetChannel() const;
68 
74  void SetChannel(int channel);
75 
76  private:
77  int channel_ = _default_channel;
78  static constexpr int _default_channel = 0;
79 };
80 
81 class GPGFRONTEND_CORE_EXPORT SingletonStorage {
82  public:
88  void ReleaseChannel(int channel);
89 
96  ChannelObject* FindObjectInChannel(int channel);
97 
103  std::vector<int> GetAllChannelId();
104 
112  ChannelObject* SetObjectInChannel(int channel,
113  std::unique_ptr<ChannelObject> p_obj);
114 
115  private:
116  std::shared_mutex instances_mutex_;
117  std::map<int, std::unique_ptr<ChannelObject>>
119 };
120 
121 class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection {
122  public:
128  static SingletonStorageCollection* GetInstance();
129 
136  SingletonStorage* GetSingletonStorage(const std::type_info&);
137 
138  private:
139  std::shared_mutex storages_mutex_;
140  std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_;
141 };
147 template <typename T>
149  public:
155 
162  delete;
163 
170  static T& GetInstance(
171  int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
172  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
173  "T not derived from SingletonFunctionObject<T>");
174 
175  auto p_storage =
177  typeid(T));
178 
179  auto* _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
180 
181  if (_p_pbj == nullptr) {
182  auto new_obj = std::unique_ptr<ChannelObject>(new T(channel));
183  return *(T*)(p_storage->SetObjectInChannel(channel, std::move(new_obj)));
184  } else {
185  return *_p_pbj;
186  }
187  }
188 
196  static T& CreateInstance(
197  int channel,
198  std::function<std::unique_ptr<ChannelObject>(void)> factory) {
199  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
200  "T not derived from SingletonFunctionObject<T>");
201 
202  auto p_storage =
204  typeid(T));
205 
206  auto _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
207 
208  if (_p_pbj == nullptr) {
209  return *(
210  T*)(p_storage->SetObjectInChannel(channel, std::move(factory())));
211  } else
212  return *_p_pbj;
213  }
214 
221  static void ReleaseChannel(int channel) {
223  ->GetSingletonStorage(typeid(T))
224  ->ReleaseChannel(channel);
225  }
226 
233 
239  [[nodiscard]] int GetChannel() const { return ChannelObject::GetChannel(); }
240 
246  static std::vector<int> GetAllChannelId() {
248  ->GetSingletonStorage(typeid(T))
249  ->GetAllChannelId();
250  }
251 
256  SingletonFunctionObject(T&&) = delete;
257 
262  SingletonFunctionObject(const T&) = delete;
263 
268  void operator=(const T&) = delete;
269 
270  protected:
276 
282  explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {}
283 
288  virtual ~SingletonFunctionObject() = default;
289 };
290 } // namespace GpgFrontend
291 
292 #endif // GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
object which in channel system
Definition: GpgFunctionObject.h:40
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.cpp:43
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.cpp:45
ChannelObject() noexcept
Construct a new Default Channel Object object.
Definition: GpgFunctionObject.h:148
SingletonFunctionObject()=default
Construct a new Singleton Function Object object.
static T & CreateInstance(int channel, std::function< std::unique_ptr< ChannelObject >(void)> factory)
Create a Instance object.
Definition: GpgFunctionObject.h:196
SingletonFunctionObject(const SingletonFunctionObject< T > &)=delete
prohibit copy
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:282
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:232
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:246
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:170
SingletonFunctionObject(const T &)=delete
Construct a new Singleton Function Object object.
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.h:239
SingletonFunctionObject & operator=(const SingletonFunctionObject< T > &)=delete
prohibit copy
SingletonFunctionObject(T &&)=delete
Construct a new Singleton Function Object object.
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:221
Definition: GpgFunctionObject.h:121
static SingletonStorageCollection * GetInstance()
Get the Instance object.
Definition: GpgFunctionObject.cpp:125
SingletonStorage * GetSingletonStorage(const std::type_info &)
Get the Singleton Storage object.
Definition: GpgFunctionObject.cpp:99
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:139
Definition: GpgFunctionObject.h:81
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:116
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:71
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:118
void ReleaseChannel(int channel)
Definition: GpgFunctionObject.cpp:47
Executive files related to the basic operations that are provided by GpgBasicOperator.
Definition: CoreCommonUtil.cpp:29