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

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

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-05-10 16:09:48

本篇文章主要介绍一下如何使用 boost中的shared_ptr 和模板编程来实现一个简易的缓冲池(CachePool)
该缓冲池的内部存储容器为 stl 中的map>
其中 key 默认是 std::string 类型
value 的类型是根据创建 CachePool 的时候所使用的类型指针,该指针用来指向缓冲区中缓存的对象在系统中的位置
所包含的方法有: 
                       1. 从缓冲池中根据索引值(map->key) 来返回查找到的指向对象拷贝的指针(可修改)
                        2. 从缓冲池中根据索引值(map->key) 来返回查找到的指向对象本体的指针(不可修改)
                        3. 向缓冲池中根据索引值(map->key) 来插入传入的对象拷贝的指针
                        4. 清空缓冲池中指定索引值(map->key) 的缓冲区,并释放该块缓冲区空间
                        5. 清空缓冲池中全部缓冲区,并安全释放所有的缓冲空间

编程环境: vs2010 
涉及到的编程知识 : c++ , boost , template ,stl



//SharedPtr.hpp

点击(此处)折叠或打开

  1. #ifndef SHARED_PTR_HPP__
  2. #define SHARED_PTR_HPP__


  3. #include <boost/shared_ptr.hpp>
  4. template <typename T>
  5. class sharedptr
  6. {
  7. public :
  8.     typedef boost::shared_ptr<T> type ;
  9. } ;


  10. #endif
//CachePool.hpp

点击(此处)折叠或打开

  1. #ifndef CACHE_POOL_HPP__
  2. #define CACHE_POOL_HPP__

  3. #include "SharedPtr.hpp"
  4. #include <string>
  5. #include <map>

  6. template <typename T, typename Id = std::string>
  7. class CachePool
  8. {
  9. public :
  10.     typedef typename sharedptr<T>::type TPtr ;

  11.     TPtr get( const Id &inId )
  12.     {
  13.         TPtr result = mCachePool[inId] ;

  14.         if ( result )
  15.             result = TPtr( new T(*result)) ;
  16.         return result ;
  17.     }

  18.     const TPtr getConst ( const Id &inId )
  19.     {
  20.         return mCachePool[inId] ;
  21.     }

  22.     void add ( TPtr inPtr , const Id &inId )
  23.     {
  24.         TPtr copy( new T(*inPtr )) ;
  25.         // new T( object )
  26.         mCachePool[inId] = copy ;
  27.     }

  28.     void drop ()
  29.     {
  30.         mCachePool.clear() ;
  31.     }

  32.     void drop ( const Id &inId )
  33.     {
  34.         mCachePool[inId].reset() ;
  35.     }


  36. private :
  37.     typedef std::map<Id, TPtr> CachePoolCont ;
  38.     CachePoolCont mCachePool ;
  39. } ;
  40. #endif //CachePool.hpp

//Main.cpp

点击(此处)折叠或打开 

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

  5. #include "SharedPtr.hpp"
  6. #include "CachePool.hpp"

  7. class obj1
  8. {
  9. public :

  10.     obj1( std::string name )
  11.     {
  12.         std::cout <<"create "<< name << std::endl ;
  13.         _name = name ;
  14.         
  15.     }

  16.     void print ()
  17.     {
  18.         std::cout<<"print " << _name <<std::endl ;
  19.     }
  20.     
  21. public:
  22.     std::string _name ;
  23. } ;



  24. typedef struct btnode
  25. {
  26.     std::string name ;
  27.     btnode () :name(NULL)
  28.     {}
  29.     btnode ( std::string & n ) : name (n)
  30.     {}
  31. }BtNode_t ;


  32. int main ()
  33. {
  34.   
  35. // here we test whether type of T can be a class type

  36.     sharedptr<obj1>::type p1 ( new obj1("obj1->Aimer")) ;
  37.  // create a object and points it by shared_ptr

  38.     CachePool<obj1> cachePool1 ;                        
  39.  // create a CachePool just store obj1 type 

  40.     cachePool1.add( p1 , p1->_name ) ;                    
  41. // adds the obj1's instance 's shared pointer into the CachePool

  42.     p1 = cachePool1.get( "obj1->Aimer" ) ;                
  43. // try to get object from CachePool with key = "obj1->Aimer"


  44.     std::cout << "name " << p1->_name << std::endl ;    
  45. // out put the object 's name 

  46.     
  47. // here we test whether type of T can be a struct type

  48.     CachePool<BtNode_t> BtNodePool ;                        
  49. // creates a object and points it by shared pointer

  50.     sharedptr<BtNode_t>::type pNode ( new BtNode_t( std::string("node->Kokia")) )
  51.     // create a struct instance and points it by shared pointer with type of BtNode_t

  52.     BtNodePool.add(pNode , pNode->name ) ;                   
  53.     // insert the shared pointer (with type of BtNode_t) into CachePool (type of BtNode_t )

  54.     pNode = BtNodePool.getConst("node->Kokia") ;            
  55. // try to get object from Cache< BtNode_t type> with key = "ode->Kokia"

  56.     std::cout << "name " << pNode->name << std::endl ;        
  57. // out put get object 's name 

  58.     system("pause") ;
  59.     return 0 ;
  60. }

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

losper2018-03-27 14:49:35

夏目玲子:楼上说的没错,比较有新意的也就是模板的使用了~ 
其实你不说的话,也不会有人知道,这个博客实质上是由我们两个人共同运营的.....

孤单寂寞冷

回复 | 举报

夏目玲子2015-06-03 14:49:45

夏目玲子:其实在不断学习中才发现,这里实现的 缓冲池也好,缓冲队列也好,仅仅是在数据结构上面简单描述一下 内存空间的划分与回收,

实质上说,所编写的代码仅仅是在“仿真” 内存的简单分配与回收,但是却并没有对程序运行的系统中的内存有丝毫 的 影响。

可以直接参与到内存空间的划分与回收的 boost 库函数应该是
boost::interprocess 中的一些库函数。 也是我最近在看并且打算写成博客的内容

楼上说的没错,比较有新意的也就是模板的使用了~ 
其实你不说的话,也不会有人知道,这个博客实质上是由我们两个人共同运营的.....

回复 | 举报

夏目玲子2015-06-03 14:47:34

其实在不断学习中才发现,这里实现的 缓冲池也好,缓冲队列也好,仅仅是在数据结构上面简单描述一下 内存空间的划分与回收,

实质上说,所编写的代码仅仅是在“仿真” 内存的简单分配与回收,但是却并没有对程序运行的系统中的内存有丝毫 的 影响。

可以直接参与到内存空间的划分与回收的 boost 库函数应该是
boost::interprocess 中的一些库函数。 也是我最近在看并且打算写成博客的内容