1 | /*! |
---|
2 | * @file multi_type.h |
---|
3 | * @brief Definition of a MultiType, that is able to hold one Value of many types. |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _MULTI_TYPE_H |
---|
7 | #define _MULTI_TYPE_H |
---|
8 | |
---|
9 | #include <string> |
---|
10 | |
---|
11 | // FORWARD DECLARATION |
---|
12 | |
---|
13 | //! An enumerator defining Types, that can be stored inside a MultiType. |
---|
14 | typedef enum |
---|
15 | { |
---|
16 | MT_NULL = 0, //!< No Value at all. |
---|
17 | MT_BOOL = 1, //!< A bool Value. |
---|
18 | MT_INT = 2, //!< An int Value. |
---|
19 | MT_UINT = 2, |
---|
20 | MT_LONG = 2, |
---|
21 | MT_FLOAT = 4, //!< A float Value. |
---|
22 | MT_CHAR = 8, //!< A single char. |
---|
23 | MT_STRING = 16, //!< An entire String. |
---|
24 | MT_EXT1 = 32, //!< An external Type. |
---|
25 | MT_EXT2 = 64, //!< An external Type. |
---|
26 | } MT_Type; |
---|
27 | |
---|
28 | |
---|
29 | |
---|
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 | */ |
---|
34 | class MultiType { |
---|
35 | public: |
---|
36 | MultiType(MT_Type type = MT_NULL); |
---|
37 | MultiType(bool value); |
---|
38 | MultiType(int value); |
---|
39 | MultiType(double value); |
---|
40 | MultiType(char value); |
---|
41 | MultiType(const std::string& value); |
---|
42 | MultiType(const MultiType& multiType); |
---|
43 | virtual ~MultiType(); |
---|
44 | |
---|
45 | MultiType& operator=(const MultiType& mt); |
---|
46 | MultiType& operator=(bool value) { this->setBool(value); return *this; }; |
---|
47 | MultiType& operator=(int value) { this->setInt(value); return *this; }; |
---|
48 | MultiType& operator=(float value) { this->setFloat(value); return *this; }; |
---|
49 | MultiType& operator=(char value) { this->setChar(value); return *this; }; |
---|
50 | MultiType& operator=(const std::string& value) { this->setString(value); return *this; }; |
---|
51 | |
---|
52 | bool operator==(const MultiType& mt) const; |
---|
53 | bool operator==(bool value) const { return (this->getBool() == value); }; |
---|
54 | bool operator==(int value) const { return (this->getInt() == value); }; |
---|
55 | bool operator==(float value) const { return (this->getFloat() == value); }; |
---|
56 | bool operator==(char value) const { return (this->getChar() == value); }; |
---|
57 | bool operator==(const std::string& value) const { return (this->getString() == value); }; |
---|
58 | bool operator==(MT_Type type) const { return (this->type == type); } |
---|
59 | bool operator!=(MT_Type type) const { return (this->type != type); } |
---|
60 | |
---|
61 | void setType(MT_Type type); |
---|
62 | |
---|
63 | void setBool(bool value); |
---|
64 | void setInt(int value); |
---|
65 | void setFloat(float value); |
---|
66 | void setChar(char value); |
---|
67 | void setString(const std::string& value); |
---|
68 | |
---|
69 | // for your convenience. |
---|
70 | inline void setValue(bool value) { this->setBool(value); }; |
---|
71 | inline void setValue(int value) { this->setInt(value); }; |
---|
72 | inline void setValue(float value) { this->setFloat(value); }; |
---|
73 | inline void setValue(char value) { this->setChar(value); }; |
---|
74 | inline void setValue(const char* value) { this->setString(value); }; |
---|
75 | inline void setValue(const std::string& value) { this->setString(value); }; |
---|
76 | void setValueOf(const MultiType& mt); |
---|
77 | |
---|
78 | /** @returns the Type of the Value stored in this MultiType */ |
---|
79 | inline MT_Type getType() const { return this->type; }; |
---|
80 | |
---|
81 | |
---|
82 | /* RETRIEVING FUNCTIONS */ |
---|
83 | bool getBool() const; |
---|
84 | int getInt() const; |
---|
85 | float getFloat() const; |
---|
86 | char getChar() const; |
---|
87 | const char* getCString(); |
---|
88 | std::string getString() const; |
---|
89 | const std::string& getConstString() const; |
---|
90 | |
---|
91 | void reset(); |
---|
92 | |
---|
93 | void debug() const; |
---|
94 | |
---|
95 | static const std::string& MultiTypeToString(MT_Type type); |
---|
96 | static MT_Type StringToMultiType(const std::string& type); |
---|
97 | |
---|
98 | private: |
---|
99 | //! A union, that combines types into as little memory as possible. |
---|
100 | union MultiTypeValue |
---|
101 | { |
---|
102 | bool Bool; //!< If it is a BOOL |
---|
103 | int Int; //!< If it is an INT |
---|
104 | float Float; //!< If it is a FLOAT |
---|
105 | char Char; //!< If it is a CHAR |
---|
106 | } value; //!< The Value. |
---|
107 | std::string storedString; //!< The Stored String. |
---|
108 | MT_Type type; //!< The Type stored in this MultiType |
---|
109 | |
---|
110 | |
---|
111 | static std::string constString; //!< A String for returning Constant strings. |
---|
112 | |
---|
113 | static const std::string typeNames[]; //!< List of TypeNames for conversion. |
---|
114 | }; |
---|
115 | |
---|
116 | #endif /* _MULTI_TYPE_H */ |
---|