单列模版 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
相关推荐
怎么比啊都是大神1 小时前
关于==null、isEmpty()和StrUtil.isBlank()
java
唐青枫1 小时前
Java Neo4j 实战指南:从图模型、Cypher 到 Spring Boot 关系查询
java
小保CPP1 小时前
OCR C++ Tesseract生成可搜索的pdf
c++·人工智能·ocr·模式识别·光学字符识别
观远数据2 小时前
从离线开发到实时同步:DataFlow如何支撑企业级数据治理闭环
java·windows·microsoft·excel
今天AI了吗3 小时前
Hermes Agent 搭建全流程:从本机试跑到可持续运行的个人 AI Agent
java·人工智能·python·学习·embedding
春卷同学3 小时前
032-关系型数据库在记账应用中的建模与查询优化
java·jvm·数据库
是个西兰花3 小时前
Linux:深入解析Linux线程原理与实现
linux·运维·c++·线程·互斥锁
不平衡的叉叉树3 小时前
Springboot+Mockito简单使用单元测试
java·spring boot·单元测试
空中湖3 小时前
Spring AI 多模型接入实战:OpenAI、Ollama、通义千问切换只需改配置
java·人工智能·spring
tianyatest3 小时前
表格分类统计及排序
java·excel·暖通