diff options
Diffstat (limited to 'src/libjin/Common/Singleton.h')
-rw-r--r-- | src/libjin/Common/Singleton.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libjin/Common/Singleton.h b/src/libjin/Common/Singleton.h new file mode 100644 index 0000000..a6c7d6d --- /dev/null +++ b/src/libjin/Common/Singleton.h @@ -0,0 +1,38 @@ +#ifndef __JIN_SINGLETON_H +#define __JIN_SINGLETON_H + +namespace jin +{ + template<class T> + class Singleton + { + public: + static T* get() + { + if (_instance == nullptr) + _instance = new T; + return _instance; + } + static void destroy() + { + delete _instance; + _instance = nullptr; + } + protected: + Singleton() {}; + virtual ~Singleton() {}; + private: + Singleton(const Singleton&); + Singleton& operator = (const Singleton&); + + static T* _instance; + }; + + template<class T> T* Singleton<T>::_instance = nullptr; + +#define SINGLETON(T) \ + friend Singleton<T> + +} + +#endif
\ No newline at end of file |