From 40fc27154fe754181934dc7ee31375e6bdfb33fc Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 23 Oct 2018 12:23:58 +0800 Subject: *merge from minimal --- src/libjin/Common/je_singleton.hpp | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/libjin/Common/je_singleton.hpp (limited to 'src/libjin/Common/je_singleton.hpp') diff --git a/src/libjin/Common/je_singleton.hpp b/src/libjin/Common/je_singleton.hpp new file mode 100644 index 0000000..e981e7a --- /dev/null +++ b/src/libjin/Common/je_singleton.hpp @@ -0,0 +1,80 @@ +#ifndef __JE_SINGLETON_H +#define __JE_SINGLETON_H + +namespace JinEngine +{ + + /// + /// Singleton base class. + /// + template + class Singleton + { + public: + /// + /// Get singleton. + /// + /// @param Singleton instance of class. + /// + static T* get() + { + if (_instance == nullptr) + _instance = new T; + return _instance; + } + + /// + /// Destroy instance of singleton. + /// + static void destroy() + { + delete _instance; + _instance = nullptr; + } + + protected: + /// + /// Singleton constructor. + /// + Singleton() {}; + + /// + /// Singleton destructor. + /// + virtual ~Singleton() {}; + + /// + /// Singleton instance. + /// + static T* _instance; + + private: + /// + /// Singleton copy constructor. + /// + /// @param singleton Singleton of class. + /// + Singleton(const Singleton& singleton); + + /// + /// Singleton assignment. + /// + /// @param singleton Singleton of class. + /// + Singleton& operator = (const Singleton& singleton); + + }; + + /// + /// Singleton instance. + /// + template T* Singleton::_instance = nullptr; + + /// + /// Singleton notation. + /// + #define singleton(T) friend Singleton + +} // namespace JinEngine + +#endif // __JE_SINGLETON_H \ No newline at end of file -- cgit v1.1-26-g67d0