summaryrefslogtreecommitdiff
path: root/src/cat/TPTSTypes.cpp
blob: 0a3e940c94aaedf1ab30985785cf5ab5e2c003ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <sstream>
#include <stdint.h>
#include "TPTSTypes.h"

AnyType::AnyType(ValueType type_, void * value_):
	type(type_),
	value(value_)
{
}

ValueType AnyType::GetType()
{
	return type;
}

AnyType::AnyType(const AnyType & v):
	type(v.type),
	value(v.value)
{
	if(type == TypeString)
	{
		value = new std::string(*((std::string*)value));
	}
	else if(type == TypePoint)
	{
		value = new ui::Point(*((ui::Point*)value));
	}
}

AnyType::operator NumberType()
{
	if(type != TypeNumber)
		throw InvalidConversionException(type, TypeNumber);
	else
		return NumberType((intptr_t)value);
}

AnyType::operator StringType()
{
	if(type == TypeNumber)
	{
		std::stringstream numberStream;
		numberStream << ((NumberType*)this)->Value();
		return StringType(numberStream.str());
	}
	else if(type == TypeString && value)
	{
		return StringType(*((std::string*)value));
	}
	else if (type == TypePoint && value)
	{
		ui::Point thisPoint = *((ui::Point*)value);
		std::stringstream pointStream;
		pointStream << thisPoint.X << "," << thisPoint.Y;
		return StringType(pointStream.str());
	}
	else
		throw InvalidConversionException(type, TypeString);

}

AnyType::operator PointType()
{
	if(type == TypePoint)
	{
		return PointType(*((ui::Point*)value));
	}
	else if(type == TypeString)
	{
		std::stringstream pointStream(*((std::string*)value));
		int x, y;
		char comma;
		pointStream >> x >> comma >> y;
		if (pointStream.fail() || comma != ',')
			throw InvalidConversionException(type, TypePoint);
		return PointType(ui::Point(x, y));
	}
	else
		throw InvalidConversionException(type, TypePoint);
}

AnyType::~AnyType()
{
	if(type == TypeString || type == TypePoint)
		delete value;
}

//Number Type

NumberType::NumberType(int number):	AnyType(TypeNumber, (void*)number) { }

int NumberType::Value()
{
	return (intptr_t)value;
}

//String type

StringType::StringType(std::string string):	AnyType(TypeString, new std::string(string)) { }

std::string StringType::Value()
{
	return std::string(*((std::string*)value));
}

//Point type

PointType::PointType(ui::Point point): AnyType(TypePoint, new ui::Point(point)) { }

PointType::PointType(int pointX, int pointY): AnyType(TypePoint, new ui::Point(pointX, pointY)) { }

ui::Point PointType::Value()
{
	return ui::Point(*((ui::Point*)value));
}