单列模版 C++ Qt

单列模版

cpp 复制代码
#ifndef _SINGTON_H_
#define  _SINGTON_H_
#include <QMutex>
#include <QSharedPointer>
    template<typename T>
    class Singleton {
    public:
        static T *instance() {
            if(m_instance.isNull()){
                QMutexLocker lock(&m_mutex);
                if(m_instance.isNull()){
                    m_instance=QSharedPointer<T>(new T);
                }
            }
            return m_instance.data();
        }
        virtual ~Singleton()=0;
    protected:
        Singleton() {

        }


    private:
        static QMutex m_mutex;
        static QSharedPointer<T> m_instance;
    };
    template <typename T>
    Singleton<T>::~Singleton<T>(){


    }
    template<typename T>
    QMutex  Singleton<T>::m_mutex;

    template<typename T>
    QSharedPointer<T>  Singleton<T>::m_instance;


#define  DECLARE_SINGTON_CONSTRUCT(CLASS_NAME)\
    protected: \
    CLASS_NAME(); \
    friend class Singleton<CLASS_NAME>;\
    public:\
    virtual ~CLASS_NAME();

#define DECLARE_SINGTON_CONSTRUCT_WITH_BASE(CLASS_NAME,BASE_NAME)\
    protected:\
    CLASS_NAME(BASE_NAME*parent=nullptr);\
    friend class Singleton<CLASS_NAME>;\
    public:\
    virtual ~CLASS_NAME();


#endif _SINGTON_H_

使用案列

cpp 复制代码
#ifndef GENERATEDATANODE_H
#define GENERATEDATANODE_H

#include "listnode.h"
#include "uicommondef.h"
#include "base/Singleton.hpp"
class GenerateDataNode : public Singleton<GenerateDataNode>
{
    DECLARE_SINGTON_CONSTRUCT(GenerateDataNode)
public:
    QList<DataNode> buildDataNode(const MENU_WIDGET_INDEX &index);

protected:
    QList<DataNode> DiyMenuManagerIndex();


    QList<DataNode> DiySlideMenuIndex();

    QList<DataNode> MainMenuIndex();
    QList<DataNode> ShortCutMenuIndex();
    QList<DataNode> ColorPalettesIndex();

    QList<DataNode> ImageSettingIndex(); // 图像设置菜单
    //    QList<DataNode> CompensttionIndex();

    QList<DataNode> ReticleZeroIndex();

    QList<DataNode> PowerSettingIndex();

    QList<DataNode> RecordSettingIndex();

    QList<DataNode> GeneralSettingIndex();

    QList<DataNode> ReticleSettingIndex();

    QList<DataNode> DateTimeSettingIndex();
};

#endif // GENERATEDATANODE_H
相关推荐
CS Beginner7 分钟前
【IDEA】记录webapp下创建相同目录的一次错误
java·intellij-idea·web app
donotshow9 分钟前
DBeaver连接本地MySQL、创建数据库表的基础操作
java·后端
MMjeaty16 分钟前
查找及其算法
c++·算法
王元_SmallA29 分钟前
Go环境搭建(vscode调试)
java·后端
恋恋西风1 小时前
Qt 打开文件列表选择文件,实现拖拽方式打开文件,拖拽加载
开发语言·qt
yong15858553431 小时前
1. Linux C++ muduo 库学习——库的编译安装
linux·c++·学习
952361 小时前
数据结构-顺序表
java·数据结构·学习
奔跑吧 android1 小时前
【Qt】【1. 版本特性介绍】
qt·cpp·qml
chxii2 小时前
Apache Tomcat 介绍
java·tomcat·apache
码界奇点2 小时前
Java Web学习 第1篇前端基石HTML 入门与核心概念解析
java·前端·学习·xhtml