aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Common/Singleton.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-07-27 00:01:34 +0800
committerchai <chaifix@163.com>2018-07-27 00:01:34 +0800
commitb855ebb91ad8d97617ec1aa418b4add84670a07d (patch)
treeb11847a1879a2582c9cfd380074ad4c70f17a1a8 /src/libjin/Common/Singleton.h
parent1a882936e91826e4d69584745e16a79650272015 (diff)
*change name
Diffstat (limited to 'src/libjin/Common/Singleton.h')
-rw-r--r--src/libjin/Common/Singleton.h38
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