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" |
---|
30 | #include "Convert.h" |
---|
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 | MultiTypeString::MultiTypeString(const MultiTypeString& mts) : MultiTypePrimitive(mts) |
---|
41 | { |
---|
42 | this->type_ = mts.type_; |
---|
43 | this->value_ = mts.value_; |
---|
44 | } |
---|
45 | |
---|
46 | MultiTypeString& MultiTypeString::operator=(const MultiTypeString& mts) |
---|
47 | { |
---|
48 | this->type_ = mts.type_; |
---|
49 | this->value_ = mts.value_; |
---|
50 | return *this; |
---|
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 false; |
---|
64 | } |
---|
65 | |
---|
66 | bool MultiTypeString::operator!=(const MultiTypeString& mts) const |
---|
67 | { |
---|
68 | if (MultiTypePrimitive::operator==(mts) && this->type_ == mts.type_) |
---|
69 | { |
---|
70 | if (this->type_ == MT_constchar) |
---|
71 | return (this->string_ != mts.string_); |
---|
72 | else if (this->type_ == MT_string) |
---|
73 | return (this->string_ != mts.string_); |
---|
74 | } |
---|
75 | |
---|
76 | return true; |
---|
77 | } |
---|
78 | |
---|
79 | MultiTypeString::operator std::string() const |
---|
80 | { |
---|
81 | return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypePrimitive, std::string>(*this); |
---|
82 | } |
---|
83 | |
---|
84 | MultiTypeString::operator const char*() const |
---|
85 | { |
---|
86 | return (this->type_ == MT_constchar) ? this->string_.c_str() : ConvertValueAndReturn<MultiTypePrimitive, const char*>(*this); |
---|
87 | } |
---|
88 | |
---|
89 | void MultiTypeString::setValue(const MultiTypeString& mts) |
---|
90 | { |
---|
91 | this->type_ = mts.type_; |
---|
92 | this->value_ = mts.value_; |
---|
93 | } |
---|
94 | |
---|
95 | std::string MultiTypeString::toString() const |
---|
96 | { |
---|
97 | if (this->type_ == MT_constchar) |
---|
98 | return this->string_; |
---|
99 | else if (this->type_ == MT_string) |
---|
100 | return this->string_; |
---|
101 | else |
---|
102 | return MultiTypePrimitive::toString(); |
---|
103 | } |
---|
104 | |
---|
105 | bool MultiTypeString::fromString(const std::string value) |
---|
106 | { |
---|
107 | if (this->type_ == MT_constchar) |
---|
108 | this->string_ = value; |
---|
109 | else if (this->type_ == MT_string) |
---|
110 | this->string_ = value; |
---|
111 | else |
---|
112 | return MultiTypePrimitive::fromString(value); |
---|
113 | |
---|
114 | return true; |
---|
115 | } |
---|
116 | |
---|
117 | std::ostream& operator<<(std::ostream& out, MultiTypeString& mts) |
---|
118 | { |
---|
119 | if (mts.isA(MT_constchar)) |
---|
120 | out << mts.getConstChar(); |
---|
121 | else if (mts.isA(MT_string)) |
---|
122 | out << mts.getString(); |
---|
123 | else |
---|
124 | out << ((MultiTypePrimitive)mts); |
---|
125 | |
---|
126 | return out; |
---|
127 | } |
---|