[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5643] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5536] | 18 | #include "multi_type.h" |
---|
[1853] | 19 | |
---|
[5659] | 20 | //#include "stdincl.h" |
---|
| 21 | #include <stddef.h> |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <stdio.h> |
---|
[7199] | 25 | #include <sstream> |
---|
[5659] | 26 | |
---|
| 27 | #ifdef DEBUG |
---|
| 28 | #include "debug.h" |
---|
| 29 | #endif |
---|
| 30 | |
---|
[1856] | 31 | using namespace std; |
---|
[1853] | 32 | |
---|
[5545] | 33 | /** |
---|
[7199] | 34 | * @brief creates a multiType without any stored value at all. |
---|
[5545] | 35 | */ |
---|
[7197] | 36 | MultiType::MultiType(MT_Type type) |
---|
[5540] | 37 | { |
---|
[7197] | 38 | this->type = type; |
---|
[7200] | 39 | switch (this->type) |
---|
| 40 | { |
---|
[7211] | 41 | default: |
---|
| 42 | this->value.Float = 0.0f; |
---|
| 43 | break; |
---|
[7200] | 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; |
---|
[7201] | 56 | case MT_STRING: |
---|
| 57 | this->storedString = ""; |
---|
| 58 | break; |
---|
[7200] | 59 | } |
---|
[5540] | 60 | } |
---|
[5545] | 61 | /** |
---|
[7199] | 62 | * @brief creates a multiType out of a boolean |
---|
[5545] | 63 | * @param value the Value of this MulitType |
---|
| 64 | */ |
---|
[5537] | 65 | MultiType::MultiType(bool value) |
---|
| 66 | { |
---|
[5540] | 67 | this->setBool(value); |
---|
[5537] | 68 | } |
---|
| 69 | |
---|
[5545] | 70 | /** |
---|
[7199] | 71 | * @brief creates a multiType out of an integer |
---|
[5545] | 72 | * @param value the Value of this MulitType |
---|
| 73 | */ |
---|
[5536] | 74 | MultiType::MultiType(int value) |
---|
| 75 | { |
---|
[5540] | 76 | this->setInt(value); |
---|
[5536] | 77 | } |
---|
[1856] | 78 | |
---|
[5545] | 79 | /** |
---|
[7199] | 80 | * @brief creates a multiType out of a float (double) |
---|
[5545] | 81 | * @param value the Value of this MulitType |
---|
| 82 | */ |
---|
[5539] | 83 | MultiType::MultiType(double value) |
---|
[3365] | 84 | { |
---|
[5540] | 85 | this->setFloat(value); |
---|
[5536] | 86 | } |
---|
[4320] | 87 | |
---|
[5545] | 88 | /** |
---|
[7199] | 89 | * @brief creates a multiType out of a char |
---|
[5545] | 90 | * @param value the Value of this MulitType |
---|
| 91 | */ |
---|
[5536] | 92 | MultiType::MultiType(char value) |
---|
| 93 | { |
---|
[5540] | 94 | this->setChar(value); |
---|
[3365] | 95 | } |
---|
[1853] | 96 | |
---|
[5545] | 97 | /** |
---|
[7199] | 98 | * @brief creates a multiType out of a C-String |
---|
[5545] | 99 | * @param value the Value of this MulitType |
---|
| 100 | */ |
---|
[5536] | 101 | MultiType::MultiType(const char* value) |
---|
| 102 | { |
---|
[5540] | 103 | this->setString(value); |
---|
[5536] | 104 | } |
---|
| 105 | |
---|
[7199] | 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 | */ |
---|
[5659] | 118 | MultiType::MultiType(const MultiType& multiType) |
---|
| 119 | { |
---|
| 120 | *this = multiType; |
---|
| 121 | } |
---|
| 122 | |
---|
[3245] | 123 | /** |
---|
[7199] | 124 | * @brief standard deconstructor |
---|
[3245] | 125 | */ |
---|
[5536] | 126 | MultiType::~MultiType () |
---|
[7199] | 127 | { } |
---|
[5537] | 128 | |
---|
[5540] | 129 | /** |
---|
[7199] | 130 | * @brief copy operator |
---|
[5540] | 131 | * @param mt: the entity to copy |
---|
[7199] | 132 | * @returns A Copy of itself. (strings inside are copied as well) |
---|
[5540] | 133 | */ |
---|
[5659] | 134 | MultiType& MultiType::operator= (const MultiType& mt) |
---|
| 135 | { |
---|
| 136 | this->type = mt.type; |
---|
| 137 | this->value = mt.value; |
---|
[7199] | 138 | this->storedString = mt.storedString; |
---|
| 139 | |
---|
[5659] | 140 | return *this; |
---|
| 141 | } |
---|
| 142 | |
---|
[5545] | 143 | /** |
---|
[6643] | 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: |
---|
[7199] | 170 | return (this->storedString == mt.storedString); |
---|
[6643] | 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | /** |
---|
[7199] | 176 | * @brief sets the type of this MultiType and resets to the default value |
---|
[5643] | 177 | * @param type the new Type |
---|
| 178 | */ |
---|
[6645] | 179 | void MultiType::setType(MT_Type type) |
---|
[5540] | 180 | { |
---|
[7199] | 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 | } |
---|
[5540] | 202 | } |
---|
| 203 | |
---|
[5545] | 204 | /** |
---|
[7199] | 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 |
---|
[5545] | 223 | * @param value the new Value as a bool |
---|
| 224 | */ |
---|
[5540] | 225 | void MultiType::setBool(bool value) |
---|
| 226 | { |
---|
| 227 | this->type = MT_BOOL; |
---|
| 228 | this->value.Bool = value; |
---|
| 229 | } |
---|
| 230 | |
---|
[5545] | 231 | /** |
---|
[7199] | 232 | * @brief sets a new Value to the MultiType |
---|
[5545] | 233 | * @param value the new Value as an int |
---|
| 234 | */ |
---|
[5540] | 235 | void MultiType::setInt(int value) |
---|
| 236 | { |
---|
| 237 | this->type = MT_INT; |
---|
| 238 | this->value.Int = value; |
---|
| 239 | } |
---|
| 240 | |
---|
[5545] | 241 | /** |
---|
[7199] | 242 | * @brief sets a new Value to the MultiType |
---|
[5545] | 243 | * @param value the new Value as a float |
---|
| 244 | */ |
---|
[5540] | 245 | void MultiType::setFloat(float value) |
---|
| 246 | { |
---|
| 247 | this->type = MT_FLOAT; |
---|
| 248 | this->value.Float = value; |
---|
| 249 | } |
---|
| 250 | |
---|
[5545] | 251 | /** |
---|
[7199] | 252 | * @brief sets a new Value to the MultiType |
---|
[5545] | 253 | * @param value the new Value as a char |
---|
| 254 | */ |
---|
[5540] | 255 | void MultiType::setChar(char value) |
---|
| 256 | { |
---|
| 257 | this->type = MT_CHAR; |
---|
| 258 | this->value.Char = value; |
---|
| 259 | } |
---|
| 260 | |
---|
[5545] | 261 | /** |
---|
[7199] | 262 | * @brief sets a new Value to the MultiType |
---|
[5545] | 263 | * @param value the new Value as a String |
---|
| 264 | */ |
---|
[7199] | 265 | void MultiType::setString(const std::string& value) |
---|
[5540] | 266 | { |
---|
| 267 | this->type = MT_STRING; |
---|
[7199] | 268 | this->storedString = value; |
---|
[5540] | 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
[7199] | 272 | /************************** |
---|
| 273 | *** RETRIEVAL FUNCTIONS *** |
---|
| 274 | **************************/ |
---|
[5545] | 275 | /** |
---|
| 276 | * @returns the Value of this MultiType as a int |
---|
| 277 | */ |
---|
[5544] | 278 | bool MultiType::getBool() const |
---|
[5537] | 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; |
---|
[7199] | 287 | else if (this->type & MT_STRING) return (this->storedString == "true" || |
---|
| 288 | this->storedString == "TRUE" || |
---|
| 289 | this->storedString != "0"); //! @TODO make this better... |
---|
[5538] | 290 | |
---|
| 291 | return false; |
---|
[5537] | 292 | } |
---|
| 293 | |
---|
[5545] | 294 | /** |
---|
| 295 | * @returns the Value of this MultiType as a int |
---|
| 296 | */ |
---|
[5544] | 297 | int MultiType::getInt() const |
---|
[5537] | 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; |
---|
[7199] | 305 | else if (this->type & MT_STRING) |
---|
| 306 | { |
---|
[7201] | 307 | if (this->storedString == "") return 0; |
---|
[5538] | 308 | char* endPtr = NULL; |
---|
[7199] | 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())) |
---|
[5538] | 311 | return 0; |
---|
| 312 | else |
---|
| 313 | return result; |
---|
| 314 | } |
---|
| 315 | return 0; |
---|
[5537] | 316 | } |
---|
| 317 | |
---|
[7199] | 318 | |
---|
[5545] | 319 | /** |
---|
| 320 | * @returns the Value of this MultiType as a float |
---|
| 321 | */ |
---|
[5544] | 322 | float MultiType::getFloat() const |
---|
[5537] | 323 | { |
---|
[7199] | 324 | // default case: |
---|
[5539] | 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; |
---|
[5537] | 328 | else if (this->type & MT_INT) return (float) this->value.Int; |
---|
| 329 | else if (this->type & MT_CHAR) return (float) this->value.Char; |
---|
[7199] | 330 | else if (this->type & MT_STRING) |
---|
| 331 | { |
---|
[5538] | 332 | char* endPtr = NULL; |
---|
[7199] | 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())) |
---|
[5538] | 335 | return 0.0f; |
---|
| 336 | else |
---|
| 337 | return result; |
---|
| 338 | } |
---|
| 339 | return 0.0f; |
---|
[5537] | 340 | } |
---|
| 341 | |
---|
| 342 | |
---|
[5545] | 343 | /** |
---|
| 344 | * @returns the Value of this MultiType as a char |
---|
| 345 | */ |
---|
[5544] | 346 | char MultiType::getChar() const |
---|
[5537] | 347 | { |
---|
[7199] | 348 | // default case: |
---|
[5537] | 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; |
---|
[7199] | 354 | else if (this->type & MT_STRING) return this->storedString[0]; |
---|
[5538] | 355 | |
---|
| 356 | return '\0'; |
---|
[5537] | 357 | } |
---|
| 358 | |
---|
[7199] | 359 | |
---|
[5545] | 360 | /** |
---|
| 361 | * @returns the Value of this MultiType as a String |
---|
| 362 | */ |
---|
[7199] | 363 | std::string MultiType::getString() const |
---|
[5537] | 364 | { |
---|
[7199] | 365 | // default case: |
---|
[5537] | 366 | if (this->type & MT_STRING) |
---|
[7199] | 367 | return this->storedString; |
---|
[5538] | 368 | else |
---|
| 369 | { |
---|
| 370 | if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false"; |
---|
[7199] | 371 | |
---|
[5641] | 372 | else if (this->type & MT_INT) |
---|
| 373 | { |
---|
[7199] | 374 | char tmpString[32]; |
---|
[5538] | 375 | sprintf(tmpString, "%d", this->value.Int); |
---|
[7199] | 376 | return tmpString; |
---|
[5538] | 377 | } |
---|
[7199] | 378 | else if (this->type & MT_FLOAT) |
---|
[5538] | 379 | { |
---|
[7199] | 380 | char tmpString[64]; |
---|
[5538] | 381 | sprintf(tmpString, "%f", this->value.Float); |
---|
[7199] | 382 | return tmpString; |
---|
[5538] | 383 | } |
---|
[7200] | 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 | } |
---|
[5538] | 391 | } |
---|
| 392 | return ""; |
---|
[5537] | 393 | } |
---|
| 394 | |
---|
[5545] | 395 | /** |
---|
[7199] | 396 | * @returns a formated c-string of the held value |
---|
[5545] | 397 | */ |
---|
[7199] | 398 | const char* MultiType::getCString() |
---|
[5544] | 399 | { |
---|
[7199] | 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 | { |
---|
[5659] | 413 | #ifdef DEBUG |
---|
| 414 | PRINT(0) |
---|
| 415 | #else |
---|
| 416 | printf |
---|
| 417 | #endif |
---|
[7199] | 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 | } |
---|
[5544] | 427 | |
---|
| 428 | |
---|
[5545] | 429 | /** |
---|
[7199] | 430 | * @brief Resets the MultiType to default values. |
---|
[5643] | 431 | */ |
---|
| 432 | void MultiType::reset() |
---|
| 433 | { |
---|
[5659] | 434 | switch ( this->type ) |
---|
[5643] | 435 | { |
---|
| 436 | case MT_BOOL: |
---|
| 437 | this->setBool(false); |
---|
[7200] | 438 | break; |
---|
[5643] | 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; |
---|
[5659] | 451 | default: |
---|
| 452 | #ifdef DEBUG |
---|
| 453 | PRINTF(2)("Unknown Type not reseting\n"); |
---|
| 454 | #endif |
---|
| 455 | break; |
---|
[5643] | 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | /** |
---|
[7199] | 460 | * @brief converts a MT_Type into a String |
---|
[5545] | 461 | * @param type: the MT_Type |
---|
| 462 | * @returns: the Type as a constant String (do not delete) |
---|
| 463 | */ |
---|
[5544] | 464 | const char* MultiType::MultiTypeToString(MT_Type type) |
---|
| 465 | { |
---|
[5659] | 466 | switch ( type ) |
---|
[5544] | 467 | { |
---|
[7199] | 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"; |
---|
[5544] | 478 | } |
---|
[7200] | 479 | return "NONE"; |
---|
[5544] | 480 | } |
---|
| 481 | |
---|
[5545] | 482 | /** |
---|
[7199] | 483 | * @brief converts a String into a MT_Type |
---|
[5545] | 484 | * @param type: the Type as a String |
---|
| 485 | * @returns: the Type as MT_Type |
---|
| 486 | */ |
---|
[5544] | 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; |
---|
[7200] | 499 | |
---|
| 500 | return MT_NULL; |
---|
[5544] | 501 | } |
---|