智能指针_删除器:可以针对特殊的对象进行特殊释放
<span style="font-size:18px;">#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
//删除器
//函数对象 operator()
void My_Delete(int *p) //特别定制的删除器
{
delete p;
}
int main()
{
int *p = new int(10);
shared_ptr<int> ps(p,My_Deleter);
return 0;
}</span>
实现:
<1>shared_count.h
<span style="font-size:18px;">#ifndef _SHARED_COUNT_H
#define _SHARED_COUNT_H
#include "sp_counted_base.h"
#include "sp_counted_impl_xx.h"
class shared_count
{
public:
template<class Y> //没有删除器
shared_count(Y *p):pi_(new sp_counted_impl_xx<Y>(p))
{}
template<class Y,class D> //具有删除器
shared_count(Y * p, D d): pi_(0)
{
typedef Y* p;
pi_ = new sp_counted_impl_pd<P,D>(p,d);
}
shared_count(const shared_count &r):pi_(r.pi_)
{
if(pi_ != 0)
{
pi_->add_ref_copy();
}
}
~shared_count()
{
if(pi_ != 0) //释放shared_count所指的空间
pi_->release();
}
public:
long use_count()const
{
return pi_ != 0 ? pi_->use_count : 0;
}
bool unique()const
{
return use_count() == 1;
}
void swap(shared_count &r)
{
sp_counted_base *tmp = r.pi;
r.pi_ = pi_;
pi_ = tmp;
}
private:
sp_counted_base *pi_; //父类指针作为接口,实现多态
};
#endif</span>
<1-2>sp_counted_base.h
<span style="font-size:18px;">#ifndef _SP_COUNTED_BASE_H
#define _SP_COUNTED_BASE_H
class sp_counted_base
{
public:
sp_counted_base():use_count_(1)
{}
virtual ~sp_counted_base()
{}
public:
virtual void dispose()=0;
void release()
{
if(--use_count_ == 0)
{
dispose(); //析构sp_counted_xx所指的数据空间
delete this; //析构自身sp_counted_imple_xx
}
}
viod add_ref_copy()
{
++use_count_;
}
public:
long use_count()const
{
return use_count_;
}
private:
long use_count_;
};
#endif</span>
<1-3>sp_counted_impl_xx.h
<span style="font-size:18px;">#ifndef _SP_COUNTED_IMPL_XX_H
#define _SP_COUNTED_IMPL_XX_H
#include "sp_counted_base.h"
template<class T> //没有删除器的子类
class sp_counted_impl_xxt:public sp_counted_base
{
public:
sp_counted_impl_xx(T *p):px_(p)
{}
~sp_counted_impl_xx()
{}
public:
virtual void dispose()
{
delete px_;
}
private:
T *px_;
};
///
template<class P, class D> //具有删除器的子类
class sp_counted_impl_pd:public sp_counted_base
{
public:
sp_counted_impl_pd(P p, D d): ptr(p),del(d)
{}
~sp_counted_impl_pd()
{}
public:
virtual void dispose()
{
del(ptr); //调用删除器
}
private:
P ptr;
D del;
};
#endif</span>
<2>shared_ptr.h
<span style="font-size:18px;">#ifndef _SHARED_PTR_H
#define _SHARED_PTR_H
#include "shared_count.h"
template<class T>
class shared_ptr
{
typedef shared_ptr<T> this_type;
public:
shared_ptr(T *p = 0):px(p),pn(p) //没有删除器的构造函数
{}
template<class Y,class D> //具有删除器的构造函数
shared_ptr(Y *p, D d):px(p),pn(p,d)
{}
shared_ptr(const shared_ptr<T> &r):px(r.px),pn(r.pn)
{}
shared_ptr<T>& operator=(const shared_ptr<T> &r)
{
if(this != &r)
{
this_type(r).swap(*this);
}
return *this;
}
~shared_ptr()
{
cout<<"Free shared_ptr object!"<<endl;
}
pulic:
T& operator*()const
{return *(get());}
T* operator->()const
{return get();}
T* get()const
{return px;}
viod swap(shared_ptr<T> &other)
{
std::swap(px,other.px);
pn.swap(other.pn);
}
public:
long use_count()const
{
return pn.use_count();
}
bool unique()const
{
return pn.unique();
}
private:
T *px;
shared_count pn;
};
#endif</span>
<3>main.cpp
<span style="font-size:18px;">#include <iostream>
#include "shared_ptr.h"
using namespace std;
void My_Delete(int *p) //特别定制的删除器
{
delete p;
}
int main()
{
int *p = new int(10);
shared_ptr<int> ps(p,My_Deleter);
return 0;
}</span>