39 lines
791 B
C++
39 lines
791 B
C++
/*
|
|
* singleton.h
|
|
*
|
|
* Created on: Sep 11, 2019
|
|
* Author: compi
|
|
*/
|
|
|
|
#ifndef SINGLETON_H_
|
|
#define SINGLETON_H_
|
|
|
|
#include <utility>
|
|
|
|
namespace f4ll_cpp {
|
|
|
|
template<typename T> class Strangeton {
|
|
public:
|
|
static T &Instance() {
|
|
return *m_instance;
|
|
}
|
|
template<typename ... Args> static T &Init(Args &&... args) {
|
|
static T instance{ std::forward<Args>(args)... };
|
|
if(!m_instance)
|
|
m_instance = &instance;
|
|
return instance;
|
|
}
|
|
|
|
protected:
|
|
Strangeton() = default;
|
|
Strangeton(const Strangeton &) = delete;
|
|
Strangeton &operator=(const Strangeton &) = delete;
|
|
virtual ~Strangeton() = default;
|
|
static T *m_instance;
|
|
};
|
|
|
|
template<typename T> T* Strangeton<T>::m_instance = nullptr;
|
|
|
|
} // f4ll_cpp
|
|
|
|
#endif /* SINGLETON_H_ */
|