C++智能指针与enable_shared_from_this机制深度解析
发表时间: 2021-07-12 10:45
int *ptr;
int main()
{
auto_ptr<int> ptr(new int(6));//定义auto_ptr指针ptr
auto_ptr<int> ptr1(ptr); //拷贝构造ptr定义ptr1
*ptr=8;//对空指针ptr赋值会产生不可预料的错误
return 0;
}
template<class _Ty>
class auto_ptr
{
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty * _Ptr=ptr) noexcept
: _Myptr(_Ptr)//初始化列表
{ //构造函数
}
auto_ptr(auto_ptr& _Right) noexcept
: _Myptr(_Right.release())
{ //拷贝构造函数,会调用release()函数
}
_Ty * release() noexcept
{
/*使用拷贝构造时,最后一个auto_ptr持有资源,
其余被置为ptr*/
_Ty * _Tmp = _Myptr;
_Myptr = ptr;
return (_Tmp);
}
private:
_Ty * _Myptr;//指向资源
};
scoped_ptr<int> sp1(new int(6));//初始化sp1指针
scoped_ptr<int> sp2(sp1);//错误,无法拷贝构造
scoped_ptr<int> sp3(new int(5))//初始化sp2指针
sp1=sp3;//错误,无法赋值
template<class T>
class scoped_ptr
{
private:
T * px;
scoped_ptr(scoped_ptr const &);//拷贝构造函数
scoped_ptr & operator=(scoped_ptr const &);//赋值构造函数
public:
typedef T element_type;
explicit scoped_ptr( T * p = ptr ): px( p )
{
}
~scoped_ptr() //析构函数
};
unique_ptr<int> p1(new int(6));//正确写法
unique_ptr<int> p2(p1); //这么写是错误的:
// unique_ptr不支持拷贝
unique_ptr<int> p3;
p3=p2;//这么写是错误的:unique_ptr不支持赋值
template<class _Ty,class _Dx>
class unique_ptr: public _Unique_ptr_base<_Ty, _Dx>
{
public:
typedef _Unique_ptr_base<_Ty, _Dx> _Mybase;
typedef typename _Mybase::pointer pointer;
typedef _Ty element_type;
typedef _Dx deleter_type;
unique_ptr(unique_ptr&& _Right) noexcept
: _Mybase(_Right.release(),
_STD forward<_Dx>(_Right.get_deleter()))
{ // 右值引用的拷贝构造函数
}
unique_ptr& operator=(unique_ptr&& _Right) noexcept
{ //提供了右值引用的operator=赋值构造函数
if (this != _STD addressof(_Right))
{
reset(_Right.release());
this->get_deleter() = _STD forward<_Dx>
(_Right.get_deleter());
}
return (*this);
}
/*
删除了unique_ptr的拷贝构造和赋值函数,拒绝浅拷贝
*/
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
};
//场景代码
class ObjectPool:boost::noncopyable
{
public:
shared_ptr<Object> get(const string& key)
{
shared_ptr<Object> shObject;
MutexLockGuard lock(mutex);
weak_ptr<Object>& wkObject=object[key];
shObject=wkObject.lock();
//对象存在,提升成功并返回
if(!shObject){
/*对象不存在,提升失败,shOject重新
指向新创建的Object对象,
并绑定回调函数,让对象Oject需要析构时
调用OjectPool对象的成员函数*/
shObject.reset(new Object(key),
boost::bind(&
ObjectPool::deleteObject,this,
_1));
wkObject=shObject;
}
return shObject;
}
private:
void deleteObject(Object* obj)
{ /*回调函数,在对象需要析构时调用,从map中
删除对象和对应的key*/
if(obj){
MutexLockGuard lock(mutex);
object.erase(obj->key());
}
delete obj;
}
mutable MutexLock mutex;
std::map<string,weak_ptr<Object>> object;
/*map中不能使用shared_ptr,这会导致Oject对象永远不会被销
毁*/
};
shared_ptr<A> getSharedPtr()
{
return shared_ptr<A>(this);
}
template<class _Ty>
class _Ptr_base
{
private:
element_type * _Ptr{ptr}; // 指向资源的指针
_Ref_count_base * _Rep{ptr}; // 指向资源引用计数的指针
};
class __declspec(novtable) _Ref_count_base
{
private:
_Atomic_counter_t _Uses;//记录资源引用个数
_Atomic_counter_t _Weaks;//记录观察者个数
}
class A
{
public:
A(){}
~A(){}
};
A *p = new A();
shared_ptr<A> ptr1(p);//调用构造函数
shared_ptr<A> ptr2(p);//调用构造函数
class A
{
public:
A(){}
~A(){}
}
A *p = new A();
shared_ptr<A> ptr1(p);//调用构造函数
shared_ptr<A> ptr2(ptr1);//调用拷贝构造函数
template<class _Ty>
class enable_shared_from_this
{
public:
mutable weak_ptr<_Ty> _Wptr;//指向资源
};
shared_ptr<A> getSharedPtr()
{
return shared_from_this();
}
class ObjectPool:boost::noncopyable
//为
class ObjectPool:public boost::enable_shared_from_this<ObjectPool>,
boost::noncopyable
{/*...*/};
//改变
shared_ptr<Object> get(const string& key)
{
/*...*/
shObject.reset(new Object(key),
boost::bind(&ObjectPool::deleteObject,this,_1));
/*...*/
}
//为
shared_ptr<Object> get(const string& key)
{
/*...*/
shObject.reset(new Object(key),
boost::bind(&ObjectPool::deleteObject,shared_from_this(),_1));
/*...*/
}
class ObjectPool:public boost::enable_shared_from_this<ObjectPool>,
boost::noncopyable
{
public:
shared_ptr<Object> get(const string& key)
{
shared_ptr<Object> shObject;
MutexLockGuard lock(mutex);
weak_ptr<Object>& wkObject=object[key];
shObject=wkObject.lock();//对象存在,提升成功并返回
if(!shObject){
/*对象不存在,提升失败,shOject重新指向新创建的
Object对象,并绑定回调函数,让对象Oject需要析构时
调用OjectPool对象的成员函数*/
shObject.reset(new Object(key),
boost::bind(&
ObjectPool::deleteObject,shared_from_this(),
_1));
wkObject=shObject;
}
return shObject;
}
private:
void deleteObject(Object* obj)
{ /*回调函数,在对象需要析构时调用,从map中删除对象和对
应的key*/
if(obj){
MutexLockGuard lock(mutex);
object.erase(obj->key());
}
delete obj;
}
mutable MutexLock mutex;
std::map<string,weak_ptr<Object>> object;
/*map中不能使用shared_ptr,这会导致Oject对象永远不会被销
毁*/
};