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:
275  SingletonFunctionObject() = default;
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
GpgFrontend::ChannelObject::channel_
int channel_
The channel id.
Definition: GpgFunctionObject.h:77
GpgFrontend::SingletonFunctionObject
Definition: GpgFunctionObject.h:148
GpgFrontend::ChannelObject::GetChannel
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.cpp:43
GpgFrontend::SingletonStorageCollection::GetSingletonStorage
SingletonStorage * GetSingletonStorage(const std::type_info &)
Get the Singleton Storage object.
Definition: GpgFunctionObject.cpp:99
GpgFrontend
Definition: CoreCommonUtil.cpp:29
GpgFrontend::SingletonFunctionObject::GetInstance
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:170
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:282
GpgFrontend::SingletonFunctionObject::operator=
SingletonFunctionObject & operator=(const SingletonFunctionObject< T > &)=delete
prohibit copy
GpgFrontend::ChannelObject::SetChannel
void SetChannel(int channel)
Set the Channel object.
Definition: GpgFunctionObject.cpp:39
GpgFrontend::SingletonFunctionObject::GetChannel
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.h:239
GpgFrontend::SingletonFunctionObject::~SingletonFunctionObject
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
GpgFrontend::SingletonStorage::SetObjectInChannel
ChannelObject * SetObjectInChannel(int channel, std::unique_ptr< ChannelObject > p_obj)
Set a new object in channel object.
Definition: GpgFunctionObject.cpp:79
GpgFrontend::SingletonStorage::instances_map_
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:118
GpgFrontend::SingletonStorageCollection::storages_mutex_
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:139
GpgFrontend::SingletonStorageCollection
Definition: GpgFunctionObject.h:121
GpgFrontend::SingletonStorage::FindObjectInChannel
ChannelObject * FindObjectInChannel(int channel)
Definition: GpgFunctionObject.cpp:56
GpgFrontend::SingletonStorageCollection::GetInstance
static SingletonStorageCollection * GetInstance()
Get the Instance object.
Definition: GpgFunctionObject.cpp:125
GpgFrontend::SingletonFunctionObject::GetAllChannelId
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:246
GpgFrontend::ChannelObject::ChannelObject
ChannelObject() noexcept
Construct a new Default Channel Object object.
GpgFrontend::SingletonFunctionObject::CreateInstance
static T & CreateInstance(int channel, std::function< std::unique_ptr< ChannelObject >(void)> factory)
Create a Instance object.
Definition: GpgFunctionObject.h:196
GpgFrontend::ChannelObject::GetDefaultChannel
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.cpp:45
GpgFrontend::ChannelObject
object which in channel system
Definition: GpgFunctionObject.h:40
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject()=default
Construct a new Singleton Function Object object.
GpgFrontend::SingletonStorage::ReleaseChannel
void ReleaseChannel(int channel)
Definition: GpgFunctionObject.cpp:47
GpgFrontend::SingletonFunctionObject::GetDefaultChannel
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:232
GpgFrontend::SingletonStorage
Definition: GpgFunctionObject.h:81
GpgFrontend::SingletonStorage::instances_mutex_
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:116
GpgFrontend::SingletonStorage::GetAllChannelId
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:71
GpgFrontend::SingletonFunctionObject::ReleaseChannel
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:221