GpgFunctionObject.h
#ifndef GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
#define GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
#include "GpgConstants.h"
namespace GpgFrontend {
class GPGFRONTEND_CORE_EXPORT ChannelObject {
public:
ChannelObject() noexcept;
ChannelObject(int channel);
static int GetDefaultChannel();
[[nodiscard]] int GetChannel() const;
void SetChannel(int channel);
private:
int channel_ = _default_channel;
static constexpr int _default_channel = 0;
};
class GPGFRONTEND_CORE_EXPORT SingletonStorage {
public:
void ReleaseChannel(int channel);
ChannelObject* FindObjectInChannel(int channel);
std::vector<int> GetAllChannelId();
ChannelObject* SetObjectInChannel(int channel,
std::unique_ptr<ChannelObject> p_obj);
private:
std::shared_mutex instances_mutex_;
std::map<int, std::unique_ptr<ChannelObject>>
instances_map_;
};
class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection {
public:
static SingletonStorageCollection* GetInstance();
SingletonStorage* GetSingletonStorage(const std::type_info&);
private:
std::shared_mutex storages_mutex_;
std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_;
};
template <typename T>
class SingletonFunctionObject : public ChannelObject {
public:
SingletonFunctionObject(const SingletonFunctionObject<T>&) = delete;
SingletonFunctionObject& operator=(const SingletonFunctionObject<T>&) =
delete;
static T& GetInstance(
int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
"T not derived from SingletonFunctionObject<T>");
auto p_storage =
SingletonStorageCollection::GetInstance()->GetSingletonStorage(
typeid(T));
auto* _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
if (_p_pbj == nullptr) {
auto new_obj = std::unique_ptr<ChannelObject>(new T(channel));
return *(T*)(p_storage->SetObjectInChannel(channel, std::move(new_obj)));
} else {
return *_p_pbj;
}
}
static T& CreateInstance(
int channel,
std::function<std::unique_ptr<ChannelObject>(void)> factory) {
static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
"T not derived from SingletonFunctionObject<T>");
auto p_storage =
SingletonStorageCollection::GetInstance()->GetSingletonStorage(
typeid(T));
auto _p_pbj = (T*)(p_storage->FindObjectInChannel(channel));
if (_p_pbj == nullptr) {
return *(
T*)(p_storage->SetObjectInChannel(channel, std::move(factory())));
} else
return *_p_pbj;
}
static void ReleaseChannel(int channel) {
SingletonStorageCollection::GetInstance()
->GetSingletonStorage(typeid(T))
->ReleaseChannel(channel);
}
static int GetDefaultChannel() { return ChannelObject::GetDefaultChannel(); }
[[nodiscard]] int GetChannel() const { return ChannelObject::GetChannel(); }
static std::vector<int> GetAllChannelId() {
return SingletonStorageCollection::GetInstance()
->GetSingletonStorage(typeid(T))
->GetAllChannelId();
}
SingletonFunctionObject(T&&) = delete;
SingletonFunctionObject(const T&) = delete;
void operator=(const T&) = delete;
protected:
SingletonFunctionObject() = default;
explicit SingletonFunctionObject(int channel) : ChannelObject(channel) {}
virtual ~SingletonFunctionObject() = default;
};
} // namespace GpgFrontend
#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