Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/util/MultiTypeValue.h @ 2861

Last change on this file since 2861 was 2861, checked in by scheusso, 15 years ago
  • moved serialise functions from SynchronisableVariableSpecialisation.cc

to util/Serialise.h

  • made MultiType serialisable (use functions importData/exportData or

operator << / >> )

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