Posted on 2008-11-13 17:37
空心菜 閱讀(2515)
評論(3) 編輯 收藏 引用
看一下Ogre里面的ShagedPtr,其實就是一個Smart Pointers。下面的代碼是刪去了原有注釋的。現不談里面的MUTEX。
#ifndef __SharedPtr_H__
#define __SharedPtr_H__
#include "OgrePrerequisites.h"
namespace Ogre {
template<class T> class SharedPtr {
protected:
T* pRep;
unsigned int* pUseCount; //看到這里,應該能知道,SharedPtr是通過引用計數來管理pRep的壽命
public:
OGRE_AUTO_SHARED_MUTEX
SharedPtr() : pRep(0), pUseCount(0)
{
OGRE_SET_AUTO_SHARED_MUTEX_NULL
}//允許有一個空的SharedPtr,不指向任何的對象。
template< class Y>
explicit SharedPtr(Y* rep) : pRep(rep), pUseCount(new unsigned int(1))
{
OGRE_SET_AUTO_SHARED_MUTEX_NULL
OGRE_NEW_AUTO_SHARED_MUTEX
}//這個寫法是Member Templates,很有用,這樣就允許用一個Y對象的指針來初始化一個SharedPtr<T>
//下面還能看到很多這樣的Member Templates
//要是以前沒見過的人,推薦看一下C++ Templates的第5章、第3節
//try this: vector<int> intvec;
// vector<float> floatvec;
// floatvec = intvec ???????
//提一下,所有的初始化函數都沒有檢查rep是否非空,所以SharedPtr接受一個Null指針
//安全檢查在每次調用的時候
//這里還使用了關鍵字explicit,禁止了隱式轉換
SharedPtr(const SharedPtr& r)
: pRep(0), pUseCount(0)
{
OGRE_SET_AUTO_SHARED_MUTEX_NULL
OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
{
OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
pRep = r.pRep; //注意下與后面的不同
pUseCount = r.pUseCount;
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
}
}
}
SharedPtr& operator=(const SharedPtr& r) {
if (pRep == r.pRep)
return *this;
SharedPtr<T> tmp(r);
swap(tmp);
return *this;
}//這里的寫法有點意思,本來在pRep指向r.pRep之前對pRep做一次release,
//但是這里沒看到,其實是通過tmp這個局部變量的自動解析實現的。
template< class Y>
SharedPtr(const SharedPtr<Y>& r)
: pRep(0), pUseCount(0)
{
OGRE_SET_AUTO_SHARED_MUTEX_NULL
OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
{
OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
pRep = r.getPointer(); //這里用的是函數,和上面那個的區別??
pUseCount = r.useCountPointer();
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
}
}
}
template< class Y>
SharedPtr& operator=(const SharedPtr<Y>& r) {
if (pRep == r.pRep)
return *this;
SharedPtr<T> tmp(r);
swap(tmp);
return *this;
}
virtual ~SharedPtr() {
release();
}
inline T& operator*() const { assert(pRep); return *pRep; }
inline T* operator->() const { assert(pRep); return pRep; }
//在用的時候檢查pRep的合法性
inline T* get() const { return pRep; }
void bind(T* rep) {
assert(!pRep && !pUseCount);
OGRE_NEW_AUTO_SHARED_MUTEX
OGRE_LOCK_AUTO_SHARED_MUTEX
pUseCount = new unsigned int(1);
pRep = rep;
}
inline bool unique() const { OGRE_LOCK_AUTO_SHARED_MUTEX assert(pUseCount); return *pUseCount == 1; }
inline unsigned int useCount() const { OGRE_LOCK_AUTO_SHARED_MUTEX assert(pUseCount); return *pUseCount; }
inline unsigned int* useCountPointer() const { return pUseCount; }
inline T* getPointer() const { return pRep; }
inline bool isNull(void) const { return pRep == 0; }
inline void setNull(void) {
if (pRep)
{
// can't scope lock mutex before release incase deleted
release();
pRep = 0;
pUseCount = 0;
}
}
protected:
inline void release(void)
{
bool destroyThis = false;
OGRE_MUTEX_CONDITIONAL(OGRE_AUTO_MUTEX_NAME)
{
OGRE_LOCK_AUTO_SHARED_MUTEX
if (pUseCount)
{
if (--(*pUseCount) == 0)
{
destroyThis = true;
}
}
}
if (destroyThis)
destroy();
OGRE_SET_AUTO_SHARED_MUTEX_NULL
}
virtual void destroy(void)
{
delete pRep;
delete pUseCount;
OGRE_DELETE_AUTO_SHARED_MUTEX
}
virtual void swap(SharedPtr<T> &other)
{
std::swap(pRep, other.pRep);
std::swap(pUseCount, other.pUseCount);
#if OGRE_THREAD_SUPPORT
std::swap(OGRE_AUTO_MUTEX_NAME, other.OGRE_AUTO_MUTEX_NAME);
#endif
}
};
template<class T, class U> inline bool operator==(SharedPtr<T> const& a, SharedPtr<U> const& b)
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=(SharedPtr<T> const& a, SharedPtr<U> const& b)
{
return a.get() != b.get();
}
}
#endif
最后有注意到:
inline T* get() const { return pRep; }
inline T* getPointer() const { return pRep; }
不知道為啥要這樣,有一個不就行了么。
更多的細節、使用方法放到下次把。