Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 18.1 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 type) : MT_ValueBase(&this->value_, type), value_(value) {}
58
59        /// Creates a copy of itself.
60        virtual inline MT_ValueBase* clone() const override { return new MT_Value<T>(this->value_, this->type_); }
61
62        /// Resets the current value to the default.
63        virtual inline void reset() override { this->value_ = zeroise<T>(); bLastConversionSuccessful = true; }
64
65        virtual inline bool getValue(char*                 value) const override { 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        virtual inline bool getValue(unsigned char*        value) const override { 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        virtual inline bool getValue(short*                value) const override { 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        virtual inline bool getValue(unsigned short*       value) const override { 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        virtual inline bool getValue(int*                  value) const override { 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        virtual inline bool getValue(unsigned int*         value) const override { 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        virtual inline bool getValue(long*                 value) const override { 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        virtual inline bool getValue(unsigned long*        value) const override { 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        virtual inline bool getValue(long long*            value) const override { 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        virtual inline bool getValue(unsigned long long*   value) const override { 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        virtual inline bool getValue(float*                value) const override { 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        virtual inline bool getValue(double*               value) const override { 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        virtual inline bool getValue(long double*          value) const override { 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        virtual inline bool getValue(bool*                 value) const override { 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        virtual inline bool getValue(void**                value) const override { return convertValue<T, void*               >(value, value_, nullptr); } ///< Assigns the value to the given pointer. The value gets converted if the types don't match.
80        virtual inline bool getValue(std::string*          value) const override { 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        virtual inline bool getValue(orxonox::Vector2*     value) const override { 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        virtual inline bool getValue(orxonox::Vector3*     value) const override { 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        virtual inline bool getValue(orxonox::Vector4*     value) const override { 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        virtual inline bool getValue(orxonox::ColourValue* value) const override { 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        virtual inline bool getValue(orxonox::Quaternion*  value) const override { 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        virtual inline bool getValue(orxonox::Radian*      value) const override { 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        virtual inline bool getValue(orxonox::Degree*      value) const override { 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        virtual inline bool setValue(const MultiType& other) override
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        virtual inline bool setValue(const char& value)                 override { return (bLastConversionSuccessful = convertValue<char                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
107        virtual inline bool setValue(const unsigned char& value)        override { return (bLastConversionSuccessful = convertValue<unsigned char       , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
108        virtual inline bool setValue(const short& value)                override { return (bLastConversionSuccessful = convertValue<short               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
109        virtual inline bool setValue(const unsigned short& value)       override { return (bLastConversionSuccessful = convertValue<unsigned short      , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
110        virtual inline bool setValue(const int& value)                  override { return (bLastConversionSuccessful = convertValue<int                 , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
111        virtual inline bool setValue(const unsigned int& value)         override { return (bLastConversionSuccessful = convertValue<unsigned int        , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
112        virtual inline bool setValue(const long& value)                 override { return (bLastConversionSuccessful = convertValue<long                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
113        virtual inline bool setValue(const unsigned long& value)        override { return (bLastConversionSuccessful = convertValue<unsigned long       , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
114        virtual inline bool setValue(const long long& value)            override { return (bLastConversionSuccessful = convertValue<long long           , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
115        virtual inline bool setValue(const unsigned long long& value)   override { return (bLastConversionSuccessful = convertValue<unsigned long long  , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
116        virtual inline bool setValue(const float& value)                override { return (bLastConversionSuccessful = convertValue<float               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
117        virtual inline bool setValue(const double& value)               override { return (bLastConversionSuccessful = convertValue<double              , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
118        virtual inline bool setValue(const long double& value)          override { return (bLastConversionSuccessful = convertValue<long double         , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
119        virtual inline bool setValue(const bool& value)                 override { return (bLastConversionSuccessful = convertValue<bool                , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
120        virtual inline bool setValue(      void* const& value)          override { return (bLastConversionSuccessful = convertValue<void*               , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
121        virtual inline bool setValue(const std::string& value)          override { return (bLastConversionSuccessful = convertValue<std::string         , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
122        virtual inline bool setValue(const orxonox::Vector2& value)     override { return (bLastConversionSuccessful = convertValue<orxonox::Vector2    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
123        virtual inline bool setValue(const orxonox::Vector3& value)     override { return (bLastConversionSuccessful = convertValue<orxonox::Vector3    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
124        virtual inline bool setValue(const orxonox::Vector4& value)     override { return (bLastConversionSuccessful = convertValue<orxonox::Vector4    , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
125        virtual inline bool setValue(const orxonox::ColourValue& value) override { return (bLastConversionSuccessful = convertValue<orxonox::ColourValue, T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
126        virtual inline bool setValue(const orxonox::Quaternion& value)  override { return (bLastConversionSuccessful = convertValue<orxonox::Quaternion , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
127        virtual inline bool setValue(const orxonox::Radian& value)      override { return (bLastConversionSuccessful = convertValue<orxonox::Radian     , T>(&value_, value, NilValue<T>())); } ///< Assigns the value by converting it to T.
128        virtual inline bool setValue(const orxonox::Degree& value)      override { 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        virtual inline void toString(std::ostream& outstream) const override { 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        virtual inline void importData( uint8_t*& mem ) override         { 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        virtual inline void exportData( uint8_t*& mem ) const override   { saveAndIncrease( /*(const T&)*/this->value_, mem ); }
137        /// returns the size of the data that would be saved by exportData
138        virtual inline uint8_t getSize() const override { 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.