C++:Boost库智能指针_删除器

智能指针_删除器:可以针对特殊的对象进行特殊释放

<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>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值