Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5537 in orxonox.OLD


Ignore:
Timestamp:
Nov 11, 2005, 11:25:17 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: more elaborate MultiType

Location:
trunk/src/lib/util
Files:
2 edited

Legend:

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

    r5536 r5537  
    2121using namespace std;
    2222
     23MultiType::MultiType(bool value)
     24{
     25  this->init();
     26  this->type = MT_BOOL;
     27  this->value.Bool = value;
     28
     29}
     30
    2331MultiType::MultiType(int value)
    2432{
    25   this->type = M_Int;
    26   this->value.typeInt = value;
     33  this->init();
     34  this->type = MT_INT;
     35  this->value.Int = value;
    2736}
    2837
     
    3039MultiType::MultiType(float value)
    3140{
    32   this->type = M_Float;
    33   this->value.typeFloat = value;
     41  this->init();
     42  this->type = MT_FLOAT;
     43  this->value.Float = value;
    3444}
    3545
     
    3747MultiType::MultiType(char value)
    3848{
    39   this->type = M_Char;
    40   this->value.typeChar = value;
     49  this->init();
     50  this->type = MT_CHAR;
     51  this->value.Char = value;
    4152}
    4253
     
    4455MultiType::MultiType(const char* value)
    4556{
    46   this->type = M_String;
    47   this->value.typeString = new char[strlen(value)+1];
    48   strcpy(this->value.typeString, value);
     57  this->init();
     58  this->type = MT_STRING;
     59  this->value.String = new char[strlen(value)+1];
     60  strcpy(this->value.String, value);
     61
     62  this->storedString = this->value.String;
    4963}
    5064
     
    5468MultiType::~MultiType ()
    5569{
    56   // delete what has to be deleted here
     70  if (this->storedString != NULL)
     71    delete this->storedString;
    5772}
     73
     74void MultiType::init()
     75{
     76  this->storedString = NULL;
     77}
     78
     79
     80bool MultiType::getBool()
     81{
     82  // default case:
     83  if (this->type & MT_BOOL)
     84    return this->value.Bool;
     85  // Special Cases:
     86  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
     87  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
     88  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
     89  else if (this->type & MT_STRING) return (!strncmp(this->value.String, "true", 4) || !strncmp(this->value.String, "TRUE", 4) || !strncmp(this->value.String, "1", 1))? true : false;
     90}
     91
     92
     93int MultiType::getInt()
     94{
     95  // default case:
     96  if (this->type & MT_INT)
     97    return this->value.Int;
     98  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
     99  else if (this->type & MT_FLOAT) return (int) this->value.Float;
     100  else if (this->type & MT_CHAR) return (int) this->value.Char;
     101  else if (this->type & MT_STRING) return 1; //! @TODO
     102}
     103
     104
     105float MultiType::getFloat()
     106{
     107 // default case:
     108  if (this->type & MT_FLOAT) return this->value.Float;
     109    return this->value.Int;
     110  if (this->type & MT_BOOL) return (this->value.Bool)? 1.0f : 0.0f;
     111  else if (this->type & MT_INT) return (float) this->value.Int;
     112  else if (this->type & MT_CHAR) return (float) this->value.Char;
     113  else if (this->type & MT_STRING) return 1; //! @TODO
     114}
     115
     116
     117char MultiType::getChar()
     118{
     119 // default case:
     120  if (this->type & MT_INT)
     121    return this->value.Int;
     122  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
     123  else if (this->type & MT_INT) return (int) this->value.Int;
     124  else if (this->type & MT_FLOAT) return (char) this->value.Float;
     125  else if (this->type & MT_STRING) return 1; //! @TODO
     126}
     127
     128const char* MultiType::getString()
     129{
     130 // default case:
     131  if (this->type & MT_STRING)
     132    return this->value.String;
     133/*  else if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
     134  else if (this->type & MT_INT) return 1; //! @TODO
     135  else if (this->type & MT_FLOAT) return (int) this->value.Float;
     136  else if (this->type & MT_CHAR) return (int) this->value.Char;*/
     137}
     138
  • trunk/src/lib/util/multi_type.h

    r5536 r5537  
    99// FORWARD DECLARATION
    1010
    11 
     11//! An enumerator defining Types, that can be stored inside a MultiType.
    1212typedef enum
    1313{
    14   M_Int,
    15   M_Float,
    16   M_Char,
    17   M_String,
     14  MT_NULL            = 0,                  //!< No Value at all.
     15  MT_BOOL            = 1,                  //!< A bool Value.
     16  MT_INT             = 2,                  //!< An int Value.
     17  MT_FLOAT           = 4,                  //!< A float Value.
     18  MT_CHAR            = 8,                  //!< A single char.
     19  MT_STRING          = 16,                 //!< An entire String.
    1820
    19 } M_Type;
     21} MT_Type;
    2022
    2123
    2224
    23 //! A class for ...
     25//! A class that encapsulates multiple differen types.
     26/**
     27 * Only one Value can be Stored inside this Class, but it can have any type: @see MT_Type.
     28 */
    2429class MultiType {
    2530
    2631 public:
     32   MultiType(bool value);
    2733   MultiType(int value);
    2834   MultiType(float value);
    2935   MultiType(char value);
    3036   MultiType(const char* value);
     37   virtual ~MultiType();
     38   void init();
    3139
    32   virtual ~MultiType();
     40
     41  /** @returns the Type of the Value stored in this MultiType */
     42  inline MT_Type getType() const { return this->type; };
     43
     44  bool getBool();
     45  int getInt();
     46  float getFloat();
     47  char getChar();
     48  const char* getString();
    3349
    3450
    3551 private:
    36 
    37    M_Type             type;
     52   MT_Type             type;
    3853   union Type
    3954   {
    40      int              typeInt;
    41      float            typeFloat;
    42      char             typeChar;
    43      char*            typeString;
     55     bool             Bool;
     56     int              Int;
     57     float            Float;
     58     char             Char;
     59     char*            String;
    4460   } value;
    4561
    46 
    47 
     62   char* storedString;
    4863};
    4964
Note: See TracChangeset for help on using the changeset viewer.