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. PC端和手机访问调用不同的页面,JS和PHP不同方法

    js方法: var urlqr = location.href;    urlqr = urlqr.replace(/iphoneF_/, "pc")这里写正则替换页面地址 if( ...

  2. gridView RowDataBound事件 鼠标经过行颜色变化及根据字段值显示指定内容

    protected void gvBarInfo_RowDataBound(object sender, GridViewRowEventArgs e)        {            if ...

  3. 火车头wordpress免费万能发布模块和接口

    火车头wordpress免费万能发布模块和接口实测可以用 http://www.ggfenxiang8.com/?p=263

  4. pylot是一款开源的web性能测试工具

    pylot是一款开源的web性能测试工具,http://www.pylot.org/ 参考文档:http://www.pylot.org/gettingstarted.html很容易上手 使用分为以下 ...

  5. 优化 UltraEdit 打开大文件时的性能

    UltraEdit 原本就是被设计成可以打开超大文件的工具,只不过在默认情况下需要进行以下优化设置: 禁止临时文件 禁止显示行号 禁止文件(回车 & 换行符)转换 禁止代码折叠 禁止显示函数列 ...

  6. sublime Text3 编写java

    安装好jdk,并且配置好环境变量后(也可以放到sublime 中去配置) 2.一般的sublimetext 已经有了javac的buildsystem, 不过默认的配置不尽人意. 下面进行修改. 在S ...

  7. linux curl 下载jdk

    进入orecle官网,找到需要的安装包 需要cookie同意相关协议才能下载 curl -L "http://download.oracle.com/otn-pub/java/jdk/8u1 ...

  8. myeclipse里的调试快捷键

    好多时候在调试代码时,有的时候只会用F5,其他的快捷键却一概不知.今天百度查了一下其他快捷键调试的作用,总结如下 F5 (setp into ): 跳入当前执行的方法中 F6 (step over): ...

  9. 从小工到专家 ——读《Java程序员职场全攻略》有感

    从小工到专家 ——读<Java程序员职场全攻略>有感   <Java程序员职场全攻略>是以故事的形式,向读者介绍Java程序员的职场经验.作者牛开复在北京从事软件开发,已经是一 ...

  10. Oracle--用变量保存查询出来的值

    1:在我们一般编写存储过程中比较常见的是,习惯将查询出来的一个值赋值给一个变量,这个如何实现呢,用into,代码如下   Select ID into 变量1 from 表 where 条件 2:但当 ...