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

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);

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

C++自己用模板减少工作量的更多相关文章

  1. django使用html模板减少代码

    看下面两个页面: —————————————————————————————————————————————————————————————————————————————————— 一个显示文章列表 ...

  2. 数据层使用DBHelper.dll来减少工作量

    目前在需求确定了以后进行开发的步骤一般是这样的:建立数据表,建立model,构建数据操作层,最后在页面中进行调用.关于数据操作层,因为大量的操作都集中在增加.更新.删除等简易操作,而我之前的写法是每次 ...

  3. latex如何定义宏,插图统一尺寸减少工作量

    问题背景是这样的,因为我要在文中插入一系列的图片,但是这些图片的大小我要保持一致,来达到预期的效果. 比如我有三个figure,这三个figure中,每个figure里面有两行,5列图片,我想要的是, ...

  4. K8s Helm安装配置入门

    作为k8s现在主流的一种包部署方式,尽管不用,也需要进行一些了解.因为,它确实太流行了. 这一套太极拳打下来,感觉helm这种部署,目前还不太适合于我们公司的应用场景.它更适合需要手工编程各种yaml ...

  5. AspNet MVC与T4,我定制的视图模板

    一. 遇到的问题 文章开头部分想先说一下自己的困惑,在用AspNet MVC时,完成Action的编写,然后添加一个视图,这个时候弹出一个添加视图的选项窗口,如下: 很熟悉吧,继续上面说的,我添加一个 ...

  6. 关于使用ModelSim中编写testbench模板问题

    对于初学者来说写Testbench测试文件还是比较困难的,但Modelsim和quartus ii都提供了模板,下面就如何使用Modelsim提供的模板进行操作. Modelsim提供了很多Testb ...

  7. flask中jinjia2模板引擎使用详解5

    接上文 宏 可以理解为函数,即把一些常用的模板片段做好封装,以便于重用,减少工作量和维护难度. 宏的定义很简单: {%macro xxx()%} ##这里写内容 {%endmacro%}   下面引用 ...

  8. djiango的模板语言(template)

    老师的博客:http://www.cnblogs.com/liwenzhou/p/7931828.html 官方文档:https://docs.djangoproject.com/en/1.11/re ...

  9. eclipse 创建注释模板

    使用  Alt+Shift+J 可以快速注释. 我们每次手动敲入作者,时间,版本等信息,有一些重复,可通过设置eclipse注释模板,减少工作量. Window -> preference -& ...

随机推荐

  1. APIO 2017 商旅 洛谷3778

    Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...

  2. 禁用NX软件启动欢迎界面[转载]

    环境变量UGII_BACKGROUND_URL  NONE 转载自https://www.ugnx.net/wt/414

  3. 牛客多校第六场 G Is Today Friday? 蔡勒公式/排列

    题意: 有一堆日期,这些日期都是星期五,但是数字被映射成了字母A~J,现在让你求逆映射,如果存在多种答案,输出字典序最小的那个. 题解: 用蔡勒公式解决关于星期几的问题. 对于映射,可以用笔者刚刚学会 ...

  4. Java-Class-I:java.util.List

    ylbtech-Java-Class-I:java.util.List 1.返回顶部 1.1.import java.util.ArrayList;import java.util.List; 1.2 ...

  5. RoadFlow开源工作流源码-项目架构分析

    项目文件结构: 很明了一个标准的三层架构的系统. 表示层:Web 业务层:Business 数据访问层:Data 另外存在缓存层:Cache缓存 增加公共使用类库:Utility 下面以一个实例(系统 ...

  6. Tomacat7启动报错-org.apache.catalina.deploy.WebXml addFilter

    java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter at org.apache.tomcat.ut ...

  7. 使用Pyppeteer进行gmail模拟登录

    import asyncio import time from pyppeteer import launch async def gmailLogin(username, password, url ...

  8. 通过Matlab SDK 获取tushare数据

    概要说明 版本要求:Matlab需要2016b及以上版本 接口说明:可以用help pro_api和help pro_bar查看 demo程序:请参考tushare_pro_test.m文件 程序包下 ...

  9. **JLink Warning: Mis-aligned memory write: Address: 0x20000000, NumBytes: 2, Alignment: 2 (Halfword-aligned)

    网上也有同学遇到这个问题,http://www.openedv.com/thread-113049-1-3.html 根据他的经验我也重新安装了Jlink驱动: 顺便注意Dialog DLL:TARM ...

  10. JPA Query in 集合

    使用 :param的方式来传递参数,下面举个例子 @PersistenceContext ​​​​​​​EntityManager em @Override public List<Map> ...