//
// Created by 51bitquant on 2023/3/19.
//
ifndef IO_SINGLETON_H
define IO_SINGLETON_H
include <cstdlib>
template <typename T>
class Singleton {
public:
static T& getSingleton() {
if(!instance_.get()) {
instance_ = std::unique_ptr<T>(new T);
}
return *(instance_.get());
}
private:
static std::unique_ptr<T> instance_;
Singleton();
Singleton(const Singleton<T> & other);
Singleton & operator=(const Singleton<T> & other);
};
template <typename T>
std::unique_ptr<T> Singleton<T>::instance_ = nullptr;
template <typename T>
class Singleton1 {
public:
static T& getSingleton() {
init();
return instance_;
}
private:
static void init() {
if (instance_ == nullptr) {
instance_ = new T;
atexit(destroy);
}
}
static void destroy() {
delete instance_;
}
static T instance_;
Singleton1();
Singleton1(const Singleton<T> & other);
Singleton1 & operator=(const Singleton<T> & other);
};
template <typename T>
T* Singleton1<T>::instance_ = nullptr;