29#ifndef GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
30#define GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H
35#include <shared_mutex>
39#include "GpgConstants.h"
58 int channel = GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL) {
59 static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
60 "T not derived from SingletonFunctionObject<T>");
63 if (_p_pbj ==
nullptr)
77 std::function<std::unique_ptr<T>(
void)> factory) {
78 static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
79 "T not derived from SingletonFunctionObject<T>");
82 if (_p_pbj ==
nullptr)
96 static_assert(std::is_base_of<SingletonFunctionObject<T>, T>::value,
97 "T not derived from SingletonFunctionObject<T>");
100 if (_p_pbj ==
nullptr)
113 decltype(_instances_map.end()) _it;
115 std::shared_lock lock(_instances_mutex);
116 _it = _instances_map.find(channel);
118 if (_it != _instances_map.end()) _instances_map.erase(_it);
119 DLOG(INFO) <<
"channel" << channel <<
"released";
152 void operator=(
const T&) =
delete;
182 int channel_ = _default_channel;
183 static int _default_channel;
184 static std::mutex _instance_mutex;
185 static std::shared_mutex _instances_mutex;
186 static std::unique_ptr<T> _instance;
187 static std::map<int, std::unique_ptr<T>> _instances_map;
197 decltype(_instances_map.end()) _it;
199 std::shared_lock lock(_instances_mutex);
200 _it = _instances_map.find(channel);
201 if (_it == _instances_map.end())
204 return _it->second.get();
217 if (p_obj ==
nullptr) p_obj = std::make_unique<T>();
218 T* obj = p_obj.get();
219 obj->SetChannel(channel);
221 std::unique_lock lock(_instances_mutex);
222 _instances_map.insert({channel, std::move(p_obj)});
230int SingletonFunctionObject<T>::_default_channel =
231 GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL;
234std::mutex SingletonFunctionObject<T>::_instance_mutex;
237std::shared_mutex SingletonFunctionObject<T>::_instances_mutex;
240std::unique_ptr<T> SingletonFunctionObject<T>::_instance =
nullptr;
243std::map<int, std::unique_ptr<T>> SingletonFunctionObject<T>::_instances_map;
Definition: GpgFunctionObject.h:49
SingletonFunctionObject()=default
Construct a new Singleton Function Object object.
static T & CreateInstance(int channel, std::unique_ptr< T > p_obj=nullptr)
Create a Instance object.
Definition: GpgFunctionObject.h:95
static T & CreateInstance(int channel, std::function< std::unique_ptr< T >(void)> factory)
Create a Instance object.
Definition: GpgFunctionObject.h:76
SingletonFunctionObject(int channel)
Construct a new Singleton Function Object object.
Definition: GpgFunctionObject.h:166
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:127
static T * set_object_in_channel(int channel, std::unique_ptr< T > p_obj)
Set the object in channel object.
Definition: GpgFunctionObject.h:215
void SetChannel(int channel)
Set the Channel object.
Definition: GpgFunctionObject.h:179
SingletonFunctionObject(const T &)=delete
Construct a new Singleton Function Object object.
static T * find_object_in_channel(int channel)
Definition: GpgFunctionObject.h:195
virtual ~SingletonFunctionObject()=default
Destroy the Singleton Function Object object.
static T & GetInstance(int channel=GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL)
Get the Instance object.
Definition: GpgFunctionObject.h:57
int GetChannel() const
Get the Channel object.
Definition: GpgFunctionObject.h:134
SingletonFunctionObject(T &&)=delete
Construct a new Singleton Function Object object.
static T & ReleaseChannel(int channel)
Definition: GpgFunctionObject.h:112
Definition: CoreCommonUtil.cpp:29