Qt 单例模板

直接上代码:

#ifndef SINGLETON_H
#define SINGLETON_H

#include <QMutex>
#include <QSharedPointer>


template<typename T>
class TSingleton
{
public:
    static T* getInstance()
    {
        QMutexLocker mutexLocker(&m_mutex);
        if (m_pInstance.isNull())
        {
            m_pInstance = QSharedPointer<T>(new T);
        }
        return m_pInstance.data();
    }

protected:
    TSingleton(){}
    TSingleton(const TSingleton&){}
    TSingleton& operator==(const TSingleton&){}


private:
    static QSharedPointer<T> m_pInstance;
    static QMutex m_mutex;    //加入互斥锁,保证线程安全(如果存在多线程)
};

template<typename T>
QSharedPointer<T> TSingleton<T>::m_pInstance = NULL;

template<typename T>
QMutex TSingleton<T>::m_mutex;




#endif // SINGLETON_H

如何使用:

ctest.h

#ifndef CTest_H
#define CTest_H

#include "singleton.h"

class CTest :public QObject, public TSingleton<CTest>
{
    friend class TSingleton<CTest>;
    Q_OBJECT
public:
    CTest();
    ~CTest();

    int num() const;
    void setNum(int num);

private:
    int m_num;
};

#endif // CTest_H

ctest.cpp

#include "ctest.h"

CTest::CTest()
{

}

CTest::~CTest()
{
}

int CTest::num() const
{
    return m_num;
}

void CTest::setNum(int num)
{
    m_num = num;
}

main.cpp

#include <QCoreApplication>
#include "ctest.h"
#include <QDebug>

void add(int value)
{
    int num = CTest::getInstance()->num();

    num += value;

    qDebug()<<num;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    CTest::getInstance()->setNum(10);

    add(12);

    return a.exec();
}

升级版:如何将单例模板写成宏:

singleton.h

#ifndef _SINGLETON_H_
#define _SINGLETON_H_

#include <mutex>

#define DISABLE_COPY(Class) \
    Class(const Class&) = delete; \
    Class& operator=(const Class&) = delete;

#define SINGLETON_DECL(Class) \
    public: \
        static Class* instance(); \
        static void exitInstance(); \
    private: \
        DISABLE_COPY(Class) \
        static Class* s_pInstance; \
        static std::mutex s_mutex;

#define SINGLETON_IMPL(Class) \
    Class* Class::s_pInstance = NULL; \
    std::mutex Class::s_mutex; \
    Class* Class::instance() { \
        if (s_pInstance == NULL) { \
            s_mutex.lock(); \
            if (s_pInstance == NULL) { \
                s_pInstance = new Class; \
            } \
            s_mutex.unlock(); \
        } \
        return s_pInstance; \
    } \
    void Class::exitInstance() { \
        s_mutex.lock(); \
        if (s_pInstance) {  \
            delete s_pInstance; \
            s_pInstance = NULL; \
        }   \
        s_mutex.unlock(); \
    }

#endif // HV_SINGLETON_H_

使用:

#ifndef _MAINWINDOW_H_
#define _MAINWINDOW_H_

#inclue "singleton.h"

class MainWindow : public QMainWindow
{
    SINGLETON_DECL(MainWindow)
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private:
};


#endif //_MAINWINDOW_H_
#include "mainwindow.h"

SINGLETON_IMPL(MainWindow)

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

}

MainWindow::~MainWindow()
{
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值