Files
MyLogger/Singleton.h
T
2024-08-04 19:17:04 +08:00

92 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef SINGLETON_H
#define SINGLETON_H
#include <QMutex>
#include <QScopedPointer>
////////////////////////////////////////////////////////////////////////////////
/// ///
/// Singleton signature ///
/// ///
////////////////////////////////////////////////////////////////////////////////
/**
* 使用方法:
* 1. 定义类为单例:
* class ConnectionPool {
* SINGLETON(ConnectionPool) // Here
* public:
*
* 2. 实现无参构造函数,析构函数
* 3. 获取单例类的对象:
* Singleton<ConnectionPool>::getInstance();
* ConnectionPool &pool = Singleton<ConnectionPool>::getInstance();
* 注意: 如果单例的类需要释放的资源和 Qt 底层的信号系统有关系,例如 QSettingsQSqlDatabase 等,
* 需要在程序结束前手动释放(也就是在 main() 函数返回前调用释放资源的函数),
* 否则有可能在程序退出时报系统底层的信号错误,导致如 QSettings 的数据没有保存。
*/
template <typename T>
class Singleton
{
public:
static T& getInstance()
{
static T instance;
return instance;
};
Singleton(T &&) = delete;
Singleton(const T &) = delete;
void operator = (const T &) = delete;
T *operator &() = delete;
private:
Singleton() = default;
virtual ~Singleton() = default;
// static QMutex mutex;
// static QScopedPointer<T> instance;
};
////////////////////////////////////////////////////////////////////////////////
/// ///
/// Singleton definition ///
/// ///
////////////////////////////////////////////////////////////////////////////////
#if 0
template <typename T> QMutex Singleton<T>::mutex;
template <typename T> QScopedPointer<T> Singleton<T>::instance;
template <typename T>
T& Singleton<T>::getInstance()
{
if (instance.isNull())
{
mutex.lock();
if (instance.isNull())
{
instance.reset(new T());
}
mutex.unlock();
}
return *instance.data();
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// ///
/// Singleton Macro ///
/// ///
////////////////////////////////////////////////////////////////////////////////
#if 0
#define SINGLETON(Class) \
private: \
Class(); \
~Class(); \
Class(const Class &other); \
Class& operator=(const Class &other); \
friend class Singleton<Class>; \
friend struct QScopedPointerDeleter<Class>;
#endif
#define SINGLETON(Class) friend class Singleton<Class>
#endif // SINGLETON_H