Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/util/MultiTypeString.cc @ 1320

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

changed the previously implemented assimilate function to take an optional default-value and to return false if the conversion failed.

File size: 5.8 KB
RevLine 
[792]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[792]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"
[848]31#include "Convert.h"
[792]32
33MultiTypeString::MultiTypeString(MultiType type) : MultiTypePrimitive(type)
34{
[1052]35    // Nothing to do for string and xml-element
[792]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 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
[1052]64MultiTypeString::operator void*() const
65{ return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeString, void*>(*this, 0); }
[869]66MultiTypeString::operator int() const
[1052]67{ return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeString, int>(*this, 0); }
[869]68MultiTypeString::operator unsigned int() const
[1052]69{ return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeString, unsigned int>(*this, 0); }
[869]70MultiTypeString::operator char() const
[1052]71{ return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeString, char>(*this, 0); }
[869]72MultiTypeString::operator unsigned char() const
[1052]73{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeString, unsigned char>(*this, 0); }
[869]74MultiTypeString::operator short() const
[1052]75{ return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeString, short>(*this, 0); }
[869]76MultiTypeString::operator unsigned short() const
[1052]77{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeString, unsigned short>(*this, 0); }
[869]78MultiTypeString::operator long() const
[1052]79{ return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeString, long>(*this, 0); }
[869]80MultiTypeString::operator unsigned long() const
[1052]81{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeString, unsigned long>(*this, 0); }
[869]82MultiTypeString::operator float() const
[1052]83{ return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeString, float>(*this, 0); }
[869]84MultiTypeString::operator double() const
[1052]85{ return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeString, double>(*this, 0); }
[869]86MultiTypeString::operator long double() const
[1052]87{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeString, long double>(*this, 0); }
[869]88MultiTypeString::operator bool() const
[1052]89{ return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeString, bool>(*this, 0); }
[848]90MultiTypeString::operator std::string() const
[1052]91{ return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this); }
[848]92MultiTypeString::operator const char*() const
[1052]93{ return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this)).c_str(); }
[848]94
[792]95void MultiTypeString::setValue(const MultiTypeString& mts)
96{
[853]97    MultiTypePrimitive::setValue(mts);
98    this->string_ = mts.string_;
[792]99}
[797]100
[1052]101std::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
[848]111std::string MultiTypeString::toString() const
112{
[1052]113    std::string output;
114
[848]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();
[1052]121
[1064]122    // FIXME: unreachable code
123    // return output;
[848]124}
125
126bool MultiTypeString::fromString(const std::string value)
127{
128    if (this->type_ == MT_constchar)
129        this->string_ = value;
130    else if (this->type_ == MT_string)
131        this->string_ = value;
132    else
133        return MultiTypePrimitive::fromString(value);
134
135    return true;
136}
137
[1320]138bool MultiTypeString::assimilate(const MultiTypeString& mts, const MultiTypeString& defvalue)
[1319]139{
140    if (this->type_ == MT_constchar)
[1320]141        return ConvertValue(&this->string_, mts, defvalue.string_);
[1319]142    else if (this->type_ == MT_string)
[1320]143        return ConvertValue(&this->string_, mts, defvalue.string_);
[1319]144    else
[1320]145        return MultiTypePrimitive::assimilate(mts, defvalue);
[1319]146}
147
[797]148std::ostream& operator<<(std::ostream& out, MultiTypeString& mts)
149{
[869]150    out << mts.toString();
[797]151    return out;
152}
Note: See TracBrowser for help on using the repository browser.