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