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:
parent
498d35274d
commit
e2424337d6
@ -3,19 +3,33 @@
|
||||
#include <cassert>
|
||||
#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<S> arp(a);
|
||||
cout << "Creating brp" << endl;
|
||||
refptr<S> brp(b);
|
||||
cout << "Going to assign brp to arp" << endl;
|
||||
arp = brp;
|
||||
cout << "ok, exiting" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
@ -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)--;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
#ifndef REFPTR_H
|
||||
#define REFPTR_H REFPTR_H
|
||||
|
||||
#include <stdlib.h> /* NULL */
|
||||
|
||||
template <typename T>
|
||||
class refptr
|
||||
{
|
||||
@ -16,9 +18,63 @@ class refptr
|
||||
void cloneFrom(const refptr<T> & orig);
|
||||
void destroy();
|
||||
|
||||
T * m_ptr;
|
||||
const T * m_ptr;
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user