【查询x86指令集,原子操作】
template<class _Ty>
class MyDeletor
{
public:
MyDeletor() = default;
void operator()(_Ty* ptr) const
{
if (ptr != NULL)
{
delete ptr;
}
}
};
template<class _Ty>
class MyDeletor<_Ty[]>
{
public:
MyDeletor() = default;
void operator()(_Ty* ptr) const
{
if (ptr != NULL)
{
delete[] ptr;
}
}
};
成员变量:
1、指向对象的指针
2、强引用计数 //当其值为1的时候,说明对象存在
3、弱引用计数
template<class _Ty>
class RefCnt
{
public:
_Ty* _Ptr; //Obj
std::atomic_int _Uses; //shared_ptr;
std::atomic_int _Weaks; //weak_ptr 弱引用
public:
RefCnt(_Ty* p) :_Ptr(p), _Uses(1), _Weaks(1); {}
~RefCnt() {}
void _Incref() { _Uses += 1; }
void _Incwef() { _Weaks += 1; }
};
template<class _Ty,class _Dx = MyDeletor<_Ty> >
class my_shared_ptr
{
private:
_Ty* _Ptr; //指向object对象
RefCnt<_Ty>* Rep; //指向引用计数类
_Dx _mDeletor; //删除器
public:
};
my_shared_ptr(_Ty* p = nullptr) :_Ptr(nullptr), Rep(nullptr)
{
if (p != nullptr)
{
_Ptr = p;
Rep = new RefCnt<_Ty>(p);
}
}

~my_shared_ptr()
{
if (Rep != nullptr && --Rep->_Uses == 0)
{
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = nullptr;
Rep = nullptr;
my_shared_ptr(const my_shared_ptr& _Y):_Ptr(_Y._Ptr),Rep(_Y.Rep)
{
if (Rep != nullptr)
{
Rep->_Incref();// _Uses++
}
}
my_shared_ptr(my_shared_ptr&& _Y) :_Ptr(_Y._Ptr), Rep(_Y.Rep)
{
_Y._Ptr = nullptr;
_Y.Rep = nullptr;
}
my_shared_ptr& operator=(const my_shared_ptr& r)
{
if (this == *r || this->_Ptr == r._Ptr) return *this;
if (_Ptr != nullptr && --Rep->_Uses == 0)
{
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = r._Ptr;
Rep = r.Rep;
if (_Ptr != nullptr)
{
Rep->_Incref();
}
return *this;
}

my_shared_ptr& operator=(my_shared_ptr&& r)
{
if (this == &r)return *this;
if (_Ptr != nullptr && r._Ptr != nullptr && _Ptr == r._Ptr)
{
this->Rep->_Uses -= 1;
r._Ptr = nullptr;
r.Rep = nullptr;
return *this;
}
if (_Ptr != nullptr && --Rep->_Uses == 0)
{
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = r._Ptr;
Rep = r.Rep;
r._Ptr = nullptr;
r.Rep = nullptr;
return *this;
}

_Ty* get()
{
return _Ptr;
}
_Ty& operator*()const
{
return *get();
}
_Ty* operator->()const
{
return get();
}
size_t use_cout()const
{
if (Rep == nullptr) return 0;
return Rep->_Uses;
}
void swap(my_shared_ptr& r)
{
std::swap(_Ptr, r._Ptr);
std::swap(Rep, r.Rep);
}
operator bool()
{
return _Ptr != nullptr;
}
只有一个Rep指针,指向引用计数
template<class _Ty>
class my_weak_ptr
{
private:
RefCnt<_Ty>* _Rep;
public:
my_weak_ptr() :_Rep(nullptr) {}
};
my_weak_ptr(const my_shared_ptr<_Ty>& other):_Rep(other.Rep)
{
if (_Rep != nullptr)
{
_Rep->_Incwef();//weaks
}
}
my_weak_ptr(const my_weak_ptr& other) :_Rep(other._Rep)
{
if (_Rep != nullptr)
{
_Rep->_Incwef();//weaks
}
}
my_weak_ptr(my_weak_ptr&& other) :_Rep(other._Rep)
{
other._Rep = nullptr;
}
~my_weak_ptr()
{
if (_Rep != nullptr) //弱引用只能析构计数
{
delete _Rep;
}
_Rep = nullptr;
}
my_weak_ptr& operator=(const my_weak_ptr&other)
{
if (this == &other || this->_Rep == other._Rep) return *this;
if (this->_Rep != nullptr && --_Rep->_Weaks == 0)
{
delete _Rep;
}
_Rep = other._Rep;
if (_Rep != nullptr)
{
_Rep->_Incwef();
}
return *this;
}
1.防止自赋值
2.在_Rep和Other._Rep都不为空,且相等时,将弱引用计数-1,并将参数指针置为空。
3.如果_Rep 不为空,且对弱引用计数-1为0,则删除弱引用计数结构。
4.将参数赋值给_Rep,并将参数置为空
my_weak_ptr& operator=( my_weak_ptr &&other)
{
if (this == other) return *this;
if (this->_Rep != nullptr && other._Rep!=nullptr && _Rep==other._Rep)
{
this->_Rep->_Weaks -= 1;
other._Rep = nullptr;
return *this;
}
if (_Rep != nullptr && --_Rep->_Weaks==0)
{
delete _Rep;
}
_Rep = other._Rep;
other._Rep = nullptr;
return *this;
}
1.对弱引用做判断,如果减为0,删除引用计数结构
2.将_Rep指向my_shared_ptr<_Ty>中Rep指向的引用计数结构
3.如果此时_Rep不为空就 _weak+1
my_weak_ptr& operator=(const my_shared_ptr<_Ty>& other)
{
if (_Rep != nullptr && --_Rep->_Weaks == 0)
{
delete _Rep;
}
_Rep = other.Rep;
if (_Rep != nullptr)
{
_Rep->_Incwef();
}
return *this;
}
bool expired() const
{
return this->_Rep->_Uses == 0; //当强引用计数为0的时候,说明对象已经被删除
}
};
>**
不能使用移动拷贝和移动赋值的方式,强引用拷贝或赋值给弱引用,因为弱引用只能指向引用计数,不能拥有资源,弱引用指针,没有重载,解引用,指向,因为它只有一个指针,指向的是引用计数。**
my_shared_ptr<_Ty>lock()const
{
my_shared_ptr<_Ty>_Ret;
_Ret._Ptr = _Rep->_Ptr;
_Ret.Rep = _Rep;
_Ret.Rep->_Incref();
}
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << "Obejct" << endl; }
~Object() { cout << "~Object:" << endl; }
void Print() const { cout << "value: " << value << endl; }
};
void fun()
{
my_shared_ptr<Object> op1(new Object(10));
my_weak_ptr<Object> wp1(op1);//强指针拷贝给弱指针
my_weak_ptr<Object> wp2(op1);
}
int main()
{
fun();
return 0;
}

void Print() const { cout << "value: " << value << endl; }
};
void fun1()
{
my_weak_ptr<Object> wp;
{
my_shared_ptr<Object> op1(new Object(10));
wp = op1;
}//块内成员,快结束,就会被析构
if (!wp.expired())
{
wp.lock()->Print(); //Object已经被析构,所以无法打印10
}
}


完整代码;
#include
#include
using namespace std;
template<class _Ty>
class MyDeletor
{
public:
MyDeletor() = default;
void operator()(_Ty* ptr) const
{
if (ptr != NULL)
{
delete ptr;
}
}
};
template<class _Ty>
class MyDeletor<_Ty[]>
{
public:
MyDeletor() = default;
void operator()(_Ty* ptr) const
{
if (ptr != NULL)
{
delete[] ptr;
}
}
};
template<class _Ty>
class RefCnt
{
public:
_Ty* _Ptr; //Obj
std::atomic_int _Uses; //shared_ptr;
std::atomic_int _Weaks; //weak_ptr 弱引用
public:
RefCnt(_Ty* p) :_Ptr(p), _Uses(1), _Weaks(1){}
~RefCnt() {}
void _Incref() { _Uses += 1; }
void _Incwef() { _Weaks += 1; }
};
template<class _Ty,class _Dx = MyDeletor<_Ty> >
class my_shared_ptr
{
private:
_Ty* _Ptr; //指向object对象
RefCnt<_Ty>* Rep; //指向引用计数类
_Dx _mDeletor; //删除器
public:
my_shared_ptr(_Ty* p = nullptr) :_Ptr(nullptr), Rep(nullptr)
{
if (p != nullptr)
{
_Ptr = p;
Rep = new RefCnt<_Ty>(p);
}
}
~my_shared_ptr()
{
if (Rep != nullptr && --Rep->_Uses == 0)
{ //在析构的时候不仅要减强引用个数还要减弱引用个数
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = nullptr;
Rep = nullptr;
}
my_shared_ptr(const my_shared_ptr& _Y):_Ptr(_Y._Ptr),Rep(_Y.Rep)
{
if (Rep != nullptr)
{
Rep->_Incref();// _Uses++
}
}
my_shared_ptr(my_shared_ptr&& _Y) :_Ptr(_Y._Ptr), Rep(_Y.Rep)
{
_Y._Ptr = nullptr;
_Y.Rep = nullptr;
}
my_shared_ptr& operator=(const my_shared_ptr& r)
{
if (this == *r || this->_Ptr == r._Ptr) return *this;
if (_Ptr != nullptr && --_Ptr->_Uses == 0)
{
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = r._Ptr;
Rep = r.Rep;
if (_Ptr != nullptr)
{
Rep->_Incref();
}
return *this;
}
my_shared_ptr& operator=(my_shared_ptr&& r)
{
if (this == &r)return *this;
if (_Ptr != nullptr && r._Ptr != nullptr && _Ptr == r._Ptr)
{
this->Rep->_Uses -= 1;
r._Ptr = nullptr;
r.Rep = nullptr;
return *this;
}
if (_Ptr != nullptr && --Rep->_Uses == 0)
{
_mDeletor(_Ptr);
if (--Rep->_Weaks == 0)
{
delete Rep;
}
}
_Ptr = r._Ptr;
Rep = r.Rep;
r._Ptr = nullptr;
r.Rep = nullptr;
return *this;
}
_Ty* get()
{
return _Ptr;
}
_Ty& operator*()const
{
return *get();
}
_Ty* operator->()const
{
return get();
}
size_t use_cout()const
{
if (Rep == nullptr) return 0;
return Rep->_Uses;
}
void swap(my_shared_ptr& r)
{
std::swap(_Ptr, r._Ptr);
std::swap(Rep, r.Rep);
}
operator bool()
{
return _Ptr != nullptr;
}
template<class _Ty>
friend class my_weak_ptr;
};
template<class _Ty>
class my_weak_ptr
{
private: //弱指针不拥有资源
RefCnt<_Ty>* _Rep; //只负责弱引用计数,没有权限释放对象,共享性只能指针才有权限释放对象
public:
my_weak_ptr() :_Rep(nullptr) {}
//强指针拷贝给弱指针
my_weak_ptr(const my_shared_ptr<_Ty>& other):_Rep(other.Rep)
{
if (_Rep != nullptr)
{
_Rep->_Incwef();//weaks
}
}
my_weak_ptr(const my_weak_ptr& other) :_Rep(other._Rep)
{
if (_Rep != nullptr)
{
_Rep->_Incwef();//weaks
}
}
my_weak_ptr(my_weak_ptr&& other) :_Rep(other._Rep)
{
other._Rep = nullptr;
}
~my_weak_ptr()
{
if (_Rep != nullptr) //弱引用只能析构计数
{
delete _Rep;
}
_Rep = nullptr;
}
my_weak_ptr& operator=(const my_weak_ptr&other)
{
if (this == &other || this->_Rep == other._Rep) return *this;
if (this->_Rep != nullptr && --_Rep->_Weaks == 0)
{
delete _Rep;
}
_Rep = other._Rep;
if (_Rep != nullptr)
{
_Rep->_Incwef();
}
return *this;
}
my_weak_ptr& operator=( my_weak_ptr &&other)
{
if (this == other) return *this;
if (this->_Rep != nullptr && other._Rep!=nullptr && _Rep==other._Rep)
{
this->_Rep->_Weaks -= 1;
other._Rep = nullptr;
return *this;
}
if (_Rep != nullptr && --_Rep->_Weaks==0)
{
delete _Rep;
}
_Rep = other._Rep;
other._Rep = nullptr;
return *this;
}
my_weak_ptr& operator=(const my_shared_ptr<_Ty>& other)
{
if (_Rep != nullptr && --_Rep->_Weaks == 0)
{
delete _Rep;
}
_Rep = other.Rep;
if (_Rep != nullptr)
{
_Rep->_Incwef();
}
return *this;
}
//不能使用移动拷贝和移动赋值的方式,强引用拷贝或赋值给弱引用,因为弱引用只能指向引用计数,不能拥有资源。
bool expired() const
{
return this->_Rep->_Uses == 0; //当强引用计数为0的时候,说明对象已经被删除
}
//弱引用指针,没有重载,解引用,指向,因为它只有一个指针,指向的是引用计数。
//锁 会让对象的生存期变长
my_shared_ptr<_Ty>lock()const
{
my_shared_ptr<_Ty>_Ret;
_Ret._Ptr = _Rep->_Ptr;
_Ret.Rep = _Rep;
_Ret.Rep->_Incref();
}
};
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << "Obejct" << endl; }
~Object() { cout << "~Object:" << endl; }
void Print() const { cout << "value: " << value << endl; }
};
void fun()
{
my_shared_ptr<Object> op1(new Object(10));
my_weak_ptr<Object> wp1(op1);//强指针拷贝给弱指针
my_weak_ptr<Object> wp2(op1);
}
int main()
{
fun();
return 0;
}