Ice的智能指针采用对象继承IceUtil::Shared,指针通过IceUtil::Handle模板来实现。
初步研究了一下,在Shared中增加了一个计数器,用于累计对象被使用的次数,当计数器为零时,调用对象的析构函数。
[code="cpp"] void __incRef()
{
assert(_ref >= 0);
++_ref;
}
void __decRef()
{
assert(_ref > 0);
if(--_ref == 0)
{
if(!_noDelete)
{
_noDelete = true;
delete this;
}
}
}
在模板类 Handle中的实现了操作计数器的方法
Handle(const Handle& r)
{
this->_ptr = r._ptr;
if(this->_ptr)
{
this->_ptr->__incRef();
}
}
~Handle()
{
if(this->_ptr)
{
this->_ptr->__decRef();
}
}
分享到:
评论