Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/MultiTypeValue.h @ 9550

Last change on this file since 9550 was 9550, checked in by landauf, 11 years ago

merged testing branch back to trunk. unbelievable it took me 13 months to finish this chore…

  • Property svn:eol-style set to native
File size: 17.3 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    @ingroup MultiType
32    @brief Declaration and Implementation of the MT_Value<T> class.
33
34    The MT_Value<T> class is used to hold a value of type T within a MultiType.
35*/
36
37#ifndef _MultiTypeValue_H__
38#define _MultiTypeValue_H__
39
40#include "UtilPrereqs.h"
41
42#include <cassert>
43#include "MathConvert.h"
44#include "MultiType.h"
45#include "Serialise.h"
46
47namespace orxonox
48{
49    /**
50        @brief The MT_Value<T> class is used to hold a value of type T within a MultiType.
51    */
52    template <typename T>
53    class MT_Value : public MultiType::MT_ValueBase
54    {
55    public:
56        /// Constructor: Assigns the value and the type identifier.
57        MT_Value(const T& value, MultiType::Type::Enum type) : MT_ValueBase(&this->value_, type), value_(value) {}
58
59        /// Creates a copy of itself.
60        inline MT_ValueBase* clone() const { return new MT_Value<T>(this->value_, this->type_); }
61
62        /// Resets the current value to the default.
63        inline void reset() { this->value_ = zeroise<T>(); bLastConversionSuccessful = true; }
64
65        inline bool getValue(char*                 value) const { return convertValue<T, char                >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
66        inline bool getValue(unsigned char*        value) const { return convertValue<T, unsigned char       >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
67        inline bool getValue(short*                value) const { return convertValue<T, short               >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
68        inline bool getValue(unsigned short*       value) const { return convertValue<T, unsigned short      >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
69        inline bool getValue(int*                  value) const { return convertValue<T, int                 >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
70        inline bool getValue(unsigned int*         value) const { return convertValue<T, unsigned int        >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
71        inline bool getValue(long*                 value) const { return convertValue<T, long                >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
72        inline bool getValue(unsigned long*        value) const { return convertValue<T, unsigned long       >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
73        inline bool getValue(long long*            value) const { return convertValue<T, long long           >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
74        inline bool getValue(unsigned long long*   value) const { return convertValue<T, unsigned long long  >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
75        inline bool getValue(float*                value) const { return convertValue<T, float               >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
76        inline bool getValue(double*               value) const { return convertValue<T, double              >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
77        inline bool getValue(long double*          value) const { return convertValue<T, long double         >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
78        inline bool getValue(bool*                 value) const { return convertValue<T, bool                >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
79        inline bool getValue(void**                value) const { return convertValue<T, void*               >(value, value_, 0); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
80        inline bool getValue(std::string*          value) const { return convertValue<T, std::string         >(value, value_, NilValue<std::string>         ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
81        inline bool getValue(orxonox::Vector2*     value) const { return convertValue<T, orxonox::Vector2    >(value, value_, NilValue<orxonox::Vector2>    ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
82        inline bool getValue(orxonox::Vector3*     value) const { return convertValue<T, orxonox::Vector3    >(value, value_, NilValue<orxonox::Vector3>    ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
83        inline bool getValue(orxonox::Vector4*     value) const { return convertValue<T, orxonox::Vector4    >(value, value_, NilValue<orxonox::Vector4>    ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
84        inline bool getValue(orxonox::ColourValue* value) const { return convertValue<T, orxonox::ColourValue>(value, value_, NilValue<orxonox::ColourValue>()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
85        inline bool getValue(orxonox::Quaternion*  value) const { return convertValue<T, orxonox::Quaternion >(value, value_, NilValue<orxonox::Quaternion> ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
86        inline bool getValue(orxonox::Radian*      value) const { return convertValue<T, orxonox::Radian     >(value, value_, NilValue<orxonox::Radian>     ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
87        inline bool getValue(orxonox::Degree*      value) const { return convertValue<T, orxonox::Degree     >(value, value_, NilValue<orxonox::Degree>     ()); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
88
89        /**
90            @brief Assigns the value of the other MultiType, converted to T.
91            @param other The other MultiType
92        */
93        inline bool setValue(const MultiType& other)
94        {
95            if (other.value_)
96            {
97                return (this->bLastConversionSuccessful = other.value_->getValue(&value_));
98            }
99            else
100            {
101                this->value_ = zeroise<T>();
102                return (bLastConversionSuccessful = false);
103            }
104        }
105
106        inline bool setValue(const char& value)                 { return (bLastConversionSuccessful = convertValue<char                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
107        inline bool setValue(const unsigned char& value)        { return (bLastConversionSuccessful = convertValue<unsigned char       , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
108        inline bool setValue(const short& value)                { return (bLastConversionSuccessful = convertValue<short               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
109        inline bool setValue(const unsigned short& value)       { return (bLastConversionSuccessful = convertValue<unsigned short      , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
110        inline bool setValue(const int& value)                  { return (bLastConversionSuccessful = convertValue<int                 , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
111        inline bool setValue(const unsigned int& value)         { return (bLastConversionSuccessful = convertValue<unsigned int        , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
112        inline bool setValue(const long& value)                 { return (bLastConversionSuccessful = convertValue<long                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
113        inline bool setValue(const unsigned long& value)        { return (bLastConversionSuccessful = convertValue<unsigned long       , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
114        inline bool setValue(const long long& value)            { return (bLastConversionSuccessful = convertValue<long long           , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
115        inline bool setValue(const unsigned long long& value)   { return (bLastConversionSuccessful = convertValue<unsigned long long  , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
116        inline bool setValue(const float& value)                { return (bLastConversionSuccessful = convertValue<float               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
117        inline bool setValue(const double& value)               { return (bLastConversionSuccessful = convertValue<double              , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
118        inline bool setValue(const long double& value)          { return (bLastConversionSuccessful = convertValue<long double         , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
119        inline bool setValue(const bool& value)                 { return (bLastConversionSuccessful = convertValue<bool                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
120        inline bool setValue(      void* const& value)          { return (bLastConversionSuccessful = convertValue<void*               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
121        inline bool setValue(const std::string& value)          { return (bLastConversionSuccessful = convertValue<std::string         , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
122        inline bool setValue(const orxonox::Vector2& value)     { return (bLastConversionSuccessful = convertValue<orxonox::Vector2    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
123        inline bool setValue(const orxonox::Vector3& value)     { return (bLastConversionSuccessful = convertValue<orxonox::Vector3    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
124        inline bool setValue(const orxonox::Vector4& value)     { return (bLastConversionSuccessful = convertValue<orxonox::Vector4    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
125        inline bool setValue(const orxonox::ColourValue& value) { return (bLastConversionSuccessful = convertValue<orxonox::ColourValue, T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
126        inline bool setValue(const orxonox::Quaternion& value)  { return (bLastConversionSuccessful = convertValue<orxonox::Quaternion , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
127        inline bool setValue(const orxonox::Radian& value)      { return (bLastConversionSuccessful = convertValue<orxonox::Radian     , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
128        inline bool setValue(const orxonox::Degree& value)      { return (bLastConversionSuccessful = convertValue<orxonox::Degree     , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
129
130        /// Puts the current value on the stream
131        inline void toString(std::ostream& outstream) const { outstream << this->value_; }
132
133        /// loads data from the bytestream (mem) into the MT and increases the bytestream pointer by the size of the data
134        inline void importData( uint8_t*& mem )         { loadAndIncrease( /*(const T&)*/this->value_, mem ); }
135        /// saves data from the MT into the bytestream (mem) and increases the bytestream pointer by the size of the data
136        inline void exportData( uint8_t*& mem ) const   { saveAndIncrease( /*(const T&)*/this->value_, mem ); }
137        /// returns the size of the data that would be saved by exportData
138        inline uint8_t getSize() const { return returnSize( this->value_ ); }
139
140        T value_; ///< The stored value
141    };
142
143    // Import / Export specialisation
144    // ColourValue
145    template <> inline void MT_Value<ColourValue>::importData( uint8_t*& mem )
146    {
147        loadAndIncrease( this->value_.r, mem );
148        loadAndIncrease( this->value_.g, mem );
149        loadAndIncrease( this->value_.b, mem );
150        loadAndIncrease( this->value_.a, mem );
151    }
152    template <> inline void MT_Value<ColourValue>::exportData( uint8_t*& mem ) const
153    {
154        saveAndIncrease( this->value_.r, mem );
155        saveAndIncrease( this->value_.g, mem );
156        saveAndIncrease( this->value_.b, mem );
157        saveAndIncrease( this->value_.a, mem );
158    }
159    template <> inline uint8_t MT_Value<ColourValue>::getSize() const
160    {
161        return 4*returnSize(this->value_.r);
162    }
163    // Ogre::Quaternion
164    template <> inline void MT_Value<Ogre::Quaternion>::importData( uint8_t*& mem )
165    {
166        loadAndIncrease( this->value_.x, mem );
167        loadAndIncrease( this->value_.y, mem );
168        loadAndIncrease( this->value_.z, mem );
169        loadAndIncrease( this->value_.w, mem );
170    }
171    template <> inline void MT_Value<Ogre::Quaternion>::exportData( uint8_t*& mem ) const
172    {
173        saveAndIncrease( this->value_.x, mem );
174        saveAndIncrease( this->value_.y, mem );
175        saveAndIncrease( this->value_.z, mem );
176        saveAndIncrease( this->value_.w, mem );
177    }
178    template <> inline uint8_t MT_Value<Ogre::Quaternion>::getSize() const
179    {
180        return 4*returnSize(this->value_.x);
181    }
182    // Ogre::Vector2
183    template <> inline void MT_Value<Ogre::Vector2>::importData( uint8_t*& mem )
184    {
185        loadAndIncrease( this->value_.x, mem );
186        loadAndIncrease( this->value_.y, mem );
187    }
188    template <> inline void MT_Value<Ogre::Vector2>::exportData( uint8_t*& mem ) const
189    {
190        saveAndIncrease( this->value_.x, mem );
191        saveAndIncrease( this->value_.y, mem );
192    }
193    template <> inline uint8_t MT_Value<Ogre::Vector2>::getSize() const
194    {
195        return 2*returnSize(this->value_.x);
196    }
197    // Ogre::Vector3
198    template <> inline void MT_Value<Ogre::Vector3>::importData( uint8_t*& mem )
199    {
200        loadAndIncrease( this->value_.x, mem );
201        loadAndIncrease( this->value_.y, mem );
202        loadAndIncrease( this->value_.z, mem );
203    }
204    template <> inline void MT_Value<Ogre::Vector3>::exportData( uint8_t*& mem ) const
205    {
206        saveAndIncrease( this->value_.x, mem );
207        saveAndIncrease( this->value_.y, mem );
208        saveAndIncrease( this->value_.z, mem );
209    }
210    template <> inline uint8_t MT_Value<Ogre::Vector3>::getSize() const
211    {
212        return 3*returnSize(this->value_.x);
213    }
214    // Ogre::Vector4
215    template <> inline void MT_Value<Ogre::Vector4>::importData( uint8_t*& mem )
216    {
217        loadAndIncrease( this->value_.x, mem );
218        loadAndIncrease( this->value_.y, mem );
219        loadAndIncrease( this->value_.z, mem );
220        loadAndIncrease( this->value_.w, mem );
221    }
222    template <> inline void MT_Value<Ogre::Vector4>::exportData( uint8_t*& mem ) const
223    {
224        saveAndIncrease( this->value_.x, mem );
225        saveAndIncrease( this->value_.y, mem );
226        saveAndIncrease( this->value_.z, mem );
227        saveAndIncrease( this->value_.w, mem );
228    }
229    template <> inline uint8_t MT_Value<Ogre::Vector4>::getSize() const
230    {
231        return 4*returnSize(this->value_.x);
232    }
233    template <> inline void MT_Value<void*>::importData( uint8_t*& /*mem*/ )
234    {
235        assert(0);
236    }
237    template <> inline void MT_Value<void*>::exportData( uint8_t*& /*mem*/ ) const
238    {
239        assert(0);
240    }
241    template <> inline uint8_t MT_Value<void*>::getSize() const
242    {
243        assert(0); return 0;
244    }
245}
246
247#endif /* _MultiTypeValue_H__ */
Note: See TracBrowser for help on using the repository browser.