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>>
120 instances_map_;
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>
150 class SingletonFunctionObject : public ChannelObject {
151 public:
156 SingletonFunctionObject(const SingletonFunctionObject<T>&) = delete;
157
163 SingletonFunctionObject& operator=(const SingletonFunctionObject<T>&) =
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 =
188 SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(
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 =
222 SingletonStorageCollection::GetInstance(false)->GetSingletonStorage(
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) {
241 SingletonStorageCollection::GetInstance(false)
242 ->GetSingletonStorage(typeid(T))
243 ->ReleaseChannel(channel);
244 }
245
251 static int GetDefaultChannel() { return ChannelObject::GetDefaultChannel(); }
252
258 [[nodiscard]] int GetChannel() const { return ChannelObject::GetChannel(); }
259
265 static std::vector<int> GetAllChannelId() {
266 return SingletonStorageCollection::GetInstance(false)
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:
294 SingletonFunctionObject() = default;
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
GpgFrontend::ChannelObject
object which in channel system
Definition: GpgFunctionObject.h:42
GpgFrontend::ChannelObject::GetChannel
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.cpp:41
GpgFrontend::ChannelObject::GetDefaultChannel
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.cpp:43
GpgFrontend::ChannelObject::ChannelObject
ChannelObject() noexcept
Construct a new Default Channel Object object.
GpgFrontend::SingletonFunctionObject
Definition: GpgFunctionObject.h:150
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject()=default
Construct a new Singleton Function 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:215
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(const SingletonFunctionObject< T > &)=delete
prohibit copy
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:301
GpgFrontend::SingletonFunctionObject::GetDefaultChannel
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:251
GpgFrontend::SingletonFunctionObject::GetAllChannelId
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:265
GpgFrontend::SingletonFunctionObject::GetInstance
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:172
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(const T &)=delete
Construct a new Singleton Function Object object.
GpgFrontend::SingletonFunctionObject::~SingletonFunctionObject
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
GpgFrontend::SingletonFunctionObject::GetChannel
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.h:258
GpgFrontend::SingletonFunctionObject::operator=
SingletonFunctionObject & operator=(const SingletonFunctionObject< T > &)=delete
prohibit copy
GpgFrontend::SingletonFunctionObject::SingletonFunctionObject
SingletonFunctionObject(T &&)=delete
Construct a new Singleton Function Object object.
GpgFrontend::SingletonFunctionObject::ReleaseChannel
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:240
GpgFrontend::SingletonStorageCollection
Definition: GpgFunctionObject.h:123
GpgFrontend::SingletonStorageCollection::GetSingletonStorage
SingletonStorage * GetSingletonStorage(const std::type_info &)
Get the Singleton Storage object.
Definition: GpgFunctionObject.cpp:97
GpgFrontend::SingletonStorageCollection::storages_mutex_
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:141
GpgFrontend::SingletonStorageCollection::GetInstance
static SingletonStorageCollection * GetInstance(bool force_refresh)
Get the Instance object.
Definition: GpgFunctionObject.cpp:122
GpgFrontend::SingletonStorage
Definition: GpgFunctionObject.h:83
GpgFrontend::SingletonStorage::instances_mutex_
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:118
GpgFrontend::SingletonStorage::GetAllChannelId
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:69
GpgFrontend::SingletonStorage::instances_map_
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:120
GpgFrontend::SingletonStorage::ReleaseChannel
void ReleaseChannel(int channel)
Definition: GpgFunctionObject.cpp:45
class
Executive files related to the basic operations that are provided by GpgBasicOperator.
GpgFrontend
Definition: CoreCommonUtil.cpp:31