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) {
60 "T not derived from SingletonFunctionObject<T>");
63 if (_p_pbj ==
nullptr)
77 std::function<std::unique_ptr<T>(
void)> factory) {
79 "T not derived from SingletonFunctionObject<T>");
82 if (_p_pbj ==
nullptr)
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)});
229 template <
typename T>
230 int SingletonFunctionObject<T>::_default_channel =
231 GpgFrontend::GPGFRONTEND_DEFAULT_CHANNEL;
233 template <
typename T>
234 std::mutex SingletonFunctionObject<T>::_instance_mutex;
236 template <
typename T>
237 std::shared_mutex SingletonFunctionObject<T>::_instances_mutex;
239 template <
typename T>
240 std::unique_ptr<T> SingletonFunctionObject<T>::_instance =
nullptr;
242 template <
typename T>
243 std::map<int, std::unique_ptr<T>> SingletonFunctionObject<T>::_instances_map;
247 #endif // GPGFRONTEND_ZH_CN_TS_FUNCTIONOBJECT_H