自已实现一个微小的shared_ptr

因为一些特殊的原因,不能直接在项目中使用boost::shared_ptr,但是实在是喜欢用STL和boost::shared_ptr的deleter。所以自己实现了一个小型版本的sharedptr。
现在可以在vc6和mingw32上编译了。因为代码很简单,所以可以放心用,放心改。。。。。

如果列位对boost::shared_ptrr的实现感兴趣,也可以看看这个…………巨简化的版本。

哦~对了,这个sharedptr不提供类似boost的enable_shared_from_this,因为区区暂时用不上。



/*代码处于公有域,作者放弃对以下代码的拥有权*/
#ifndef JR_SHAREDPTR_HPP
#define JR_SHAREDPTR_HPP

#include<memory>

class sharedptr_count_base{
public:
  explicit sharedptr_count_base():usecount(1)
  {
  }
  void addref()
  {
    ++usecount;
  }
  void release()
  {
    if(--usecount==0){
      dispose();
      delete this;
    }
  }
  long use_count()
  {
    return usecount;
  }
  virtual void dispose()=0;
  virtual ~sharedptr_count_base(){};
private:
 
  int usecount; 
};

template<typename T>
class sharedptr_count_impl:public sharedptr_count_base{
public:
  explicit sharedptr_count_impl(T* p):px(p)
  {
  }

  virtual void dispose()
  {
    if(px)
      delete px;
  }

private:
  T* px;
};

template<typename T, typename D>
class sharedptr_count_impld:public sharedptr_count_base{
public:
  explicit sharedptr_count_impld(T* p, D d):px(p),del(d)
  {
  }

  virtual void dispose()
  {
    del(px);
  }

private:
  T* px;
  D  del;
};


class sharedptr_count{
private:
  sharedptr_count_base * pi_;
public:

  explicit sharedptr_count():pi_(0)
  {
  }
 
  template<typename Y>
  explicit sharedptr_count(Y *p)
  {
    pi_ = new sharedptr_count_impl<Y>(p);
  }

  template<typename Y, typename D>
  explicit sharedptr_count(Y *p, D d)
  {
    pi_ = new sharedptr_count_impld<Y, D>(p, d);
  } 
 
  ~sharedptr_count()
  {
    if( pi_ != 0 ) pi_->release();
  }

  sharedptr_count& operator=(sharedptr_count const &other)
  {
    if( pi_!=other.pi_){
      if( pi_)
        pi_->release();
      pi_ = other.pi_;
      if( pi_)
        pi_->addref();
    }
    return *this;
  }

  explicit sharedptr_count(sharedptr_count const &other):pi_(other.pi_)
  {
    if (pi_)
      pi_->addref();
  }

  long use_count() const
  {
    return pi_ != 0? pi_->use_count(): 0;
  }
 
  void swap(sharedptr_count & other)
  {
    std::swap(pi_,other.pi_);
  }
 
};

template<typename T>
class sharedptr
{
private:
  typedef sharedptr<T> this_type;
public:
  typedef T element_type;
  typedef T value_type;
  typedef T * pointer;
  typedef T & reference;

  explicit sharedptr(): px(0), pn()
  {
  }
 
  template<typename Y>
  explicit sharedptr( Y * p ): px( p ), pn( p )
  {
  }

  template<typename Y, typename D>
  explicit sharedptr( Y * p, D d): px( p ), pn( p, d )
  {
  }

  explicit sharedptr( sharedptr &other )
  {
    px = other.px;
    pn = other.pn;
  }
 
  T* get() const
  {
    return px;
  }

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

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

  template<class Y>
  sharedptr(sharedptr<Y> const & r): px(r.px), pn(r.pn)
  {
  }
 
  sharedptr & operator=(sharedptr const &other)
  {
    px =other.px;
    pn =other.pn;
    return *this;
  }

  long use_count() const
  {
    return pn.use_count();
  }
 
  void swap(sharedptr<T> &other)
  {
    std::swap(px, other.px);
    pn.swap(other.pn);
  }

  void reset()
  {
    this_type().swap(*this);
  }
 
  template<class Y> void reset(Y * p)
  {
    ((p == 0 || p != px) ? (void)0 : _assert("p == 0 || p != px", "sharedptr.hpp", 236));
    this_type(p).swap(*this);
  }
 
private:
  T * px;
  sharedptr_count pn;
};

template<class T, class U> inline bool operator==(sharedptr<T> const & a, sharedptr<U> const & b)
{
  return a.get() == b.get();
}

template<class T, class U> inline bool operator!=(sharedptr<T> const & a, sharedptr<U> const & b)
{
  return a.get() != b.get();
}

template<class T, class U> inline bool operator<(sharedptr<T> const & a, sharedptr<U> const & b)
{
  return a.get() < b.get();
}

#endif
 
以下是一个简单的shared_ptr实现,仅用于参考。 ```c++ template<typename T> class shared_ptr { public: shared_ptr() : ptr(nullptr), ref_count(nullptr) {} shared_ptr(T* p) : ptr(p), ref_count(new int(1)) {} shared_ptr(const shared_ptr<T>& other) : ptr(other.ptr), ref_count(other.ref_count) { if (ref_count) ++(*ref_count); } ~shared_ptr() { dispose(); } shared_ptr<T>& operator=(const shared_ptr<T>& other) { if (this != &other) { dispose(); ptr = other.ptr; ref_count = other.ref_count; if (ref_count) ++(*ref_count); } return *this; } T* operator->() const { return ptr; } T& operator*() const { return *ptr; } bool operator==(const shared_ptr<T>& other) const { return ptr == other.ptr; } bool operator!=(const shared_ptr<T>& other) const { return !(*this == other); } bool operator<(const shared_ptr<T>& other) const { return ptr < other.ptr; } bool operator>(const shared_ptr<T>& other) const { return other < *this; } bool operator<=(const shared_ptr<T>& other) const { return !(other < *this); } bool operator>=(const shared_ptr<T>& other) const { return !(*this < other); } bool is_null() const { return ptr == nullptr; } int use_count() const { return ref_count ? *ref_count : 0; } T* get() const { return ptr; } private: T* ptr; int* ref_count; void dispose() { if (ref_count) { --(*ref_count); if (*ref_count == 0) { delete ptr; delete ref_count; } ptr = nullptr; ref_count = nullptr; } } }; ``` 该shared_ptr实现了拷贝构造函数、拷贝赋值运算符、析构函数、箭头运算符、解引用运算符、相等运算符、不等运算符、比较运算符、is_null()方法、use_count()方法和get()方法。 在构造函数中,初始化指针ptr为nullptr,引用计数ref_count为nullptr。在拷贝构造函数中,ptr和ref_count被复制,并且引用计数加1。在析构函数中,如果引用计数不为0,就减1,如果减到0,就删除指针ptr和引用计数ref_count。在赋值运算符中,先dispose旧的shared_ptr,然后复制新的shared_ptr,最后增加引用计数。在箭头运算符和解引用运算符中,返回指针ptr。在相等运算符和不等运算符中,比较指针ptr。在比较运算符中,比较指针ptr。在is_null()方法中,判断指针ptr是否为nullptr。在use_count()方法中,返回引用计数ref_count的值,如果为nullptr,则返回0。在get()方法中,返回指针ptr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值