added dereference and indirection operators to util/refptr, converted parser to use refptr<Node> as YYSTYPE

git-svn-id: svn://anubis/fart/trunk@94 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-02-10 00:37:49 +00:00
parent c6e1a2c7e4
commit 0181e82653
2 changed files with 42 additions and 22 deletions

View File

@ -30,14 +30,16 @@ class Node
{ {
public: public:
Node(); Node();
Node(int type, void * ptr); ~Node();
int type; int type;
void * ptr; double * the_double;
Material * the_Material;
Vector * the_Vector;
}; };
static Scene * g_scene; static Scene * g_scene;
#define YYSTYPE Node #define YYSTYPE refptr<Node>
%} %}
@ -142,12 +144,14 @@ material_item: COLOR vector
number: DEC_NUMBER { number: DEC_NUMBER {
double * ptr = new double; double * ptr = new double;
*ptr = atoi(yytext); *ptr = atoi(yytext);
$$.ptr = ptr; $$ = new Node();
$$->the_double = ptr;
} }
| REAL_NUMBER { | REAL_NUMBER {
double * ptr = new double; double * ptr = new double;
*ptr = atof(yytext); *ptr = atof(yytext);
$$.ptr = ptr; $$ = new Node();
$$->the_double = ptr;
} }
; ;
@ -196,14 +200,12 @@ union: UNION LCURLY boolean_items RCURLY
vector: LESS number COMMA number COMMA number GREATER { vector: LESS number COMMA number COMMA number GREATER {
Vector * ptr = new Vector(); Vector * ptr = new Vector();
(*ptr)[0] = * (double *) $2.ptr; (*ptr)[0] = * (double *) $2->the_double;
(*ptr)[1] = * (double *) $4.ptr; (*ptr)[1] = * (double *) $4->the_double;
(*ptr)[2] = * (double *) $6.ptr; (*ptr)[2] = * (double *) $6->the_double;
$$.ptr = ptr; $$ = new Node();
delete (double *) $2.ptr; $$->the_Vector = ptr;
delete (double *) $4.ptr; }
delete (double *) $6.ptr;
}
; ;
%% %%
@ -223,11 +225,17 @@ int parse(Scene * scene, const char * fileName)
Node::Node() Node::Node()
{ {
type = -1; type = -1;
ptr = NULL; the_double = NULL;
the_Material = NULL;
the_Vector = NULL;
} }
Node::Node(int type, void * ptr) Node::~Node()
{ {
this->type = type; if (the_double != NULL)
this->ptr = ptr; delete the_double;
if (the_Material != NULL)
delete the_Material;
if (the_Vector != NULL)
delete the_Vector;
} }

View File

@ -9,17 +9,19 @@ class refptr
{ {
public: public:
refptr<T>(); refptr<T>();
refptr<T>(const T * ptr); refptr<T>(T * ptr);
refptr<T>(const refptr<T> & orig); refptr<T>(const refptr<T> & orig);
refptr<T> & operator=(const refptr<T> & orig); refptr<T> & operator=(const refptr<T> & orig);
refptr<T> & operator=(const T * ptr); refptr<T> & operator=(T * ptr);
~refptr<T>(); ~refptr<T>();
T & operator*();
T * operator->();
private: private:
void cloneFrom(const refptr<T> & orig); void cloneFrom(const refptr<T> & orig);
void destroy(); void destroy();
const T * m_ptr; T * m_ptr;
int * m_refCount; int * m_refCount;
}; };
@ -29,7 +31,7 @@ template <typename T> refptr<T>::refptr()
m_refCount = NULL; m_refCount = NULL;
} }
template <typename T> refptr<T>::refptr(const T * ptr) template <typename T> refptr<T>::refptr(T * ptr)
{ {
m_ptr = ptr; m_ptr = ptr;
m_refCount = new int; m_refCount = new int;
@ -48,7 +50,7 @@ template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
return *this; return *this;
} }
template <typename T> refptr<T> & refptr<T>::operator=(const T * ptr) template <typename T> refptr<T> & refptr<T>::operator=(T * ptr)
{ {
destroy(); destroy();
m_ptr = ptr; m_ptr = ptr;
@ -86,5 +88,15 @@ template <typename T> void refptr<T>::destroy()
} }
} }
template <typename T> T & refptr<T>::operator*()
{
return *m_ptr;
}
template <typename T> T * refptr<T>::operator->()
{
return m_ptr;
}
#endif #endif