[792] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | * Inspiration: MultiType by Benjamin Grauer |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "MultiTypePrimitive.h" |
---|
[848] | 30 | #include "Convert.h" |
---|
[792] | 31 | |
---|
| 32 | MultiTypePrimitive::MultiTypePrimitive(MultiType type) |
---|
| 33 | { |
---|
| 34 | this->type_ = type; |
---|
| 35 | |
---|
[925] | 36 | if (type == MT_void) |
---|
| 37 | this->value_.void_ = 0; |
---|
| 38 | else if (type == MT_int) |
---|
[792] | 39 | this->value_.int_ = 0; |
---|
| 40 | else if (type == MT_uint) |
---|
| 41 | this->value_.uint_ = 0; |
---|
| 42 | else if (type == MT_char) |
---|
| 43 | this->value_.char_ = 0; |
---|
| 44 | else if (type == MT_uchar) |
---|
| 45 | this->value_.uchar_ = 0; |
---|
| 46 | else if (type == MT_short) |
---|
| 47 | this->value_.short_ = 0; |
---|
| 48 | else if (type == MT_ushort) |
---|
| 49 | this->value_.ushort_ = 0; |
---|
| 50 | else if (type == MT_long) |
---|
| 51 | this->value_.long_ = 0; |
---|
| 52 | else if (type == MT_ulong) |
---|
| 53 | this->value_.ulong_ = 0; |
---|
| 54 | else if (type == MT_float) |
---|
| 55 | this->value_.float_ = 0.0; |
---|
| 56 | else if (type == MT_double) |
---|
| 57 | this->value_.double_ = 0.0; |
---|
| 58 | else if (type == MT_longdouble) |
---|
| 59 | this->value_.longdouble_ = 0.0; |
---|
| 60 | else if (type == MT_bool) |
---|
| 61 | this->value_.bool_ = false; |
---|
| 62 | else |
---|
[925] | 63 | this->value_.void_ = 0; |
---|
[792] | 64 | } |
---|
| 65 | |
---|
| 66 | bool MultiTypePrimitive::operator==(const MultiTypePrimitive& mtp) const |
---|
| 67 | { |
---|
| 68 | if (this->type_ == mtp.type_) |
---|
| 69 | { |
---|
[925] | 70 | if (this->type_ == MT_void) |
---|
| 71 | return (this->value_.void_ == mtp.value_.void_); |
---|
| 72 | else if (this->type_ == MT_int) |
---|
[792] | 73 | return (this->value_.int_ == mtp.value_.int_); |
---|
| 74 | else if (this->type_ == MT_uint) |
---|
| 75 | return (this->value_.uint_ == mtp.value_.uint_); |
---|
| 76 | else if (this->type_ == MT_char) |
---|
| 77 | return (this->value_.char_ == mtp.value_.char_); |
---|
| 78 | else if (this->type_ == MT_uchar) |
---|
| 79 | return (this->value_.uchar_ == mtp.value_.uchar_); |
---|
| 80 | else if (this->type_ == MT_short) |
---|
| 81 | return (this->value_.short_ == mtp.value_.short_); |
---|
| 82 | else if (this->type_ == MT_ushort) |
---|
| 83 | return (this->value_.ushort_ == mtp.value_.ushort_); |
---|
| 84 | else if (this->type_ == MT_long) |
---|
| 85 | return (this->value_.long_ == mtp.value_.long_); |
---|
| 86 | else if (this->type_ == MT_ulong) |
---|
| 87 | return (this->value_.ulong_ == mtp.value_.ulong_); |
---|
| 88 | else if (this->type_ == MT_float) |
---|
| 89 | return (this->value_.float_ == mtp.value_.float_); |
---|
| 90 | else if (this->type_ == MT_double) |
---|
| 91 | return (this->value_.double_ == mtp.value_.double_); |
---|
| 92 | else if (this->type_ == MT_longdouble) |
---|
| 93 | return (this->value_.longdouble_ == mtp.value_.longdouble_); |
---|
| 94 | else if (this->type_ == MT_bool) |
---|
| 95 | return (this->value_.bool_ == mtp.value_.bool_); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | return false; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | bool MultiTypePrimitive::operator!=(const MultiTypePrimitive& mtp) const |
---|
| 102 | { |
---|
| 103 | if (this->type_ == mtp.type_) |
---|
| 104 | { |
---|
[925] | 105 | if (this->type_ == MT_void) |
---|
| 106 | return (this->value_.void_ != mtp.value_.void_); |
---|
| 107 | else if (this->type_ == MT_int) |
---|
[792] | 108 | return (this->value_.int_ != mtp.value_.int_); |
---|
| 109 | else if (this->type_ == MT_uint) |
---|
| 110 | return (this->value_.uint_ != mtp.value_.uint_); |
---|
| 111 | else if (this->type_ == MT_char) |
---|
| 112 | return (this->value_.char_ != mtp.value_.char_); |
---|
| 113 | else if (this->type_ == MT_uchar) |
---|
| 114 | return (this->value_.uchar_ != mtp.value_.uchar_); |
---|
| 115 | else if (this->type_ == MT_short) |
---|
| 116 | return (this->value_.short_ != mtp.value_.short_); |
---|
| 117 | else if (this->type_ == MT_ushort) |
---|
| 118 | return (this->value_.ushort_ != mtp.value_.ushort_); |
---|
| 119 | else if (this->type_ == MT_long) |
---|
| 120 | return (this->value_.long_ != mtp.value_.long_); |
---|
| 121 | else if (this->type_ == MT_ulong) |
---|
| 122 | return (this->value_.ulong_ != mtp.value_.ulong_); |
---|
| 123 | else if (this->type_ == MT_float) |
---|
| 124 | return (this->value_.float_ != mtp.value_.float_); |
---|
| 125 | else if (this->type_ == MT_double) |
---|
| 126 | return (this->value_.double_ != mtp.value_.double_); |
---|
| 127 | else if (this->type_ == MT_longdouble) |
---|
| 128 | return (this->value_.longdouble_ != mtp.value_.longdouble_); |
---|
| 129 | else if (this->type_ == MT_bool) |
---|
| 130 | return (this->value_.bool_ != mtp.value_.bool_); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
[925] | 136 | MultiTypePrimitive::operator void*() const |
---|
[1001] | 137 | { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypePrimitive, void*>(*this, 0); } |
---|
[848] | 138 | MultiTypePrimitive::operator int() const |
---|
[1001] | 139 | { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypePrimitive, int>(*this, 0); } |
---|
[848] | 140 | MultiTypePrimitive::operator unsigned int() const |
---|
[1001] | 141 | { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypePrimitive, unsigned int>(*this, 0); } |
---|
[848] | 142 | MultiTypePrimitive::operator char() const |
---|
[1001] | 143 | { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypePrimitive, char>(*this, 0); } |
---|
[848] | 144 | MultiTypePrimitive::operator unsigned char() const |
---|
[1001] | 145 | { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypePrimitive, unsigned char>(*this, 0); } |
---|
[848] | 146 | MultiTypePrimitive::operator short() const |
---|
[1001] | 147 | { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypePrimitive, short>(*this, 0); } |
---|
[848] | 148 | MultiTypePrimitive::operator unsigned short() const |
---|
[1001] | 149 | { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypePrimitive, unsigned short>(*this, 0); } |
---|
[848] | 150 | MultiTypePrimitive::operator long() const |
---|
[1001] | 151 | { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypePrimitive, long>(*this, 0); } |
---|
[848] | 152 | MultiTypePrimitive::operator unsigned long() const |
---|
[1001] | 153 | { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypePrimitive, unsigned long>(*this, 0); } |
---|
[848] | 154 | MultiTypePrimitive::operator float() const |
---|
[1001] | 155 | { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypePrimitive, float>(*this, 0); } |
---|
[848] | 156 | MultiTypePrimitive::operator double() const |
---|
[1001] | 157 | { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypePrimitive, double>(*this, 0); } |
---|
[848] | 158 | MultiTypePrimitive::operator long double() const |
---|
[1001] | 159 | { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypePrimitive, long double>(*this, 0); } |
---|
[848] | 160 | MultiTypePrimitive::operator bool() const |
---|
[1001] | 161 | { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypePrimitive, bool>(*this, 0); } |
---|
[848] | 162 | |
---|
[792] | 163 | void MultiTypePrimitive::setValue(const MultiTypePrimitive& mtp) |
---|
| 164 | { |
---|
| 165 | this->type_ = mtp.type_; |
---|
| 166 | this->value_ = mtp.value_; |
---|
| 167 | } |
---|
[797] | 168 | |
---|
[947] | 169 | std::string MultiTypePrimitive::getTypename() const |
---|
| 170 | { |
---|
| 171 | if (this->type_ == MT_void) |
---|
| 172 | return "pointer"; |
---|
| 173 | else if (this->type_ == MT_int) |
---|
| 174 | return "int"; |
---|
| 175 | else if (this->type_ == MT_uint) |
---|
| 176 | return "unsigned int"; |
---|
| 177 | else if (this->type_ == MT_char) |
---|
| 178 | return "char"; |
---|
| 179 | else if (this->type_ == MT_uchar) |
---|
| 180 | return "unsigned char"; |
---|
| 181 | else if (this->type_ == MT_short) |
---|
| 182 | return "short"; |
---|
| 183 | else if (this->type_ == MT_ushort) |
---|
| 184 | return "unsigned short"; |
---|
| 185 | else if (this->type_ == MT_long) |
---|
| 186 | return "long"; |
---|
| 187 | else if (this->type_ == MT_ulong) |
---|
| 188 | return "unsigned long"; |
---|
| 189 | else if (this->type_ == MT_float) |
---|
| 190 | return "float"; |
---|
| 191 | else if (this->type_ == MT_double) |
---|
| 192 | return "double"; |
---|
| 193 | else if (this->type_ == MT_longdouble) |
---|
| 194 | return "long double"; |
---|
| 195 | else if (this->type_ == MT_bool) |
---|
| 196 | return "bool"; |
---|
| 197 | else |
---|
| 198 | return "unknown"; |
---|
| 199 | } |
---|
| 200 | |
---|
[848] | 201 | std::string MultiTypePrimitive::toString() const |
---|
| 202 | { |
---|
| 203 | std::string output; |
---|
| 204 | |
---|
[925] | 205 | if (this->type_ == MT_void) |
---|
| 206 | ConvertValue(&output, this->value_.void_); |
---|
| 207 | else if (this->type_ == MT_int) |
---|
[848] | 208 | ConvertValue(&output, this->value_.int_); |
---|
| 209 | else if (this->type_ == MT_uint) |
---|
| 210 | ConvertValue(&output, this->value_.uint_); |
---|
| 211 | else if (this->type_ == MT_char) |
---|
| 212 | ConvertValue(&output, this->value_.char_); |
---|
| 213 | else if (this->type_ == MT_uchar) |
---|
| 214 | ConvertValue(&output, this->value_.uchar_); |
---|
| 215 | else if (this->type_ == MT_short) |
---|
| 216 | ConvertValue(&output, this->value_.short_); |
---|
| 217 | else if (this->type_ == MT_ushort) |
---|
| 218 | ConvertValue(&output, this->value_.ushort_); |
---|
| 219 | else if (this->type_ == MT_long) |
---|
| 220 | ConvertValue(&output, this->value_.long_); |
---|
| 221 | else if (this->type_ == MT_ulong) |
---|
| 222 | ConvertValue(&output, this->value_.ulong_); |
---|
| 223 | else if (this->type_ == MT_float) |
---|
| 224 | ConvertValue(&output, this->value_.float_); |
---|
| 225 | else if (this->type_ == MT_double) |
---|
| 226 | ConvertValue(&output, this->value_.double_); |
---|
| 227 | else if (this->type_ == MT_longdouble) |
---|
| 228 | ConvertValue(&output, this->value_.longdouble_); |
---|
| 229 | else if (this->type_ == MT_bool) |
---|
| 230 | ConvertValue(&output, this->value_.bool_); |
---|
| 231 | |
---|
| 232 | return output; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | bool MultiTypePrimitive::fromString(const std::string value) |
---|
| 236 | { |
---|
[925] | 237 | if (this->type_ == MT_void) |
---|
| 238 | return ConvertValue(&this->value_.void_, value, (void*)0); |
---|
| 239 | else if (this->type_ == MT_int) |
---|
[848] | 240 | return ConvertValue(&this->value_.int_, value, (int)0); |
---|
| 241 | else if (this->type_ == MT_uint) |
---|
| 242 | return ConvertValue(&this->value_.uint_, value, (unsigned int)0); |
---|
| 243 | else if (this->type_ == MT_char) |
---|
| 244 | return ConvertValue(&this->value_.char_, value, (char)0); |
---|
| 245 | else if (this->type_ == MT_uchar) |
---|
| 246 | return ConvertValue(&this->value_.uchar_, value, (unsigned char)0); |
---|
| 247 | else if (this->type_ == MT_short) |
---|
| 248 | return ConvertValue(&this->value_.short_, value, (short)0); |
---|
| 249 | else if (this->type_ == MT_ushort) |
---|
| 250 | return ConvertValue(&this->value_.ushort_, value, (unsigned short)0); |
---|
| 251 | else if (this->type_ == MT_long) |
---|
| 252 | return ConvertValue(&this->value_.long_, value, (long)0); |
---|
| 253 | else if (this->type_ == MT_ulong) |
---|
| 254 | return ConvertValue(&this->value_.ulong_, value, (unsigned long)0); |
---|
| 255 | else if (this->type_ == MT_float) |
---|
| 256 | return ConvertValue(&this->value_.float_, value, (float)0.0); |
---|
| 257 | else if (this->type_ == MT_double) |
---|
| 258 | return ConvertValue(&this->value_.double_, value, (double)0.0); |
---|
| 259 | else if (this->type_ == MT_longdouble) |
---|
| 260 | return ConvertValue(&this->value_.longdouble_, value, (long double)0.0); |
---|
| 261 | else if (this->type_ == MT_bool) |
---|
| 262 | return ConvertValue(&this->value_.bool_, value, false); |
---|
| 263 | else |
---|
| 264 | return false; |
---|
| 265 | } |
---|
| 266 | |
---|
[797] | 267 | std::ostream& operator<<(std::ostream& out, const MultiTypePrimitive& mtp) |
---|
| 268 | { |
---|
[869] | 269 | out << mtp.toString(); |
---|
[797] | 270 | return out; |
---|
| 271 | } |
---|