MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_singleton.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_SINGLETON_H_1608105970
12#define INCGUARD_MGL_SINGLETON_H_1608105970
13
14#include <cassert>
15#include <mutex>
16
19
20namespace MGL
21{
24{
25public:
27 using FinalizeFunction = void (*)();
28
29 static bool Initialize() noexcept;
30
31 static void Register(FinalizeFunction function) noexcept;
32 static void Finalize() noexcept;
33 static void Unregister(FinalizeFunction function) noexcept;
34
35private:
36 static inline std::list<FinalizeFunction> _functions; // staticなのでMGL::STL::listは使用できない
37 static inline std::unique_ptr<std::recursive_mutex> _mutex;
38};
39
41template<typename T, class... Args>
43{
44public:
45 /* ------------------------------------------------------------------------- */
50 /* ------------------------------------------------------------------------- */
51 static T &CreateInstance(Args... args) noexcept
52 {
53 auto &instance = T::GetInstanceRef();
54
55 if (instance.get() == nullptr)
56 {
57 instance = STL::make_unique<T>(args...);
58 if (instance.get() != nullptr)
59 {
60 SingletonFinalizer::Register(DestroyInstance);
61 }
62 }
63
64 return *(instance.get());
65 }
66
67 /* ------------------------------------------------------------------------- */
72 /* ------------------------------------------------------------------------- */
73 static T &GetInstance() noexcept
74 {
75 auto &instance = T::GetInstanceRef();
76 assert(instance.get());
77 return *(instance.get());
78 }
79
80 /* ------------------------------------------------------------------------- */
84 /* ------------------------------------------------------------------------- */
85 static void DestroyInstance() noexcept
86 {
87 if (HasInstance())
88 {
89 SingletonFinalizer::Unregister(DestroyInstance);
90 T::GetInstanceRef().reset();
91 }
92 }
93
94 /* ------------------------------------------------------------------------- */
100 /* ------------------------------------------------------------------------- */
101 static bool HasInstance() noexcept
102 {
103 return T::GetInstanceRef().get() != nullptr;
104 }
105
106 // コピーとムーブは禁止
107 SharedSingleton(const SharedSingleton &) noexcept = delete;
108 SharedSingleton &operator=(const SharedSingleton &) noexcept = delete;
109 SharedSingleton(SharedSingleton &&) noexcept = delete;
110 SharedSingleton &operator=(SharedSingleton &&) noexcept = delete;
111
112 virtual ~SharedSingleton() noexcept = default;
113
114protected:
115 // 外から作れないようコンストラクタはprotectedに定義
116 SharedSingleton() noexcept = default;
117};
118
119
121template<typename T, class... Args>
123{
124public:
125 /* ------------------------------------------------------------------------- */
130 /* ------------------------------------------------------------------------- */
131 static T &CreateInstance(Args... args) noexcept
132 {
133 if (!HasInstance())
134 {
135 _instance = STL::make_unique<T>(args...);
136 if (HasInstance())
137 {
138 SingletonFinalizer::Register(DestroyInstance);
139 }
140 }
141
142 return *_instance;
143 }
144
145 /* ------------------------------------------------------------------------- */
150 /* ------------------------------------------------------------------------- */
151 static constexpr T &GetInstance() noexcept
152 {
153 assert(_instance);
154 return *_instance;
155 }
156
157 /* ------------------------------------------------------------------------- */
161 /* ------------------------------------------------------------------------- */
162 static void DestroyInstance() noexcept
163 {
164 if (HasInstance())
165 {
166 SingletonFinalizer::Unregister(DestroyInstance);
167 _instance.reset();
168 }
169 }
170
171 /* ------------------------------------------------------------------------- */
177 /* ------------------------------------------------------------------------- */
178 static constexpr bool HasInstance() noexcept
179 {
180 return _instance != nullptr;
181 }
182
183 // コピーとムーブは禁止
184 StaticSingleton(const StaticSingleton &) noexcept = delete;
185 StaticSingleton &operator=(const StaticSingleton &) noexcept = delete;
186 StaticSingleton(StaticSingleton &&) noexcept = delete;
187 StaticSingleton &operator=(StaticSingleton &&) noexcept = delete;
188
189 virtual ~StaticSingleton() noexcept = default;
190
191protected:
192 // 外から作れないようコンストラクタはprotectedに定義
193 StaticSingleton() noexcept = default;
194
195private:
196 static inline STL::unique_ptr<T> _instance = nullptr;
197};
198
199} // namespace MGL
200#endif // INCGUARD_MGL_SINGLETON_H_1608105970
201
202// vim: et ts=4 sw=4 sts=4
シングルトンテンプレート(共有ライブラリ用)
Definition mgl_singleton.h:43
static T & CreateInstance(Args... args) noexcept
インスタンスの生成
Definition mgl_singleton.h:51
static T & GetInstance() noexcept
インスタンスの取得
Definition mgl_singleton.h:73
static bool HasInstance() noexcept
有効なインスタンスを保持しているかを取得
Definition mgl_singleton.h:101
static void DestroyInstance() noexcept
インスタンスの破棄
Definition mgl_singleton.h:85
シングルトン解放クラス
Definition mgl_singleton.h:24
static void Unregister(FinalizeFunction function) noexcept
シングルトンクラスの登録解除
Definition mgl_singleton.cc:77
static void Register(FinalizeFunction function) noexcept
シングルトンクラスの登録
Definition mgl_singleton.cc:44
static void Finalize() noexcept
登録されたシングルトンクラスの解放
Definition mgl_singleton.cc:61
static bool Initialize() noexcept
シングルトン解放クラスの初期化
Definition mgl_singleton.cc:24
void(*)() FinalizeFunction
解放用の関数の型
Definition mgl_singleton.h:27
シングルトンテンプレート(アプリケーションまたは静的リンクライブラリのみ利用可能)
Definition mgl_singleton.h:123
static void DestroyInstance() noexcept
インスタンスの破棄
Definition mgl_singleton.h:162
static constexpr bool HasInstance() noexcept
有効なインスタンスを保持しているかを取得
Definition mgl_singleton.h:178
static constexpr T & GetInstance() noexcept
インスタンスの取得
Definition mgl_singleton.h:151
static T & CreateInstance(Args... args) noexcept
インスタンスの生成
Definition mgl_singleton.h:131
MGL STLコンテナの代替
MGL STLのメモリ関連の代替