diff options
Diffstat (limited to 'Source/Samples/LuaxTest/header.h')
-rw-r--r-- | Source/Samples/LuaxTest/header.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Source/Samples/LuaxTest/header.h b/Source/Samples/LuaxTest/header.h new file mode 100644 index 0000000..386ae71 --- /dev/null +++ b/Source/Samples/LuaxTest/header.h @@ -0,0 +1,60 @@ + +#ifndef ASSERT +#ifdef NDEBUG +#define ASSERT(x) { false ? (void)(x) : (void)0; } +#else +#ifdef _WIN32 +#define ASURA_DEBUG_BREAK() __debugbreak() +#else +#define ASURA_DEBUG_BREAK() raise(SIGTRAP) +#endif +#define ASSERT(x) do { const volatile bool asura_assert_b____ = !(x); if(asura_assert_b____) ASURA_DEBUG_BREAK(); } while (false) +#endif +#endif + +/// +/// ̳Singletonڵһʵʱʵ֮ٴʵᱨ +/// +template<class T> +class Singleton +{ +public: + + static T* Get() + { + // ֮ǰûдһ + if (!instance) instance = new T; + // ʵ + return instance; + } + + static void Destroy() + { + delete instance; + instance = nullptr; + } + +protected: + + Singleton() + { + // instanceζִһʵǴġ + ASSERT(!instance); + // ʵΪʵ + 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; |