1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
class RefCount { public: RefCount() : count_(new size_t(1)) {}
RefCount(const RefCount &rhs) : count_(rhs.count_) { (*count_)++; }
RefCount(RefCount &&rhs) : count_(rhs.count_) { rhs.count_ = nullptr; }
RefCount &operator=(const RefCount &rhs) { tryRelease(); count_ = rhs.count_; (*count_)++; return *this; }
RefCount &operator=(RefCount &&rhs) { tryRelease(); count_ = rhs.count_; rhs.count_ = nullptr; return *this; }
~RefCount() { if (*count_ == 1) { delete count_; count_ = nullptr; } else { (*count_)--; } }
size_t IsOnly() { return *count_ == 1; }
void tryRelease() { if (count_ == nullptr) { return ; } if (*count_ == 1) { delete count_; } else { (*count_)--; } }
private : size_t* count_; };
template <typename T> class SmartPtr { public : SmartPtr() : SmartPtr(nullptr) {}
explicit SmartPtr(T* ptr = nullptr) : ptr_(ptr) {}
SmartPtr(const SmartPtr& rhs) { ptr_ = rhs.ptr_; count_ = rhs.count_; }
SmartPtr(SmartPtr&& rhs) noexcept : ptr_(rhs.ptr_), count_(rhs.count_) { rhs.count_ = rhs.ptr_ = nullptr; }
SmartPtr& operator=(const SmartPtr& rhs) { if (count_.IsOnly()) { delete ptr_; } else { count_.tryRelease(); } ptr_ = rhs.ptr_; count_ = rhs.count_; return *this; }
SmartPtr& operator=(const SmartPtr&& rhs) { if (this == &rhs) { return *this; } count_ = std::move(rhs.count_); ptr_ = rhs.ptr_; return *this; }
~SmartPtr() { std::cout << "SmartPtr deconstructor" << std::endl; if (count_.IsOnly()) { delete ptr_; ptr_ = nullptr; } count_.tryRelease(); }
T& operator*() const { return *ptr_; }
T* operator->() const { return ptr_; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| template<class T> class Shared_Ptr { public : Shared_Ptr(T* ptr = nullptr) : _pPtr(ptr), _pRefCount(new int(1)), _pMutex(new mutex) {}
Shared_Ptr(const Shared_Ptr<T>& sp) : _pPtr(sp._pPtr), _pRefCount(sp._pRefCount), _pMutex(sp._pMutex) { AddRefCount(); }
~Shared_Ptr() { Release(); }
Shared_Ptr<T>& operator=(const Shared_Ptr<T>& sp) { if (this != sp) { Release(); _pPtr = sp._pPtr; _pRefCount = sp._pRefCount; _pMutex = sp._pMutex; AddRefCount(); } return *this; }
T& operator*() { return *_pPtr; }
T* operator->() { return _pPtr; }
int UseCount() { return *_pRefCount; }
T* Get() { return _pPtr; }
void AddRefCount() { _pMutex -> lock(); ++(*_pRefCount); _pMutex -> unlock(); }
private : void Release() { bool deleteflag = false; _pMutex -> lock(); if (--(*_pRefCount) == 0) { delete _pRefCount; delete _pPtr; deleteflag = true; } _pMutex -> unlock(); if (deleteflag == true) { delete _pMutex; } }
|
本文作者:jujimeizuo
本文地址: https://blog.jujimeizuo.cn/2022/07/30/shared-ptr/
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0 协议。转载请注明出处!