30 lines
662 B
C++
30 lines
662 B
C++
#pragma once
|
|
namespace Psc {
|
|
template <typename Derived>
|
|
class Singleton {
|
|
public:
|
|
static Derived* instance() {
|
|
auto& value = storage();
|
|
if (value == nullptr) {
|
|
value = new Derived();
|
|
}
|
|
return value;
|
|
}
|
|
static void destroy() {
|
|
auto& value = storage();
|
|
delete value;
|
|
value = nullptr;
|
|
}
|
|
Singleton(const Singleton&) = delete;
|
|
Singleton& operator=(const Singleton&) = delete;
|
|
protected:
|
|
Singleton() = default;
|
|
~Singleton() = default;
|
|
private:
|
|
static Derived*& storage() {
|
|
static Derived* value = nullptr;
|
|
return value;
|
|
}
|
|
};
|
|
} // namespace Psc
|