您的位置:首页 > 编程语言 > C语言/C++

C++:Boost库智能指针_shared_array

2016-08-13 22:00 375 查看
智能指针_shared_array: 共享智能指针(数组)

shared_array 类似 shared_ptr,它包装了new[]操作符在堆上分配的动态数组,同样使用引用计数机制为动态数组提供了一个代理,可以在程序的生命周期里长期存在,知道没有任何引用后才释放内存。

主要特点如下:

1.构造函数接收的指针p必须是new[]的结果,而不是new表达式的结果;

2.没有*、->操作符重载,因为scoped_array持有的不是一个普通指针;

3.析构函数使用delete[]释放资源,而不是delete;

4.提供operator[]操作符重载,可以像普通数组一样用下标访问元素;

使用:

<span style="font-size:18px;">#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;

int main()
{
int *p = new int[10];
shared_array<int> pa(p);

for(int i = 0; i < 10; ++i)
{
pa[i] = i;
}

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


////////////////////////////////

<3>checked_array.h                  //定制数组删除器

<span style="font-size:18px;">#ifndef _CHECKED_DELETE_H
#define _CHECKED_DELETE_H

void checked_array_delete(T * x)
{
delete []x;
}

template<class T>
struct checked_array_deleter
{
void operator()(T * x)const           //del.operator()(ptr)
{
checked_array_delete(x);
}
};

#endif</span>


<2>shared_array.h

<span style="font-size:18px;">#ifndef _SHARED_ARRAY_H
#define _SHARED_ARRAY_H

#include "shared_count.h"
#include "checked_delete.h"

template<class T>
class shared_array
{
typedef checked_array_deleter<T> deleter;
public:
template<class Y,class D>                     //具有删除器的构造函数
shared_ptr(Y *p, D d):px(p),pn(p,deleter())   //使用函数对象作为删除器
{}

~shared_ptr()
{}
public:
T& operator[](int i)const    //重载[]
{
return px[i];
}
private:
T *px;
shared_count pn;
};

#endif</span>


////////////////////

main.cpp

<span style="font-size:18px;">int main()
{
int *p = new int[10];
shared_array<int> pa(p);

for(int i = 0; i < 10; ++i)
{
pa[i] = i;
}

return 0;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: