Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/testing/src/libraries/util/MultiType.cc @ 9222

Last change on this file since 9222 was 9222, checked in by landauf, 12 years ago

renamed MT_Value as MultiType::Type and removed it from the public interface of MultiType
added MultiType::Null

  • Property svn:eol-style set to native
File size: 17.9 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 */
28
29/**
30    @file
31    @brief Implementation of the MultiType.
32*/
33
34#include "MultiType.h"
35#include "MultiTypeValue.h"
36
37namespace orxonox
38{
39    const MultiType MultiType::Null;
40
41    /**
42        @brief Converts the current value of the MultiType to a new type.
43        @param type The type
44    */
45    bool MultiType::convert(Type::Enum type)
46    {
47        switch (type)
48        {
49            case Type::Null:
50                this->reset(); return true;
51            case Type::Char:
52                return this->convert<char>(); break;
53            case Type::UnsignedChar:
54                return this->convert<unsigned char>(); break;
55            case Type::Short:
56                return this->convert<short>(); break;
57            case Type::UnsignedShort:
58                return this->convert<unsigned short>(); break;
59            case Type::Int:
60                return this->convert<int>(); break;
61            case Type::UnsignedInt:
62                return this->convert<unsigned int>(); break;
63            case Type::Long:
64                return this->convert<long>(); break;
65            case Type::UnsignedLong:
66                return this->convert<unsigned long>(); break;
67            case Type::LongLong:
68                return this->convert<long long>(); break;
69            case Type::UnsignedLongLong:
70                return this->convert<unsigned long long>(); break;
71            case Type::Float:
72                return this->convert<float>(); break;
73            case Type::Double:
74                return this->convert<double>(); break;
75            case Type::LongDouble:
76                return this->convert<long double>(); break;
77            case Type::Bool:
78                return this->convert<bool>(); break;
79            case Type::VoidPointer:
80                return this->convert<void*>(); break;
81            case Type::String:
82                return this->convert<std::string>(); break;
83            case Type::Vector2:
84                return this->convert<orxonox::Vector2>(); break;
85            case Type::Vector3:
86                return this->convert<orxonox::Vector3>(); break;
87            case Type::Vector4:
88                return this->convert<orxonox::Vector4>(); break;
89            case Type::ColourValue:
90                return this->convert<orxonox::ColourValue>(); break;
91            case Type::Quaternion:
92                return this->convert<orxonox::Quaternion>(); break;
93            case Type::Radian:
94                return this->convert<orxonox::Radian>(); break;
95            case Type::Degree:
96                return this->convert<orxonox::Degree>(); break;
97            default:
98                this->reset(); return false; break;
99        };
100    }
101
102    /**
103        @brief Returns the name of the current type.
104        @return The name
105    */
106    std::string MultiType::getTypename() const
107    {
108        Type::Enum type = (this->value_) ? this->value_->type_ : Type::Null;
109
110        switch (type)
111        {
112            case Type::Char:
113                return "char"; break;
114            case Type::UnsignedChar:
115                return "unsigned char"; break;
116            case Type::Short:
117                return "short"; break;
118            case Type::UnsignedShort:
119                return "unsigned short"; break;
120            case Type::Int:
121                return "int"; break;
122            case Type::UnsignedInt:
123                return "unsigned int"; break;
124            case Type::Long:
125                return "long"; break;
126            case Type::UnsignedLong:
127                return "unsigned long"; break;
128            case Type::LongLong:
129                return "long long"; break;
130            case Type::UnsignedLongLong:
131                return "unsigned long long"; break;
132            case Type::Float:
133                return "float"; break;
134            case Type::Double:
135                return "double"; break;
136            case Type::LongDouble:
137                return "long double"; break;
138            case Type::Bool:
139                return "bool"; break;
140            case Type::VoidPointer:
141                return "void*"; break;
142            case Type::String:
143                return "std::string"; break;
144            case Type::Vector2:
145                return "orxonox::Vector2"; break;
146            case Type::Vector3:
147                return "orxonox::Vector3"; break;
148            case Type::Vector4:
149                return "orxonox::Vector4"; break;
150            case Type::ColourValue:
151                return "orxonox::ColourValue"; break;
152            case Type::Quaternion:
153                return "orxonox::Quaternion"; break;
154            case Type::Radian:
155                return "orxonox::Radian"; break;
156            case Type::Degree:
157                return "orxonox::Degree"; break;
158            default:
159                return "unknown"; break;
160        };
161    }
162
163    MultiType::operator char()                 const { return (this->value_) ? ((this->value_->type_ == Type::Char            ) ? (static_cast<MT_Value<char>                *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
164    MultiType::operator unsigned char()        const { return (this->value_) ? ((this->value_->type_ == Type::UnsignedChar    ) ? (static_cast<MT_Value<unsigned char>       *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
165    MultiType::operator short()                const { return (this->value_) ? ((this->value_->type_ == Type::Short           ) ? (static_cast<MT_Value<short>               *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
166    MultiType::operator unsigned short()       const { return (this->value_) ? ((this->value_->type_ == Type::UnsignedShort   ) ? (static_cast<MT_Value<unsigned short>      *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
167    MultiType::operator int()                  const { return (this->value_) ? ((this->value_->type_ == Type::Int             ) ? (static_cast<MT_Value<int>                 *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
168    MultiType::operator unsigned int()         const { return (this->value_) ? ((this->value_->type_ == Type::UnsignedInt     ) ? (static_cast<MT_Value<unsigned int>        *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
169    MultiType::operator long()                 const { return (this->value_) ? ((this->value_->type_ == Type::Long            ) ? (static_cast<MT_Value<long>                *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
170    MultiType::operator unsigned long()        const { return (this->value_) ? ((this->value_->type_ == Type::UnsignedLong    ) ? (static_cast<MT_Value<unsigned long>       *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
171    MultiType::operator long long()            const { return (this->value_) ? ((this->value_->type_ == Type::LongLong        ) ? (static_cast<MT_Value<long long>           *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
172    MultiType::operator unsigned long long()   const { return (this->value_) ? ((this->value_->type_ == Type::UnsignedLongLong) ? (static_cast<MT_Value<unsigned long long>  *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
173    MultiType::operator float()                const { return (this->value_) ? ((this->value_->type_ == Type::Float           ) ? (static_cast<MT_Value<float>               *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
174    MultiType::operator double()               const { return (this->value_) ? ((this->value_->type_ == Type::Double          ) ? (static_cast<MT_Value<double>              *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
175    MultiType::operator long double()          const { return (this->value_) ? ((this->value_->type_ == Type::LongDouble      ) ? (static_cast<MT_Value<long double>         *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
176    MultiType::operator bool()                 const { return (this->value_) ? ((this->value_->type_ == Type::Bool            ) ? (static_cast<MT_Value<bool>                *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
177    MultiType::operator void*()                const { return (this->value_) ? ((this->value_->type_ == Type::VoidPointer     ) ? (static_cast<MT_Value<void*>               *>(this->value_))->value_ : (*this->value_)) : 0;                      } ///< Returns the current value, converted to the requested type.
178    MultiType::operator std::string()          const { return (this->value_) ? ((this->value_->type_ == Type::String          ) ? (static_cast<MT_Value<std::string>         *>(this->value_))->value_ : (*this->value_)) : NilValue<std::string>();          } ///< Returns the current value, converted to the requested type.
179    MultiType::operator orxonox::Vector2()     const { return (this->value_) ? ((this->value_->type_ == Type::Vector2         ) ? (static_cast<MT_Value<orxonox::Vector2>    *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Vector2>();     } ///< Returns the current value, converted to the requested type.
180    MultiType::operator orxonox::Vector3()     const { return (this->value_) ? ((this->value_->type_ == Type::Vector3         ) ? (static_cast<MT_Value<orxonox::Vector3>    *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Vector3>();     } ///< Returns the current value, converted to the requested type.
181    MultiType::operator orxonox::Vector4()     const { return (this->value_) ? ((this->value_->type_ == Type::Vector4         ) ? (static_cast<MT_Value<orxonox::Vector4>    *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Vector4>();     } ///< Returns the current value, converted to the requested type.
182    MultiType::operator orxonox::ColourValue() const { return (this->value_) ? ((this->value_->type_ == Type::ColourValue     ) ? (static_cast<MT_Value<orxonox::ColourValue>*>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::ColourValue>(); } ///< Returns the current value, converted to the requested type.
183    MultiType::operator orxonox::Quaternion()  const { return (this->value_) ? ((this->value_->type_ == Type::Quaternion      ) ? (static_cast<MT_Value<orxonox::Quaternion> *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Quaternion>();  } ///< Returns the current value, converted to the requested type.
184    MultiType::operator orxonox::Radian()      const { return (this->value_) ? ((this->value_->type_ == Type::Radian          ) ? (static_cast<MT_Value<orxonox::Radian>     *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Radian>();      } ///< Returns the current value, converted to the requested type.
185    MultiType::operator orxonox::Degree()      const { return (this->value_) ? ((this->value_->type_ == Type::Degree          ) ? (static_cast<MT_Value<orxonox::Degree>     *>(this->value_))->value_ : (*this->value_)) : NilValue<orxonox::Degree>();      } ///< Returns the current value, converted to the requested type.
186
187    template <> void MultiType::createNewValueContainer(const char& value)                 { this->value_ = new MT_Value<char>                (value, Type::Char            ); } ///< Creates a new value container for the given type.
188    template <> void MultiType::createNewValueContainer(const unsigned char& value)        { this->value_ = new MT_Value<unsigned char>       (value, Type::UnsignedChar    ); } ///< Creates a new value container for the given type.
189    template <> void MultiType::createNewValueContainer(const short& value)                { this->value_ = new MT_Value<short>               (value, Type::Short           ); } ///< Creates a new value container for the given type.
190    template <> void MultiType::createNewValueContainer(const unsigned short& value)       { this->value_ = new MT_Value<unsigned short>      (value, Type::UnsignedShort   ); } ///< Creates a new value container for the given type.
191    template <> void MultiType::createNewValueContainer(const int& value)                  { this->value_ = new MT_Value<int>                 (value, Type::Int             ); } ///< Creates a new value container for the given type.
192    template <> void MultiType::createNewValueContainer(const unsigned int& value)         { this->value_ = new MT_Value<unsigned int>        (value, Type::UnsignedInt     ); } ///< Creates a new value container for the given type.
193    template <> void MultiType::createNewValueContainer(const long& value)                 { this->value_ = new MT_Value<long>                (value, Type::Long            ); } ///< Creates a new value container for the given type.
194    template <> void MultiType::createNewValueContainer(const unsigned long& value)        { this->value_ = new MT_Value<unsigned long>       (value, Type::UnsignedLong    ); } ///< Creates a new value container for the given type.
195    template <> void MultiType::createNewValueContainer(const long long& value)            { this->value_ = new MT_Value<long long>           (value, Type::LongLong        ); } ///< Creates a new value container for the given type.
196    template <> void MultiType::createNewValueContainer(const unsigned long long& value)   { this->value_ = new MT_Value<unsigned long long>  (value, Type::UnsignedLongLong); } ///< Creates a new value container for the given type.
197    template <> void MultiType::createNewValueContainer(const float& value)                { this->value_ = new MT_Value<float>               (value, Type::Float           ); } ///< Creates a new value container for the given type.
198    template <> void MultiType::createNewValueContainer(const double& value)               { this->value_ = new MT_Value<double>              (value, Type::Double          ); } ///< Creates a new value container for the given type.
199    template <> void MultiType::createNewValueContainer(const long double& value)          { this->value_ = new MT_Value<long double>         (value, Type::LongDouble      ); } ///< Creates a new value container for the given type.
200    template <> void MultiType::createNewValueContainer(const bool& value)                 { this->value_ = new MT_Value<bool>                (value, Type::Bool            ); } ///< Creates a new value container for the given type.
201    template <> void MultiType::createNewValueContainer(      void* const& value)          { this->value_ = new MT_Value<void*>               (value, Type::VoidPointer     ); } ///< Creates a new value container for the given type.
202    template <> void MultiType::createNewValueContainer(const std::string& value)          { this->value_ = new MT_Value<std::string>         (value, Type::String          ); } ///< Creates a new value container for the given type.
203    template <> void MultiType::createNewValueContainer(const orxonox::Vector2& value)     { this->value_ = new MT_Value<orxonox::Vector2>    (value, Type::Vector2         ); } ///< Creates a new value container for the given type.
204    template <> void MultiType::createNewValueContainer(const orxonox::Vector3& value)     { this->value_ = new MT_Value<orxonox::Vector3>    (value, Type::Vector3         ); } ///< Creates a new value container for the given type.
205    template <> void MultiType::createNewValueContainer(const orxonox::Vector4& value)     { this->value_ = new MT_Value<orxonox::Vector4>    (value, Type::Vector4         ); } ///< Creates a new value container for the given type.
206    template <> void MultiType::createNewValueContainer(const orxonox::ColourValue& value) { this->value_ = new MT_Value<orxonox::ColourValue>(value, Type::ColourValue     ); } ///< Creates a new value container for the given type.
207    template <> void MultiType::createNewValueContainer(const orxonox::Quaternion& value)  { this->value_ = new MT_Value<orxonox::Quaternion> (value, Type::Quaternion      ); } ///< Creates a new value container for the given type.
208    template <> void MultiType::createNewValueContainer(const orxonox::Radian& value)      { this->value_ = new MT_Value<orxonox::Radian>     (value, Type::Radian          ); } ///< Creates a new value container for the given type.
209    template <> void MultiType::createNewValueContainer(const orxonox::Degree& value)      { this->value_ = new MT_Value<orxonox::Degree>     (value, Type::Degree          ); } ///< Creates a new value container for the given type.
210}
Note: See TracBrowser for help on using the repository browser.