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 <map>
33 #include <memory>
34 #include <mutex>
35 #include <shared_mutex>
36 #include <stdexcept>
37 #include <string>
38 #include <typeinfo>
39 #include <utility>
40 #include <vector>
41 
42 #include "GpgConstants.h"
43 #include "easylogging++.h"
44 
45 namespace GpgFrontend {
46 
51 class GPGFRONTEND_CORE_EXPORT ChannelObject {
52  public:
57  ChannelObject() noexcept;
58 
64  ChannelObject(int channel);
65 
71  static int GetDefaultChannel();
72 
78  [[nodiscard]] int GetChannel() const;
79 
85  void SetChannel(int channel);
86 
87  private:
88  int channel_ = _default_channel;
89  static constexpr int _default_channel = 0;
90 };
91 
92 class GPGFRONTEND_CORE_EXPORT SingletonStorage {
93  public:
99  void ReleaseChannel(int channel);
100 
107  ChannelObject* FindObjectInChannel(int channel);
108 
114  std::vector<int> GetAllChannelId();
115 
123  ChannelObject* SetObjectInChannel(int channel,
124  std::unique_ptr<ChannelObject> p_obj);
125 
126  private:
127  std::shared_mutex instances_mutex_;
128  std::map<int, std::unique_ptr<ChannelObject>>
130 };
131 
132 class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection {
133  public:
139  static SingletonStorageCollection* GetInstance();
140 
147  SingletonStorage* GetSingletonStorage(const std::type_info&);
148 
149  private:
150  std::shared_mutex storages_mutex_;
151  std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_;
152 };
158 template <typename T>
160  public:
166 
173  delete;
174 
181  static T& GetInstance(
182  int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
183  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
184  "T not derived from SingletonFunctionObject<T>");
185 
186  auto p_storage =
188  typeid(T));
189 
190  auto* _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
191 
192  LOG(INFO) << "object address" << _p_pbj;
193 
194  if (_p_pbj == nullptr) {
195  auto new_obj = std::unique_ptr<ChannelObject>(new T(channel));
196  LOG(INFO) << "create new object";
197  return *(T*)(p_storage->SetObjectInChannel(channel, std::move(new_obj)));
198  } else
199  return *_p_pbj;
200  }
201 
209  static T& CreateInstance(
210  int channel,
211  std::function<std::unique_ptr<ChannelObject>(void)> factory) {
212  static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
213  "T not derived from SingletonFunctionObject<T>");
214 
215  auto p_storage =
217  typeid(T));
218 
219  auto _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
220 
221  if (_p_pbj == nullptr) {
222  return *(
223  T*)(p_storage->SetObjectInChannel(channel, std::move(factory())));
224  } else
225  return *_p_pbj;
226  }
227 
234  static void ReleaseChannel(int channel) {
236  ->GetSingletonStorage(typeid(T))
237  ->ReleaseChannel(channel);
238  }
239 
246 
252  [[nodiscard]] int GetChannel() const { return ChannelObject::GetChannel(); }
253 
259  static std::vector<int> GetAllChannelId() {
261  ->GetSingletonStorage(typeid(T))
262  ->GetAllChannelId();
263  }
264 
269  SingletonFunctionObject(T&&) = delete;
270 
275  SingletonFunctionObject(const T&) = delete;
276 
281  void operator=(const T&) = delete;
282 
283  protected:
288  SingletonFunctionObject() = default;
289 
295  explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {
296  LOG(INFO) << "called"
297  << "channel:" << channel;
298  }
299 
304  virtual ~SingletonFunctionObject() = default;
305 };
306 } // namespace GpgFrontend
307 
308 #endif // GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
GpgFrontend::ChannelObject::channel_
int channel_
The channel id.
Definition: GpgFunctionObject.h:88
GpgFrontend::SingletonFunctionObject
Definition: GpgFunctionObject.h:159
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:102
GpgFrontend
Definition: CoreCommonUtil.cpp:29
GpgFrontend::SingletonFunctionObject::GetInstance
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:181
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:295
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:252
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:82
GpgFrontend::SingletonStorage::instances_map_
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:129
GpgFrontend::SingletonStorageCollection::storages_mutex_
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:150
GpgFrontend::SingletonStorageCollection
Definition: GpgFunctionObject.h:132
GpgFrontend::SingletonStorage::FindObjectInChannel
ChannelObject * FindObjectInChannel(int channel)
Definition: GpgFunctionObject.cpp:57
GpgFrontend::SingletonStorageCollection::GetInstance
static SingletonStorageCollection * GetInstance()
Get the Instance object.
Definition: GpgFunctionObject.cpp:130
GpgFrontend::SingletonFunctionObject::GetAllChannelId
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:259
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:209
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:51
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:245
GpgFrontend::SingletonStorage
Definition: GpgFunctionObject.h:92
GpgFrontend::SingletonStorage::instances_mutex_
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:127
GpgFrontend::SingletonStorage::GetAllChannelId
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:74
GpgFrontend::SingletonFunctionObject::ReleaseChannel
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:234