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
This commit is contained in:
Josh Holtrop 2009-02-10 00:10:04 +00:00
parent 498d35274d
commit e2424337d6
3 changed files with 81 additions and 68 deletions

View File

@ -3,19 +3,33 @@
#include <cassert> #include <cassert>
#include "util/Transform.h" #include "util/Transform.h"
#include "shapes/Sphere.h" #include "shapes/Sphere.h"
#include "util/refptr.h"
using namespace std; 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() int main()
{ {
Sphere s(1.0); S * a = new S(42);
Ray ray(Vector(0, -2.5, 0), Vector(0, 1, 0)); S * b = new S(33);
cout << "a: " << a << endl;
Shape::IntersectList res = s.intersect(ray); cout << "b: " << b << endl;
cout << "Creating arp" << endl;
for (unsigned int i = 0; i < res.size(); i++) refptr<S> arp(a);
{ cout << "Creating brp" << endl;
cout << "t: " << res[i] << endl; refptr<S> brp(b);
} cout << "Going to assign brp to arp" << endl;
arp = brp;
cout << "ok, exiting" << endl;
return 0; return 0;
} }

View File

@ -1,57 +0,0 @@
#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;
m_refCount = new int;
*m_refCount = 1;
}
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
{
cloneFrom(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->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)--;
}
}
}

View File

@ -2,6 +2,8 @@
#ifndef REFPTR_H #ifndef REFPTR_H
#define REFPTR_H REFPTR_H #define REFPTR_H REFPTR_H
#include <stdlib.h> /* NULL */
template <typename T> template <typename T>
class refptr class refptr
{ {
@ -16,9 +18,63 @@ class refptr
void cloneFrom(const refptr<T> & orig); void cloneFrom(const refptr<T> & orig);
void destroy(); void destroy();
T * m_ptr; const T * m_ptr;
int * m_refCount; int * m_refCount;
}; };
template <typename T> refptr<T>::refptr()
{
m_ptr = NULL;
m_refCount = NULL;
}
template <typename T> refptr<T>::refptr(const T * ptr)
{
m_ptr = ptr;
m_refCount = new int;
*m_refCount = 1;
}
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
{
cloneFrom(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->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)--;
}
}
}
#endif #endif