From 70b82d1981c0de3c7b77670ff8abcfeb26815142 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 12 Mar 2019 00:39:26 +0800 Subject: *misc --- Source/Samples/LuaxTest/header.h | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Source/Samples/LuaxTest/header.h (limited to 'Source/Samples/LuaxTest/header.h') 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 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(this); + }; + + virtual ~Singleton() {}; + + static T* instance; + +private: + + Singleton(const Singleton& singleton); + + Singleton& operator = (const Singleton& singleton); + +}; + +template +T* Singleton::instance = nullptr; -- cgit v1.1-26-g67d0