From cca030726d1a86c68b9175dd9fda2cc482a4a543 Mon Sep 17 00:00:00 2001 From: wyc <1104749580@qq.com> Date: Fri, 24 Jul 2026 18:09:36 +0800 Subject: [PATCH] 3D --- psc_global_include/Singleton.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/psc_global_include/Singleton.hpp b/psc_global_include/Singleton.hpp index 70e2179..4eefc79 100644 --- a/psc_global_include/Singleton.hpp +++ b/psc_global_include/Singleton.hpp @@ -4,13 +4,26 @@ template class Singleton { public: static Derived* instance() { - static Derived instance; - return &instance; + auto& value = storage(); + if (value == nullptr) { + value = new Derived(); + } + return value; + } + static void destroy() { + auto& value = storage(); + delete value; + value = nullptr; } Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; protected: Singleton() = default; ~Singleton() = default; +private: + static Derived*& storage() { + static Derived* value = nullptr; + return value; + } }; } // namespace Psc