aboutsummaryrefslogtreecommitdiff
path: root/src/astleaf.cpp
blob: c192dbf70380ab0cc0cd947bc89d85679456b38c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "astleaf.h"

AstLeaf::AstLeaf( const Location &loc, Type eType ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
}

AstLeaf::AstLeaf( const Location &loc, Type eType, int iNew ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
	setIntValue( iNew );
}

AstLeaf::AstLeaf( const Location &loc, Type eType, float fNew ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
	setFloatValue( fNew );
}

AstLeaf::AstLeaf( const Location &loc, Type eType, bool bNew ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
	setBoolValue( bNew );
}

AstLeaf::AstLeaf( const Location &loc, Type eType, const Bu::String &sNew ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
	setStrValue( sNew );
}

AstLeaf::AstLeaf( const Location &loc, Type eType, const char *sNew ) :
	AstNode( loc, eType ),
	sVal( NULL )
{
	setStrValue( sNew );
}

AstLeaf::~AstLeaf()
{
	if( getDataType() == typeDataString )
		delete sVal;
}

void AstLeaf::setIntValue( int iNew )
{
	if( getDataType() != typeDataInt )
		throw Bu::ExceptionBase("Type is not int.");
	iVal = iNew;
}

void AstLeaf::setFloatValue( float fNew )
{
	if( getDataType() != typeDataFloat )
		throw Bu::ExceptionBase("Type is not float.");
	fVal = fNew;
}

void AstLeaf::setBoolValue( bool bNew )
{
	if( getDataType() != typeDataBool )
		throw Bu::ExceptionBase("Type is not bool.");
	bVal = bNew;
}

void AstLeaf::setStrValue( const Bu::String &sNew )
{
	if( getDataType() != typeDataString )
		throw Bu::ExceptionBase("Type is not string.");
	if( sVal == NULL )
		sVal = new Bu::String( sNew );
	else
		*sVal = sNew;
}

int AstLeaf::getIntValue() const
{
	if( getDataType() != typeDataInt )
		throw Bu::ExceptionBase("Type is not int.");
	return iVal;
}

float AstLeaf::getFloatValue() const
{
	if( getDataType() != typeDataFloat )
		throw Bu::ExceptionBase("Type is not float.");
	return fVal;
}

bool AstLeaf::getBoolValue() const
{
	if( getDataType() != typeDataBool )
		throw Bu::ExceptionBase("Type is not bool.");
	return bVal;
}

Bu::String &AstLeaf::getStrValue() const
{
	if( getDataType() != typeDataString )
		throw Bu::ExceptionBase("Type is not string.");
	return *sVal;
}

Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l )
{
	switch( l.getDataType() )
	{
		case AstNode::typeDataInt:
			f << ": " << l.getIntValue();
			break;

		case AstNode::typeDataFloat:
			f << ": " << l.getFloatValue();
			break;

		case AstNode::typeDataBool:
			f << ": " << l.getBoolValue();
			break;

		case AstNode::typeDataString:
			f << ": '" << l.getStrValue() << "'";
			break;

		case AstNode::typeDataNone:
			break;

		default:
			f << ": " << "!! Invalid Type !!";
	}
	return f;
}