线程安全的shareptr 简单实现

#include <iostream>
#include <memory>
#include <atomic>

template<typename T>
class RefCount 
{
public:
  RefCount(T* t)
    :ptr_(t),
    count_(1) {}

  void AddRef() {
    count_ ++;
  }

  void DelRef() {
    count_ --;

    if (0 == count_) {
      Destory();
    }
  }

  long use_count() const { return count_;}
private:
  void Destory() {
    if (ptr_) {
      delete ptr_;
      ptr_ = nullptr;
    }
  }

private:
  T * ptr_;
  std::atomic_long count_;
};

template<typename T>
class SharedPtr 
{
  SharedPtr() {
    ptr_ = nullptr;
    count_ = nullptr;
  }

  SharedPtr(T *t)
    :ptr_(t),
    count_(new RefCount<T>(t)){}

  ~SharedPtr() {
    if (count_) {
      count_->DelRef();
      ptr_ = nullptr;
    }

    count_ = nullptr;
  }

  //拷贝构造
  SharedPtr(const SharedPtr<T>& right) {
    if (right.ptr_) {
      right.count_->AddRef();
    }

    ptr_ = right.ptr_;
    count_ = right.count_;
  }

  //移动构造
  SharedPtr(SharedPtr&& right) {
    ptr_ = right.ptr_;
    count_ = right.count_;

    right.ptr_ = nullptr;
    right.count_ = nullptr;
  }

  //赋值运算符
  SharedPtr<T> & operator=(const SharedPtr<T>& right) {

    right.count_->AddRef();

    if (count_) {
      count_->DelRef();
    }

    count_ = right.count_;
    ptr_ = right.ptr_;
    return (*this);
  }

  SharedPtr<T> operator=(SharedPtr<T>&& right) {
    = std::move 会调用移动构造函数,t获得right的数据所有权,right失去数据所有权
		SharedPtr<T> t = std::move(right); 
    //交换t和this,然后t在函数退出时清理掉,会让原本this指向的对象计数减1
		t.swap(*this); 
		return (*this);
  }

  void Swap(SharedPtr<T>& right) {
    std::swap(this->count_,right.count_);
    std::swap(this->ptr_,right.ptr_);    
  }

  T* get() { return ptr_;}

  T* operator->() { return get();}

  long use_count() { count_ ? count_->use_count():0;}
private:
  T * ptr_ = nullptr;
  RefCount<T> * count_ = nullptr;
}; 

int main()
{

  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值