From 498d35274d94ff582ab56cf9dcca70397466afa4 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 9 Feb 2009 23:51:00 +0000 Subject: [PATCH] fixed up util/refptr git-svn-id: svn://anubis/fart/trunk@91 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- util/refptr.cc | 40 +++++++++++++++++++++++++++++++++++----- util/refptr.h | 5 ++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/util/refptr.cc b/util/refptr.cc index e02f1cc..2ecd1c0 100644 --- a/util/refptr.cc +++ b/util/refptr.cc @@ -1,11 +1,18 @@ #include "refptr.h" +#include /* NULL */ + +template refptr::refptr() +{ + m_ptr = NULL; + m_refCount = NULL; +} template refptr::refptr(const T * ptr) { m_ptr = ptr; - refCount = new int; - *refCount = 1; + m_refCount = new int; + *m_refCount = 1; } template refptr::refptr(const refptr & orig) @@ -15,13 +22,36 @@ template refptr::refptr(const refptr & orig) template refptr & refptr::operator=(const refptr & orig) { + destroy(); cloneFrom(orig); return *this; } template void refptr::cloneFrom(const refptr & 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 refptr::~refptr() +{ + destroy(); +} + +template void refptr::destroy() +{ + if (m_refCount != NULL) + { + if (*m_refCount <= 1) + { + delete m_ptr; + delete m_refCount; + } + else + { + (*m_refCount)--; + } + } } diff --git a/util/refptr.h b/util/refptr.h index 0f4b96f..1ec948a 100644 --- a/util/refptr.h +++ b/util/refptr.h @@ -6,15 +6,18 @@ template class refptr { public: + refptr(); refptr(const T * ptr); refptr(const refptr & orig); refptr & operator=(const refptr & orig); + ~refptr(); private: void cloneFrom(const refptr & orig); + void destroy(); T * m_ptr; - int * refCount; + int * m_refCount; }; #endif