25 lines
389 B
C++
25 lines
389 B
C++
#ifndef WC3RE_SINGLETON_HH__
|
|
#define WC3RE_SINGLETON_HH__
|
|
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
template<typename T>
|
|
class Singleton {
|
|
public:
|
|
static T& getInstance() {
|
|
call_once(instance_flag, init);
|
|
return *instance;
|
|
}
|
|
|
|
private:
|
|
static std::unique_ptr<T> instance;
|
|
static std::once_flag instance_flag;
|
|
|
|
static void init() {
|
|
instance.reset(new T{});
|
|
}
|
|
};
|
|
|
|
#endif
|