[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 "MultiTypeString.h" |
---|
[848] | 30 | #include "Convert.h" |
---|
[792] | 31 | |
---|
| 32 | MultiTypeString::MultiTypeString(MultiType type) : MultiTypePrimitive(type) |
---|
| 33 | { |
---|
| 34 | if (type == MT_constchar) |
---|
| 35 | this->string_ = std::string(""); |
---|
| 36 | else if (type == MT_string) |
---|
| 37 | this->string_ = std::string(""); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | bool MultiTypeString::operator==(const MultiTypeString& mts) const |
---|
| 41 | { |
---|
| 42 | if (!MultiTypePrimitive::operator==(mts) && this->type_ == mts.type_) |
---|
| 43 | { |
---|
| 44 | if (this->type_ == MT_constchar) |
---|
| 45 | return (this->string_ == mts.string_); |
---|
| 46 | else if (this->type_ == MT_string) |
---|
| 47 | return (this->string_ == mts.string_); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | return false; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | bool MultiTypeString::operator!=(const MultiTypeString& mts) const |
---|
| 54 | { |
---|
| 55 | if (MultiTypePrimitive::operator==(mts) && this->type_ == mts.type_) |
---|
| 56 | { |
---|
| 57 | if (this->type_ == MT_constchar) |
---|
| 58 | return (this->string_ != mts.string_); |
---|
| 59 | else if (this->type_ == MT_string) |
---|
| 60 | return (this->string_ != mts.string_); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | return true; |
---|
| 64 | } |
---|
| 65 | |
---|
[869] | 66 | MultiTypeString::operator int() const |
---|
| 67 | { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeString, int>(*this); } |
---|
| 68 | MultiTypeString::operator unsigned int() const |
---|
| 69 | { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeString, unsigned int>(*this); } |
---|
| 70 | MultiTypeString::operator char() const |
---|
| 71 | { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeString, char>(*this); } |
---|
| 72 | MultiTypeString::operator unsigned char() const |
---|
| 73 | { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeString, unsigned char>(*this); } |
---|
| 74 | MultiTypeString::operator short() const |
---|
| 75 | { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeString, short>(*this); } |
---|
| 76 | MultiTypeString::operator unsigned short() const |
---|
| 77 | { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeString, unsigned short>(*this); } |
---|
| 78 | MultiTypeString::operator long() const |
---|
| 79 | { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeString, long>(*this); } |
---|
| 80 | MultiTypeString::operator unsigned long() const |
---|
| 81 | { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeString, unsigned long>(*this); } |
---|
| 82 | MultiTypeString::operator float() const |
---|
| 83 | { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeString, float>(*this); } |
---|
| 84 | MultiTypeString::operator double() const |
---|
| 85 | { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeString, double>(*this); } |
---|
| 86 | MultiTypeString::operator long double() const |
---|
| 87 | { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeString, long double>(*this); } |
---|
| 88 | MultiTypeString::operator bool() const |
---|
| 89 | { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeString, bool>(*this); } |
---|
[848] | 90 | MultiTypeString::operator std::string() const |
---|
[869] | 91 | { return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this); } |
---|
[848] | 92 | MultiTypeString::operator const char*() const |
---|
[869] | 93 | { return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this)).c_str(); } |
---|
[848] | 94 | |
---|
[792] | 95 | void MultiTypeString::setValue(const MultiTypeString& mts) |
---|
| 96 | { |
---|
[853] | 97 | MultiTypePrimitive::setValue(mts); |
---|
| 98 | this->string_ = mts.string_; |
---|
[792] | 99 | } |
---|
[797] | 100 | |
---|
[848] | 101 | std::string MultiTypeString::toString() const |
---|
| 102 | { |
---|
| 103 | if (this->type_ == MT_constchar) |
---|
| 104 | return this->string_; |
---|
| 105 | else if (this->type_ == MT_string) |
---|
| 106 | return this->string_; |
---|
| 107 | else |
---|
| 108 | return MultiTypePrimitive::toString(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | bool MultiTypeString::fromString(const std::string value) |
---|
| 112 | { |
---|
| 113 | if (this->type_ == MT_constchar) |
---|
| 114 | this->string_ = value; |
---|
| 115 | else if (this->type_ == MT_string) |
---|
| 116 | this->string_ = value; |
---|
| 117 | else |
---|
| 118 | return MultiTypePrimitive::fromString(value); |
---|
| 119 | |
---|
| 120 | return true; |
---|
| 121 | } |
---|
| 122 | |
---|
[797] | 123 | std::ostream& operator<<(std::ostream& out, MultiTypeString& mts) |
---|
| 124 | { |
---|
[869] | 125 | out << mts.toString(); |
---|
[797] | 126 | return out; |
---|
| 127 | } |
---|