• C++自己用模板减少工作量


    今天写代码,遇到这样一个类似如下的需求:

    auto componentClassSavedByPLC = std::make_shared<ComponentClassSavedByPLC>();
        m_componentClassies.emplace_back(componentClassSavedByPLC);
    
        auto componentClassSavedByDCSLocalControl = std::make_shared<ComponentClassSavedByDCSLocalControl>();
        m_componentClassies.emplace_back(componentClassSavedByDCSLocalControl);
    
        auto componentClassSavedByCCS = std::make_shared<ComponentClassSavedByCCS>();
        m_componentClassies.emplace_back(componentClassSavedByCCS);
    
        auto componentClassSavedBySIS_1 = std::make_shared<ComponentClassSavedBySIS_1>();
        m_componentClassies.emplace_back(componentClassSavedBySIS_1);
        
        auto componentClassSavedBySIS_2 = std::make_shared<ComponentClassSavedBySIS_2>();
        m_componentClassies.emplace_back(componentClassSavedBySIS_2);
    
        auto componentClassSavedBySLNIPLC = std::make_shared<ComponentClassSavedBySLNIPLC>();
        m_componentClassies.emplace_back(componentClassSavedBySLNIPLC);
    
        auto componentClassSavedByPLCLocalControl = std::make_shared<ComponentClassSavedByPLCLocalControl>();
        m_componentClassies.emplace_back(componentClassSavedByPLCLocalControl);
    
        auto componentClassSavedByLocalInstrument = std::make_shared<ComponentClassSavedByLocalInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByLocalInstrument);
    
        auto componentClassSavedByPanelInstrument = std::make_shared<ComponentClassSavedByPanelInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByPanelInstrument);
    
        auto componentClassSavedByBehindPanelInstrument = std::make_shared<ComponentClassSavedByBehindPanelInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByBehindPanelInstrument);
    
        auto componentClassSavedByLocalPanelInstrument = std::make_shared<ComponentClassSavedByLocalPanelInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByLocalPanelInstrument);
    
        auto componentClassSavedByFarInstrument = std::make_shared<ComponentClassSavedByFarInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByFarInstrument);
    
        auto componentClassSavedByTracedInstrument = std::make_shared<ComponentClassSavedByTracedInstrument>();
        m_componentClassies.emplace_back(componentClassSavedByTracedInstrument);

    就是执行的代码一样,类型不一样,于是我在hpp里面写上如下代码:

    template <class T>
    std::shared_ptr<T> generateInstrumentComponent()
    {
        std::shared_ptr<T> componentClass = std::make_shared<T>();
        return componentClass;
    }

    后面每个改动都直接在这个函数里面改动就行了,每个代码直接改成

    auto componentClass = generateInstrumentComponent<ComponentClassSavedByDCS>();
    m_componentClassies.emplace_back(componentClass);

    就行了,要学会用高级工具!

  • 相关阅读:
    Navicat for MySQL破解版安装
    LACP学习笔记
    MUX VLAN
    Beyond Compare用于文件比较还是蛮好的选择,特别是我们程序袁用于比较两个项目的时候,最初使用的是Beyond Compare3一直用着挺好的,几年前更新了版本4,用着用着就提示试用期30天已过期,于是我尝试如下步骤:
    思科交换机如何进行备份与还原?
    vSphere ESXi 6.7 注册码(有效)
    VMware ESXi 6.7密码正确不能登录
    Esxi 6.5 6.7的root密码经过一段时间就不可用的解决方法
    Windows Server 2012 R2 安装密钥
    ubuntu 16 添加多个IP
  • 原文地址:https://www.cnblogs.com/tangmiao/p/10844434.html
Copyright © 2020-2023  润新知