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);
就行了,要学会用高级工具!
C++自己用模板减少工作量的更多相关文章
- django使用html模板减少代码
看下面两个页面: —————————————————————————————————————————————————————————————————————————————————— 一个显示文章列表 ...
- 数据层使用DBHelper.dll来减少工作量
目前在需求确定了以后进行开发的步骤一般是这样的:建立数据表,建立model,构建数据操作层,最后在页面中进行调用.关于数据操作层,因为大量的操作都集中在增加.更新.删除等简易操作,而我之前的写法是每次 ...
- latex如何定义宏,插图统一尺寸减少工作量
问题背景是这样的,因为我要在文中插入一系列的图片,但是这些图片的大小我要保持一致,来达到预期的效果. 比如我有三个figure,这三个figure中,每个figure里面有两行,5列图片,我想要的是, ...
- K8s Helm安装配置入门
作为k8s现在主流的一种包部署方式,尽管不用,也需要进行一些了解.因为,它确实太流行了. 这一套太极拳打下来,感觉helm这种部署,目前还不太适合于我们公司的应用场景.它更适合需要手工编程各种yaml ...
- AspNet MVC与T4,我定制的视图模板
一. 遇到的问题 文章开头部分想先说一下自己的困惑,在用AspNet MVC时,完成Action的编写,然后添加一个视图,这个时候弹出一个添加视图的选项窗口,如下: 很熟悉吧,继续上面说的,我添加一个 ...
- 关于使用ModelSim中编写testbench模板问题
对于初学者来说写Testbench测试文件还是比较困难的,但Modelsim和quartus ii都提供了模板,下面就如何使用Modelsim提供的模板进行操作. Modelsim提供了很多Testb ...
- flask中jinjia2模板引擎使用详解5
接上文 宏 可以理解为函数,即把一些常用的模板片段做好封装,以便于重用,减少工作量和维护难度. 宏的定义很简单: {%macro xxx()%} ##这里写内容 {%endmacro%} 下面引用 ...
- djiango的模板语言(template)
老师的博客:http://www.cnblogs.com/liwenzhou/p/7931828.html 官方文档:https://docs.djangoproject.com/en/1.11/re ...
- eclipse 创建注释模板
使用 Alt+Shift+J 可以快速注释. 我们每次手动敲入作者,时间,版本等信息,有一些重复,可通过设置eclipse注释模板,减少工作量. Window -> preference -& ...
随机推荐
- DELPHI中如何让FORM窗体透明,只显示控件?
DELPHI中如何让FORM窗体透明,只显示控件?分享到: 对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:7largewanglargewanglargewang等级:Blank ...
- 浅谈使用RestKit将服务器的Json直接映射为本地对象
RestKit是一个主要用于iOS上网络通信的开源框架,除了发送请求与接受响应这些基本功能外,还附带coredata,以及将远程JSON映射为本地对象的功能.目前版本0.9.3,coredata还不是 ...
- error C1083: 无法打开包括文件: “ui_roadquery.h”: No such file or directory c:\users\administrator\desktop\g_w_j\04 开发阶段\开发工程\imcshowapp\imcshowapp\roadquery.h 5 1 IMCShowApp
1.我这里出现的原因是忘记将项目文件添加进入项目中,在项目中右击添加即可
- 文本数据增量导入到mysql
实现思路: 实现Java读取TXT文件中的内容并存到内存,将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则 ...
- java 读取mysql中数据 并取出
public static String url = null; public static String username = null; public static String password ...
- 活动:月末送Java技术书福利|抽奖
本公众号运营了快一年了 原创干货超过200+ 收获了也快1W粉丝 这么多粉丝-- 送书活动怎能少? 虽然这次我们是有备而来 但是-- 所有书籍为作者自掏腰包 所以本次送书数量有限 不能满足到所有人 重 ...
- 《转》python 10 集合
自 http://www.cnblogs.com/BeginMan/p/3160565.html 一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除) ...
- 解决通过vmware克隆虚拟机后,无法上网的问题
注意:如果源主机是CentOS 6.8,复制出来的机器会出现无法上网. 如果源主机是CentOS 7,复制出来的机器可以正常上网.复制后,只要改下IP地址即可上网. 出现该问题的原因是,我们克隆后,将 ...
- 2019-8-31-dotnet-判断程序当前使用管理员运行降低权使用普通权限运行
title author date CreateTime categories dotnet 判断程序当前使用管理员运行降低权使用普通权限运行 lindexi 2019-08-31 16:55:58 ...
- el-dialog(点击左上角的关闭x)执行弹窗关闭之前的回调
绑定的事件: :before-close="handleDialogClose" html: <!-- 新增.编辑弹窗 --> <el-dialog :close ...