diff --git a/src/kiwano-audio/AudioModule.h b/src/kiwano-audio/AudioModule.h index a97e4078..0e51eb72 100644 --- a/src/kiwano-audio/AudioModule.h +++ b/src/kiwano-audio/AudioModule.h @@ -68,11 +68,11 @@ public: void DestroyModule() override; + ~AudioModule(); + private: AudioModule(); - ~AudioModule(); - private: IXAudio2* x_audio2_; IXAudio2MasteringVoice* mastering_voice_; diff --git a/src/kiwano/base/Director.h b/src/kiwano/base/Director.h index fd054672..6a775670 100644 --- a/src/kiwano/base/Director.h +++ b/src/kiwano/base/Director.h @@ -102,11 +102,11 @@ public: void HandleEvent(Event* evt) override; + virtual ~Director(); + private: Director(); - virtual ~Director(); - private: bool render_border_enabled_; Stack stages_; diff --git a/src/kiwano/core/Singleton.h b/src/kiwano/core/Singleton.h index 7ed4e853..c067e1d4 100644 --- a/src/kiwano/core/Singleton.h +++ b/src/kiwano/core/Singleton.h @@ -19,12 +19,13 @@ // THE SOFTWARE. #pragma once +#include namespace kiwano { template -struct Singleton +class Singleton { protected: Singleton() = default; @@ -32,29 +33,47 @@ protected: Singleton& operator=(const Singleton&) = delete; private: - struct ObjectCreator + struct InstanceCreator { - ObjectCreator() + InstanceCreator() { - (void)Singleton<_Ty>::GetInstance(); + (void)Singleton<_Ty>::GetInstancePtr(); } inline void Dummy() const {} }; - static ObjectCreator creator_; + static InstanceCreator creator_; public: using object_type = _Ty; + static std::unique_ptr instance_ptr_; + static inline object_type& GetInstance() { - static object_type instance; + return *GetInstancePtr(); + } + + static inline object_type* GetInstancePtr() + { creator_.Dummy(); - return instance; + if (!instance_ptr_) + { + instance_ptr_.reset(new object_type); + } + return instance_ptr_.get(); + } + + static inline void DestroyInstance() + { + instance_ptr_.reset(); } }; template -typename Singleton<_Ty>::ObjectCreator Singleton<_Ty>::creator_; +typename Singleton<_Ty>::InstanceCreator Singleton<_Ty>::creator_; + +template +typename std::unique_ptr<_Ty> Singleton<_Ty>::instance_ptr_; } // namespace kiwano diff --git a/src/kiwano/platform/FileSystem.h b/src/kiwano/platform/FileSystem.h index 943fed7a..a012ff78 100644 --- a/src/kiwano/platform/FileSystem.h +++ b/src/kiwano/platform/FileSystem.h @@ -102,11 +102,11 @@ public: */ bool ExtractResourceToFile(const Resource& res, const String& dest_file_name) const; + ~FileSystem(); + private: FileSystem(); - ~FileSystem(); - private: Vector search_paths_; UnorderedMap file_lookup_dict_; diff --git a/src/kiwano/platform/Input.h b/src/kiwano/platform/Input.h index d2a72793..2bd4ad84 100644 --- a/src/kiwano/platform/Input.h +++ b/src/kiwano/platform/Input.h @@ -112,11 +112,11 @@ public: void HandleEvent(Event* evt) override; + ~Input(); + private: Input(); - ~Input(); - void UpdateKey(KeyCode key, bool down); void UpdateButton(MouseButton btn, bool down); diff --git a/src/kiwano/render/TextureCache.h b/src/kiwano/render/TextureCache.h index 76caef85..4f82832c 100644 --- a/src/kiwano/render/TextureCache.h +++ b/src/kiwano/render/TextureCache.h @@ -74,11 +74,11 @@ public: /// @brief 清空缓存 void Clear(); + virtual ~TextureCache(); + private: TextureCache(); - virtual ~TextureCache(); - private: using TextureMap = UnorderedMap; TextureMap texture_cache_; diff --git a/src/kiwano/utils/Logger.h b/src/kiwano/utils/Logger.h index b4177507..d35fdc35 100644 --- a/src/kiwano/utils/Logger.h +++ b/src/kiwano/utils/Logger.h @@ -126,11 +126,11 @@ public: /// @note 此操作会重定向输出流到标准输出流 void ShowConsole(bool show); + ~Logger(); + private: Logger(); - ~Logger(); - void Prepare(Level level, StringStream& sstream); void Output(Level level, StringStream& sstream); diff --git a/src/kiwano/utils/ResourceCache.h b/src/kiwano/utils/ResourceCache.h index e6aaa9e8..58430b32 100644 --- a/src/kiwano/utils/ResourceCache.h +++ b/src/kiwano/utils/ResourceCache.h @@ -88,11 +88,11 @@ public: /// @brief 清空所有资源 void Clear(); + virtual ~ResourceCache(); + private: ResourceCache(); - virtual ~ResourceCache(); - private: UnorderedMap object_cache_; };