From ad483a2d1c25f2f986eedbdadf4dbf1f24d0c532 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 22 Jul 2018 15:25:01 +0800 Subject: *singleton --- src/libjin/common/singleton.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/libjin/common/singleton.h (limited to 'src/libjin/common/singleton.h') diff --git a/src/libjin/common/singleton.h b/src/libjin/common/singleton.h new file mode 100644 index 0000000..e6326b2 --- /dev/null +++ b/src/libjin/common/singleton.h @@ -0,0 +1,34 @@ +#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() {}; + private: + Singleton(const Singleton&); + Singleton& operator = (const Singleton&); + + static T* _instance; + }; + + template T* Singleton::_instance = nullptr; +} + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0