template <class Apolicy>
class Host
{
  Apolicy direct_policy_use;
  Apolicy <SomeInternalType> InternalClone;  // Problem 1: Can't do this
};
template <class T, template <class T> class Apolicy>
class Host2
{
  Apolicy <T> common_use;  
  Apolicy <SomeInternalType> InternalClone;  
  // Can do this now but 
  // Problem 2: policies that require more than one type parameter can't participate.
};
Solution and Sample Code[edit]
A member template struct (called rebind) is used to pass a different type parameter to the policy class template. For example,
template <typename T>
class NiftyAlloc
{
  public:
    template <typename Other>
    struct rebind // The Policy Clone idiom
    { 
       typedef NiftyAlloc <Other> other;
    };
    //...
};
template <typename T, class Alloc = NiftyAlloc <T> >
class Vector 
{
  public:
    typedef typename Alloc::template rebind<long>::other ClonePolicy;
    // Here, Alloc may not be a template class or a parametrized instantiation of
    // a class that takes unknown number of type parameters.
}; 

#include <iostream>

#include <vector>
#include <list>
using namespace std;
class SliderWidget
{
public:
 SliderWidget()
 {
  std::cout<<"Slider Widget created"<<std::endl;
 }
};
class BoxWidget
{
public:
 BoxWidget()
 {
  std::cout<<"Box Widget created"<<std::endl;
 }
};
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;
};
template<class T>
class ContainerVec
{
public:
 void push(T* widget)
 {
  mVecContainer.push_back(widget);
 }
 ~ContainerVec(){}
private:
 std::vector<T*> mVecContainer;
};
template <class T>
class ContainerList
{
public:
 void push(T* widget)
 {
  mListContainer.insert(widget);
 }
 
 ~ContainerList(){}
private:
 std::list<T*> mListContainer;
};
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");
}

policy的更多相关文章

  1. Security Policy:行级安全(Row-Level Security)

    行级安全RLS(Row-Level Security)是在数据行级别上控制用户的访问,控制用户只能访问数据库表的特定数据行.断言是逻辑表达式,在SQL Server 2016中,RLS是基于安全断言( ...

  2. Content Security Policy 入门教程

    阮一峰文章:Content Security Policy 入门教程

  3. 使用 SecurityManager 和 Policy File 管理 Java 程序的权限

    参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...

  4. Information Management Policy(信息管理策略)的使用范例

    基础知识 很多人都会定期收拾自己的书架或者抽屉,把里面过旧的资料拿走,为新的资料腾出空间来,这样既可以节省空间,而且当冗余资料过多的时候也会降低你查找的速度和效率.那么,在企业的SharePoint中 ...

  5. Utility2:Appropriate Evaluation Policy

    UCP收集所有Managed Instance的数据的机制,是通过启用各个Managed Instances上的Collection Set:Utility information(位于Managem ...

  6. Policy Management

    策略管理用于管理数据库实例.数据库以及数据库对象的各种属性,Policy Management 位于Management Catalog下, 一,Basic concepts 引用园子里深蓝的博客&l ...

  7. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...

  8. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9001/api/size/get. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:/ ...

  9. A Quick Introduction to Linux Policy Routing

    A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...

  10. HBase change split policy on an existing table

    hbase(main)::> create 'test_table_region', 'username' row(s) in 1.2150 seconds hbase(main)::> ...

随机推荐

  1. CentOS7.1配置源

    现在网上最新的是CentOS7.1, 但是在配置国内流行的163源的时候,网上的文章大多数我这里都通不过. 错误信息大概是: One of the configured repositories fa ...

  2. C# 属性控件的应用(备忘)

    自己定义的控件属性:[Browsable(true),Bindable(true),Category("数据"),DefaultValue(""),Locali ...

  3. Voreen (一) GPU Raycast主流程

    最近看了下Voreen这个开源体绘制快速原型开发包.Voreen里面集合了不少体绘制的基本算法,还是值得研究和试验图像显示效果的.今天第一篇以它自带工程为例,介绍下DICOM数据读取和GPU Rayc ...

  4. Swift 编程语言自己实践 -自己在Xcode6 动手写20140603

    Swift 是什么,大家都回去百度或者Google,有的甚至认为是Taylor Swift(她是我的偶像),但是如果今天在百度百科里搜索绝对没有说是Apple最新推出的编程语言,因为是在2014年6月 ...

  5. MySQL 重装

    由于之前第一次装MySQL,默认的datadir在启动盘中,我想要将datadir移动到更大的存储盘中.无奈网上的各种文章的方法在我这里总是不work.我决定重新用homebrew来装一遍MySQL. ...

  6. ABAP屏幕设计

    转自 http://www.cnblogs.com/aBaoRong/archive/2012/06/05/2536591.html abap 屏幕控制 ******************** 屏幕 ...

  7. Oracle笔记3-高级查询

    高级查询 1.关联查询 作用:可以跨多表查询 --查询出员工的名字和他所在部门的名字 //错误//select first_name,name from s_emp,s_dept; //错误原因:产生 ...

  8. 修改PHP上传文件大小限制的方法

    感谢分享,原文地址:http://www.cnblogs.com/newsouls/archive/2012/12/27/2835628.html 修改PHP上传文件大小限制的方法1. 一般的文件上传 ...

  9. Convert.ToInt32()与int.Parse()的区别

    Convert.ToInt32()与int.Parse()的区别 (1)这两个方法的最大不同是它们对null值的处理方法:    Convert.ToInt32(null)会返回0而不会产生任何异常, ...

  10. Codeforces Round #379 (Div. 2) E. Anton and Tree

    题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...