/*! * @file multi_type.h * @brief Definition of a MultiType, that is able to hold one Value of many types. */ #ifndef _MULTI_TYPE_H #define _MULTI_TYPE_H #include // FORWARD DECLARATION //! An enumerator defining Types, that can be stored inside a MultiType. typedef enum { MT_NULL = 0, //!< No Value at all. MT_BOOL = 1, //!< A bool Value. MT_INT = 2, //!< An int Value. MT_UINT = 2, //!< A insigned int Value. MT_LONG = 2, //!< A long Value MT_FLOAT = 4, //!< A float Value. MT_CHAR = 8, //!< A single char. MT_STRING = 16, //!< An entire String. MT_EXT1 = 32, //!< An external Type. MT_EXT2 = 64, //!< An external Type. } MT_Type; //! A class that encapsulates multiple differen types. /** * Only one Value can be Stored inside this Class, but it can have any type: @see MT_Type. */ class MultiType { public: MultiType(MT_Type type = MT_NULL); MultiType(bool value); MultiType(int value); MultiType(double value); MultiType(char value); MultiType(const std::string& value); MultiType(const MultiType& multiType); virtual ~MultiType(); MultiType& operator=(const MultiType& mt); /** @param value the value to set (here a bool) @returns the MultiType where this the bool is stored */ MultiType& operator=(bool value) { this->setBool(value); return *this; }; /** @param value the value to set (here an int) @returns the MultiType where this the int is stored */ MultiType& operator=(int value) { this->setInt(value); return *this; }; /** @param value the value to set (here an int) @returns the MultiType where this the float is stored */ MultiType& operator=(float value) { this->setFloat(value); return *this; }; /** @param value the value to set (here a char) @returns the MultiType where this the char is stored */ MultiType& operator=(char value) { this->setChar(value); return *this; }; /** @param value the value to set (here a string) @returns the MultiType where this the string is stored */ MultiType& operator=(const std::string& value) { this->setString(value); return *this; }; bool operator==(const MultiType& mt) const; /** @param value the value to compare this MultiType against @returns true if comparison was io */ bool operator==(bool value) const { return (this->getBool() == value); }; /** @param value the value to compare this MultiType against @returns true if comparison was io */ bool operator==(int value) const { return (this->getInt() == value); }; /** @param value the value to compare this MultiType against @returns true if comparison was io */ bool operator==(float value) const { return (this->getFloat() == value); }; /** @param value the value to compare this MultiType against @returns true if comparison was io */ bool operator==(char value) const { return (this->getChar() == value); }; /** @param value the value to compare this MultiType against @returns true if comparison was io */ bool operator==(const std::string& value) const { return (this->getString() == value); }; /** @param type the Type to compare this MultiType against @returns true if the types matched */ bool operator==(MT_Type type) const { return (this->type == type); } /** @param type the type to compare this MultiType against @returns true if the types do not match */ bool operator!=(MT_Type type) const { return (this->type != type); } void setType(MT_Type type); void setBool(bool value); void setInt(int value); void setFloat(float value); void setChar(char value); void setString(const std::string& value); // for your convenience. /** @param value the value to set. Here a bool */ inline void setValue(bool value) { this->setBool(value); }; /** @param value the value to set. Here an int */ inline void setValue(int value) { this->setInt(value); }; /** @param value the value to set. Here a float */ inline void setValue(float value) { this->setFloat(value); }; /** @param value the value to set. Here a char */ inline void setValue(char value) { this->setChar(value); }; /** @param value the value to set. Here a char array (string) */ inline void setValue(const char* value) { this->setString(value); }; /** @param value the value to set. Here a string */ inline void setValue(const std::string& value) { this->setString(value); }; void setValueOf(const MultiType& mt); /** @returns the Type of the Value stored in this MultiType */ inline MT_Type getType() const { return this->type; }; void storeString(); /* RETRIEVING FUNCTIONS */ bool getBool() const; int getInt() const; float getFloat() const; char getChar() const; const char* getCString(); std::string getString() const; const std::string& getConstString() const; const std::string& getStoredString() const; void reset(); void debug() const; static const std::string& MultiTypeToString(MT_Type type); static MT_Type StringToMultiType(const std::string& type); private: //! A union, that combines types into as little memory as possible. union MultiTypeValue { bool Bool; //!< If it is a BOOL int Int; //!< If it is an INT float Float; //!< If it is a FLOAT char Char; //!< If it is a CHAR } value; //!< The Value. std::string storedString; //!< The Stored String. MT_Type type; //!< The Type stored in this MultiType static std::string constString; //!< A String for returning Constant strings. static const std::string typeNames[]; //!< List of TypeNames for conversion. }; #endif /* _MULTI_TYPE_H */