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 <mutex>
33 
34 #include "GpgConstants.h"
35 
36 namespace GpgFrontend {
37 
42 class GPGFRONTEND_CORE_EXPORT ChannelObject {
43  public:
48  ChannelObject() noexcept;
49 
55  ChannelObject(int channel);
56 
62  static int GetDefaultChannel();
63 
69  [[nodiscard]] int GetChannel() const;
70 
76  void SetChannel(int channel);
77 
78  private:
79  int channel_ = _default_channel;
80  static constexpr int _default_channel = 0;
81 };
82 
83 class GPGFRONTEND_CORE_EXPORT SingletonStorage {
84  public:
90  void ReleaseChannel(int channel);
91 
98  ChannelObject* FindObjectInChannel(int channel);
99 
105  std::vector<int> GetAllChannelId();
106 
114  ChannelObject* SetObjectInChannel(int channel,
115  std::unique_ptr<ChannelObject> p_obj);
116 
117  private:
118  std::shared_mutex instances_mutex_;
119  std::map<int, std::unique_ptr<ChannelObject>>
121 };
122 
123 class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection {
124  public:
130  static SingletonStorageCollection* GetInstance(bool force_refresh);
131 
138  SingletonStorage* GetSingletonStorage(const std::type_info&);
139 
140  private:
141  std::shared_mutex storages_mutex_;
142  std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_;
143 };
149 template <typename T>
151  public:
157 
164  delete;
165 
172  static T& GetInstance(
173  int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
174  static std::mutex g_channel_mutex_map_lock;
175  static std::map<int, std::mutex> g_channel_mutex_map;
176 
177  {
178  std::lock_guard<std::mutex> guard(g_channel_mutex_map_lock);
179  if (g_channel_mutex_map.find(channel) == g_channel_mutex_map.end()) {
180  g_channel_mutex_map[channel];
181  }
182  }
183 
184  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
185  "T not derived from SingletonFunctionObject<T>");
186 
187  auto* p_storage =
189  typeid(T));
190  auto* _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
191 
192  if (_p_pbj == nullptr) {
193  // lock this channel
194  std::lock_guard<std::mutex> guard(g_channel_mutex_map[channel]);
195 
196  // double check
197  if ((_p_pbj = (T*)(p_storage->FindObjectInChannel(channel))) != nullptr)
198  return *_p_pbj;
199 
200  // do create object of this channel
201  auto new_obj = std::unique_ptr<ChannelObject>(new T(channel));
202  return *(T*)(p_storage->SetObjectInChannel(channel, std::move(new_obj)));
203  } else {
204  return *_p_pbj;
205  }
206  }
207 
215  static T& CreateInstance(
216  int channel,
217  std::function<std::unique_ptr<ChannelObject>(void)> factory) {
218  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
219  "T not derived from SingletonFunctionObject<T>");
220 
221  auto p_storage =
223  typeid(T));
224 
225  auto _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
226 
227  if (_p_pbj == nullptr) {
228  return *(
229  T*)(p_storage->SetObjectInChannel(channel, std::move(factory())));
230  } else
231  return *_p_pbj;
232  }
233 
240  static void ReleaseChannel(int channel) {
242  ->GetSingletonStorage(typeid(T))
243  ->ReleaseChannel(channel);
244  }
245 
252 
258  [[nodiscard]] int GetChannel() const { return ChannelObject::GetChannel(); }
259 
265  static std::vector<int> GetAllChannelId() {
267  ->GetSingletonStorage(typeid(T))
268  ->GetAllChannelId();
269  }
270 
275  SingletonFunctionObject(T&&) = delete;
276 
281  SingletonFunctionObject(const T&) = delete;
282 
287  void operator=(const T&) = delete;
288 
289  protected:
295 
301  explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {}
302 
307  virtual ~SingletonFunctionObject() = default;
308 };
309 } // namespace GpgFrontend
310 
311 #endif // GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
object which in channel system
Definition: GpgFunctionObject.h:42
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.cpp:41
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.cpp:43
ChannelObject() noexcept
Construct a new Default Channel Object object.
Definition: GpgFunctionObject.h:150
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:215
SingletonFunctionObject(const SingletonFunctionObject< T > &)=delete
prohibit copy
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:301
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:251
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:265
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:172
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:258
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:240
Definition: GpgFunctionObject.h:123
SingletonStorage * GetSingletonStorage(const std::type_info &)
Get the Singleton Storage object.
Definition: GpgFunctionObject.cpp:97
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:141
static SingletonStorageCollection * GetInstance(bool force_refresh)
Get the Instance object.
Definition: GpgFunctionObject.cpp:122
Definition: GpgFunctionObject.h:83
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:118
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:69
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:120
void ReleaseChannel(int channel)
Definition: GpgFunctionObject.cpp:45
Executive files related to the basic operations that are provided by GpgBasicOperator.
Definition: CoreCommonUtil.cpp:31