C++ 基于Policy 的 模板编程


在没真正接触C++  模板编程之前,真的没有想到C++ 还可以这么用,最大的感触是:太灵活了,太强大了。最初接触模板威力还是在Delta3d中,感觉里面的模板使用实在是灵活与方便,特别是dtAI中使用了大量的模板,大大增强了库的可扩展性。

本文基于《C++ 设计新思维》 而写。 下载见

C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码

读C++ Primer 之构造函数陷阱

读C++ Primer 之智能指针

读C++ Primer 之句柄类

将C语言梳理一下,分布在以下10个章节中:

  1. Linux-C成长之路(一):Linux下C编程概要
  2. Linux-C成长之路(二):基本数据类型
  3. Linux-C成长之路(三):基本IO函数操作
  4. Linux-C成长之路(四):运算符
  5. Linux-C成长之路(五):控制流
  6. Linux-C成长之路(六):函数要义
  7. Linux-C成长之路(七):数组与指针
  8. Linux-C成长之路(八):存储类,动态内存
  9. Linux-C成长之路(九):复合数据类型
  10. Linux-C成长之路(十):其他高级议题

先看一段代码:

#include <iostream>
#include <vector>
#include <list>
using namespace std;
//--------------------------------------------------------------------------------
/////////Widget类型
class SliderWidget
{
public:
 SliderWidget()
 {
  std::cout<<"Slider Widget created"<<std::endl;
 }
};

class BoxWidget
{
public:
 BoxWidget()
 {
  std::cout<<"Box Widget created"<<std::endl;
 }
};
//--------------------------------------------------------------------------------
//创建widget方法

template <class T>
class OpNewCreateor
{
public:
 static T* create()
 {
  return new T;
 }
protected:
 ~OpNewCreateor(){}
};

template <class T>
class MallocCreator
{
public:
 static T* create()
 {
  void * buf = std::malloc(sizeof(T));
  if(!buf) return 0;

  return new(buf) T;
 }
protected:
 ~MallocCreator(){}
};

template <class T>
class PrototypeCreator
{
public:
 PrototypeCreator(T* pObj = 0)
  :pPrototype(pObj)
 {

 }
 T* create()
 {
  return pPrototype ? pPrototype->clone() : 0;
 }
 T* getPrototype(){return pPrototype;}
 void setPrototype(T*pObj){pPrototype = pObj;}

protected:
 ~PrototypeCreator(){}
private:
 T* pPrototype;
};
//--------------------------------------------------------------------------------
//存储widget容器类型
template<class T>
class ContainerVec
{
public:
 void push(T* widget)
 {
  mVecContainer.push_back(widget);
 }
//protected://Container 不能是保护类型,因为WidgetManager 不继承此类
 ~ContainerVec(){}

private:
 std::vector<T*> mVecContainer;//Vector容器
};

template <class T>
class ContainerList
{
public:
 void push(T* widget)
 {
  mListContainer.insert(widget);
 }
 
 ~ContainerList(){}//Container 不能是保护类型,因为WidgetManager 不继承此类
private:
 std::list<T*> mListContainer;//List容器
};
//--------------------------------------------------------------------------------
//--------widget管理类
template <
 class T,
 template<class > class CreationPolicy = MallocCreator,
 template<class > class Container = ContainerVec
>
class WidgetManager :public CreationPolicy<T>     
{
public:
 typedef CreationPolicy<T> BaseClass;
 T* create()
 {
  T* tmp =  BaseClass::create();
  mContainer.push(tmp);
  return tmp;
 }


private:
 Container<T> mContainer;
};

//--------------------------------------------------------------------------------
typedef WidgetManager<BoxWidget,OpNewCreateor,ContainerVec> BoxWidgetManager;
typedef WidgetManager<SliderWidget,OpNewCreateor,ContainerList> SliderWidgetManager;
//--------------------------------------------------------------------------------

 
int main()
{
 BoxWidgetManager boxWidgetManager;

 BoxWidget * boxWidget = boxWidgetManager.create();


  cout << typeid(BoxWidgetManager).name() << endl; 

 

 system( "pause");
}

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容