Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/MultiTypeString.cc @ 960

Last change on this file since 960 was 960, checked in by landauf, 16 years ago

next try

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