From 639b34294ffc20721c66db46e59e07d9100ac4b8 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 10 Sep 2020 20:30:31 +0800 Subject: *init --- Runner/Utilities/Singleton.h | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Runner/Utilities/Singleton.h (limited to 'Runner/Utilities/Singleton.h') diff --git a/Runner/Utilities/Singleton.h b/Runner/Utilities/Singleton.h new file mode 100644 index 0000000..65c9e60 --- /dev/null +++ b/Runner/Utilities/Singleton.h @@ -0,0 +1,45 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +template +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(this); + }; + + virtual ~Singleton() {}; + + static T* instance; + +private: + + Singleton(const Singleton& singleton); + + Singleton& operator = (const Singleton& singleton); + +}; + +template +T* Singleton::instance = nullptr; + +#define Get(T) T::Instance() + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0