1. 程式人生 > >C++:Boost庫智慧指標_刪除器

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>