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