diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-19 09:42:16 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-19 09:42:16 -0700 |
commit | c33bfc4ede827d3335e353fd35815c1613257942 (patch) | |
tree | b7cc27b6205bd571b3d56162f2f9b9dd47b69254 | |
parent | 16a5fdbed6f57d62f2ac44f37988c35319222d79 (diff) | |
download | stage-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.cpp | 182 | ||||
-rw-r--r-- | src/variable.h | 48 |
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 | ||
3 | typedef Bu::ExceptionBase VariableException; | ||
4 | |||
3 | Variable::Variable() : | 5 | Variable::Variable() : |
4 | eType( Null ), | 6 | eType( tNull ), |
7 | iValue( 0 ) | ||
8 | { | ||
9 | } | ||
10 | |||
11 | Variable::Variable( const Variable &src ) : | ||
12 | eType( tNull ), | ||
5 | iValue( 0 ) | 13 | iValue( 0 ) |
6 | { | 14 | { |
15 | (*this) = src; | ||
7 | } | 16 | } |
8 | 17 | ||
9 | Variable::Variable( Type eType ) : | 18 | Variable::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 | ||
19 | Variable::Variable( int64_t iValue ) : | 25 | Variable::Variable( int64_t iValue ) : |
20 | eType( Int ), | 26 | eType( tInt ), |
21 | iValue( iValue ) | 27 | iValue( iValue ) |
22 | { | 28 | { |
23 | } | 29 | } |
24 | 30 | ||
25 | Variable::Variable( double fValue ) : | 31 | Variable::Variable( double fValue ) : |
26 | eType( Float ), | 32 | eType( tFloat ), |
27 | fValue( fValue ) | 33 | fValue( fValue ) |
28 | { | 34 | { |
29 | } | 35 | } |
30 | 36 | ||
31 | Variable::Variable( bool bValue ) : | 37 | Variable::Variable( bool bValue ) : |
32 | eType( Bool ), | 38 | eType( tBool ), |
33 | bValue( bValue ) | 39 | bValue( bValue ) |
34 | { | 40 | { |
35 | } | 41 | } |
36 | 42 | ||
37 | Variable::Variable( const Bu::String &sValue ) : | 43 | Variable::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 | ||
44 | Variable::~Variable() | 50 | Variable::~Variable() |
45 | { | 51 | { |
46 | if( eType == String ) | 52 | deinitType(); |
47 | delete sValue; | 53 | } |
54 | |||
55 | Variable &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 | /* | ||
92 | Variable &Variable::operator+=( const Variable &rhs ); | ||
93 | Variable &Variable::operator-=( const Variable &rhs ); | ||
94 | Variable &Variable::operator*=( const Variable &rhs ); | ||
95 | Variable &Variable::operator/=( const Variable &rhs ); | ||
96 | Variable &Variable::operator+( const Variable &rhs ) const; | ||
97 | Variable &Variable::operator-( const Variable &rhs ) const; | ||
98 | Variable &Variable::operator*( const Variable &rhs ) const; | ||
99 | Variable &Variable::operator/( const Variable &rhs ) const; | ||
100 | */ | ||
101 | |||
102 | bool 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 | |||
132 | bool Variable::operator!=( const Variable &rhs ) const | ||
133 | { | ||
134 | return !(*this == rhs); | ||
135 | } | ||
136 | |||
137 | /* | ||
138 | Variable &Variable::operator>( const Variable &rhs ); | ||
139 | Variable &Variable::operator<( const Variable &rhs ); | ||
140 | Variable &Variable::operator>=( const Variable &rhs ); | ||
141 | Variable &Variable::operator<=( const Variable &rhs ); | ||
142 | */ | ||
143 | void 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 | |||
162 | void 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 | |||
182 | template<> 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 | |||
205 | template<> 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 | ||
9 | class Variable | 9 | class Variable |
10 | { | 10 | { |
11 | friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k ); | ||
11 | public: | 12 | public: |
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 | ||
23 | public: | 24 | public: |
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 | |||
52 | private: | ||
53 | void initType(); | ||
54 | void deinitType(); | ||
46 | 55 | ||
47 | private: | 56 | private: |
48 | Type eType; | 57 | Type eType; |
@@ -61,4 +70,11 @@ private: | |||
61 | }; | 70 | }; |
62 | }; | 71 | }; |
63 | 72 | ||
73 | namespace 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 |