C++的模板(八):子系统

2024-06-24 14:28
文章标签 模板 c++ 子系统

本文主要是介绍C++的模板(八):子系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。

但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外可以被派生类构造,因此可以是多态的。这就有了一点子系统或者framework的意味了:


template <class Event, class Response>
class SubSystem{
public:map<Event*, Response*>  table;
public:void bind(Event *e, Response *r);void unbind(Event *e);
public:int OnMessage(Event *e);
};

上面示例的子系统,绑定了事件和响应的关系,并处理收到的消息。他不使用参数类型的构造函数。因此,就允许了Event和Response的多态存在。当然也可以直接用Event和Response的派生类来实例化这个模板,这样做就丢弃了派生类的别的分支了:

class Event {
public:int ev_id;~Event(){printf("~Event(id_%d)\n", ev_id);}
};
class Response{
public:virtual int handler()=0;virtual ~Response(){}
};
class ResponseA: public Response
{int a;
public:ResponseA(int A){ a=A;}int handler();~ResponseA(){printf("~R(%d)\n", a);}
};
class ResponseB: public Response
{double b;
public:ResponseB(double B){ b=B;}int handler();~ResponseB(){printf("~R(%lf)\n", b);}
};
template SubSystem<Event, Response>;template SubSystem<Event, ResponseA>;
template SubSystem<Event, ResponseB>;

好了,可以准备试用一下这个子系统。用bind()函数组织一张event, response 的对照表。然后用OnMessage()来处理消息。当然,这个模型太简陋了。真实的子系统会有更复杂的内容。

在试用之前,考虑一下在这个系统外参数类对象的构造。他们在main()函数中构造,并在函数结束时析构。或者用一个list管理器自动的管理他们:

template <class T>
class DMM {
public:list<T*> l;~DMM(){typename list<T*>::iterator it;it=l.begin();while(it!= l.end()) {delete *it;it++;}}template <class O>T* NewObj(O o) {T *r= new T(o); l.push_back(r); return r;}
};

这已经足够。怪名字DMM意思是动态内存管理。避免构造函数就有了多态和子系统。少即是多。看来这是真的了。

main()函数大致是这样的:

int main()
{Event *pe;Response *pr;SubSystem<Event,Response> mys;DMM<Event> de;DMM<Response> dr;pe = new Event;de.l.insert(de.l.end(), pe);pe->ev_id=1;pr= ((DMM<ResponseA>&)dr).NewObj(3);mys.bind(pe, pr);pe = new(Event);de.l.push_back(pe);pe->ev_id=2;pr= ((DMM<ResponseB>&)dr).NewObj(3.14);mys.bind(pe, pr);Event e;e.ev_id=2;pe=find(de.l, e);mys.OnMessage(pe);return 0;
}

这里有2个((DMM&)dr)、((DMM&)dr)的强制类型转换。ResponseA、ResponseB都是Response类的派生类。dr中的list存的又是他们的基类指针,这样转化当然没问题。

现在可以跑一下了,运行结果是:

response B(=3.140000)
~Event(id_2)
~R(3)
~R(3.140000)
~Event(id_1)
~Event(id_2)

最后,贴上完整的源代码。因为自称子系统,又写得太简陋了,很不好意思贴上来。自己扩充吧!

#include <stdio.h>
#include <list>
#include <map>
using namespace std;class Event {
public:int ev_id;~Event(){printf("~Event(id_%d)\n", ev_id);}
};
class Response{
public:virtual int handler()=0;virtual ~Response(){}
};
class ResponseA: public Response
{int a;
public:ResponseA(int A){ a=A;}int handler();~ResponseA(){printf("~R(%d)\n", a);}
};
class ResponseB: public Response
{double b;
public:ResponseB(double B){ b=B;}int handler();~ResponseB(){printf("~R(%lf)\n", b);}
};int ResponseA::handler()
{printf("handle A(=%d)\n",a);return 0;
}
int ResponseB::handler()
{printf("response B(=%lf)\n",b);return 0;
}template <class Event, class Response>
class SubSystem{
public:map<Event*, Response*>  table;
public:void bind(Event *e, Response *r);void unbind(Event *e);
public:int OnMessage(Event *e);
};template <class Event, class Response>
void SubSystem<Event,Response>::bind(Event *e, Response *r)
{table[e]=r;
}template <class Event, class Response>
void SubSystem<Event,Response>::unbind(Event *e)
{table.erase(e);
}template <class Event, class Response>
int SubSystem<Event,Response>::OnMessage(Event *e)
{Response *r;r= table[e];if(!r) return 0;return r->handler();
}template <class T>
class DMM {
public:list<T*> l;~DMM(){typename list<T*>::iterator it;it=l.begin();while(it!= l.end()) {delete *it;it++;}}template <class O>T* NewObj(O o) {T *r= new T(o); l.push_back(r); return r;}
};Event *find(list<Event*> &l, Event &e)
{list<Event*>::iterator i;for(i=l.begin(); i!=l.end(); i++) {if ((*i)->ev_id==e.ev_id) return *i;}return 0;
}int main()
{Event *pe;Response *pr;SubSystem<Event,Response> mys;DMM<Event> de;DMM<Response> dr;pe = new Event;de.l.insert(de.l.end(), pe);pe->ev_id=1;pr= ((DMM<ResponseA>&)dr).NewObj(3);mys.bind(pe, pr);pe = new(Event);de.l.push_back(pe);pe->ev_id=2;pr= ((DMM<ResponseB>&)dr).NewObj(3.14);mys.bind(pe, pr);Event e;e.ev_id=2;pe=find(de.l, e);mys.OnMessage(pe);return 0;
}

这篇关于C++的模板(八):子系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090406

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经