单列模版 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
相关推荐
jay神2 小时前
基于SpringBoot的宠物生命周期信息管理系统
java·数据库·spring boot·后端·web开发·宠物·管理系统
万亿少女的梦1682 小时前
基于SpringBoot的在线考试管理系统设计与实现
java·spring boot·后端
艾莉丝努力练剑3 小时前
【Linux网络】Linux 网络编程入门:TCP Socket 编程(下)
linux·运维·服务器·网络·c++·tcp/ip
宵时待雨3 小时前
linux笔记归纳4:进程概念
linux·运维·服务器·c++·笔记
凯瑟琳.奥古斯特3 小时前
力扣2760 C++滑动窗口解法
数据结构·c++·算法·leetcode·职场和发展
一勺菠萝丶3 小时前
如何在 Linux 服务器上使用 Speedtest 官方 CLI 测试带宽(小白教程)
java·服务器·前端
ximu_polaris3 小时前
设计模式(C++)-行为型模式-访问者模式
c++·设计模式·访问者模式
范什么特西3 小时前
第一个Mybatis
java·开发语言·mybatis
下次再写3 小时前
【Redis实战】深入理解Redis缓存策略:从原理到Spring Boot实践
java·spring boot·redis·缓存穿透·缓存击穿·分布式缓存·缓存策略
超梦dasgg3 小时前
智慧充电系统计费定价服务Java 实现
java·开发语言·spring·微服务