vtk、osg的智能指针vtkSmartPointer、ref_ptr学习

在vtk和osg库中,都自己的智能指针,其实现原理基本相同。二者都是引用计数的侵入式智能指针。
vtk的智能指针模板类为vtkSmartPointer<T>,而所有要实现引用计数的类都要继承自vtkObjectBase。其中vtkSmartPointer继承自vtkSmartPointerBase,这是一个非模板类,实现了关于引用计数的大部分功能。
osg的智能指针模板类为ref_ptr<T>,而所有要实现引用计数的类都要继承自Referenced。

1、构造函数
vtk
vtkSmartPointerBase::vtkSmartPointerBase():
Object(nullptr)
{
this->Register();
}

vtkSmartPointerBase::vtkSmartPointerBase(vtkObjectBase* r):
Object(r)
{
this->Register();
}

vtkSmartPointer() {}
vtkSmartPointer(T* r): vtkSmartPointerBase(r) {}

osg
ref_ptr() : _ptr(0) {}
ref_ptr(T* ptr) : _ptr(ptr) { if (_ptr) _ptr->ref(); }

可以看到,二者的构造函数做了相同的工作,存储一个对象的指针,并且把对象的引用计数加1。该引用计数变量保存在对象内部,并且是原子变量,是线程安全的。

2、复制构造函数
vtk
vtkSmartPointerBase::vtkSmartPointerBase(const vtkSmartPointerBase& r):
Object(r.Object)
{
this->Register();
}
template <class U>
vtkSmartPointer(const vtkSmartPointer<U>& r):
vtkSmartPointerBase(CheckType(r.GetPointer())) {}

osg
ref_ptr(const ref_ptr& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
template<class Other> ref_ptr(const ref_ptr<Other>& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }

二者原理相同,都是把对象的引用计数再加1。注意这里有类型转换的问题,如果构造函数的右侧不是该对象的同类或子类的话,则构造失败。

3、赋值构造函数
vtk
vtkSmartPointerBase&
vtkSmartPointerBase::operator=(vtkObjectBase* r)
{
vtkSmartPointerBase(r).Swap(*this);
return *this;
}
vtkSmartPointerBase&
vtkSmartPointerBase::operator=(const vtkSmartPointerBase& r)
{
vtkSmartPointerBase(r).Swap(*this);
return *this;
}
void vtkSmartPointerBase::Swap(vtkSmartPointerBase& r)
{
vtkObjectBase* temp = r.Object;
r.Object = this->Object;
this->Object = temp;
}
vtkSmartPointer& operator=(T* r)
{
this->vtkSmartPointerBase::operator=(r);
return *this;
}
template <class U>
vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
{
this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
return *this;
}

osg
ref_ptr& operator = (const ref_ptr& rp)
{
assign(rp);
return *this;
}

template<class Other> ref_ptr& operator = (const ref_ptr<Other>& rp)
{
assign(rp);
return *this;
}
inline ref_ptr& operator = (T* ptr)
{
if (_ptr==ptr) return *this;
T* tmp_ptr = _ptr;
_ptr = ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
return *this;
}

template<class Other> void assign(const ref_ptr<Other>& rp)
{
if (_ptr==rp._ptr) return;
T* tmp_ptr = _ptr;
_ptr = rp._ptr;
if (_ptr) _ptr->ref();
// unref second to prevent any deletion of any object which might
// be referenced by the other object. i.e rp is child of the
// original _ptr.
if (tmp_ptr) tmp_ptr->unref();
}

vtk用了copy and swap模式,保证了自我赋值和异常安全,而osg则稍显冗余。其实vtk有个地方不太明白,赋值之后原始的引用没有减少啊,而osg则调用了unref()。

4、析构函数
vtk
vtkSmartPointerBase::~vtkSmartPointerBase()
{
// The main pointer must be set to nullptr before calling UnRegister,
// so use a local variable to save the pointer. This is because the
// garbage collection reference graph traversal may make it back to
// this smart pointer, and we do not want to include this reference.
vtkObjectBase* object = this->Object;
if(object)
{
this->Object = nullptr;
object->UnRegister(nullptr);
}
}

osg
~ref_ptr() { if (_ptr) _ptr->unref(); _ptr = 0; }

都是减少引用计数

5、指针逻辑运算
vtk中是显式的实现了逻辑比较符号,同时也提供了隐式类型转换。
operator T* () const
{
return static_cast<T*>(this->Object);
}

而osg则提供了隐式转换
perator T*() const { return _ptr; }

6、指针操作
/**
* Dereference the pointer and return a reference to the contained
* object.
*/
T& operator*() const
{
return *static_cast<T*>(this->Object);
}

/**
* Provides normal pointer target member access using operator ->.
*/
T* operator->() const
{
return static_cast<T*>(this->Object);
}

T& operator*() const
{
return *_ptr;
}
T* operator->() const
{
return _ptr;
}

7、get函数
vtk
T* GetPointer() const
{
return static_cast<T*>(this->Object);
}
T* Get() const
{
return static_cast<T*>(this->Object);
}

osg
T* get() const { return _ptr; }

8、swap及转换函数
template<class T> inline
void swap(ref_ptr<T>& rp1, ref_ptr<T>& rp2) { rp1.swap(rp2); }

template<class T> inline
T* get_pointer(const ref_ptr<T>& rp) { return rp.get(); }

template<class T, class Y> inline
ref_ptr<T> static_pointer_cast(const ref_ptr<Y>& rp) { return static_cast<T*>(rp.get()); }

template<class T, class Y> inline
ref_ptr<T> dynamic_pointer_cast(const ref_ptr<Y>& rp) { return dynamic_cast<T*>(rp.get()); }

template<class T, class Y> inline
ref_ptr<T> const_pointer_cast(const ref_ptr<Y>& rp) { return const_cast<T*>(rp.get()); }

9、osg特有
/** release the pointer from ownership by this ref_ptr<>, decrementing the objects refencedCount() via unref_nodelete() to prevent the Object
* object from being deleted even if the reference count goes to zero. Use when using a local ref_ptr<> to an Object that you want to return
* from a function/method via a C pointer, whilst preventing the normal ref_ptr<> destructor from cleaning up the object. When using release()
* you are implicitly expecting other code to take over management of the object, otherwise a memory leak will result. */
T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp; }
返回一个原始指针,同时接收方负责所有权。

10、vtk特有

/**
* Transfer ownership of one reference to the given VTK object to
* this smart pointer. This does not increment the reference count
* of the object, but will decrement it later. The caller is
* effectively passing ownership of one reference to the smart
* pointer. This is useful for code like:

* vtkSmartPointer<vtkFoo> foo;
* foo.TakeReference(bar->NewFoo());

* The input argument may not be another smart pointer.
*/
void TakeReference(T* t)
{
*this = vtkSmartPointer<T>(t, NoReference());
}

/**
* Create an instance of a VTK object.
*/
static vtkSmartPointer<T> New()
{
return vtkSmartPointer<T>(T::New(), NoReference());
}

/**
* Create a new instance of the given VTK object.
*/
static vtkSmartPointer<T> NewInstance(T* t)
{
return vtkSmartPointer<T>(t->NewInstance(), NoReference());
}
其实与osg的release函数类似,都是所有权的转移。

原文地址:https://www.cnblogs.com/ljy339/p/10685886.html