From da9fb1ed0f259b9d776d12adfed0df0b92097ed1 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 25 Sep 2022 20:51:33 +0200 Subject: [PATCH] Util: Allow setting instance pointer manually, ex: in constructor --- src/ruc/singleton.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ruc/singleton.h b/src/ruc/singleton.h index c753dc0..922880c 100644 --- a/src/ruc/singleton.h +++ b/src/ruc/singleton.h @@ -29,7 +29,9 @@ public: static inline void destroy() { - VERIFY(s_instance, "singleton does not exist"); + if (!s_instance) { + return; + } delete s_instance; s_instance = nullptr; @@ -44,6 +46,13 @@ public: protected: Singleton() {} + // Hack: in certain situations the instance needs to be set early + static void set(T* instance) + { + VERIFY(!s_instance, "singleton already exists"); + s_instance = instance; + } + // Constructor token struct s {};