- Timestamp:
- Nov 11, 2005, 11:25:17 AM (19 years ago)
- Location:
- trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/multi_type.cc
r5536 r5537 21 21 using namespace std; 22 22 23 MultiType::MultiType(bool value) 24 { 25 this->init(); 26 this->type = MT_BOOL; 27 this->value.Bool = value; 28 29 } 30 23 31 MultiType::MultiType(int value) 24 32 { 25 this->type = M_Int; 26 this->value.typeInt = value; 33 this->init(); 34 this->type = MT_INT; 35 this->value.Int = value; 27 36 } 28 37 … … 30 39 MultiType::MultiType(float value) 31 40 { 32 this->type = M_Float; 33 this->value.typeFloat = value; 41 this->init(); 42 this->type = MT_FLOAT; 43 this->value.Float = value; 34 44 } 35 45 … … 37 47 MultiType::MultiType(char value) 38 48 { 39 this->type = M_Char; 40 this->value.typeChar = value; 49 this->init(); 50 this->type = MT_CHAR; 51 this->value.Char = value; 41 52 } 42 53 … … 44 55 MultiType::MultiType(const char* value) 45 56 { 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; 49 63 } 50 64 … … 54 68 MultiType::~MultiType () 55 69 { 56 // delete what has to be deleted here 70 if (this->storedString != NULL) 71 delete this->storedString; 57 72 } 73 74 void MultiType::init() 75 { 76 this->storedString = NULL; 77 } 78 79 80 bool 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 93 int 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 105 float 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 117 char 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 128 const 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 9 9 // FORWARD DECLARATION 10 10 11 11 //! An enumerator defining Types, that can be stored inside a MultiType. 12 12 typedef enum 13 13 { 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. 18 20 19 } M _Type;21 } MT_Type; 20 22 21 23 22 24 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 */ 24 29 class MultiType { 25 30 26 31 public: 32 MultiType(bool value); 27 33 MultiType(int value); 28 34 MultiType(float value); 29 35 MultiType(char value); 30 36 MultiType(const char* value); 37 virtual ~MultiType(); 38 void init(); 31 39 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(); 33 49 34 50 35 51 private: 36 37 M_Type type; 52 MT_Type type; 38 53 union Type 39 54 { 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; 44 60 } value; 45 61 46 47 62 char* storedString; 48 63 }; 49 64
Note: See TracChangeset
for help on using the changeset viewer.