123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #ifndef MODULE_SINGLETON_BASE_H
- #define MODULE_SINGLETON_BASE_H
- #include<mutex>
- template<typename T>
- class singleton_base
- {
- public:
- static T *instance()
- {
- if(nullptr != _instance)
- {
- return _instance;
- }
- std::lock_guard<std::mutex> ll(_mutex_singleton_base);
- if(nullptr == _instance)
- {
- _instance = new(std::nothrow) T();
- }
- return _instance;
- }
- protected:
-
- singleton_base(){}
-
- private:
-
- singleton_base(const singleton_base&){}
- singleton_base &operator=(const singleton_base&){}
-
- class Garbo
- {
- public:
- ~Garbo()
- {
- if (singleton_base::_instance)
- {
- delete singleton_base::_instance;
- singleton_base::_instance = nullptr;
- }
- }
- };
-
- static Garbo garbo;
- static T *_instance;
- static std::mutex _mutex_singleton_base;
- };
- template<typename T>
- T *singleton_base<T>::_instance = nullptr;
- template<typename T>
- std::mutex singleton_base<T>::_mutex_singleton_base;
- #endif
|