单列模版 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
相关推荐
知其然亦知其所以然10 分钟前
JVM社招面试题:队列和栈是什么?有什么区别?我在面试现场讲了个故事…
java·后端·面试
harmful_sheep17 分钟前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
星辰大海的精灵19 分钟前
如何确保全球数据管道中的跨时区数据完整性和一致性
java·后端·架构
大大。21 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
nc_kai24 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
Codebee34 分钟前
OneCode:AI时代的先锋——注解驱动技术引领开发范式变革
java
勤奋的知更鸟35 分钟前
Java 编程之状态模式
java·开发语言·状态模式
清醒的兰42 分钟前
Qt 基于TCP套接字编程
网络·qt·tcp
架构个驾驾1 小时前
深入浅出MyBatis-Plus实战指南
java
爱喝茶的小茶1 小时前
周赛98补题
开发语言·c++·算法