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. [驱动开发] windbg符号表

    新建"环境变量 - 系统":_NT_SYMBOL_PATH 值为:SRV*FullDirPath*http://msdl.microsoft.com/download/symbol ...

  2. wampserver 403 禁止访问

    解决方法:修改Apache配置文件httpd.conf,注释掉 deny from all:将Allow from 127.0.0.1改为Allow from all

  3. Ajax作用、及Ajax函数的编写

    关于Ajax 指的是异步 (Asynchronous JavaScript and XML) <异步的javascript和XML> 1. Ajax并非缩写词,而是由Jesse James ...

  4. delegate和protocol

    协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...

  5. psp记录个人项目花费时间

    预估时间 120min 设计分析时间 30min 具体设计时间 ≍40min 编码时间 ≍1h 测试时间 10min 整理结论时间 10min 总结与探讨时间 5min

  6. ListView的item里面控件文本颜色修改

    @SuppressLint("InflateParams") @Override public View getChildView(int groupPosition, int c ...

  7. Kali Linux additional tools setup

    The steps are pretty straight forward. The only tool that might cause some confusion is SMBexec. Thi ...

  8. 如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:

    32位的Windows:---------------------------------------------------------------------------1. 运行->cmd ...

  9. DotNet 资源大全中文版【转】

    转自:https://github.com/jobbole/awesome-dotnet-cn 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesom ...

  10. 使用扩展方法将DataTable转换为List<T>

    在将DataTable转换为List<T>时,找到了网上的方案,原文链接:http://stackoverflow.com/questions/4593663/fetch-datarow- ...