Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

next try

File size: 9.2 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 "MultiTypeMath.h"
30#include "Convert.h"
31
32MultiTypeMath::MultiTypeMath(MultiType type) : MultiTypeString(type)
33{
34    if (type == MT_vector2)
35        this->vector2_ = orxonox::Vector2(0, 0);
36    else if (type == MT_vector3)
37        this->vector3_ = orxonox::Vector3(0, 0, 0);
38    else if (type == MT_colourvalue)
39        this->colourvalue_ = orxonox::ColourValue(0, 0, 0, 0);
40    else if (type == MT_quaternion)
41        this->quaternion_ = orxonox::Quaternion(1, 0, 0, 0);
42    else if (type == MT_radian)
43        this->radian_ = orxonox::Radian(0);
44    else if (type == MT_degree)
45        this->degree_ = orxonox::Degree(0);
46}
47
48bool MultiTypeMath::operator==(const MultiTypeMath& mtm) const
49{
50    if (!MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
51    {
52        if (this->type_ == MT_vector2)
53            return (this->vector2_ == mtm.vector2_);
54        else if (this->type_ == MT_vector3)
55            return (this->vector3_ == mtm.vector3_);
56        else if (this->type_ == MT_colourvalue)
57            return (this->colourvalue_ == mtm.colourvalue_);
58        else if (this->type_ == MT_quaternion)
59            return (this->quaternion_ == mtm.quaternion_);
60        else if (this->type_ == MT_radian)
61            return (this->radian_ == mtm.radian_);
62        else if (this->type_ == MT_degree)
63            return (this->degree_ == mtm.degree_);
64    }
65
66    return false;
67}
68
69bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const
70{
71    if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
72    {
73        if (this->type_ == MT_vector2)
74            return (this->vector2_ != mtm.vector2_);
75        else if (this->type_ == MT_vector3)
76            return (this->vector3_ != mtm.vector3_);
77        else if (this->type_ == MT_colourvalue)
78            return (this->colourvalue_ != mtm.colourvalue_);
79        else if (this->type_ == MT_quaternion)
80            return (this->quaternion_ != mtm.quaternion_);
81        else if (this->type_ == MT_radian)
82            return (this->radian_ != mtm.radian_);
83        else if (this->type_ == MT_degree)
84            return (this->degree_ != mtm.degree_);
85    }
86
87    return true;
88}
89
90MultiTypeMath::operator void*() const
91{ return (this->type_ == MT_void) ? this->value_.void_ : ConvertValueAndReturn<MultiTypeMath, void*>(*this, 0); }
92MultiTypeMath::operator int() const
93{ return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeMath, int>(*this, 0); }
94MultiTypeMath::operator unsigned int() const
95{ return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeMath, unsigned int>(*this, 0); }
96MultiTypeMath::operator char() const
97{ return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeMath, char>(*this, 0); }
98MultiTypeMath::operator unsigned char() const
99{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeMath, unsigned char>(*this, 0); }
100MultiTypeMath::operator short() const
101{ return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeMath, short>(*this, 0); }
102MultiTypeMath::operator unsigned short() const
103{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeMath, unsigned short>(*this, 0); }
104MultiTypeMath::operator long() const
105{ return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeMath, long>(*this, 0); }
106MultiTypeMath::operator unsigned long() const
107{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeMath, unsigned long>(*this, 0); }
108MultiTypeMath::operator float() const
109{ return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeMath, float>(*this, 0); }
110MultiTypeMath::operator double() const
111{ return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeMath, double>(*this, 0); }
112MultiTypeMath::operator long double() const
113{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeMath, long double>(*this, 0); }
114MultiTypeMath::operator bool() const
115{ return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeMath, bool>(*this, 0); }
116MultiTypeMath::operator std::string() const
117{ return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this); }
118MultiTypeMath::operator const char*() const
119{ return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this)).c_str(); }
120MultiTypeMath::operator orxonox::Vector2() const
121{ return (this->type_ == MT_vector2) ? this->vector2_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector2>(*this); }
122MultiTypeMath::operator orxonox::Element() const
123{ return (this->type_ == MT_xmlelement) ? this->xmlelement_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Element>(*this); }
124MultiTypeMath::operator orxonox::Vector3() const
125{ return (this->type_ == MT_vector3) ? this->vector3_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector3>(*this); }
126MultiTypeMath::operator orxonox::Quaternion() const
127{ return (this->type_ == MT_quaternion) ? this->quaternion_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Quaternion>(*this); }
128MultiTypeMath::operator orxonox::ColourValue() const
129{ return (this->type_ == MT_colourvalue) ? this->colourvalue_ : ConvertValueAndReturn<MultiTypeMath, orxonox::ColourValue>(*this); }
130MultiTypeMath::operator orxonox::Radian() const
131{ return (this->type_ == MT_radian) ? this->radian_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Radian>(*this); }
132MultiTypeMath::operator orxonox::Degree() const
133{ return (this->type_ == MT_degree) ? this->degree_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Degree>(*this); }
134
135void MultiTypeMath::setValue(const MultiTypeMath& mtm)
136{
137    MultiTypeString::setValue(mtm);
138    this->vector2_ = mtm.vector2_;
139    this->vector3_ = mtm.vector3_;
140    this->quaternion_ = mtm.quaternion_;
141    this->colourvalue_ = mtm.colourvalue_;
142    this->radian_ = mtm.radian_;
143    this->degree_ = mtm.degree_;
144}
145
146std::string MultiTypeMath::getTypename() const
147{
148    if (this->type_ == MT_vector2)
149        return "Vector2";
150    else if (this->type_ == MT_vector3)
151        return "Vector3";
152    else if (this->type_ == MT_colourvalue)
153        return "ColourValue";
154    else if (this->type_ == MT_quaternion)
155        return "Quaternion";
156    else if (this->type_ == MT_radian)
157        return "Radian";
158    else if (this->type_ == MT_degree)
159        return "Degree";
160    else
161        return MultiTypeString::getTypename();
162}
163
164std::string MultiTypeMath::toString() const
165{
166    std::string output;
167
168    if (this->type_ == MT_vector2)
169        ConvertValue(&output, this->vector2_);
170    else if (this->type_ == MT_vector3)
171        ConvertValue(&output, this->vector3_);
172    else if (this->type_ == MT_colourvalue)
173        ConvertValue(&output, this->colourvalue_);
174    else if (this->type_ == MT_quaternion)
175        ConvertValue(&output, this->quaternion_);
176    else if (this->type_ == MT_radian)
177        ConvertValue(&output, this->radian_);
178    else if (this->type_ == MT_degree)
179        ConvertValue(&output, this->degree_);
180    else
181        return MultiTypeString::toString();
182
183    return output;
184}
185
186bool MultiTypeMath::fromString(const std::string value)
187{
188    if (this->type_ == MT_vector2)
189        return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0));
190    else if (this->type_ == MT_vector3)
191        return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0));
192    else if (this->type_ == MT_colourvalue)
193        return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0));
194    else if (this->type_ == MT_quaternion)
195        return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0));
196    else if (this->type_ == MT_radian)
197        return ConvertValue(&this->radian_, value, orxonox::Radian(0));
198    else if (this->type_ == MT_degree)
199        return ConvertValue(&this->degree_, value, orxonox::Degree(0));
200    else
201        return MultiTypeString::fromString(value);
202}
203
204std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm)
205{
206    out << mtm.toString();
207    return out;
208}
Note: See TracBrowser for help on using the repository browser.