自己简单的实现了c11的几个智能指针,记录下
注意:不能将同一个裸指针重复赋值给不同的shared_ptr,否则在析构的时候会崩溃,因为各个count都是1,会析构多次同一个动态内存
#pragma once
class counter {
public:
int shared_count_ = 0;
int weak_count_ = 0;
};
template<class T> class my_weak_ptr;
template<class T>
class my_shared_ptr {
public:
friend class my_weak_ptr<T>;
my_shared_ptr();
explicit my_shared_ptr(T* p);
my_shared_ptr(const my_shared_ptr<T>& m_s_p);
my_shared_ptr(const my_weak_ptr<T>& m_w_p);
my_shared_ptr<T>& operator=(const my_shared_ptr<T>& m_s_p);
~my_shared_ptr();
T* operator->();
void reset(T* p_other);
T* get();
int get_count();
private:
void release();
private:
T* p_ = nullptr;
counter* p_counter_ = nullptr;
};
template<class T>
class my_weak_ptr {
public:
friend class my_shared_ptr<T>;
my_weak_ptr();
my_weak_ptr(const my_weak_ptr<T>& m_w_p);
my_weak_ptr(const my_shared_ptr<T>& m_s_p);
my_weak_ptr<T>& operator=(const my_weak_ptr<T>& m_w_p);
my_weak_ptr<T>& operator=(const my_shared_ptr<T>& m_s_p);
~my_weak_ptr();
my_shared_ptr<T> lock();
bool expired();
private:
void release();
private:
T* p_ = nullptr;
counter* p_counter_ = nullptr;
};
template<class T>
class my_unique_ptr {
public:
explicit my_unique_ptr(T* ptr = nullptr);
~my_unique_ptr();
my_unique_ptr(const my_unique_ptr& m_u_p) = delete;
my_unique_ptr& operator=(const my_unique_ptr& m_u_p) = delete;
my_unique_ptr(my_unique_ptr&& m_u_p) noexcept;
my_unique_ptr<T>& operator=(my_unique_ptr&& m_u_p) noexcept;
explicit operator bool() noexcept;
void reset(T* p)noexcept;
T* release() noexcept;
void swap(my_unique_ptr& m_u_p) noexcept;
T* get();
T* operator*();
T& operator->();
private:
T* p_;
};
#include "point_test.h"
#include <utility>
template<class T>
my_shared_ptr<T>::my_shared_ptr() {
p_ = nullptr;
p_counter_ = nullptr;
}
template<class T>
my_shared_ptr<T>::my_shared_ptr(T* p) {
if (p != nullptr) {
p_ = p;
p_counter_ = new counter();
if (p_counter_) {
p_counter_->shared_count_++;
}
}
}
template<class T>
my_shared_ptr<T>::my_shared_ptr(const my_shared_ptr<T>& m_s_p) {
p_ = m_s_p.p_;
p_counter_ = m_s_p.p_counter_;
p_counter_->shared_count_++;
}
template<class T>
my_shared_ptr<T>::my_shared_ptr(const my_weak_ptr<T>& m_w_p) {
p_ = m_w_p.p_;
p_counter_ = m_w_p.p_counter_;
}
template<class T>
my_shared_ptr<T>& my_shared_ptr<T>::operator=(const my_shared_ptr<T>& m_s_p) {
if (this == &m_s_p) {
return *this;
}
release();
p_ = m_s_p.p_;
p_counter_ = m_s_p.p_counter_;
if (p_ != nullptr) {
p_counter_->shared_count_++;
}
return *this;
}
template<class T>
my_shared_ptr<T>::~my_shared_ptr() {
release();
}
template<class T>
T* my_shared_ptr<T>::operator->() {
return p_;
}
template<class T>
void my_shared_ptr<T>::reset(T* p_other) {
release();
p_ = p_other;
if (p_ != nullptr) {
p_counter_ = new counter();
p_counter_->shared_count_++;
}
}
template<class T>
T* my_shared_ptr<T>::get() {
return p_;
}
template<class T>
int my_shared_ptr<T>::get_count() {
if (p_counter_) {
return p_counter_->shared_count_;
}
return 0;
}
template<class T>
void my_shared_ptr<T>::release() {
if (p_counter_ == nullptr) {
return;
}
--p_counter_->shared_count_;
if (p_counter_->shared_count_ == 0) {
delete p_;
p_ = nullptr;
if (p_counter_->weak_count_ == 0) {
delete p_counter_;
p_counter_ = nullptr;
}
}
}
template<class T>
my_weak_ptr<T>::my_weak_ptr() {
p_ = nullptr;
p_counter_ = nullptr;
}
template<class T>
my_weak_ptr<T>::my_weak_ptr(const my_weak_ptr<T>& m_w_p) {
p_ = m_w_p.p_;
p_counter_ = m_w_p.p_counter_;
if (p_counter_ != nullptr) {
++(p_counter_->weak_count_);
}
}
template<class T>
my_weak_ptr<T>::my_weak_ptr(const my_shared_ptr<T>& m_s_p) {
p_ = m_s_p.p_;
p_counter_ = m_s_p.p_counter_;
if (p_counter_ != nullptr) {
++(p_counter_->weak_count_);
}
}
template<class T>
my_weak_ptr<T>& my_weak_ptr<T>::operator=(const my_weak_ptr<T>& m_w_p) {
if (this == &m_w_p) {
return *this;
}
release();
p_ = m_w_p.p_;
p_counter_ = m_w_p.p_counter_;
if (p_counter_ != nullptr) {
++(p_counter_->weak_count_);
}
return *this;
}
template<class T>
my_weak_ptr<T>& my_weak_ptr<T>::operator=(const my_shared_ptr<T>& m_s_p) {
release();
p_ = m_s_p.p_;
p_counter_ = m_s_p.p_counter_;
if (p_counter_ != nullptr) {
++(p_counter_->weak_count_);
}
return *this;
}
template<class T>
my_weak_ptr<T>::~my_weak_ptr() {
release();
}
template<class T>
my_shared_ptr<T> my_weak_ptr<T>::lock() {
return my_shared_ptr<T>(*this);
}
template<class T>
bool my_weak_ptr<T>::expired() {
if (p_counter_ && p_counter_->shared_count_ > 0) {
return false;
}
return true;
}
template<class T>
void my_weak_ptr<T>::release() {
if (p_counter_ == nullptr) {
return;
}
--p_counter_->weak_count_;
if (p_counter_->weak_count_ == 0 && p_counter_->shared_count_ == 0) {
delete p_counter_;
p_counter_ = nullptr;
}
}
template<class T>
my_unique_ptr<T>::my_unique_ptr(T* ptr /*= nullptr*/) {
p_ = ptr;
}
template<class T>
my_unique_ptr<T>::~my_unique_ptr() {
if (p_) {
delete p_;
p_ = nullptr;
}
}
template<class T>
my_unique_ptr<T>::my_unique_ptr(my_unique_ptr&& m_u_p) noexcept {
p_ = m_u_p.p_;
m_u_p.p_ = nullptr;
}
template<class T>
my_unique_ptr<T>& my_unique_ptr<T>::operator=(my_unique_ptr&& m_u_p) noexcept {
swap(*this, m_u_p);
return *this;
}
template<class T>
my_unique_ptr<T>::operator bool() noexcept {
return p_;
}
template<class T>
T& my_unique_ptr<T>::operator->() {
return *p_;
}
template<class T>
T* my_unique_ptr<T>::operator*() {
return p_;
}
template<class T>
T* my_unique_ptr<T>::get() {
return p_;
}
template<class T>
void my_unique_ptr<T>::swap(my_unique_ptr& m_u_p) noexcept {
std::swap(p_, m_u_p.p_);
}
template<class T>
T* my_unique_ptr<T>::release() noexcept {
T* tmp = p_;
p_ = nullptr;
return tmp;
}
template<class T>
void my_unique_ptr<T>::reset(T* p) noexcept {
if (p != p_) {
if (p_) {
delete p_;
}
p_ = p;
}
}