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

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. 返回字符串中最长连续相同字串的长度---正则实现与JavaScript实现

    JavaScript 实现 let str = 'AAABBAACCAAAADDE' function continuousString(str) { let finalObj = {} let te ...

  2. bzoj1029题解

    [解题思路] 贪心,先按结束时间排序,从左到右扫描过去,如果当前建筑可以修复则入大根堆,否则,若其修复时间比堆顶小则弹出堆顶并入堆,处理完堆后则更新总时间.复杂度O(nlog2n). [参考代码] # ...

  3. jQuery.speech实现文本转语音播报功能

    先放一个实例的地址https://github.com/wenco/speech jQuery.speech是用jQuery写的扩展插件,主要是用来语音播报. 接口调用百度翻译的接口,所以存在url参 ...

  4. hive的行列互转

    行转列 多行转多列 数据表 row2col col1 col2 col3 a c 1 a d 2 a e 3 b c 4 b d 5 b e 6 现在要将其转化为: col1 c d e a 1 2 ...

  5. NX二次开发-UFUN拾取向量对话框UF_UI_specify_vector

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //拾取向量对话框 ], pnt[]; int mode = UF_UI ...

  6. NX二次开发-UFUN体找面函数UF_MODL_ask_body_faces

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> #include <u ...

  7. Ubuntu 18.04.2 aliases 设置永久生效解决方案

    设置 临时 alias alias ll="ls -al" 缺点是下次登录时就不生效了 永久生效解决方案 进入到 etc 文件夹下 cd /etc/ 创建 bash_aliases ...

  8. (转)python基础之迭代器协议和生成器(一)

    一 递归和迭代 二 什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前 ...

  9. js 关闭页面(Scripts may close only the windows that were opened by it.)

    传送http://blog.csdn.net/kuangfengbuyi/article/details/52052301 js关闭当前页面,当该页面不是其他页面打开的,而是直接输入url, 直接用w ...

  10. Python中字典的详细用法

    #字典 #字典是Python中唯一内建的映射类型.字典中没有特殊的顺序,但都是存储在一个特定的键(key)下面,键可以是数字,字符串,甚至是元组 #一.字典的使用 #在某些情况下,字典比列表更加适用: ...