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

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. Django 查漏补缺

    Django 查漏补缺 Django  内容回顾: 一. Http 请求本质: 网络传输,运用socket Django程序: socket 服务端 a. 服务端监听IP和端口 b. 浏览器发送请求 ...

  2. js实现canvas保存图片为png格式并下载到本地

    canvas 保存图片 下载到本地 function base64Img2Blob(code){ var parts = code.split(';base64,'); var contentType ...

  3. 利用html2canvas截图,得到base64上传ajax

    <script type="text/javascript" src="js/html2canvas.js"></script> //布 ...

  4. luoguP1134 阶乘问题 [数论]

    题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...

  5. luoguP4768 [NOI2018]归程

    传送门 kruskal重构树: kruskal合并两个联通块时合并的边一定是联通块中权值最大的边,小于等于这条边的边所能联通的所有点在这个联通块中. 在合并两个联通块的时候新建一个点作为两个联通块代表 ...

  6. char*转LPCWSTR【转载】

    文章转载自https://blog.csdn.net/zhouxuguang236/article/details/8761497 通过MultiByteToWideChar函数转换 MultiByt ...

  7. IOS自动化打包介绍

    IOS自动化打包介绍  标签: app打包 , Ios打包 , iphone打包 , iphone自动化打渠道包    分类:无线客户端技术, 贴吧技术 摘要 随着苹果手持设备用户的不断增加,ios应 ...

  8. POJ3241 最小曼哈顿距离生成树 - 真有趣哇

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  给你n个坐标, ...

  9. Day 11:函数装饰器

    在说装饰器前,先说一个东西,再Python里,有一个 一切皆对象,一切皆变量. 例: def hello(name="sunjinyao"): return "hi &q ...

  10. 史上最全Html和CSS布局技巧

      单列布局水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元 ...