aboutsummaryrefslogtreecommitdiff
path: root/src/astleaf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/astleaf.cpp')
-rw-r--r--src/astleaf.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/astleaf.cpp b/src/astleaf.cpp
new file mode 100644
index 0000000..62773ce
--- /dev/null
+++ b/src/astleaf.cpp
@@ -0,0 +1,137 @@
1#include "astleaf.h"
2
3AstLeaf::AstLeaf( Type eType ) :
4 AstNode( eType ),
5 sVal( NULL )
6{
7}
8
9AstLeaf::AstLeaf( Type eType, int iNew ) :
10 AstNode( eType ),
11 sVal( NULL )
12{
13 setIntValue( iNew );
14}
15
16AstLeaf::AstLeaf( Type eType, float fNew ) :
17 AstNode( eType ),
18 sVal( NULL )
19{
20 setFloatValue( fNew );
21}
22
23AstLeaf::AstLeaf( Type eType, bool bNew ) :
24 AstNode( eType ),
25 sVal( NULL )
26{
27 setBoolValue( bNew );
28}
29
30AstLeaf::AstLeaf( Type eType, const Bu::FString &sNew ) :
31 AstNode( eType ),
32 sVal( NULL )
33{
34 setStrValue( sNew );
35}
36
37AstLeaf::AstLeaf( Type eType, const char *sNew ) :
38 AstNode( eType ),
39 sVal( NULL )
40{
41 setStrValue( sNew );
42}
43
44AstLeaf::~AstLeaf()
45{
46 if( getDataType() == typeDataString )
47 delete sVal;
48}
49
50void AstLeaf::setIntValue( int iNew )
51{
52 if( getDataType() != typeDataInt )
53 throw Bu::ExceptionBase("Type is not int.");
54 iVal = iNew;
55}
56
57void AstLeaf::setFloatValue( float fNew )
58{
59 if( getDataType() != typeDataFloat )
60 throw Bu::ExceptionBase("Type is not float.");
61 fVal = fNew;
62}
63
64void AstLeaf::setBoolValue( bool bNew )
65{
66 if( getDataType() != typeDataBool )
67 throw Bu::ExceptionBase("Type is not bool.");
68 bVal = bNew;
69}
70
71void AstLeaf::setStrValue( const Bu::FString &sNew )
72{
73 if( getDataType() != typeDataString )
74 throw Bu::ExceptionBase("Type is not string.");
75 if( sVal == NULL )
76 sVal = new Bu::FString( sNew );
77 else
78 *sVal = sNew;
79}
80
81int AstLeaf::getIntValue() const
82{
83 if( getDataType() != typeDataInt )
84 throw Bu::ExceptionBase("Type is not int.");
85 return iVal;
86}
87
88float AstLeaf::getFloatValue() const
89{
90 if( getDataType() != typeDataFloat )
91 throw Bu::ExceptionBase("Type is not float.");
92 return fVal;
93}
94
95bool AstLeaf::getBoolValue() const
96{
97 if( getDataType() != typeDataBool )
98 throw Bu::ExceptionBase("Type is not bool.");
99 return bVal;
100}
101
102Bu::FString &AstLeaf::getStrValue() const
103{
104 if( getDataType() != typeDataString )
105 throw Bu::ExceptionBase("Type is not string.");
106 return *sVal;
107}
108
109Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l )
110{
111 switch( l.getDataType() )
112 {
113 case AstNode::typeDataInt:
114 f << ": " << l.getIntValue();
115 break;
116
117 case AstNode::typeDataFloat:
118 f << ": " << l.getFloatValue();
119 break;
120
121 case AstNode::typeDataBool:
122 f << ": " << l.getBoolValue();
123 break;
124
125 case AstNode::typeDataString:
126 f << ": '" << l.getStrValue() << "'";
127 break;
128
129 case AstNode::typeDataNone:
130 break;
131
132 default:
133 f << ": " << "!! Invalid Type !!";
134 }
135 return f;
136}
137