1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "multi_type.h" |
---|
19 | |
---|
20 | //#include "stdincl.h" |
---|
21 | #include <stddef.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <sstream> |
---|
26 | |
---|
27 | #ifdef DEBUG |
---|
28 | #include "debug.h" |
---|
29 | #endif |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | /** |
---|
34 | * @brief creates a multiType without any stored value at all. |
---|
35 | */ |
---|
36 | MultiType::MultiType(MT_Type type) |
---|
37 | { |
---|
38 | this->type = type; |
---|
39 | switch (this->type) |
---|
40 | { |
---|
41 | default: |
---|
42 | this->value.Float = 0.0f; |
---|
43 | break; |
---|
44 | case MT_BOOL: |
---|
45 | this->value.Bool = false; |
---|
46 | break; |
---|
47 | case MT_INT: |
---|
48 | this->value.Int = 0; |
---|
49 | break; |
---|
50 | case MT_FLOAT: |
---|
51 | this->value.Float = 0.0f; |
---|
52 | break; |
---|
53 | case MT_CHAR: |
---|
54 | this->value.Char = '\0'; |
---|
55 | break; |
---|
56 | case MT_STRING: |
---|
57 | this->storedString = ""; |
---|
58 | break; |
---|
59 | } |
---|
60 | } |
---|
61 | /** |
---|
62 | * @brief creates a multiType out of a boolean |
---|
63 | * @param value the Value of this MulitType |
---|
64 | */ |
---|
65 | MultiType::MultiType(bool value) |
---|
66 | { |
---|
67 | this->setBool(value); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief creates a multiType out of an integer |
---|
72 | * @param value the Value of this MulitType |
---|
73 | */ |
---|
74 | MultiType::MultiType(int value) |
---|
75 | { |
---|
76 | this->setInt(value); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief creates a multiType out of a float (double) |
---|
81 | * @param value the Value of this MulitType |
---|
82 | */ |
---|
83 | MultiType::MultiType(double value) |
---|
84 | { |
---|
85 | this->setFloat(value); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * @brief creates a multiType out of a char |
---|
90 | * @param value the Value of this MulitType |
---|
91 | */ |
---|
92 | MultiType::MultiType(char value) |
---|
93 | { |
---|
94 | this->setChar(value); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * @brief creates a multiType out of a C-String |
---|
99 | * @param value the Value of this MulitType |
---|
100 | */ |
---|
101 | MultiType::MultiType(const char* value) |
---|
102 | { |
---|
103 | this->setString(value); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * @brief creates a multiType out of a String |
---|
108 | * @param value the Value of this MulitType |
---|
109 | */ |
---|
110 | MultiType::MultiType(const std::string& value) |
---|
111 | { |
---|
112 | this->setString(value); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * @brief constructs a new MultiType from another one (copy) |
---|
117 | */ |
---|
118 | MultiType::MultiType(const MultiType& multiType) |
---|
119 | { |
---|
120 | *this = multiType; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief standard deconstructor |
---|
125 | */ |
---|
126 | MultiType::~MultiType () |
---|
127 | { } |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief copy operator |
---|
131 | * @param mt: the entity to copy |
---|
132 | * @returns A Copy of itself. (strings inside are copied as well) |
---|
133 | */ |
---|
134 | MultiType& MultiType::operator= (const MultiType& mt) |
---|
135 | { |
---|
136 | this->type = mt.type; |
---|
137 | this->value = mt.value; |
---|
138 | this->storedString = mt.storedString; |
---|
139 | |
---|
140 | return *this; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief checks if the two Multitypes match |
---|
145 | * @param mt MultiType to check against this one |
---|
146 | * @returns true on match. false otherwise |
---|
147 | * |
---|
148 | * Two MultiType match if and only if |
---|
149 | * 1. the internal Type is the same |
---|
150 | * 2. the stored values match |
---|
151 | */ |
---|
152 | bool MultiType::operator==(const MultiType& mt) const |
---|
153 | { |
---|
154 | if (this->type != mt.type) |
---|
155 | return false; |
---|
156 | |
---|
157 | switch (this->type) |
---|
158 | { |
---|
159 | case MT_NULL: |
---|
160 | return true; |
---|
161 | case MT_BOOL: |
---|
162 | return (this->value.Bool == mt.value.Bool); |
---|
163 | case MT_INT: |
---|
164 | return (this->value.Int == mt.value.Int); |
---|
165 | case MT_CHAR: |
---|
166 | return (this->value.Char == mt.value.Char); |
---|
167 | case MT_FLOAT: |
---|
168 | return (this->value.Float == mt.value.Float); |
---|
169 | case MT_STRING: |
---|
170 | return (this->storedString == mt.storedString); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | /** |
---|
176 | * @brief sets the type of this MultiType and resets to the default value |
---|
177 | * @param type the new Type |
---|
178 | */ |
---|
179 | void MultiType::setType(MT_Type type) |
---|
180 | { |
---|
181 | if (this->type == type) |
---|
182 | return; |
---|
183 | |
---|
184 | switch (type) |
---|
185 | { |
---|
186 | case MT_BOOL: |
---|
187 | this->setBool(this->getBool()); |
---|
188 | break; |
---|
189 | case MT_INT: |
---|
190 | this->setInt(this->getInt()); |
---|
191 | break; |
---|
192 | case MT_FLOAT: |
---|
193 | this->setFloat(this->getFloat()); |
---|
194 | break; |
---|
195 | case MT_CHAR: |
---|
196 | this->setChar(this->getChar()); |
---|
197 | break; |
---|
198 | case MT_STRING: |
---|
199 | this->setString(this->getString()); |
---|
200 | break; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * @brief sets the Value of mt without changing the type of this MultiType |
---|
206 | * @param mt: the MultiType to get the value from |
---|
207 | * |
---|
208 | * This is a pure Value copy. The current type will be preserved. |
---|
209 | * |
---|
210 | * @TODO speedup |
---|
211 | */ |
---|
212 | void MultiType::setValueOf(const MultiType& mt) |
---|
213 | { |
---|
214 | MT_Type prevType = this->type; |
---|
215 | |
---|
216 | *this = mt; |
---|
217 | this->setType(prevType); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * @brief sets a new Value to the MultiType |
---|
223 | * @param value the new Value as a bool |
---|
224 | */ |
---|
225 | void MultiType::setBool(bool value) |
---|
226 | { |
---|
227 | this->type = MT_BOOL; |
---|
228 | this->value.Bool = value; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * @brief sets a new Value to the MultiType |
---|
233 | * @param value the new Value as an int |
---|
234 | */ |
---|
235 | void MultiType::setInt(int value) |
---|
236 | { |
---|
237 | this->type = MT_INT; |
---|
238 | this->value.Int = value; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * @brief sets a new Value to the MultiType |
---|
243 | * @param value the new Value as a float |
---|
244 | */ |
---|
245 | void MultiType::setFloat(float value) |
---|
246 | { |
---|
247 | this->type = MT_FLOAT; |
---|
248 | this->value.Float = value; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * @brief sets a new Value to the MultiType |
---|
253 | * @param value the new Value as a char |
---|
254 | */ |
---|
255 | void MultiType::setChar(char value) |
---|
256 | { |
---|
257 | this->type = MT_CHAR; |
---|
258 | this->value.Char = value; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @brief sets a new Value to the MultiType |
---|
263 | * @param value the new Value as a String |
---|
264 | */ |
---|
265 | void MultiType::setString(const std::string& value) |
---|
266 | { |
---|
267 | this->type = MT_STRING; |
---|
268 | this->storedString = value; |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | /************************** |
---|
273 | *** RETRIEVAL FUNCTIONS *** |
---|
274 | **************************/ |
---|
275 | /** |
---|
276 | * @returns the Value of this MultiType as a int |
---|
277 | */ |
---|
278 | bool MultiType::getBool() const |
---|
279 | { |
---|
280 | // default case: |
---|
281 | if (this->type & MT_BOOL) |
---|
282 | return this->value.Bool; |
---|
283 | // Special Cases: |
---|
284 | else if (this->type & MT_INT) return (this->value.Int == 0)? false : true; |
---|
285 | else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true; |
---|
286 | else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true; |
---|
287 | else if (this->type & MT_STRING) return (this->storedString == "true" || |
---|
288 | this->storedString == "TRUE" || |
---|
289 | this->storedString != "0"); //! @TODO make this better... |
---|
290 | |
---|
291 | return false; |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * @returns the Value of this MultiType as a int |
---|
296 | */ |
---|
297 | int MultiType::getInt() const |
---|
298 | { |
---|
299 | // default case: |
---|
300 | if (this->type & MT_INT) |
---|
301 | return this->value.Int; |
---|
302 | if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0; |
---|
303 | else if (this->type & MT_FLOAT) return (int) this->value.Float; |
---|
304 | else if (this->type & MT_CHAR) return (int) this->value.Char; |
---|
305 | else if (this->type & MT_STRING) |
---|
306 | { |
---|
307 | if (this->storedString == "") return 0; |
---|
308 | char* endPtr = NULL; |
---|
309 | int result = strtol(this->storedString.c_str(), &endPtr, 10); |
---|
310 | if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + strlen(this->storedString.c_str())) |
---|
311 | return 0; |
---|
312 | else |
---|
313 | return result; |
---|
314 | } |
---|
315 | return 0; |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | /** |
---|
320 | * @returns the Value of this MultiType as a float |
---|
321 | */ |
---|
322 | float MultiType::getFloat() const |
---|
323 | { |
---|
324 | // default case: |
---|
325 | if (this->type & MT_FLOAT) |
---|
326 | return this->value.Float; |
---|
327 | if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f; |
---|
328 | else if (this->type & MT_INT) return (float) this->value.Int; |
---|
329 | else if (this->type & MT_CHAR) return (float) this->value.Char; |
---|
330 | else if (this->type & MT_STRING) |
---|
331 | { |
---|
332 | char* endPtr = NULL; |
---|
333 | double result = strtod(this->storedString.c_str(), &endPtr); |
---|
334 | if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + strlen(this->storedString.c_str())) |
---|
335 | return 0.0f; |
---|
336 | else |
---|
337 | return result; |
---|
338 | } |
---|
339 | return 0.0f; |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | /** |
---|
344 | * @returns the Value of this MultiType as a char |
---|
345 | */ |
---|
346 | char MultiType::getChar() const |
---|
347 | { |
---|
348 | // default case: |
---|
349 | if (this->type & MT_INT) |
---|
350 | return this->value.Int; |
---|
351 | if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n'; |
---|
352 | else if (this->type & MT_INT) return (int) this->value.Int; |
---|
353 | else if (this->type & MT_FLOAT) return (char) this->value.Float; |
---|
354 | else if (this->type & MT_STRING) return this->storedString[0]; |
---|
355 | |
---|
356 | return '\0'; |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | /** |
---|
361 | * @returns the Value of this MultiType as a String |
---|
362 | */ |
---|
363 | std::string MultiType::getString() const |
---|
364 | { |
---|
365 | // default case: |
---|
366 | if (this->type & MT_STRING) |
---|
367 | return this->storedString; |
---|
368 | else |
---|
369 | { |
---|
370 | if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false"; |
---|
371 | |
---|
372 | else if (this->type & MT_INT) |
---|
373 | { |
---|
374 | char tmpString[32]; |
---|
375 | sprintf(tmpString, "%d", this->value.Int); |
---|
376 | return tmpString; |
---|
377 | } |
---|
378 | else if (this->type & MT_FLOAT) |
---|
379 | { |
---|
380 | char tmpString[64]; |
---|
381 | sprintf(tmpString, "%f", this->value.Float); |
---|
382 | return tmpString; |
---|
383 | } |
---|
384 | else if (this->type & MT_CHAR) |
---|
385 | { |
---|
386 | char tmpString[2]; |
---|
387 | tmpString[0] = this->value.Char; |
---|
388 | tmpString[1] = '\0'; |
---|
389 | return tmpString; |
---|
390 | } |
---|
391 | } |
---|
392 | return ""; |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * @returns a formated c-string of the held value |
---|
397 | */ |
---|
398 | const char* MultiType::getCString() |
---|
399 | { |
---|
400 | if (this->type & MT_STRING) return this->storedString.c_str(); |
---|
401 | else |
---|
402 | { |
---|
403 | this->storedString = this->getString(); |
---|
404 | return this->storedString.c_str(); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * @brief prints out some nice debug output |
---|
410 | */ |
---|
411 | void MultiType::debug() const |
---|
412 | { |
---|
413 | #ifdef DEBUG |
---|
414 | PRINT(0) |
---|
415 | #else |
---|
416 | printf |
---|
417 | #endif |
---|
418 | ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n", |
---|
419 | MultiType::MultiTypeToString(this->type), |
---|
420 | this->getBool(), |
---|
421 | this->getInt(), |
---|
422 | this->getFloat(), |
---|
423 | this->getChar(), |
---|
424 | this->getString().c_str() |
---|
425 | ); |
---|
426 | } |
---|
427 | |
---|
428 | |
---|
429 | /** |
---|
430 | * @brief Resets the MultiType to default values. |
---|
431 | */ |
---|
432 | void MultiType::reset() |
---|
433 | { |
---|
434 | switch ( this->type ) |
---|
435 | { |
---|
436 | case MT_BOOL: |
---|
437 | this->setBool(false); |
---|
438 | break; |
---|
439 | case MT_INT: |
---|
440 | this->setInt(0); |
---|
441 | break; |
---|
442 | case MT_FLOAT: |
---|
443 | this->setFloat(0.0f); |
---|
444 | break; |
---|
445 | case MT_CHAR: |
---|
446 | this->setChar('\0'); |
---|
447 | break; |
---|
448 | case MT_STRING: |
---|
449 | this->setString(""); |
---|
450 | break; |
---|
451 | default: |
---|
452 | #ifdef DEBUG |
---|
453 | PRINTF(2)("Unknown Type not reseting\n"); |
---|
454 | #endif |
---|
455 | break; |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * @brief converts a MT_Type into a String |
---|
461 | * @param type: the MT_Type |
---|
462 | * @returns: the Type as a constant String (do not delete) |
---|
463 | */ |
---|
464 | const char* MultiType::MultiTypeToString(MT_Type type) |
---|
465 | { |
---|
466 | switch ( type ) |
---|
467 | { |
---|
468 | case MT_BOOL: |
---|
469 | return "bool"; |
---|
470 | case MT_INT: |
---|
471 | return "int"; |
---|
472 | case MT_FLOAT: |
---|
473 | return "float"; |
---|
474 | case MT_CHAR: |
---|
475 | return "char"; |
---|
476 | case MT_STRING: |
---|
477 | return "string"; |
---|
478 | } |
---|
479 | return "NONE"; |
---|
480 | } |
---|
481 | |
---|
482 | /** |
---|
483 | * @brief converts a String into a MT_Type |
---|
484 | * @param type: the Type as a String |
---|
485 | * @returns: the Type as MT_Type |
---|
486 | */ |
---|
487 | MT_Type MultiType::StringToMultiType(const char* type) |
---|
488 | { |
---|
489 | if (!strncmp(type, "bool", 4)) |
---|
490 | return MT_BOOL; |
---|
491 | if (!strncmp(type, "int", 3)) |
---|
492 | return MT_INT; |
---|
493 | if (!strncmp(type, "float", 5)) |
---|
494 | return MT_FLOAT; |
---|
495 | if (!strncmp(type, "char", 4)) |
---|
496 | return MT_CHAR; |
---|
497 | if (!strncmp(type, "string", 6)) |
---|
498 | return MT_STRING; |
---|
499 | |
---|
500 | return MT_NULL; |
---|
501 | } |
---|