2020-01-16 12:37:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2020-02-17 20:53:08 +00:00
|
|
|
namespace ext
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Thread-unsafe singleton. It works simply like a global variable.
|
|
|
|
* Supports deinitialization.
|
|
|
|
*
|
|
|
|
* In most of the cases, you don't need this class.
|
|
|
|
* Use "Meyers Singleton" instead: static T & instance() { static T x; return x; }
|
|
|
|
*/
|
2020-01-22 15:20:19 +00:00
|
|
|
|
2020-01-16 12:37:29 +00:00
|
|
|
template <class T>
|
2020-02-03 17:06:59 +00:00
|
|
|
class Singleton
|
2020-01-16 12:37:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-02-03 17:06:59 +00:00
|
|
|
Singleton()
|
2020-01-16 12:37:29 +00:00
|
|
|
{
|
2020-02-03 17:06:59 +00:00
|
|
|
if (!instance)
|
2020-02-17 20:53:08 +00:00
|
|
|
instance = std::make_unique<T>();
|
2020-01-16 12:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T * operator->()
|
|
|
|
{
|
|
|
|
return instance.get();
|
|
|
|
}
|
|
|
|
|
2020-02-11 15:16:53 +00:00
|
|
|
static bool isInitialized()
|
|
|
|
{
|
2020-02-03 17:06:59 +00:00
|
|
|
return !!instance;
|
|
|
|
}
|
|
|
|
|
2020-02-11 15:16:53 +00:00
|
|
|
static void reset()
|
|
|
|
{
|
|
|
|
instance.reset();
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:37:29 +00:00
|
|
|
private:
|
|
|
|
inline static std::unique_ptr<T> instance{};
|
|
|
|
};
|
2020-01-22 15:20:19 +00:00
|
|
|
|
|
|
|
}
|