在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函數類似,都是所有權的轉移。