Changeset 7199 in orxonox.OLD for trunk/src/lib/util/multi_type.cc
- Timestamp:
- Mar 8, 2006, 10:46:37 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/multi_type.cc
r7197 r7199 23 23 #include <string.h> 24 24 #include <stdio.h> 25 #include <sstream> 25 26 26 27 #ifdef DEBUG … … 31 32 32 33 /** 33 * creates a multiType without any stored value at all.34 * @brief creates a multiType without any stored value at all. 34 35 */ 35 36 MultiType::MultiType(MT_Type type) … … 39 40 } 40 41 /** 41 * creates a multiType out of a boolean42 * @brief creates a multiType out of a boolean 42 43 * @param value the Value of this MulitType 43 44 */ … … 49 50 50 51 /** 51 * creates a multiType out of an integer52 * @brief creates a multiType out of an integer 52 53 * @param value the Value of this MulitType 53 54 */ … … 59 60 60 61 /** 61 * creates a multiType out of a float (double)62 * @brief creates a multiType out of a float (double) 62 63 * @param value the Value of this MulitType 63 64 */ … … 69 70 70 71 /** 71 * creates a multiType out of a char72 * @brief creates a multiType out of a char 72 73 * @param value the Value of this MulitType 73 74 */ … … 79 80 80 81 /** 81 * creates a multiType out of aString82 * @brief creates a multiType out of a C-String 82 83 * @param value the Value of this MulitType 83 84 */ … … 88 89 } 89 90 91 /** 92 * @brief creates a multiType out of a String 93 * @param value the Value of this MulitType 94 */ 95 MultiType::MultiType(const std::string& value) 96 { 97 this->init(); 98 this->setString(value); 99 } 100 101 /** 102 * @brief constructs a new MultiType from another one (copy) 103 */ 90 104 MultiType::MultiType(const MultiType& multiType) 91 105 { 92 this->init();93 106 *this = multiType; 94 107 } 95 108 96 109 /** 97 * standard deconstructor110 * @brief standard deconstructor 98 111 */ 99 112 MultiType::~MultiType () 100 { 101 if (this->storedString != NULL) 102 delete[] this->storedString; 103 } 104 105 /** 106 * copy Constructor 113 { } 114 115 /** 116 * @brief copy operator 107 117 * @param mt: the entity to copy 108 * @returns aCopy of itself. (strings inside are copied as well)118 * @returns A Copy of itself. (strings inside are copied as well) 109 119 */ 110 120 MultiType& MultiType::operator= (const MultiType& mt) … … 112 122 this->type = mt.type; 113 123 this->value = mt.value; 114 if (mt.type == MT_STRING && mt.storedString != NULL) 115 { 116 this->setString(mt.storedString); 117 } 118 else 119 this->storedString = NULL; 124 this->storedString = mt.storedString; 125 120 126 return *this; 121 127 } … … 148 154 return (this->value.Float == mt.value.Float); 149 155 case MT_STRING: 150 if (this->value.String != NULL && mt.value.String != NULL) 151 return (!strcmp(this->value.String, mt.value.String)); 152 else 153 return (this->value.String == NULL && mt.value.String == NULL); 154 } 155 } 156 157 /** 158 * @brief checks if the internal value matches the boolean value. 159 * @param value to check against this one 160 * @returns true on match. false otherwise 156 return (this->storedString == mt.storedString); 157 } 158 } 159 160 161 /** 162 * @brief initializes the MultiType 163 */ 164 void MultiType::init() 165 { 166 this->type = MT_NULL; 167 this->value.Float = 0.0f; 168 } 169 170 171 /** 172 * @brief sets the type of this MultiType and resets to the default value 173 * @param type the new Type 174 */ 175 void MultiType::setType(MT_Type type) 176 { 177 if (this->type == type) 178 return; 179 180 switch (type) 181 { 182 case MT_BOOL: 183 this->setBool(this->getBool()); 184 break; 185 case MT_INT: 186 this->setInt(this->getInt()); 187 break; 188 case MT_FLOAT: 189 this->setFloat(this->getFloat()); 190 break; 191 case MT_CHAR: 192 this->setChar(this->getChar()); 193 break; 194 case MT_STRING: 195 this->setString(this->getString()); 196 break; 197 } 198 } 199 200 /** 201 * @brief sets the Value of mt without changing the type of this MultiType 202 * @param mt: the MultiType to get the value from 161 203 * 162 * Two MultiType match if and only if 163 * 1. the stored values match the given Value 164 */ 165 bool MultiType::operator==(const char* value) const 166 { 167 if (this->value.String != NULL && value != NULL) 168 return (!strcmp(this->value.String, value)); 169 else 170 return (this->value.String == NULL && value == NULL); 171 } 172 173 174 /** 175 * initializes the MultiType 176 */ 177 void MultiType::init() 178 { 179 this->type = MT_NULL; 180 this->storedString = NULL; 181 this->value.Float = 0.0f; 182 } 183 184 185 /** 186 * sets the type of this MultiType and resets to the default value 187 * @param type the new Type 188 */ 189 void MultiType::setType(MT_Type type) 190 { 191 if (this->type != type) 192 this->type = type; 193 } 194 195 /** 196 * sets a new Value to the MultiType 204 * This is a pure Value copy. The current type will be preserved. 205 * 206 * @TODO speedup 207 */ 208 void MultiType::setValueOf(const MultiType& mt) 209 { 210 MT_Type prevType = this->type; 211 212 *this = mt; 213 this->setType(prevType); 214 } 215 216 217 /** 218 * @brief sets a new Value to the MultiType 197 219 * @param value the new Value as a bool 198 220 */ … … 204 226 205 227 /** 206 * sets a new Value to the MultiType228 * @brief sets a new Value to the MultiType 207 229 * @param value the new Value as an int 208 230 */ … … 214 236 215 237 /** 216 * sets a new Value to the MultiType238 * @brief sets a new Value to the MultiType 217 239 * @param value the new Value as a float 218 240 */ … … 224 246 225 247 /** 226 * sets a new Value to the MultiType248 * @brief sets a new Value to the MultiType 227 249 * @param value the new Value as a char 228 250 */ … … 234 256 235 257 /** 236 * sets a new Value to the MultiType258 * @brief sets a new Value to the MultiType 237 259 * @param value the new Value as a String 238 260 */ 239 void MultiType::setString(const char*value)261 void MultiType::setString(const std::string& value) 240 262 { 241 263 this->type = MT_STRING; 242 243 if (this->storedString != NULL) 244 delete[] this->storedString; 245 246 if (value == NULL) 247 { 248 this->storedString = new char[1]; 249 this->storedString[0] = '\0'; 250 this->value.String = this->storedString; 251 return; 252 } 253 else 254 { 255 this->storedString = new char[strlen(value)+1]; 256 strcpy(storedString, value); 257 this->value.String = this->storedString; 258 } 259 } 260 261 262 263 264 264 this->storedString = value; 265 } 266 267 268 /************************** 269 *** RETRIEVAL FUNCTIONS *** 270 **************************/ 265 271 /** 266 272 * @returns the Value of this MultiType as a int … … 275 281 else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true; 276 282 else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true; 277 else if (this->type & MT_STRING && this->value.String != NULL) return (!strncmp(this->value.String, "true", 4) || !strncmp(this->value.String, "TRUE", 4) || !strncmp(this->value.String, "1", 1))? true : false; //! @TODO make this better. 283 else if (this->type & MT_STRING) return (this->storedString == "true" || 284 this->storedString == "TRUE" || 285 this->storedString != "0"); //! @TODO make this better... 278 286 279 287 return false; … … 291 299 else if (this->type & MT_FLOAT) return (int) this->value.Float; 292 300 else if (this->type & MT_CHAR) return (int) this->value.Char; 293 else if (this->type & MT_STRING && this->value.String != NULL) { 301 else if (this->type & MT_STRING) 302 { 294 303 char* endPtr = NULL; 295 int result = strtol(this-> value.String, &endPtr, 10);296 if ( endPtr >= this-> value.String && endPtr < this->value.String + strlen(this->value.String))304 int result = strtol(this->storedString.c_str(), &endPtr, 10); 305 if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + strlen(this->storedString.c_str())) 297 306 return 0; 298 307 else 299 308 return result; 300 309 } 301 302 310 return 0; 303 311 } 304 312 313 305 314 /** 306 315 * @returns the Value of this MultiType as a float … … 308 317 float MultiType::getFloat() const 309 318 { 310 // default case:319 // default case: 311 320 if (this->type & MT_FLOAT) 312 321 return this->value.Float; … … 314 323 else if (this->type & MT_INT) return (float) this->value.Int; 315 324 else if (this->type & MT_CHAR) return (float) this->value.Char; 316 else if (this->type & MT_STRING && this->value.String != NULL) { 325 else if (this->type & MT_STRING) 326 { 317 327 char* endPtr = NULL; 318 double result = strtod(this-> value.String, &endPtr);319 if ( endPtr >= this-> value.String && endPtr < this->value.String + strlen(this->value.String))328 double result = strtod(this->storedString.c_str(), &endPtr); 329 if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + strlen(this->storedString.c_str())) 320 330 return 0.0f; 321 331 else 322 332 return result; 323 333 } 324 325 334 return 0.0f; 326 335 } … … 332 341 char MultiType::getChar() const 333 342 { 334 // default case:343 // default case: 335 344 if (this->type & MT_INT) 336 345 return this->value.Int; … … 338 347 else if (this->type & MT_INT) return (int) this->value.Int; 339 348 else if (this->type & MT_FLOAT) return (char) this->value.Float; 340 else if (this->type & MT_STRING && this->value.String != NULL) return *this->value.String;349 else if (this->type & MT_STRING) return this->storedString[0]; 341 350 342 351 return '\0'; 343 352 } 344 353 354 345 355 /** 346 356 * @returns the Value of this MultiType as a String 347 357 */ 348 const char* MultiType::getString() 349 { 350 // default case:358 std::string MultiType::getString() const 359 { 360 // default case: 351 361 if (this->type & MT_STRING) 352 return (this->value.String != NULL)? this->value.String : "";362 return this->storedString; 353 363 else 354 364 { 355 365 if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false"; 356 char tmpString[128]; 357 if (this->storedString != NULL) 358 { 359 delete[] this->storedString; 360 this->storedString = NULL; 361 } 366 362 367 if (this->type & MT_CHAR) 363 368 { 364 this->storedString = new char[2];365 t his->storedString[0] = this->value.Char;366 t his->storedString[1] = '\0';367 return t his->storedString;369 char tmpString[2]; 370 tmpString[0] = this->value.Char; 371 tmpString[1] = '\0'; 372 return tmpString; 368 373 } 369 374 else if (this->type & MT_INT) 370 375 { 376 char tmpString[32]; 371 377 sprintf(tmpString, "%d", this->value.Int); 372 this->storedString = new char[strlen(tmpString)+1]; 373 strcpy (this->storedString, tmpString); 374 return this->storedString; 378 return tmpString; 375 379 } 376 if (this->type & MT_FLOAT)380 else if (this->type & MT_FLOAT) 377 381 { 382 char tmpString[64]; 378 383 sprintf(tmpString, "%f", this->value.Float); 379 this->storedString = new char[strlen (tmpString)+1]; 380 strcpy (this->storedString, tmpString); 381 return this->storedString; 384 return tmpString; 382 385 } 383 386 } 384 385 387 return ""; 386 388 } 387 389 388 390 /** 389 * prints out some nice debug output 390 */ 391 void MultiType::debug() 391 * @returns a formated c-string of the held value 392 */ 393 const char* MultiType::getCString() 394 { 395 if (this->type & MT_STRING) return this->storedString.c_str(); 396 else 397 { 398 this->storedString = this->getString(); 399 return this->storedString.c_str(); 400 } 401 } 402 403 /** 404 * @brief prints out some nice debug output 405 */ 406 void MultiType::debug() const 392 407 { 393 408 #ifdef DEBUG … … 396 411 printf 397 412 #endif 398 ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n", 399 MultiType::MultiTypeToString(this->type), 400 this->getBool(), 401 this->getInt(), 402 this->getFloat(), 403 this->getChar(), 404 this->getString() 405 ); 406 407 408 } 409 410 /** 411 * Resets the MultiType to default values. 413 ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n", 414 MultiType::MultiTypeToString(this->type), 415 this->getBool(), 416 this->getInt(), 417 this->getFloat(), 418 this->getChar(), 419 this->getString().c_str() 420 ); 421 } 422 423 424 /** 425 * @brief Resets the MultiType to default values. 412 426 */ 413 427 void MultiType::reset() … … 438 452 439 453 /** 440 * converts a MT_Type into a String454 * @brief converts a MT_Type into a String 441 455 * @param type: the MT_Type 442 456 * @returns: the Type as a constant String (do not delete) … … 446 460 switch ( type ) 447 461 { 448 default:449 return "NONE";450 case MT_BOOL:451 return "bool";452 case MT_INT:453 return "int";454 case MT_FLOAT:455 return "float";456 case MT_CHAR:457 return "char";458 case MT_STRING:459 return "string";460 } 461 } 462 463 /** 464 * converts a String into a MT_Type462 default: 463 return "NONE"; 464 case MT_BOOL: 465 return "bool"; 466 case MT_INT: 467 return "int"; 468 case MT_FLOAT: 469 return "float"; 470 case MT_CHAR: 471 return "char"; 472 case MT_STRING: 473 return "string"; 474 } 475 } 476 477 /** 478 * @brief converts a String into a MT_Type 465 479 * @param type: the Type as a String 466 480 * @returns: the Type as MT_Type
Note: See TracChangeset
for help on using the changeset viewer.