You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.3 KiB

10 months ago
#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