diff --git a/parser/parser.yy b/parser/parser.yy index 13286eb..de2c41b 100644 --- a/parser/parser.yy +++ b/parser/parser.yy @@ -30,14 +30,16 @@ class Node { public: Node(); - Node(int type, void * ptr); + ~Node(); int type; - void * ptr; + double * the_double; + Material * the_Material; + Vector * the_Vector; }; static Scene * g_scene; -#define YYSTYPE Node +#define YYSTYPE refptr %} @@ -142,12 +144,14 @@ material_item: COLOR vector number: DEC_NUMBER { double * ptr = new double; *ptr = atoi(yytext); - $$.ptr = ptr; + $$ = new Node(); + $$->the_double = ptr; } | REAL_NUMBER { double * ptr = new double; *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 * ptr = new Vector(); - (*ptr)[0] = * (double *) $2.ptr; - (*ptr)[1] = * (double *) $4.ptr; - (*ptr)[2] = * (double *) $6.ptr; - $$.ptr = ptr; - delete (double *) $2.ptr; - delete (double *) $4.ptr; - delete (double *) $6.ptr; - } + (*ptr)[0] = * (double *) $2->the_double; + (*ptr)[1] = * (double *) $4->the_double; + (*ptr)[2] = * (double *) $6->the_double; + $$ = new Node(); + $$->the_Vector = ptr; + } ; %% @@ -223,11 +225,17 @@ int parse(Scene * scene, const char * fileName) Node::Node() { type = -1; - ptr = NULL; + the_double = NULL; + the_Material = NULL; + the_Vector = NULL; } -Node::Node(int type, void * ptr) +Node::~Node() { - this->type = type; - this->ptr = ptr; + if (the_double != NULL) + delete the_double; + if (the_Material != NULL) + delete the_Material; + if (the_Vector != NULL) + delete the_Vector; } diff --git a/util/refptr.h b/util/refptr.h index d6a07be..6eed839 100644 --- a/util/refptr.h +++ b/util/refptr.h @@ -9,17 +9,19 @@ class refptr { public: refptr(); - refptr(const T * ptr); + refptr(T * ptr); refptr(const refptr & orig); refptr & operator=(const refptr & orig); - refptr & operator=(const T * ptr); + refptr & operator=(T * ptr); ~refptr(); + T & operator*(); + T * operator->(); private: void cloneFrom(const refptr & orig); void destroy(); - const T * m_ptr; + T * m_ptr; int * m_refCount; }; @@ -29,7 +31,7 @@ template refptr::refptr() m_refCount = NULL; } -template refptr::refptr(const T * ptr) +template refptr::refptr(T * ptr) { m_ptr = ptr; m_refCount = new int; @@ -48,7 +50,7 @@ template refptr & refptr::operator=(const refptr & orig) return *this; } -template refptr & refptr::operator=(const T * ptr) +template refptr & refptr::operator=(T * ptr) { destroy(); m_ptr = ptr; @@ -86,5 +88,15 @@ template void refptr::destroy() } } +template T & refptr::operator*() +{ + return *m_ptr; +} + +template T * refptr::operator->() +{ + return m_ptr; +} + #endif