fixed up util/refptr
git-svn-id: svn://anubis/fart/trunk@91 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
d34242252a
commit
498d35274d
@ -1,11 +1,18 @@
|
||||
|
||||
#include "refptr.h"
|
||||
#include <stdlib.h> /* NULL */
|
||||
|
||||
template <typename T> refptr<T>::refptr()
|
||||
{
|
||||
m_ptr = NULL;
|
||||
m_refCount = NULL;
|
||||
}
|
||||
|
||||
template <typename T> refptr<T>::refptr(const T * ptr)
|
||||
{
|
||||
m_ptr = ptr;
|
||||
refCount = new int;
|
||||
*refCount = 1;
|
||||
m_refCount = new int;
|
||||
*m_refCount = 1;
|
||||
}
|
||||
|
||||
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
|
||||
@ -15,13 +22,36 @@ template <typename T> refptr<T>::refptr(const refptr<T> & orig)
|
||||
|
||||
template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
|
||||
{
|
||||
destroy();
|
||||
cloneFrom(orig);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
|
||||
{
|
||||
this->ptr = orig.ptr;
|
||||
this->refCount = orig.refCount;
|
||||
(*refCount)++;
|
||||
this->m_ptr = orig.m_ptr;
|
||||
this->m_refCount = orig.m_refCount;
|
||||
if (m_refCount != NULL)
|
||||
(*m_refCount)++;
|
||||
}
|
||||
|
||||
template <typename T> refptr<T>::~refptr()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
template <typename T> void refptr<T>::destroy()
|
||||
{
|
||||
if (m_refCount != NULL)
|
||||
{
|
||||
if (*m_refCount <= 1)
|
||||
{
|
||||
delete m_ptr;
|
||||
delete m_refCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*m_refCount)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,15 +6,18 @@ template <typename T>
|
||||
class refptr
|
||||
{
|
||||
public:
|
||||
refptr<T>();
|
||||
refptr<T>(const T * ptr);
|
||||
refptr<T>(const refptr<T> & orig);
|
||||
refptr<T> & operator=(const refptr<T> & orig);
|
||||
~refptr<T>();
|
||||
|
||||
private:
|
||||
void cloneFrom(const refptr<T> & orig);
|
||||
void destroy();
|
||||
|
||||
T * m_ptr;
|
||||
int * refCount;
|
||||
int * m_refCount;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user