GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
|
29 #ifndef GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
30 #define GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
35 #include <shared_mutex>
42 #include "GpgConstants.h"
43 #include "easylogging++.h"
71 static int GetDefaultChannel();
78 [[nodiscard]]
int GetChannel()
const;
85 void SetChannel(
int channel);
88 int channel_ = _default_channel;
89 static constexpr
int _default_channel = 0;
99 void ReleaseChannel(
int channel);
114 std::vector<int> GetAllChannelId();
124 std::unique_ptr<ChannelObject> p_obj);
128 std::map<int, std::unique_ptr<ChannelObject>>
151 std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_;
158 template <
typename T>
182 int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
184 "T not derived from SingletonFunctionObject<T>");
190 auto* _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
192 LOG(INFO) <<
"object address" << _p_pbj;
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)));
211 std::function<std::unique_ptr<ChannelObject>(
void)> factory) {
213 "T not derived from SingletonFunctionObject<T>");
219 auto _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
221 if (_p_pbj ==
nullptr) {
223 T*)(p_storage->SetObjectInChannel(channel, std::move(factory())));
296 LOG(INFO) <<
"called"
297 <<
"channel:" << channel;
308 #endif // GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
int channel_
The channel id.
Definition: GpgFunctionObject.h:88
Definition: GpgFunctionObject.h:159
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.cpp:43
SingletonStorage * GetSingletonStorage(const std::type_info &)
Get the Singleton Storage object.
Definition: GpgFunctionObject.cpp:102
Definition: CoreCommonUtil.cpp:29
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:181
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:295
SingletonFunctionObject & operator=(const SingletonFunctionObject< T > &)=delete
prohibit copy
void SetChannel(int channel)
Set the Channel object.
Definition: GpgFunctionObject.cpp:39
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.h:252
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
ChannelObject * SetObjectInChannel(int channel, std::unique_ptr< ChannelObject > p_obj)
Set a new object in channel object.
Definition: GpgFunctionObject.cpp:82
std::map< int, std::unique_ptr< ChannelObject > > instances_map_
map of singleton instances
Definition: GpgFunctionObject.h:129
std::shared_mutex storages_mutex_
mutex for storages_map_
Definition: GpgFunctionObject.h:150
Definition: GpgFunctionObject.h:132
ChannelObject * FindObjectInChannel(int channel)
Definition: GpgFunctionObject.cpp:57
static SingletonStorageCollection * GetInstance()
Get the Instance object.
Definition: GpgFunctionObject.cpp:130
static std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.h:259
ChannelObject() noexcept
Construct a new Default Channel Object object.
static T & CreateInstance(int channel, std::function< std::unique_ptr< ChannelObject >(void)> factory)
Create a Instance object.
Definition: GpgFunctionObject.h:209
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.cpp:45
object which in channel system
Definition: GpgFunctionObject.h:51
SingletonFunctionObject()=default
Construct a new Singleton Function Object object.
void ReleaseChannel(int channel)
Definition: GpgFunctionObject.cpp:47
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:245
Definition: GpgFunctionObject.h:92
std::shared_mutex instances_mutex_
mutex for _instances_map
Definition: GpgFunctionObject.h:127
std::vector< int > GetAllChannelId()
Get all the channel ids.
Definition: GpgFunctionObject.cpp:74
static void ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:234