Files
openglplayground/Singleton.hh
2015-03-10 00:48:25 +01:00

25 lines
415 B
C++

#ifndef __OPENGLPLAYGROUND_SINGLETON_HH__
#define __OPENGLPLAYGROUND_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