[4838] | 1 | /*! |
---|
[5536] | 2 | * @file multi_type.h |
---|
[7199] | 3 | * @brief Definition of a MultiType, that is able to hold one Value of many types. |
---|
[5545] | 4 | */ |
---|
[1853] | 5 | |
---|
[5536] | 6 | #ifndef _MULTI_TYPE_H |
---|
| 7 | #define _MULTI_TYPE_H |
---|
[1853] | 8 | |
---|
[7199] | 9 | #include <string> |
---|
| 10 | |
---|
[4838] | 11 | // FORWARD DECLARATION |
---|
[3543] | 12 | |
---|
[5537] | 13 | //! An enumerator defining Types, that can be stored inside a MultiType. |
---|
[5536] | 14 | typedef enum |
---|
| 15 | { |
---|
[5537] | 16 | MT_NULL = 0, //!< No Value at all. |
---|
| 17 | MT_BOOL = 1, //!< A bool Value. |
---|
| 18 | MT_INT = 2, //!< An int Value. |
---|
[9869] | 19 | MT_UINT = 2, //!< A insigned int Value. |
---|
| 20 | MT_LONG = 2, //!< A long Value |
---|
[5537] | 21 | MT_FLOAT = 4, //!< A float Value. |
---|
| 22 | MT_CHAR = 8, //!< A single char. |
---|
| 23 | MT_STRING = 16, //!< An entire String. |
---|
[5633] | 24 | MT_EXT1 = 32, //!< An external Type. |
---|
| 25 | MT_EXT2 = 64, //!< An external Type. |
---|
[5537] | 26 | } MT_Type; |
---|
[5536] | 27 | |
---|
| 28 | |
---|
| 29 | |
---|
[5537] | 30 | //! A class that encapsulates multiple differen types. |
---|
| 31 | /** |
---|
| 32 | * Only one Value can be Stored inside this Class, but it can have any type: @see MT_Type. |
---|
| 33 | */ |
---|
[5536] | 34 | class MultiType { |
---|
[5545] | 35 | public: |
---|
[7197] | 36 | MultiType(MT_Type type = MT_NULL); |
---|
[5545] | 37 | MultiType(bool value); |
---|
| 38 | MultiType(int value); |
---|
| 39 | MultiType(double value); |
---|
| 40 | MultiType(char value); |
---|
[7199] | 41 | MultiType(const std::string& value); |
---|
[5659] | 42 | MultiType(const MultiType& multiType); |
---|
[5545] | 43 | virtual ~MultiType(); |
---|
[1853] | 44 | |
---|
[6643] | 45 | MultiType& operator=(const MultiType& mt); |
---|
[9869] | 46 | /** @param value the value to set (here a bool) @returns the MultiType where this the bool is stored */ |
---|
[6644] | 47 | MultiType& operator=(bool value) { this->setBool(value); return *this; }; |
---|
[9869] | 48 | /** @param value the value to set (here an int) @returns the MultiType where this the int is stored */ |
---|
[6644] | 49 | MultiType& operator=(int value) { this->setInt(value); return *this; }; |
---|
[9869] | 50 | /** @param value the value to set (here an int) @returns the MultiType where this the float is stored */ |
---|
[6644] | 51 | MultiType& operator=(float value) { this->setFloat(value); return *this; }; |
---|
[9869] | 52 | /** @param value the value to set (here a char) @returns the MultiType where this the char is stored */ |
---|
[6644] | 53 | MultiType& operator=(char value) { this->setChar(value); return *this; }; |
---|
[9869] | 54 | /** @param value the value to set (here a string) @returns the MultiType where this the string is stored */ |
---|
[7199] | 55 | MultiType& operator=(const std::string& value) { this->setString(value); return *this; }; |
---|
[6644] | 56 | |
---|
[6643] | 57 | bool operator==(const MultiType& mt) const; |
---|
[9869] | 58 | /** @param value the value to compare this MultiType against @returns true if comparison was io */ |
---|
[6644] | 59 | bool operator==(bool value) const { return (this->getBool() == value); }; |
---|
[9869] | 60 | /** @param value the value to compare this MultiType against @returns true if comparison was io */ |
---|
[6644] | 61 | bool operator==(int value) const { return (this->getInt() == value); }; |
---|
[9869] | 62 | /** @param value the value to compare this MultiType against @returns true if comparison was io */ |
---|
[6644] | 63 | bool operator==(float value) const { return (this->getFloat() == value); }; |
---|
[9869] | 64 | /** @param value the value to compare this MultiType against @returns true if comparison was io */ |
---|
[6644] | 65 | bool operator==(char value) const { return (this->getChar() == value); }; |
---|
[9869] | 66 | /** @param value the value to compare this MultiType against @returns true if comparison was io */ |
---|
[7199] | 67 | bool operator==(const std::string& value) const { return (this->getString() == value); }; |
---|
[9869] | 68 | /** @param type the Type to compare this MultiType against @returns true if the types matched */ |
---|
[6645] | 69 | bool operator==(MT_Type type) const { return (this->type == type); } |
---|
[9869] | 70 | /** @param type the type to compare this MultiType against @returns true if the types do not match */ |
---|
[6645] | 71 | bool operator!=(MT_Type type) const { return (this->type != type); } |
---|
[3245] | 72 | |
---|
[6645] | 73 | void setType(MT_Type type); |
---|
[5540] | 74 | |
---|
[5545] | 75 | void setBool(bool value); |
---|
| 76 | void setInt(int value); |
---|
| 77 | void setFloat(float value); |
---|
| 78 | void setChar(char value); |
---|
[7199] | 79 | void setString(const std::string& value); |
---|
[5540] | 80 | |
---|
[6644] | 81 | // for your convenience. |
---|
[9869] | 82 | /** @param value the value to set. Here a bool */ |
---|
[5545] | 83 | inline void setValue(bool value) { this->setBool(value); }; |
---|
[9869] | 84 | /** @param value the value to set. Here an int */ |
---|
[5545] | 85 | inline void setValue(int value) { this->setInt(value); }; |
---|
[9869] | 86 | /** @param value the value to set. Here a float */ |
---|
[5545] | 87 | inline void setValue(float value) { this->setFloat(value); }; |
---|
[9869] | 88 | /** @param value the value to set. Here a char */ |
---|
[5545] | 89 | inline void setValue(char value) { this->setChar(value); }; |
---|
[9869] | 90 | /** @param value the value to set. Here a char array (string) */ |
---|
[5545] | 91 | inline void setValue(const char* value) { this->setString(value); }; |
---|
[9869] | 92 | /** @param value the value to set. Here a string */ |
---|
[7199] | 93 | inline void setValue(const std::string& value) { this->setString(value); }; |
---|
| 94 | void setValueOf(const MultiType& mt); |
---|
[5541] | 95 | |
---|
[5545] | 96 | /** @returns the Type of the Value stored in this MultiType */ |
---|
| 97 | inline MT_Type getType() const { return this->type; }; |
---|
[5536] | 98 | |
---|
[9869] | 99 | void storeString(); |
---|
[5552] | 100 | |
---|
| 101 | /* RETRIEVING FUNCTIONS */ |
---|
[5545] | 102 | bool getBool() const; |
---|
| 103 | int getInt() const; |
---|
| 104 | float getFloat() const; |
---|
| 105 | char getChar() const; |
---|
[7199] | 106 | const char* getCString(); |
---|
| 107 | std::string getString() const; |
---|
[8035] | 108 | const std::string& getConstString() const; |
---|
[9869] | 109 | const std::string& getStoredString() const; |
---|
[5537] | 110 | |
---|
[5643] | 111 | void reset(); |
---|
[5537] | 112 | |
---|
[7199] | 113 | void debug() const; |
---|
[5544] | 114 | |
---|
[7401] | 115 | static const std::string& MultiTypeToString(MT_Type type); |
---|
[7221] | 116 | static MT_Type StringToMultiType(const std::string& type); |
---|
[5544] | 117 | |
---|
[5545] | 118 | private: |
---|
[7401] | 119 | //! A union, that combines types into as little memory as possible. |
---|
[5551] | 120 | union MultiTypeValue |
---|
[5545] | 121 | { |
---|
[7401] | 122 | bool Bool; //!< If it is a BOOL |
---|
| 123 | int Int; //!< If it is an INT |
---|
| 124 | float Float; //!< If it is a FLOAT |
---|
| 125 | char Char; //!< If it is a CHAR |
---|
| 126 | } value; //!< The Value. |
---|
| 127 | std::string storedString; //!< The Stored String. |
---|
| 128 | MT_Type type; //!< The Type stored in this MultiType |
---|
| 129 | |
---|
[8035] | 130 | |
---|
| 131 | static std::string constString; //!< A String for returning Constant strings. |
---|
| 132 | |
---|
[7401] | 133 | static const std::string typeNames[]; //!< List of TypeNames for conversion. |
---|
[1853] | 134 | }; |
---|
| 135 | |
---|
[5536] | 136 | #endif /* _MULTI_TYPE_H */ |
---|