shared_ptr的原理及仿写
- 共享指针允许多个指针指向同一份数据,因为它使用了引用计数,每多一个指针指向这个数据,引用技术加一,每销毁一个指针,引用技术减一,如果引用计数为0,则delete这个数据。
- 但是共享指针也不能将同一个裸指针赋值给多个智能指针,因为这样会是两个独立的共享指针,它们会分别计数,也就是意味着到时候它们会重复释放。
- 第一个没有计数器设计的版本:
template<typename T>
class MyShared_ptr {
private:
T* m_ptr;
long* m_count;
public:
explicit MyShared_ptr(T* ptr = nullptr) : m_ptr(ptr) {
m_count = new long(1);
}
MyShared_ptr(MyUnique_ptr<T>&& src) {
m_ptr = src.release();
m_count = new long(1);
}
MyShared_ptr(const MyShared_ptr& src) {
m_ptr = src.m_ptr;
m_count = src.m_count;
(*m_count)++;
}
MyShared_ptr& operator=(const MyShared_ptr& src) {
if (&src == this) return *this;
if (m_ptr != nullptr) {
this->~MyShared_ptr();
}
m_ptr = src.m_ptr;
m_count = src.m_count;
(*m_count)++;
return *this;
}
MyShared_ptr& operator=(const MyUnique_ptr<T>&& src) {
if (m_ptr != nullptr) {
this->~MyShared_ptr();
}
m_ptr = src.release();
m_count = new long(1);
return *this;
}
~MyShared_ptr() {
(*m_count)--;
if (0 == *m_count) {
delete m_ptr;
delete m_count;
m_ptr = nullptr;
m_count = nullptr;
}
}
T* get() {
return m_ptr;
}
void reset(T* ptr = nullptr) {
this->~MyShared_ptr();
m_ptr = ptr;
m_count = new long(1);
}
long use_count() {
return *m_count;
}
operator bool() const {
return m_ptr != nullptr;
}
bool unique() {
if (1 == *m_count) return true;
else return false;
}
void swap(MyShared_ptr<T>& src) {
T* temp_ptr;
long* temp_count;
temp_ptr = src.m_ptr;
temp_count = src.m_count;
src.m_ptr = m_ptr;
src.m_count = m_count;
m_ptr = temp_ptr;
m_count = temp_count;
}
T& operator*() const {
return *m_ptr;
}
T* operator->() const {
return (&**this);
}
};
template<class T>
MyShared_ptr<T> make_MyShared(MyShared_ptr<T>&& src) {
return MyShared_ptr<T>(forward<MyShared_ptr<T>>(src));
}
template<typename T>
class MyCount {
private:
long use_count;
long weak_count;
public:
MyCount(long u,long w) : use_count(u), weak_count(w) {}
void Increase_weak_count() {
++weak_count;
}
void Decrease_weak_count() {
--weak_count;
}
void Increase_use_count() {
++use_count;
}
void Decrease_use_count() {
--use_count;
}
long get_use_count() {
return use_count;
}
void DestroyPtr(T* ptr) {
delete ptr;
}
void DestroyThis() {
if(0 == weak_count)
delete this;
}
};
template<typename T>
class MyShared_ptr {
private:
T* m_ptr;
MyCount<T>* m_count;
public:
explicit MyShared_ptr(T* ptr = nullptr) : m_ptr(ptr) {
m_count = new MyCount<T>(1,0);
}
MyShared_ptr(MyUnique_ptr<T>&& src) {
m_ptr = src.release();
m_count = new MyCount<T>(1, 0);
}
MyShared_ptr(const MyShared_ptr& src) {
m_ptr = src.m_ptr;
m_count = src.m_count;
m_count->Increase_use_count();
}
MyShared_ptr& operator=(const MyShared_ptr& src) {
if (&src == this) return *this;
if (m_ptr != nullptr) {
this->~MyShared_ptr();
}
m_ptr = src.m_ptr;
m_count = src.m_count;
m_count->Increase_use_count();
return *this;
}
MyShared_ptr& operator=(const MyUnique_ptr<T>&& src) {
if (m_ptr != nullptr) {
this->~MyShared_ptr();
}
m_ptr = src.release();
m_count = new MyCount<T>(1, 0);
return *this;
}
~MyShared_ptr() {
m_count->Decrease_use_count();
if (0 == m_count->get_use_count()) {
m_count->DestroyPtr(m_ptr);
m_count->DestroyThis();
m_ptr = nullptr;
m_count = nullptr;
}
}
T* get() {
return m_ptr;
}
void reset(T* ptr = nullptr) {
this->~MyShared_ptr();
m_ptr = ptr;
m_count = new MyCount<T>(1, 0);
}
long use_count() {
return m_count->get_use_count();
}
operator bool() const {
return m_ptr != nullptr;
}
bool unique() {
if (1 == m_count->get_use_count()) return true;
else return false;
}
void swap(MyShared_ptr<T>& src) {
T* temp_ptr;
MyCount<T>* temp_count;
temp_ptr = src.m_ptr;
temp_count = src.m_count;
src.m_ptr = m_ptr;
src.m_count = m_count;
m_ptr = temp_ptr;
m_count = temp_count;
}
T& operator*() const {
return *m_ptr;
}
T* operator->() const {
return (&**this);
}
template<typename T>
friend class MyWeak_ptr;
};
template<class T>
MyShared_ptr<T> make_MyShared(MyShared_ptr<T>&& src) {
return MyShared_ptr<T>(forward<MyShared_ptr<T>>(src));
}