Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/MultiTypeString.cc @ 1573

Last change on this file since 1573 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
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
33MultiTypeString::MultiTypeString(MultiType type) : MultiTypePrimitive(type)
34{
35    // Nothing to do for string
36}
37
38bool 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
51bool MultiTypeString::operator==(const MultiTypePrimitive& mtp) const
52{
53    return MultiTypePrimitive::operator==(mtp);
54}
55
56bool MultiTypeString::operator!=(const MultiTypeString& mts) const
57{
58    if (MultiTypePrimitive::operator==(mts) && this->type_ == mts.type_)
59    {
60        if (this->type_ == MT_constchar)
61            return (this->string_ != mts.string_);
62        else if (this->type_ == MT_string)
63            return (this->string_ != mts.string_);
64    }
65
66    return true;
67}
68
69bool MultiTypeString::operator!=(const MultiTypePrimitive& mtp) const
70{
71    return MultiTypePrimitive::operator!=(mtp);
72}
73
74MultiTypeString::operator void*() const
75{ return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeString, void*>(*this, 0); }
76MultiTypeString::operator int() const
77{ return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeString, int>(*this, 0); }
78MultiTypeString::operator unsigned int() const
79{ return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeString, unsigned int>(*this, 0); }
80MultiTypeString::operator char() const
81{ return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeString, char>(*this, 0); }
82MultiTypeString::operator unsigned char() const
83{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeString, unsigned char>(*this, 0); }
84MultiTypeString::operator short() const
85{ return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeString, short>(*this, 0); }
86MultiTypeString::operator unsigned short() const
87{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeString, unsigned short>(*this, 0); }
88MultiTypeString::operator long() const
89{ return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeString, long>(*this, 0); }
90MultiTypeString::operator unsigned long() const
91{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeString, unsigned long>(*this, 0); }
92MultiTypeString::operator float() const
93{ return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeString, float>(*this, 0); }
94MultiTypeString::operator double() const
95{ return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeString, double>(*this, 0); }
96MultiTypeString::operator long double() const
97{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeString, long double>(*this, 0); }
98MultiTypeString::operator bool() const
99{ return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeString, bool>(*this, 0); }
100MultiTypeString::operator std::string() const
101{ return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this); }
102MultiTypeString::operator const char*() const
103{ return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this)).c_str(); }
104
105void MultiTypeString::setValue(const MultiTypeString& mts)
106{
107    MultiTypePrimitive::setValue(mts);
108    this->string_ = mts.string_;
109}
110
111void MultiTypeString::setValue(const MultiTypePrimitive& mtp)
112{
113    MultiTypePrimitive::setValue(mtp);
114}
115
116std::string MultiTypeString::getTypename() const
117{
118    if (this->type_ == MT_constchar)
119        return "string";
120    else if (this->type_ == MT_string)
121        return "string";
122    else
123        return MultiTypePrimitive::getTypename();
124}
125
126std::string MultiTypeString::toString() const
127{
128    std::string output;
129
130    if (this->type_ == MT_constchar)
131        return this->string_;
132    else if (this->type_ == MT_string)
133        return this->string_;
134    else
135        return MultiTypePrimitive::toString();
136
137    // FIXME: unreachable code
138    // return output;
139}
140
141bool MultiTypeString::fromString(const std::string value)
142{
143    if (this->type_ == MT_constchar)
144        this->string_ = value;
145    else if (this->type_ == MT_string)
146        this->string_ = value;
147    else
148        return MultiTypePrimitive::fromString(value);
149
150    return true;
151}
152
153bool MultiTypeString::assimilate(const MultiTypeString& mts, const MultiTypeString& defvalue)
154{
155    if (this->type_ == MT_void)
156        return ConvertValue(&this->value_.void_, mts, defvalue.value_.void_);
157    else if (this->type_ == MT_int)
158        return ConvertValue(&this->value_.int_, mts, defvalue.value_.int_);
159    else if (this->type_ == MT_uint)
160        return ConvertValue(&this->value_.uint_, mts, defvalue.value_.uint_);
161    else if (this->type_ == MT_char)
162        return ConvertValue(&this->value_.char_, mts, defvalue.value_.char_);
163    else if (this->type_ == MT_uchar)
164        return ConvertValue(&this->value_.uchar_, mts, defvalue.value_.uchar_);
165    else if (this->type_ == MT_short)
166        return ConvertValue(&this->value_.short_, mts, defvalue.value_.short_);
167    else if (this->type_ == MT_ushort)
168        return ConvertValue(&this->value_.ushort_, mts, defvalue.value_.ushort_);
169    else if (this->type_ == MT_long)
170        return ConvertValue(&this->value_.long_, mts, defvalue.value_.long_);
171    else if (this->type_ == MT_ulong)
172        return ConvertValue(&this->value_.ulong_, mts, defvalue.value_.ulong_);
173    else if (this->type_ == MT_float)
174        return ConvertValue(&this->value_.float_, mts, defvalue.value_.float_);
175    else if (this->type_ == MT_double)
176        return ConvertValue(&this->value_.double_, mts, defvalue.value_.double_);
177    else if (this->type_ == MT_longdouble)
178        return ConvertValue(&this->value_.longdouble_, mts, defvalue.value_.longdouble_);
179    else if (this->type_ == MT_bool)
180        return ConvertValue(&this->value_.bool_, mts, defvalue.value_.bool_);
181    else if (this->type_ == MT_constchar)
182        return ConvertValue(&this->string_, mts, defvalue.string_);
183    else if (this->type_ == MT_string)
184        return ConvertValue(&this->string_, mts, defvalue.string_);
185    else
186        return false;
187}
188
189std::ostream& operator<<(std::ostream& out, MultiTypeString& mts)
190{
191    out << mts.toString();
192    return out;
193}
Note: See TracBrowser for help on using the repository browser.