Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563968
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-05-10 10:22:31

这篇文章中的实例代码用来掩饰如何声明与使用模板类中模板函数的实例
设计的初衷是这样的:
我想设计可以容纳任意类型对象的缓冲池,比如说,有 class obj1 , class obj2 ,class obj3 
通过将不同类型的对象传入到 CachePool 中之后,缓冲池会根据不同的类型来实例化出不同的缓冲区,
但是所有的缓冲区确实共享同一个缓冲空间的,也就是说,所有的缓冲区所访问的空间均来自于同一个缓冲池实例(CahcePool).
每个缓冲区 Cache* 通过索引来获取各自类型的缓冲对象,
但是实质上该方法是对CachePool 获取缓冲池中存放的方法的封装。

这就需要 CachePool 中可以容纳的对象的种类,并且在通过索引进行查找的时候,应该知道缓冲区的类型,
然后在缓冲池中的符合该类型的所有缓冲对象中查找索引值相匹配的对象。

CachePool|--> Cache1 ;
                                         |--> Cache2 ;
                                         |--> Cache3 ;

所以,在 Cache 中的get, set 方法就需要已知对象的类型,才能够访问<写入,读出>缓冲池中类型匹配的对象。

实例代码如下:

编程环境: vs 2010

// template's template test

点击(此处)折叠或打开

  1. #include <cstdio>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>

  5. template <typename TYPELIST>
  6. class Cache
  7. {
  8.     public :
  9.     template <typename T>
  10.     void get ( const int &inId )
  11.     {
  12.         std::cout << "input " << inId << std::endl ;
  13.     }
  14. } ;

  15. int main ( void )
  16. {
  17.     typedef std::vector<int> CacheAbleTypes ;
  18.     Cache<CacheAbleTypes> myCache ;
  19.     
  20.     myCache.get<std::string>(20) ;

  21.     return 0 ;
  22. }

// simple cache cache_pool model test
// use boost's mpl vector 

点击(此处)折叠或打开

  1. #include <cstdio>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>

  5. #include <boost/mpl/vector.hpp>
  6. #include <boost/shared_ptr.hpp>

  7. template <typename T>
  8. class sharedptr
  9. {
  10.     public :
  11.        typedef boost::shared_ptr<T> Type ;
  12. } ;



  13. class obj1
  14. {
  15.     public :
  16.     
  17.      static void print ()
  18.      {
  19.         std::cout << "obj1" << std::endl ;
  20.      }
  21. } ;

  22. class obj2
  23. {
  24.     public :
  25.    
  26.     void print ()
  27.      {
  28.         std::cout << "obj2" << std::endl ;
  29.      }
  30.     
  31. } ;

  32. class obj3
  33. {
  34.     public :
  35.  
  36.      void print ()
  37.      {
  38.         std::cout << "obj3" << std::endl ;
  39.      }
  40. } ;


  41. typedef boost::mpl::vector<obj1, obj2,obj2> CacheableTypes ;


  42. template <typename TYPELIST>
  43. class Cache
  44. {
  45.     public :
  46.     template <typename T>
  47.     void print ( T & obj )
  48.     {
  49.      // typename sharedptr<T>::Type Tptr( new T(obj) ) ;
  50.         
  51.        // Tptr->print() ;
  52.         obj.print () ;
  53.         
  54.       // std::cout << Tptr->print () << std::endl ;
  55.      
  56.     }
  57.     
  58. } ;


  59. int main ( void )
  60. {
  61.    Cache<CacheableTypes> myCache ; // 在后续的代码中将会在 Cache 中创建一个仅能容那 CacheableTypes 类型的哈希散列表作为 CachePool的本体
  62.                                    // 在本代码中通过 boost::mpl::vector 定义的 CacheableTypes 然而并没有什么卵用...
  63.    obj1 O1 ;
  64.    obj2 O2 ;
  65.    obj3 O3 ;

  66.    // 在这里初步实现了,方法可以根据传入的不同类型调用各自的方法
  67.    myCache.print<obj1>(O1) ;
  68.    myCache.print<obj2>(O2) ;
  69.    myCache.print<obj3>(O3) ;
  70.    
  71.     return 0 ;
  72. }
end


阅读(3870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~