added util/refptr module

git-svn-id: svn://anubis/fart/trunk@90 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-02-09 23:31:15 +00:00
parent fc8a46853b
commit d34242252a
3 changed files with 69 additions and 3 deletions

View File

@ -11,6 +11,7 @@ extern "C" {
} }
extern FILE * yyin; extern FILE * yyin;
extern char * yytext;
void yyerror(const char * str) void yyerror(const char * str)
{ {
@ -135,8 +136,16 @@ material_item: COLOR vector
| SHININESS number | SHININESS number
; ;
number: DEC_NUMBER number: DEC_NUMBER {
| REAL_NUMBER double * ptr = new double;
*ptr = atoi(yytext);
$$.ptr = ptr;
}
| REAL_NUMBER {
double * ptr = new double;
*ptr = atof(yytext);
$$.ptr = ptr;
}
; ;
plane: PLANE LCURLY plane_items RCURLY plane: PLANE LCURLY plane_items RCURLY
@ -182,7 +191,16 @@ subtract: SUBTRACT LCURLY boolean_items RCURLY
union: UNION LCURLY boolean_items RCURLY 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();
(*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;
}
; ;
%% %%

27
util/refptr.cc Normal file
View File

@ -0,0 +1,27 @@
#include "refptr.h"
template <typename T> refptr<T>::refptr(const T * ptr)
{
m_ptr = ptr;
refCount = new int;
*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)
{
cloneFrom(orig);
return *this;
}
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
{
this->ptr = orig.ptr;
this->refCount = orig.refCount;
(*refCount)++;
}

21
util/refptr.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef REFPTR_H
#define REFPTR_H REFPTR_H
template <typename T>
class refptr
{
public:
refptr<T>(const T * ptr);
refptr<T>(const refptr<T> & orig);
refptr<T> & operator=(const refptr<T> & orig);
private:
void cloneFrom(const refptr<T> & orig);
T * m_ptr;
int * refCount;
};
#endif