Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7199 in orxonox.OLD for trunk/src/lib/util/multi_type.cc


Ignore:
Timestamp:
Mar 8, 2006, 10:46:37 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: MultiType rework (now uses std::string) this is more compliant, and better to handle

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/multi_type.cc

    r7197 r7199  
    2323#include <string.h>
    2424#include <stdio.h>
     25#include <sstream>
    2526
    2627#ifdef DEBUG
     
    3132
    3233/**
    33  * creates a multiType without any stored value at all.
     34 * @brief creates a multiType without any stored value at all.
    3435 */
    3536MultiType::MultiType(MT_Type type)
     
    3940}
    4041/**
    41  * creates a multiType out of a boolean
     42 * @brief creates a multiType out of a boolean
    4243 * @param value the Value of this MulitType
    4344 */
     
    4950
    5051/**
    51  * creates a multiType out of an integer
     52 * @brief creates a multiType out of an integer
    5253 * @param value the Value of this MulitType
    5354 */
     
    5960
    6061/**
    61  * creates a multiType out of a float (double)
     62 * @brief creates a multiType out of a float (double)
    6263 * @param value the Value of this MulitType
    6364 */
     
    6970
    7071/**
    71  * creates a multiType out of a char
     72 * @brief creates a multiType out of a char
    7273 * @param value the Value of this MulitType
    7374 */
     
    7980
    8081/**
    81  * creates a multiType out of a String
     82 * @brief creates a multiType out of a C-String
    8283 * @param value the Value of this MulitType
    8384 */
     
    8889}
    8990
     91/**
     92 * @brief creates a multiType out of a String
     93 * @param value the Value of this MulitType
     94 */
     95MultiType::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 */
    90104MultiType::MultiType(const MultiType& multiType)
    91105{
    92   this->init();
    93106  *this = multiType;
    94107}
    95108
    96109/**
    97  * standard deconstructor
     110 * @brief standard deconstructor
    98111*/
    99112MultiType::~MultiType ()
    100 {
    101   if (this->storedString != NULL)
    102     delete[] this->storedString;
    103 }
    104 
    105 /**
    106  * copy Constructor
     113{ }
     114
     115/**
     116 * @brief copy operator
    107117 * @param mt: the entity to copy
    108  * @returns a Copy of itself. (strings inside are copied as well)
     118 * @returns A Copy of itself. (strings inside are copied as well)
    109119 */
    110120MultiType& MultiType::operator= (const MultiType& mt)
     
    112122  this->type = mt.type;
    113123  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
    120126  return *this;
    121127}
     
    148154      return (this->value.Float == mt.value.Float);
    149155    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 */
     164void 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 */
     175void 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
    161203 *
    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 */
     208void 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
    197219 * @param value the new Value as a bool
    198220 */
     
    204226
    205227/**
    206  * sets a new Value to the MultiType
     228 * @brief sets a new Value to the MultiType
    207229 * @param value the new Value as an int
    208230 */
     
    214236
    215237/**
    216  * sets a new Value to the MultiType
     238 * @brief sets a new Value to the MultiType
    217239 * @param value the new Value as a float
    218240 */
     
    224246
    225247/**
    226  * sets a new Value to the MultiType
     248 * @brief sets a new Value to the MultiType
    227249 * @param value the new Value as a char
    228250 */
     
    234256
    235257/**
    236  * sets a new Value to the MultiType
     258 * @brief sets a new Value to the MultiType
    237259 * @param value the new Value as a String
    238260 */
    239 void MultiType::setString(const char* value)
     261void MultiType::setString(const std::string& value)
    240262{
    241263  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**************************/
    265271/**
    266272 * @returns the Value of this MultiType as a int
     
    275281  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
    276282  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...
    278286
    279287  return false;
     
    291299  else if (this->type & MT_FLOAT) return (int) this->value.Float;
    292300  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  {
    294303    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()))
    297306      return 0;
    298307    else
    299308      return result;
    300309  }
    301 
    302310  return 0;
    303311}
    304312
     313
    305314/**
    306315 * @returns the Value of this MultiType as a float
     
    308317float MultiType::getFloat() const
    309318{
    310  // default case:
     319  // default case:
    311320  if (this->type & MT_FLOAT)
    312321    return this->value.Float;
     
    314323  else if (this->type & MT_INT) return (float) this->value.Int;
    315324  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  {
    317327    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()))
    320330      return 0.0f;
    321331    else
    322332      return result;
    323333  }
    324 
    325334  return 0.0f;
    326335}
     
    332341char MultiType::getChar() const
    333342{
    334  // default case:
     343  // default case:
    335344  if (this->type & MT_INT)
    336345    return this->value.Int;
     
    338347  else if (this->type & MT_INT) return (int) this->value.Int;
    339348  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];
    341350
    342351  return '\0';
    343352}
    344353
     354
    345355/**
    346356 * @returns the Value of this MultiType as a String
    347357 */
    348 const char* MultiType::getString()
    349 {
    350  // default case:
     358std::string MultiType::getString() const
     359{
     360  // default case:
    351361  if (this->type & MT_STRING)
    352     return (this->value.String != NULL)? this->value.String : "";
     362    return this->storedString;
    353363  else
    354364  {
    355365    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
    362367    if (this->type & MT_CHAR)
    363368    {
    364       this->storedString = new char[2];
    365       this->storedString[0] = this->value.Char;
    366       this->storedString[1] = '\0';
    367       return this->storedString;
     369      char tmpString[2];
     370      tmpString[0] = this->value.Char;
     371      tmpString[1] = '\0';
     372      return tmpString;
    368373    }
    369374    else if (this->type & MT_INT)
    370375    {
     376      char tmpString[32];
    371377      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;
    375379    }
    376     if (this->type & MT_FLOAT)
     380    else if (this->type & MT_FLOAT)
    377381    {
     382      char tmpString[64];
    378383      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;
    382385    }
    383386  }
    384 
    385387  return "";
    386388}
    387389
    388390/**
    389  * prints out some nice debug output
    390  */
    391 void MultiType::debug()
     391 * @returns a formated c-string of the held value
     392 */
     393const 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 */
     406void MultiType::debug() const
    392407{
    393408#ifdef DEBUG
     
    396411  printf
    397412#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.
    412426 */
    413427void MultiType::reset()
     
    438452
    439453/**
    440  * converts a MT_Type into a String
     454 * @brief converts a MT_Type into a String
    441455 * @param type: the MT_Type
    442456 * @returns: the Type as a constant String (do not delete)
     
    446460  switch ( type )
    447461  {
    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_Type
     462    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
    465479 * @param type: the Type as a String
    466480 * @returns: the Type as MT_Type
Note: See TracChangeset for help on using the changeset viewer.