update singleton

This commit is contained in:
Nomango 2020-06-08 20:20:06 +08:00
parent a794894452
commit f786ab67ef
8 changed files with 41 additions and 22 deletions

View File

@ -68,11 +68,11 @@ public:
void DestroyModule() override; void DestroyModule() override;
~AudioModule();
private: private:
AudioModule(); AudioModule();
~AudioModule();
private: private:
IXAudio2* x_audio2_; IXAudio2* x_audio2_;
IXAudio2MasteringVoice* mastering_voice_; IXAudio2MasteringVoice* mastering_voice_;

View File

@ -102,11 +102,11 @@ public:
void HandleEvent(Event* evt) override; void HandleEvent(Event* evt) override;
virtual ~Director();
private: private:
Director(); Director();
virtual ~Director();
private: private:
bool render_border_enabled_; bool render_border_enabled_;
Stack<StagePtr> stages_; Stack<StagePtr> stages_;

View File

@ -19,12 +19,13 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include <memory>
namespace kiwano namespace kiwano
{ {
template <typename _Ty> template <typename _Ty>
struct Singleton class Singleton
{ {
protected: protected:
Singleton() = default; Singleton() = default;
@ -32,29 +33,47 @@ protected:
Singleton& operator=(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete;
private: private:
struct ObjectCreator struct InstanceCreator
{ {
ObjectCreator() InstanceCreator()
{ {
(void)Singleton<_Ty>::GetInstance(); (void)Singleton<_Ty>::GetInstancePtr();
} }
inline void Dummy() const {} inline void Dummy() const {}
}; };
static ObjectCreator creator_; static InstanceCreator creator_;
public: public:
using object_type = _Ty; using object_type = _Ty;
static std::unique_ptr<object_type> instance_ptr_;
static inline object_type& GetInstance() static inline object_type& GetInstance()
{ {
static object_type instance; return *GetInstancePtr();
}
static inline object_type* GetInstancePtr()
{
creator_.Dummy(); 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 _Ty> template <typename _Ty>
typename Singleton<_Ty>::ObjectCreator Singleton<_Ty>::creator_; typename Singleton<_Ty>::InstanceCreator Singleton<_Ty>::creator_;
template <typename _Ty>
typename std::unique_ptr<_Ty> Singleton<_Ty>::instance_ptr_;
} // namespace kiwano } // namespace kiwano

View File

@ -102,11 +102,11 @@ public:
*/ */
bool ExtractResourceToFile(const Resource& res, const String& dest_file_name) const; bool ExtractResourceToFile(const Resource& res, const String& dest_file_name) const;
~FileSystem();
private: private:
FileSystem(); FileSystem();
~FileSystem();
private: private:
Vector<String> search_paths_; Vector<String> search_paths_;
UnorderedMap<String, String> file_lookup_dict_; UnorderedMap<String, String> file_lookup_dict_;

View File

@ -112,11 +112,11 @@ public:
void HandleEvent(Event* evt) override; void HandleEvent(Event* evt) override;
~Input();
private: private:
Input(); Input();
~Input();
void UpdateKey(KeyCode key, bool down); void UpdateKey(KeyCode key, bool down);
void UpdateButton(MouseButton btn, bool down); void UpdateButton(MouseButton btn, bool down);

View File

@ -74,11 +74,11 @@ public:
/// @brief 헌왕뻠닸 /// @brief 헌왕뻠닸
void Clear(); void Clear();
virtual ~TextureCache();
private: private:
TextureCache(); TextureCache();
virtual ~TextureCache();
private: private:
using TextureMap = UnorderedMap<size_t, TexturePtr>; using TextureMap = UnorderedMap<size_t, TexturePtr>;
TextureMap texture_cache_; TextureMap texture_cache_;

View File

@ -126,11 +126,11 @@ public:
/// @note 此操作会重定向输出流到标准输出流 /// @note 此操作会重定向输出流到标准输出流
void ShowConsole(bool show); void ShowConsole(bool show);
~Logger();
private: private:
Logger(); Logger();
~Logger();
void Prepare(Level level, StringStream& sstream); void Prepare(Level level, StringStream& sstream);
void Output(Level level, StringStream& sstream); void Output(Level level, StringStream& sstream);

View File

@ -88,11 +88,11 @@ public:
/// @brief 清空所有资源 /// @brief 清空所有资源
void Clear(); void Clear();
virtual ~ResourceCache();
private: private:
ResourceCache(); ResourceCache();
virtual ~ResourceCache();
private: private:
UnorderedMap<String, ObjectBasePtr> object_cache_; UnorderedMap<String, ObjectBasePtr> object_cache_;
}; };