网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VC编程 >> 文章正文
  boost.shared_ptr源码重列            【字体:
boost.shared_ptr源码重列
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    
正在装载数据……
 

#pragma once

 

//shared_ptr的简单实现版本

//基于引用记数的智能指针

//它可以和stl容器完美的配合

namespace kimi_boost

{

 

template<class T>

class shared_ptr

{

public:

    typedef T element_type;

    typedef T value_type;

    typedef T * pointer;

    typedef T& reference;

   typedef unsigned long size_type;

 

 

 

   explicit shared_ptr(T* p=0) : px(p)

   {

      try { pn = new size_type(1); }

      catch (...) { delete(p); throw; }

   }

   shared_ptr& operator= (T* p)

   {

      if(this->px == p) return *this;

      dispose();

      try { pn = new size_type(1); }

      catch (...) { delete(p); throw; }

      px=p;

      return *this;

   }

 

 

 

   shared_ptr(const shared_ptr& r) throw(): px(r.px)

   {

      ++*r.pn;

      pn = r.pn;

   }

   shared_ptr& operator= (const shared_ptr& r) throw()

   {

      if(this == &r) return *this;

      dispose();

      px = r.px;

      ++*r.pn;

      pn = r.pn;

      return *this;

   }

 

 

 

   template<typename Y> friend class shared_ptr;

   //为了让有继续关系的shared_ptr类型赋值或构造

   template<typename Y>

   shared_ptr(const shared_ptr<Y>& r)

   {

        px = r.px;

      ++*r.pn;

        pn = r.pn; // shared_count::op= doesn't throw

   }

   template<typename Y>

   shared_ptr& operator= (const shared_ptr<Y>& r)

   {

      dispose();

        px = r.px;

      ++*r.pn;

        pn = r.pn; // shared_count::op= doesn't throw

      return *this;

   }

 

 

 

   template<typename Y>

   shared_ptr(Y* py)

   {

      try { pn = new size_type(1); }

      catch (...) { delete(py); throw; }

      px=py;

   }

   template<typename Y>

   shared_ptr& operator= (Y* py)

   {

      if(this->px == py) return *this;

      dispose();

      try { pn = new size_type(1); }

      catch (...) { delete(py); throw; }

      px=py;

      return *this;

   }

 

 

   ~shared_ptr() { dispose(); }

 

 

 

 

 

 

 

 

   void reset(T* p=0)

   {

      if ( px == p ) return;

      if (--*pn == 0)

      { delete(px); }

      else

      { // allocate new reference

        // counter

        // fix: divvent leak if new throws

        try { pn = new size_type; }

        catch (...) {

        // undo effect of —*pn above to

        // meet effects guarantee

        ++*pn;

        delete(p);

        throw;

        } // catch

      } // allocate new reference counter

      *pn = 1;

      px = p;

   } // reset

 

 

   reference operator*() const throw(){ return *px; }

   pointer operator->() const throw(){ return px; }

   pointer get() const throw(){ return px; }

   size_type use_count() const throw()//

   { return *pn; }

   bool unique() const throw()//

   { return *pn == 1; }

 

private:

   void dispose() throw()

   {

      if (--*pn == 0)

        { delete px; delete pn; }

   }

 

    T * px;    // contained pointer

    size_type* pn;  // reference counter

 

};  // shared_ptr

 

 

 

template<typename A,typename B>

inline bool operator==(shared_ptr<A> const & l, shared_ptr<B> const & r)

{

   return l.get() == r.get();

}

 

template<typename A,typename B>

inline bool operator!=(shared_ptr<A> const & l, shared_ptr<B> const & r)

{

   return l.get() != r.get();

}

 

}//namespace kimi_boost

 

 

 




本文来源:http://blog.csdn.net/laibach0304/archive/2007/08/24/1758086.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     directx 图形接口指南(…
     win2k下的api函数的拦截
     用crypto  api  实现公钥…
     根据别人的md5源码封装的…
     vc中使用gdi+合并jpg图片
     document/view的交互 --…
     windows下的函数hook技术
     windows api函数大全一
     用vc 6.0实现串行通信的…
     vc++技术内幕(第四版)…
  • page、request、session、ap…

  • JSF 的性能远不及 JSP 或 St…

  • Struts2学习:在struts2中集…

  • 从头开始认识jboss

  • SPRING+STRUTS+HIBERNATE登录…

  • JSP标准模板库(JSTL)入门教…

  • Cookie又见Cookie-使用Html…

  • 搭建JSTL运行环境

  • struts多附件上传

  • jsf自定义toolbar组件

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David