diff options
author | chai <chaifix@163.com> | 2020-10-19 09:13:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-10-19 09:13:58 +0800 |
commit | f0807fc44dde14531759306317611bab87c8fccf (patch) | |
tree | 6e78fed61c16a70cda5fa732635f89f9faac9720 /Runtime/Utilities/Singleton.h | |
parent | 639b34294ffc20721c66db46e59e07d9100ac4b8 (diff) |
+gamelab proj
Diffstat (limited to 'Runtime/Utilities/Singleton.h')
-rw-r--r-- | Runtime/Utilities/Singleton.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Runtime/Utilities/Singleton.h b/Runtime/Utilities/Singleton.h new file mode 100644 index 0000000..f864a42 --- /dev/null +++ b/Runtime/Utilities/Singleton.h @@ -0,0 +1,43 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +template<class T> +class Singleton +{ +public: + + static T* Instance() + { + if (!instance) instance = new T; + return instance; + } + + static void Destroy() + { + delete instance; + instance = nullptr; + } + +protected: + + Singleton() + { + instance = static_cast<T*>(this); + }; + + virtual ~Singleton() {}; + + static T* instance; + +private: + + Singleton(const Singleton& singleton); + + Singleton& operator = (const Singleton& singleton); + +}; + +template<class T> +T* Singleton<T>::instance = nullptr; + +#endif
\ No newline at end of file |