_proper_ typing system using a union instead of multicast void*
This commit is contained in:
parent
9183fa1755
commit
71d107aac2
@ -3,7 +3,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "TPTSTypes.h"
|
#include "TPTSTypes.h"
|
||||||
|
|
||||||
AnyType::AnyType(ValueType type_, void * value_):
|
AnyType::AnyType(ValueType type_, ValueValue value_):
|
||||||
type(type_),
|
type(type_),
|
||||||
value(value_)
|
value(value_)
|
||||||
{
|
{
|
||||||
@ -19,13 +19,9 @@ AnyType::AnyType(const AnyType & v):
|
|||||||
value(v.value)
|
value(v.value)
|
||||||
{
|
{
|
||||||
if(type == TypeString)
|
if(type == TypeString)
|
||||||
{
|
value.str = new std::string(*(value.str));
|
||||||
value = new std::string(*((std::string*)value));
|
|
||||||
}
|
|
||||||
else if(type == TypePoint)
|
else if(type == TypePoint)
|
||||||
{
|
value.pt = new ui::Point(*(value.pt));
|
||||||
value = new ui::Point(*((ui::Point*)value));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AnyType::operator NumberType()
|
AnyType::operator NumberType()
|
||||||
@ -33,7 +29,7 @@ AnyType::operator NumberType()
|
|||||||
if(type != TypeNumber)
|
if(type != TypeNumber)
|
||||||
throw InvalidConversionException(type, TypeNumber);
|
throw InvalidConversionException(type, TypeNumber);
|
||||||
else
|
else
|
||||||
return NumberType((intptr_t)value);
|
return NumberType(value.num);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnyType::operator StringType()
|
AnyType::operator StringType()
|
||||||
@ -41,16 +37,16 @@ AnyType::operator StringType()
|
|||||||
if(type == TypeNumber)
|
if(type == TypeNumber)
|
||||||
{
|
{
|
||||||
std::stringstream numberStream;
|
std::stringstream numberStream;
|
||||||
numberStream << ((NumberType*)this)->Value();
|
numberStream << ((NumberType *)this)->Value();
|
||||||
return StringType(numberStream.str());
|
return StringType(numberStream.str());
|
||||||
}
|
}
|
||||||
else if(type == TypeString && value)
|
else if(type == TypeString && value.str)
|
||||||
{
|
{
|
||||||
return StringType(*((std::string*)value));
|
return StringType(*(value.str));
|
||||||
}
|
}
|
||||||
else if (type == TypePoint && value)
|
else if (type == TypePoint && value.pt)
|
||||||
{
|
{
|
||||||
ui::Point thisPoint = *((ui::Point*)value);
|
ui::Point thisPoint = *(value.pt);
|
||||||
std::stringstream pointStream;
|
std::stringstream pointStream;
|
||||||
pointStream << thisPoint.X << "," << thisPoint.Y;
|
pointStream << thisPoint.X << "," << thisPoint.Y;
|
||||||
return StringType(pointStream.str());
|
return StringType(pointStream.str());
|
||||||
@ -64,11 +60,11 @@ AnyType::operator PointType()
|
|||||||
{
|
{
|
||||||
if(type == TypePoint)
|
if(type == TypePoint)
|
||||||
{
|
{
|
||||||
return PointType(*((ui::Point*)value));
|
return PointType(*(value.pt));
|
||||||
}
|
}
|
||||||
else if(type == TypeString)
|
else if(type == TypeString)
|
||||||
{
|
{
|
||||||
std::stringstream pointStream(*((std::string*)value));
|
std::stringstream pointStream(*(value.str));
|
||||||
int x, y;
|
int x, y;
|
||||||
char comma;
|
char comma;
|
||||||
pointStream >> x >> comma >> y;
|
pointStream >> x >> comma >> y;
|
||||||
@ -82,35 +78,49 @@ AnyType::operator PointType()
|
|||||||
|
|
||||||
AnyType::~AnyType()
|
AnyType::~AnyType()
|
||||||
{
|
{
|
||||||
if(type == TypeString || type == TypePoint)
|
if(type == TypeString)
|
||||||
delete value;
|
delete value.str;
|
||||||
|
else if(type == TypePoint)
|
||||||
|
delete value.pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Number Type
|
//Number Type
|
||||||
|
|
||||||
NumberType::NumberType(int number): AnyType(TypeNumber, (void*)number) { }
|
NumberType::NumberType(int number): AnyType(TypeNumber, ValueValue())
|
||||||
|
{
|
||||||
|
value.num = number;
|
||||||
|
}
|
||||||
|
|
||||||
int NumberType::Value()
|
int NumberType::Value()
|
||||||
{
|
{
|
||||||
return (intptr_t)value;
|
return value.num;
|
||||||
}
|
}
|
||||||
|
|
||||||
//String type
|
//String type
|
||||||
|
|
||||||
StringType::StringType(std::string string): AnyType(TypeString, new std::string(string)) { }
|
StringType::StringType(std::string string): AnyType(TypeString, ValueValue())
|
||||||
|
{
|
||||||
|
value.str = new std::string(string);
|
||||||
|
}
|
||||||
|
|
||||||
std::string StringType::Value()
|
std::string StringType::Value()
|
||||||
{
|
{
|
||||||
return std::string(*((std::string*)value));
|
return *value.str;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Point type
|
//Point type
|
||||||
|
|
||||||
PointType::PointType(ui::Point point): AnyType(TypePoint, new ui::Point(point)) { }
|
PointType::PointType(ui::Point point): AnyType(TypePoint, ValueValue())
|
||||||
|
{
|
||||||
|
value.pt = new ui::Point(point);
|
||||||
|
}
|
||||||
|
|
||||||
PointType::PointType(int pointX, int pointY): AnyType(TypePoint, new ui::Point(pointX, pointY)) { }
|
PointType::PointType(int pointX, int pointY): AnyType(TypePoint, ValueValue())
|
||||||
|
{
|
||||||
|
value.pt = new ui::Point(pointX, pointY);
|
||||||
|
}
|
||||||
|
|
||||||
ui::Point PointType::Value()
|
ui::Point PointType::Value()
|
||||||
{
|
{
|
||||||
return ui::Point(*((ui::Point*)value));
|
return *value.pt;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "gui/interface/Point.h"
|
#include "gui/interface/Point.h"
|
||||||
|
|
||||||
enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction };
|
enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction };
|
||||||
|
typedef union { int num; std::string* str; ui::Point* pt; } ValueValue;
|
||||||
|
|
||||||
class GeneralException
|
class GeneralException
|
||||||
{
|
{
|
||||||
@ -29,9 +30,9 @@ class AnyType
|
|||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
ValueType type;
|
ValueType type;
|
||||||
void * value;
|
ValueValue value;
|
||||||
public:
|
public:
|
||||||
AnyType(ValueType type_, void * value_);
|
AnyType(ValueType type_, ValueValue value_);
|
||||||
AnyType(const AnyType & v);
|
AnyType(const AnyType & v);
|
||||||
operator NumberType();
|
operator NumberType();
|
||||||
operator StringType();
|
operator StringType();
|
||||||
|
@ -154,7 +154,7 @@ int TPTScriptInterface::parseNumber(char * stringData)
|
|||||||
AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
|
AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
|
||||||
{
|
{
|
||||||
if(words->size() < 1)
|
if(words->size() < 1)
|
||||||
return AnyType(TypeNull, NULL);
|
return AnyType(TypeNull, ValueValue());
|
||||||
std::string word = words->front(); words->pop_front();
|
std::string word = words->front(); words->pop_front();
|
||||||
char * rawWord = (char *)word.c_str();
|
char * rawWord = (char *)word.c_str();
|
||||||
ValueType wordType = testType(word);
|
ValueType wordType = testType(word);
|
||||||
|
Reference in New Issue
Block a user