summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-19 09:42:16 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-19 09:42:16 -0700
commitc33bfc4ede827d3335e353fd35815c1613257942 (patch)
treeb7cc27b6205bd571b3d56162f2f9b9dd47b69254
parent16a5fdbed6f57d62f2ac44f37988c35319222d79 (diff)
downloadstage-c33bfc4ede827d3335e353fd35815c1613257942.tar.gz
stage-c33bfc4ede827d3335e353fd35815c1613257942.tar.bz2
stage-c33bfc4ede827d3335e353fd35815c1613257942.tar.xz
stage-c33bfc4ede827d3335e353fd35815c1613257942.zip
Variables are assignable and comperable now.
Not >, <. >=, <=, but those should be really easy.
-rw-r--r--src/variable.cpp182
-rw-r--r--src/variable.h48
2 files changed, 203 insertions, 27 deletions
diff --git a/src/variable.cpp b/src/variable.cpp
index fab0c46..a698b1c 100644
--- a/src/variable.cpp
+++ b/src/variable.cpp
@@ -1,41 +1,47 @@
1#include "variable.h" 1#include "variable.h"
2 2
3typedef Bu::ExceptionBase VariableException;
4
3Variable::Variable() : 5Variable::Variable() :
4 eType( Null ), 6 eType( tNull ),
7 iValue( 0 )
8{
9}
10
11Variable::Variable( const Variable &src ) :
12 eType( tNull ),
5 iValue( 0 ) 13 iValue( 0 )
6{ 14{
15 (*this) = src;
7} 16}
8 17
9Variable::Variable( Type eType ) : 18Variable::Variable( Type eType ) :
10 eType( eType ), 19 eType( eType ),
11 iValue( 0 ) 20 iValue( 0 )
12{ 21{
13 if( eType == String ) 22 initType();
14 {
15 sValue = new Bu::String();
16 }
17} 23}
18 24
19Variable::Variable( int64_t iValue ) : 25Variable::Variable( int64_t iValue ) :
20 eType( Int ), 26 eType( tInt ),
21 iValue( iValue ) 27 iValue( iValue )
22{ 28{
23} 29}
24 30
25Variable::Variable( double fValue ) : 31Variable::Variable( double fValue ) :
26 eType( Float ), 32 eType( tFloat ),
27 fValue( fValue ) 33 fValue( fValue )
28{ 34{
29} 35}
30 36
31Variable::Variable( bool bValue ) : 37Variable::Variable( bool bValue ) :
32 eType( Bool ), 38 eType( tBool ),
33 bValue( bValue ) 39 bValue( bValue )
34{ 40{
35} 41}
36 42
37Variable::Variable( const Bu::String &sValue ) : 43Variable::Variable( const Bu::String &sValue ) :
38 eType( String ), 44 eType( tString ),
39 iValue( 0 ) 45 iValue( 0 )
40{ 46{
41 this->sValue = new Bu::String( sValue ); 47 this->sValue = new Bu::String( sValue );
@@ -43,7 +49,161 @@ Variable::Variable( const Bu::String &sValue ) :
43 49
44Variable::~Variable() 50Variable::~Variable()
45{ 51{
46 if( eType == String ) 52 deinitType();
47 delete sValue; 53}
54
55Variable &Variable::operator=( const Variable &rhs )
56{
57 deinitType();
58 eType = rhs.eType;
59 initType();
60
61 switch( eType )
62 {
63 case tNull:
64 break;
65
66 case tBool:
67 bValue = rhs.bValue;
68 break;
69
70 case tInt:
71 iValue = rhs.iValue;
72 break;
73
74 case tFloat:
75 fValue = rhs.fValue;
76 break;
77
78 case tString:
79 (*sValue) = *rhs.sValue;
80 break;
81
82 case tList:
83 (*lValue) = *rhs.lValue;
84 break;
85
86 case tDictionary:
87 (*hValue) = *rhs.hValue;
88 break;
89 }
90}
91/*
92Variable &Variable::operator+=( const Variable &rhs );
93Variable &Variable::operator-=( const Variable &rhs );
94Variable &Variable::operator*=( const Variable &rhs );
95Variable &Variable::operator/=( const Variable &rhs );
96Variable &Variable::operator+( const Variable &rhs ) const;
97Variable &Variable::operator-( const Variable &rhs ) const;
98Variable &Variable::operator*( const Variable &rhs ) const;
99Variable &Variable::operator/( const Variable &rhs ) const;
100*/
101
102bool Variable::operator==( const Variable &rhs ) const
103{
104 if( eType != rhs.eType )
105 return false;
106
107 switch( eType )
108 {
109 case tNull:
110 return true;
111
112 case tBool:
113 return bValue == rhs.bValue;
114
115 case tInt:
116 return iValue == rhs.iValue;
117
118 case tFloat:
119 return fValue == rhs.fValue;
120
121 case tString:
122 return (*sValue) == (*rhs.sValue);
123
124 case tList:
125 return (*lValue) == (*rhs.lValue);
126
127 case tDictionary:
128 return (*hValue) == (*rhs.hValue);
129 }
130}
131
132bool Variable::operator!=( const Variable &rhs ) const
133{
134 return !(*this == rhs);
135}
136
137/*
138Variable &Variable::operator>( const Variable &rhs );
139Variable &Variable::operator<( const Variable &rhs );
140Variable &Variable::operator>=( const Variable &rhs );
141Variable &Variable::operator<=( const Variable &rhs );
142*/
143void Variable::initType()
144{
145 iValue = 0;
146 switch( eType )
147 {
148 case tString:
149 sValue = new Bu::String();
150 break;
151
152 case tList:
153 lValue = new VList();
154 break;
155
156 case tDictionary:
157 hValue = new VHash();
158 break;
159 }
160}
161
162void Variable::deinitType()
163{
164 switch( eType )
165 {
166 case tString:
167 delete sValue;
168 break;
169
170 case tList:
171 delete lValue;
172 break;
173
174 case tDictionary:
175 delete hValue;
176 break;
177 }
178
179 iValue = 0;
180}
181
182template<> uint32_t Bu::__calcHashCode<Variable>( const Variable &k )
183{
184 switch( k.getType() )
185 {
186 case Variable::tNull:
187 return 0;
188
189 case Variable::tInt:
190 return k.iValue;
191
192 case Variable::tFloat:
193 return k.fValue;
194
195 case Variable::tString:
196 return Bu::__calcHashCode( *k.sValue );
197
198 case Variable::tList:
199 throw VariableException("You cannot use a list as a key in a dictionary.");
200 case Variable::tDictionary:
201 throw VariableException("You cannot use a dictionary as a key in a dictionary.");
202 }
203}
204
205template<> bool Bu::__cmpHashKeys<Variable>( const Variable &a, const Variable &b )
206{
207 return a == b;
48} 208}
49 209
diff --git a/src/variable.h b/src/variable.h
index c9665f5..0236951 100644
--- a/src/variable.h
+++ b/src/variable.h
@@ -8,20 +8,22 @@
8 8
9class Variable 9class Variable
10{ 10{
11friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k );
11public: 12public:
12 enum Type 13 enum Type
13 { 14 {
14 Null, 15 tNull,
15 Int, 16 tBool,
16 Float, 17 tInt,
17 Bool, 18 tFloat,
18 String, 19 tString,
19 List, 20 tList,
20 Dictionary 21 tDictionary
21 }; 22 };
22 23
23public: 24public:
24 Variable(); 25 Variable();
26 Variable( const Variable &src );
25 Variable( Type eType ); 27 Variable( Type eType );
26 Variable( int64_t iValue ); 28 Variable( int64_t iValue );
27 Variable( double fValue ); 29 Variable( double fValue );
@@ -29,20 +31,27 @@ public:
29 Variable( const Bu::String &sValue ); 31 Variable( const Bu::String &sValue );
30 virtual ~Variable(); 32 virtual ~Variable();
31 33
34 Type getType() const { return eType; }
35
32 Variable &operator=( const Variable &rhs ); 36 Variable &operator=( const Variable &rhs );
33 Variable &operator+=( const Variable &rhs ); 37 Variable &operator+=( const Variable &rhs );
34 Variable &operator-=( const Variable &rhs ); 38 Variable &operator-=( const Variable &rhs );
35 Variable &operator*=( const Variable &rhs ); 39 Variable &operator*=( const Variable &rhs );
36 Variable &operator/=( const Variable &rhs ); 40 Variable &operator/=( const Variable &rhs );
37 Variable &operator+( const Variable &rhs ); 41 Variable &operator+( const Variable &rhs ) const;
38 Variable &operator-( const Variable &rhs ); 42 Variable &operator-( const Variable &rhs ) const;
39 Variable &operator*( const Variable &rhs ); 43 Variable &operator*( const Variable &rhs ) const;
40 Variable &operator/( const Variable &rhs ); 44 Variable &operator/( const Variable &rhs ) const;
41 Variable &operator!=( const Variable &rhs ); 45 bool operator==( const Variable &rhs ) const;
42 Variable &operator>( const Variable &rhs ); 46 bool operator!=( const Variable &rhs ) const;
43 Variable &operator<( const Variable &rhs ); 47 bool operator>( const Variable &rhs ) const;
44 Variable &operator>=( const Variable &rhs ); 48 bool operator<( const Variable &rhs ) const;
45 Variable &operator<=( const Variable &rhs ); 49 bool operator>=( const Variable &rhs ) const;
50 bool operator<=( const Variable &rhs ) const;
51
52private:
53 void initType();
54 void deinitType();
46 55
47private: 56private:
48 Type eType; 57 Type eType;
@@ -61,4 +70,11 @@ private:
61 }; 70 };
62}; 71};
63 72
73namespace Bu
74{
75 template<> uint32_t __calcHashCode<Variable>( const Variable &k );
76 template<> bool __cmpHashKeys<Variable>( const Variable &a, const Variable &b );
77}
78
79
64#endif 80#endif