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