From d033400614e7e2c0ff49e5100c81e937e5818e74 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 28 Jul 2018 13:47:51 +0800 Subject: *update --- src/libjin/Common/Singleton.hpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/libjin/Common/Singleton.hpp (limited to 'src/libjin/Common/Singleton.hpp') diff --git a/src/libjin/Common/Singleton.hpp b/src/libjin/Common/Singleton.hpp new file mode 100644 index 0000000..c3ce467 --- /dev/null +++ b/src/libjin/Common/Singleton.hpp @@ -0,0 +1,37 @@ +#ifndef __JIN_SINGLETON_H +#define __JIN_SINGLETON_H + +namespace jin +{ + template + 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() {}; + static T* _instance; + private: + Singleton(const Singleton&); + Singleton& operator = (const Singleton&); + }; + + template T* Singleton::_instance = nullptr; + +#define SINGLETON(T) \ + friend Singleton + +} // jin + +#endif // __JIN_SINGLETON_H \ No newline at end of file -- cgit v1.1-26-g67d0