This commit is contained in:
2026-07-24 18:09:36 +08:00
parent e8bcc7ecfd
commit cca030726d
+15 -2
View File
@@ -4,13 +4,26 @@ template <typename Derived>
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