单列模版 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
相关推荐
珍珠先生5 分钟前
P16 · IDEA 启动报错:找不到或无法加载主类
java
子非鱼a7 分钟前
【WEB】[RoarCTF 2019]Easy Java
java·开发语言
西峰u8 分钟前
电商项目测试报告
java·项目测试报告
lemon_sjdk17 分钟前
各种Binding(1)
java·javafx·绑定·源码解析
李少兄21 分钟前
记一次 IDEA 打开 Vue 项目后目录“闪现即消失“ 问题排查
java·vue.js·intellij-idea
Wang's Blog24 分钟前
Java框架快速入门: Spring Security+OAuth2之安全配置基础及函数式风格对比
java·安全·spring
小荷才露尖尖角,早有蜻蜓立上头25 分钟前
class java.util.LinkedHashMap cannot be cast to xxx
java·spring boot
陈皮波比茶26 分钟前
计算机二级MySQL笔记
java·数据库
木井巳30 分钟前
【BFS/DFS 解决 FloodFill 算法】衣橱整理
java·算法·leetcode·深度优先·广度优先·宽度优先
zzzll111130 分钟前
LangChain 1.3 新特性详解与实战指南
java·数据库·langchain