From e2424337d6ccec2a557bb5174a7ae386bf616605 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 10 Feb 2009 00:10:04 +0000 Subject: [PATCH] moved all refptr functions into util/refptr.h since they are templated, deleted util/refptr.cc, updated test/tests.cc to test refptr functionality git-svn-id: svn://anubis/fart/trunk@92 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- test/tests.cc | 34 ++++++++++++++++++++--------- util/refptr.cc | 57 ------------------------------------------------- util/refptr.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 68 deletions(-) delete mode 100644 util/refptr.cc diff --git a/test/tests.cc b/test/tests.cc index 4cffcce..1edd2fc 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -3,19 +3,33 @@ #include #include "util/Transform.h" #include "shapes/Sphere.h" +#include "util/refptr.h" using namespace std; +class S +{ + public: + S(int val) + { + cout << "S constructor, val " << val << endl; + this->val = val; + } + ~S() { cout << "S destructor, val " << val << endl; } + int val; +}; + int main() { - Sphere s(1.0); - Ray ray(Vector(0, -2.5, 0), Vector(0, 1, 0)); - - Shape::IntersectList res = s.intersect(ray); - - for (unsigned int i = 0; i < res.size(); i++) - { - cout << "t: " << res[i] << endl; - } - + S * a = new S(42); + S * b = new S(33); + cout << "a: " << a << endl; + cout << "b: " << b << endl; + cout << "Creating arp" << endl; + refptr arp(a); + cout << "Creating brp" << endl; + refptr brp(b); + cout << "Going to assign brp to arp" << endl; + arp = brp; + cout << "ok, exiting" << endl; return 0; } diff --git a/util/refptr.cc b/util/refptr.cc deleted file mode 100644 index 2ecd1c0..0000000 --- a/util/refptr.cc +++ /dev/null @@ -1,57 +0,0 @@ - -#include "refptr.h" -#include /* NULL */ - -template refptr::refptr() -{ - m_ptr = NULL; - m_refCount = NULL; -} - -template refptr::refptr(const T * ptr) -{ - m_ptr = ptr; - m_refCount = new int; - *m_refCount = 1; -} - -template refptr::refptr(const refptr & orig) -{ - cloneFrom(orig); -} - -template refptr & refptr::operator=(const refptr & orig) -{ - destroy(); - cloneFrom(orig); - return *this; -} - -template void refptr::cloneFrom(const refptr & orig) -{ - 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 1ec948a..ebd59e5 100644 --- a/util/refptr.h +++ b/util/refptr.h @@ -2,6 +2,8 @@ #ifndef REFPTR_H #define REFPTR_H REFPTR_H +#include /* NULL */ + template class refptr { @@ -16,9 +18,63 @@ class refptr void cloneFrom(const refptr & orig); void destroy(); - T * m_ptr; + const T * m_ptr; int * m_refCount; }; +template refptr::refptr() +{ + m_ptr = NULL; + m_refCount = NULL; +} + +template refptr::refptr(const T * ptr) +{ + m_ptr = ptr; + m_refCount = new int; + *m_refCount = 1; +} + +template refptr::refptr(const refptr & orig) +{ + cloneFrom(orig); +} + +template refptr & refptr::operator=(const refptr & orig) +{ + destroy(); + cloneFrom(orig); + return *this; +} + +template void refptr::cloneFrom(const refptr & orig) +{ + 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)--; + } + } +} + #endif