刚刚看了Ogre中的引用计数指针实现,在OgreShared.h中,严格说,它算不上智能指针
比如:
int * t = new int(5)
sharedPtr<int> t1(t);
sharedPtr<int> t2(t),
则t1,t2中都会有一次引用计数,可能如它的名字那样,只是共享指针而已。
下面是我的测试代码:
Code
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <assert.h>
using namespace std;
namespace Ogre {
/// The method to use to free memory on destruction
enum SharedPtrFreeMethod
{
/// Use OGRE_DELETE to free the memory
SPFM_DELETE,
/// Use OGRE_DELETE_T to free (only MEMCATEGORY_GENERAL supported)
SPFM_DELETE_T,
/// Use OGRE_FREE to free (only MEMCATEGORY_GENERAL supported)
SPFM_FREE
};
/**
If OGRE_THREAD_SUPPORT is defined to be 1, use of this class is thread-safe.
如果定义了OGRE_THREAD_SUPPORT,该类是线程安全的
*/
template<class T> class SharedPtr
{
protected:
T* pRep;
unsigned int* pUseCount;
SharedPtrFreeMethod useFreeMethod; // if we should use OGRE_FREE instead of OGRE_DELETE
public:
//OGRE_AUTO_SHARED_MUTEX // public to allow external locking
/** Constructor, does not initialise the SharedPtr.
@remarks
<b>Dangerous!</b> You have to call bind() before using the SharedPtr.
默认构造函数,产生一个空的智能指针,用户计数为0
*/
SharedPtr() : pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
//OGRE_SET_AUTO_SHARED_MUTEX_NULL,其实就是空,直接注释掉
}
/** Constructor.成员模板,允许通过Y指针来初始化SharedPtr<T>
@param rep The pointer to take ownership of
@param freeMode The mechanism to use to free the pointer
pUseCount(rep ? new (unsigned int(1)) : 0)改成这样主要是为了避免用ogre的分配函数,便于调试程序
*/
template< class Y>
explicit SharedPtr(Y* rep, SharedPtrFreeMethod freeMethod = SPFM_DELETE)
: pRep(rep)
//, pUseCount(rep ? new (unsigned int(1)) : 0)//pUseCount(rep ? OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1) : 0)
, useFreeMethod(freeMethod)
{
// OGRE_SET_AUTO_SHARED_MUTEX_NULL 统统为空
//OGRE_NEW_AUTO_SHARED_MUTEX
pUseCount = new unsigned int;
*pUseCount = rep?1:0;
cout<<"引用计数为:"<<*pUseCount<<endl;
}
SharedPtr(const SharedPtr& r)
: pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
// lock & copy other mutex pointer
//OGRE_SET_AUTO_SHARED_MUTEX_NULL
//OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) //#define OGRE_MUTEX_CONDITIONAL(name) if(true)
if(true)
{
//OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
//OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
pRep = r.pRep;
pUseCount = r.pUseCount;
useFreeMethod = r.useFreeMethod;
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
cout<<"引用计数为:"<<*pUseCount<<",加1"<<endl;
}
}
}
SharedPtr& operator=(const SharedPtr& r) {
if (pRep == r.pRep)
return *this;
// Swap current data into a local copy
// this ensures we deal with rhs and this being dependent
//本来在pRep指向r.pRep之前对pRep做一次release,但是这里没看到,其实是通过tmp这个局部变量的自动解析实现的
SharedPtr<T> tmp(r);
swap(tmp);
return *this;
}
template< class Y>
SharedPtr(const SharedPtr<Y>& r)
: pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
// lock & copy other mutex pointer
//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();
useFreeMethod = r.freeMethod();
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
cout<<"引用计数为:"<<*pUseCount<<",加1"<<endl;
}
}
}
template< class Y>
SharedPtr& operator=(const SharedPtr<Y>& r) {
if (pRep == r.pRep)
return *this;
// Swap current data into a local copy
// this ensures we deal with rhs and this being dependent
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; }
inline T* get() const { return pRep; }
/** Binds rep to the SharedPtr.
@remarks
Assumes that the SharedPtr is uninitialised!
未初始化的智能指针需要绑定
*/
void bind(T* rep, SharedPtrFreeMethod freeMethod = SPFM_DELETE)
{
assert(!pRep && !pUseCount);
// OGRE_NEW_AUTO_SHARED_MUTEX
//OGRE_LOCK_AUTO_SHARED_MUTEX
pUseCount = new unsigned int(1);//OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1);
pRep = rep;
useFreeMethod = freeMethod;
cout<<"绑定对象,引用计数为:"<<*pUseCount<<endl;
}
//是否是单例,只有一个引用计数
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 SharedPtrFreeMethod freeMethod() const { return useFreeMethod; }
inline bool isNull(void) const { return pRep == 0; }
inline void setNull(void)
{
if (pRep)
{
// can't scope lock mutex before release in case deleted
release();
pRep = 0;
pUseCount = 0;
}
}
protected:
inline void release(void)
{
bool destroyThis = false;
/* If the mutex is not initialized to a non-zero value, then
neither is pUseCount nor pRep.
*/
// OGRE_MUTEX_CONDITIONAL(OGRE_AUTO_MUTEX_NAME)
{
// lock own mutex in limited scope (must unlock before destroy)
//OGRE_LOCK_AUTO_SHARED_MUTEX
if (pUseCount)
{
cout<<"引用计数为"<<*pUseCount<<",减1"<<endl;
if (--(*pUseCount) == 0)
{
destroyThis = true;
cout<<"引用计数为0"<<endl;
}
}
}
if (destroyThis)
{
destroy();
cout<<"删除对象"<<endl;
}
//OGRE_SET_AUTO_SHARED_MUTEX_NULL
}
virtual void destroy(void)
{
// IF YOU GET A CRASH HERE, YOU FORGOT TO FREE UP POINTERS
// BEFORE SHUTTING OGRE DOWN
// Use setNull() before shutdown or make sure your pointer goes
// out of scope before OGRE shuts down to avoid this.
switch(useFreeMethod)
{
case SPFM_DELETE:
//OGRE_DELETE pRep;
delete pRep;
break;
case SPFM_DELETE_T:
//OGRE_DELETE_T(pRep, T, MEMCATEGORY_GENERAL);
delete pRep;
break;
case SPFM_FREE:
//OGRE_FREE(pRep, MEMCATEGORY_GENERAL);
delete pRep;
break;
};
// use OGRE_FREE instead of OGRE_DELETE_T since 'unsigned int' isn't a destructor
// we only used OGRE_NEW_T to be able to use constructor
//OGRE_FREE(pUseCount, MEMCATEGORY_GENERAL);
delete pUseCount;
/*OGRE_DELETE_AUTO_SHARED_MUTEX*/
}
virtual void swap(SharedPtr<T> &other)
{
std::swap(pRep, other.pRep);
std::swap(pUseCount, other.pUseCount);
std::swap(useFreeMethod, other.useFreeMethod);
#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();
}
template<class T, class U> inline bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b)
{
return std::less<const void*>()(a.get(), b.get());
}
}
int main()
{
using namespace Ogre;
int* x = new int(5);
SharedPtr<int> t;//创建一个空的引用对象,现在把他指向x
t.bind(x);
SharedPtr<int> t1(t);
getchar();
return 0;
}
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <assert.h>
using namespace std;
namespace Ogre {
/// The method to use to free memory on destruction
enum SharedPtrFreeMethod
{
/// Use OGRE_DELETE to free the memory
SPFM_DELETE,
/// Use OGRE_DELETE_T to free (only MEMCATEGORY_GENERAL supported)
SPFM_DELETE_T,
/// Use OGRE_FREE to free (only MEMCATEGORY_GENERAL supported)
SPFM_FREE
};
/**
If OGRE_THREAD_SUPPORT is defined to be 1, use of this class is thread-safe.
如果定义了OGRE_THREAD_SUPPORT,该类是线程安全的
*/
template<class T> class SharedPtr
{
protected:
T* pRep;
unsigned int* pUseCount;
SharedPtrFreeMethod useFreeMethod; // if we should use OGRE_FREE instead of OGRE_DELETE
public:
//OGRE_AUTO_SHARED_MUTEX // public to allow external locking
/** Constructor, does not initialise the SharedPtr.
@remarks
<b>Dangerous!</b> You have to call bind() before using the SharedPtr.
默认构造函数,产生一个空的智能指针,用户计数为0
*/
SharedPtr() : pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
//OGRE_SET_AUTO_SHARED_MUTEX_NULL,其实就是空,直接注释掉
}
/** Constructor.成员模板,允许通过Y指针来初始化SharedPtr<T>
@param rep The pointer to take ownership of
@param freeMode The mechanism to use to free the pointer
pUseCount(rep ? new (unsigned int(1)) : 0)改成这样主要是为了避免用ogre的分配函数,便于调试程序
*/
template< class Y>
explicit SharedPtr(Y* rep, SharedPtrFreeMethod freeMethod = SPFM_DELETE)
: pRep(rep)
//, pUseCount(rep ? new (unsigned int(1)) : 0)//pUseCount(rep ? OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1) : 0)
, useFreeMethod(freeMethod)
{
// OGRE_SET_AUTO_SHARED_MUTEX_NULL 统统为空
//OGRE_NEW_AUTO_SHARED_MUTEX
pUseCount = new unsigned int;
*pUseCount = rep?1:0;
cout<<"引用计数为:"<<*pUseCount<<endl;
}
SharedPtr(const SharedPtr& r)
: pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
// lock & copy other mutex pointer
//OGRE_SET_AUTO_SHARED_MUTEX_NULL
//OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) //#define OGRE_MUTEX_CONDITIONAL(name) if(true)
if(true)
{
//OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
//OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
pRep = r.pRep;
pUseCount = r.pUseCount;
useFreeMethod = r.useFreeMethod;
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
cout<<"引用计数为:"<<*pUseCount<<",加1"<<endl;
}
}
}
SharedPtr& operator=(const SharedPtr& r) {
if (pRep == r.pRep)
return *this;
// Swap current data into a local copy
// this ensures we deal with rhs and this being dependent
//本来在pRep指向r.pRep之前对pRep做一次release,但是这里没看到,其实是通过tmp这个局部变量的自动解析实现的
SharedPtr<T> tmp(r);
swap(tmp);
return *this;
}
template< class Y>
SharedPtr(const SharedPtr<Y>& r)
: pRep(0), pUseCount(0), useFreeMethod(SPFM_DELETE)
{
// lock & copy other mutex pointer
//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();
useFreeMethod = r.freeMethod();
// Handle zero pointer gracefully to manage STL containers
if(pUseCount)
{
++(*pUseCount);
cout<<"引用计数为:"<<*pUseCount<<",加1"<<endl;
}
}
}
template< class Y>
SharedPtr& operator=(const SharedPtr<Y>& r) {
if (pRep == r.pRep)
return *this;
// Swap current data into a local copy
// this ensures we deal with rhs and this being dependent
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; }
inline T* get() const { return pRep; }
/** Binds rep to the SharedPtr.
@remarks
Assumes that the SharedPtr is uninitialised!
未初始化的智能指针需要绑定
*/
void bind(T* rep, SharedPtrFreeMethod freeMethod = SPFM_DELETE)
{
assert(!pRep && !pUseCount);
// OGRE_NEW_AUTO_SHARED_MUTEX
//OGRE_LOCK_AUTO_SHARED_MUTEX
pUseCount = new unsigned int(1);//OGRE_NEW_T(unsigned int, MEMCATEGORY_GENERAL)(1);
pRep = rep;
useFreeMethod = freeMethod;
cout<<"绑定对象,引用计数为:"<<*pUseCount<<endl;
}
//是否是单例,只有一个引用计数
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 SharedPtrFreeMethod freeMethod() const { return useFreeMethod; }
inline bool isNull(void) const { return pRep == 0; }
inline void setNull(void)
{
if (pRep)
{
// can't scope lock mutex before release in case deleted
release();
pRep = 0;
pUseCount = 0;
}
}
protected:
inline void release(void)
{
bool destroyThis = false;
/* If the mutex is not initialized to a non-zero value, then
neither is pUseCount nor pRep.
*/
// OGRE_MUTEX_CONDITIONAL(OGRE_AUTO_MUTEX_NAME)
{
// lock own mutex in limited scope (must unlock before destroy)
//OGRE_LOCK_AUTO_SHARED_MUTEX
if (pUseCount)
{
cout<<"引用计数为"<<*pUseCount<<",减1"<<endl;
if (--(*pUseCount) == 0)
{
destroyThis = true;
cout<<"引用计数为0"<<endl;
}
}
}
if (destroyThis)
{
destroy();
cout<<"删除对象"<<endl;
}
//OGRE_SET_AUTO_SHARED_MUTEX_NULL
}
virtual void destroy(void)
{
// IF YOU GET A CRASH HERE, YOU FORGOT TO FREE UP POINTERS
// BEFORE SHUTTING OGRE DOWN
// Use setNull() before shutdown or make sure your pointer goes
// out of scope before OGRE shuts down to avoid this.
switch(useFreeMethod)
{
case SPFM_DELETE:
//OGRE_DELETE pRep;
delete pRep;
break;
case SPFM_DELETE_T:
//OGRE_DELETE_T(pRep, T, MEMCATEGORY_GENERAL);
delete pRep;
break;
case SPFM_FREE:
//OGRE_FREE(pRep, MEMCATEGORY_GENERAL);
delete pRep;
break;
};
// use OGRE_FREE instead of OGRE_DELETE_T since 'unsigned int' isn't a destructor
// we only used OGRE_NEW_T to be able to use constructor
//OGRE_FREE(pUseCount, MEMCATEGORY_GENERAL);
delete pUseCount;
/*OGRE_DELETE_AUTO_SHARED_MUTEX*/
}
virtual void swap(SharedPtr<T> &other)
{
std::swap(pRep, other.pRep);
std::swap(pUseCount, other.pUseCount);
std::swap(useFreeMethod, other.useFreeMethod);
#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();
}
template<class T, class U> inline bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b)
{
return std::less<const void*>()(a.get(), b.get());
}
}
int main()
{
using namespace Ogre;
int* x = new int(5);
SharedPtr<int> t;//创建一个空的引用对象,现在把他指向x
t.bind(x);
SharedPtr<int> t1(t);
getchar();
return 0;
}