Orxonox  0.0.5 Codename: Arcturus
MultiTypeValue.h
Go to the documentation of this file.
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 
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 
47 namespace orxonox
48 {
52  template <typename T>
54  {
55  public:
57  MT_Value(const T& value, MultiType::Type type) : MT_ValueBase(&this->value_, type), value_(value) {}
58 
60  virtual inline MT_ValueBase* clone() const override { return new MT_Value<T>(this->value_, this->type_); }
61 
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); }
66  virtual inline bool getValue(unsigned char* value) const override { return convertValue<T, unsigned char >(value, value_, 0); }
67  virtual inline bool getValue(short* value) const override { return convertValue<T, short >(value, value_, 0); }
68  virtual inline bool getValue(unsigned short* value) const override { return convertValue<T, unsigned short >(value, value_, 0); }
69  virtual inline bool getValue(int* value) const override { return convertValue<T, int >(value, value_, 0); }
70  virtual inline bool getValue(unsigned int* value) const override { return convertValue<T, unsigned int >(value, value_, 0); }
71  virtual inline bool getValue(long* value) const override { return convertValue<T, long >(value, value_, 0); }
72  virtual inline bool getValue(unsigned long* value) const override { return convertValue<T, unsigned long >(value, value_, 0); }
73  virtual inline bool getValue(long long* value) const override { return convertValue<T, long long >(value, value_, 0); }
74  virtual inline bool getValue(unsigned long long* value) const override { return convertValue<T, unsigned long long >(value, value_, 0); }
75  virtual inline bool getValue(float* value) const override { return convertValue<T, float >(value, value_, 0); }
76  virtual inline bool getValue(double* value) const override { return convertValue<T, double >(value, value_, 0); }
77  virtual inline bool getValue(long double* value) const override { return convertValue<T, long double >(value, value_, 0); }
78  virtual inline bool getValue(bool* value) const override { return convertValue<T, bool >(value, value_, 0); }
79  virtual inline bool getValue(void** value) const override { return convertValue<T, void* >(value, value_, nullptr); }
80  virtual inline bool getValue(std::string* value) const override { return convertValue<T, std::string >(value, value_, NilValue<std::string> ()); }
81  virtual inline bool getValue(orxonox::Vector2* value) const override { return convertValue<T, orxonox::Vector2 >(value, value_, NilValue<orxonox::Vector2> ()); }
82  virtual inline bool getValue(orxonox::Vector3* value) const override { return convertValue<T, orxonox::Vector3 >(value, value_, NilValue<orxonox::Vector3> ()); }
83  virtual inline bool getValue(orxonox::Vector4* value) const override { return convertValue<T, orxonox::Vector4 >(value, value_, NilValue<orxonox::Vector4> ()); }
84  virtual inline bool getValue(orxonox::ColourValue* value) const override { return convertValue<T, orxonox::ColourValue>(value, value_, NilValue<orxonox::ColourValue>()); }
85  virtual inline bool getValue(orxonox::Quaternion* value) const override { return convertValue<T, orxonox::Quaternion >(value, value_, NilValue<orxonox::Quaternion> ()); }
86  virtual inline bool getValue(orxonox::Radian* value) const override { return convertValue<T, orxonox::Radian >(value, value_, NilValue<orxonox::Radian> ()); }
87  virtual inline bool getValue(orxonox::Degree* value) const override { return convertValue<T, orxonox::Degree >(value, value_, NilValue<orxonox::Degree> ()); }
88 
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>())); }
107  virtual inline bool setValue(const unsigned char& value) override { return (bLastConversionSuccessful = convertValue<unsigned char , T>(&value_, value, NilValue<T>())); }
108  virtual inline bool setValue(const short& value) override { return (bLastConversionSuccessful = convertValue<short , T>(&value_, value, NilValue<T>())); }
109  virtual inline bool setValue(const unsigned short& value) override { return (bLastConversionSuccessful = convertValue<unsigned short , T>(&value_, value, NilValue<T>())); }
110  virtual inline bool setValue(const int& value) override { return (bLastConversionSuccessful = convertValue<int , T>(&value_, value, NilValue<T>())); }
111  virtual inline bool setValue(const unsigned int& value) override { return (bLastConversionSuccessful = convertValue<unsigned int , T>(&value_, value, NilValue<T>())); }
112  virtual inline bool setValue(const long& value) override { return (bLastConversionSuccessful = convertValue<long , T>(&value_, value, NilValue<T>())); }
113  virtual inline bool setValue(const unsigned long& value) override { return (bLastConversionSuccessful = convertValue<unsigned long , T>(&value_, value, NilValue<T>())); }
114  virtual inline bool setValue(const long long& value) override { return (bLastConversionSuccessful = convertValue<long long , T>(&value_, value, NilValue<T>())); }
115  virtual inline bool setValue(const unsigned long long& value) override { return (bLastConversionSuccessful = convertValue<unsigned long long , T>(&value_, value, NilValue<T>())); }
116  virtual inline bool setValue(const float& value) override { return (bLastConversionSuccessful = convertValue<float , T>(&value_, value, NilValue<T>())); }
117  virtual inline bool setValue(const double& value) override { return (bLastConversionSuccessful = convertValue<double , T>(&value_, value, NilValue<T>())); }
118  virtual inline bool setValue(const long double& value) override { return (bLastConversionSuccessful = convertValue<long double , T>(&value_, value, NilValue<T>())); }
119  virtual inline bool setValue(const bool& value) override { return (bLastConversionSuccessful = convertValue<bool , T>(&value_, value, NilValue<T>())); }
120  virtual inline bool setValue( void* const& value) override { return (bLastConversionSuccessful = convertValue<void* , T>(&value_, value, NilValue<T>())); }
121  virtual inline bool setValue(const std::string& value) override { return (bLastConversionSuccessful = convertValue<std::string , T>(&value_, value, NilValue<T>())); }
122  virtual inline bool setValue(const orxonox::Vector2& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Vector2 , T>(&value_, value, NilValue<T>())); }
123  virtual inline bool setValue(const orxonox::Vector3& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Vector3 , T>(&value_, value, NilValue<T>())); }
124  virtual inline bool setValue(const orxonox::Vector4& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Vector4 , T>(&value_, value, NilValue<T>())); }
125  virtual inline bool setValue(const orxonox::ColourValue& value) override { return (bLastConversionSuccessful = convertValue<orxonox::ColourValue, T>(&value_, value, NilValue<T>())); }
126  virtual inline bool setValue(const orxonox::Quaternion& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Quaternion , T>(&value_, value, NilValue<T>())); }
127  virtual inline bool setValue(const orxonox::Radian& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Radian , T>(&value_, value, NilValue<T>())); }
128  virtual inline bool setValue(const orxonox::Degree& value) override { return (bLastConversionSuccessful = convertValue<orxonox::Degree , T>(&value_, value, NilValue<T>())); }
129 
131  virtual inline void toString(std::ostream& outstream) const override { outstream << this->value_; }
132 
134  virtual inline void importData( uint8_t*& mem ) override { loadAndIncrease( /*(const T&)*/this->value_, mem ); }
136  virtual inline void exportData( uint8_t*& mem ) const override { saveAndIncrease( /*(const T&)*/this->value_, mem ); }
138  virtual inline uint8_t getSize() const override { return returnSize( this->value_ ); }
139 
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__ */
virtual bool setValue(const unsigned long &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:113
virtual bool setValue(const bool &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:119
MT_ValueBase * value_
A pointer to the value container.
Definition: MultiType.h:490
bool bLastConversionSuccessful
True if the last conversion was successful.
Definition: MultiType.h:299
virtual bool setValue(const unsigned char &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:107
MT_Value(const T &value, MultiType::Type type)
Constructor: Assigns the value and the type identifier.
Definition: MultiTypeValue.h:57
virtual bool getValue(orxonox::ColourValue *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:84
virtual bool setValue(const unsigned int &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:111
virtual bool setValue(const int &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:110
virtual bool setValue(const float &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:116
MT_ValueBase(void *data, Type type)
Definition: MultiType.h:174
virtual bool getValue(std::string *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:80
virtual bool getValue(unsigned short *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:68
Functions to serialise most of the types/classed used in Orxonox.
Provides zero value symbols that can be returned as reference.
Definition: Math.h:223
virtual bool getValue(bool *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:78
virtual bool getValue(unsigned long *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:72
virtual bool setValue(const unsigned long long &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:115
virtual bool setValue(const char &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:106
virtual bool getValue(orxonox::Radian *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:86
virtual bool setValue(const orxonox::ColourValue &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:125
::std::string string
Definition: gtest-port.h:756
virtual bool setValue(const orxonox::Vector4 &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:124
uint32_t returnSize(T *&variable)
returns the size of the objectID needed to synchronise the pointer
Definition: Serialise.h:49
virtual bool getValue(float *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:75
virtual bool getValue(orxonox::Vector4 *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:83
virtual bool getValue(orxonox::Vector3 *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:82
virtual bool getValue(long *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:71
virtual bool setValue(const orxonox::Vector2 &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:122
virtual bool getValue(double *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:76
Type type_
The type of the current value.
Definition: MultiType.h:298
virtual bool setValue(const long double &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:118
virtual bool setValue(const short &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:108
virtual bool setValue(const orxonox::Vector3 &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:123
virtual bool setValue(const long long &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:114
virtual bool getValue(long long *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:73
virtual bool setValue(const std::string &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:121
Type
Enum of all possible types of a MultiType.
Definition: MultiType.h:138
virtual MT_ValueBase * clone() const override
Creates a copy of itself.
Definition: MultiTypeValue.h:60
virtual bool setValue(const orxonox::Degree &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:128
virtual void toString(std::ostream &outstream) const override
Puts the current value on the stream.
Definition: MultiTypeValue.h:131
virtual uint8_t getSize() const override
returns the size of the data that would be saved by exportData
Definition: MultiTypeValue.h:138
virtual bool getValue(long double *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:77
virtual void reset() override
Resets the current value to the default.
Definition: MultiTypeValue.h:63
virtual bool setValue(void *const &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:120
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Declaration of the MultiType and some helper constructs.
virtual bool getValue(orxonox::Quaternion *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:85
void saveAndIncrease(T *&variable, uint8_t *&mem)
saves the objectID of a pointer into the bytestream and increases the mem pointer ...
Definition: Serialise.h:62
Shared library macros, enums, constants and forward declarations for the util library ...
virtual bool getValue(void **value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:79
virtual bool getValue(char *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:65
virtual bool getValue(int *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:69
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
virtual bool getValue(unsigned char *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:66
virtual bool setValue(const MultiType &other) override
Assigns the value of the other MultiType, converted to T.
Definition: MultiTypeValue.h:93
Definition: InputPrereqs.h:78
virtual bool setValue(const unsigned short &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:109
virtual bool getValue(unsigned int *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:70
virtual bool getValue(orxonox::Degree *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:87
virtual bool getValue(unsigned long long *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:74
Conversion functions for Math types like Ogre::Vector3 (definitions are in Math.cc) ...
T value_
The stored value.
Definition: MultiTypeValue.h:140
virtual bool setValue(const orxonox::Quaternion &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:126
virtual bool getValue(short *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:67
The MT_Value<T> class is used to hold a value of type T within a MultiType.
Definition: MultiTypeValue.h:53
virtual bool setValue(const orxonox::Radian &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:127
virtual void importData(uint8_t *&mem) override
loads data from the bytestream (mem) into the MT and increases the bytestream pointer by the size of ...
Definition: MultiTypeValue.h:134
virtual bool getValue(orxonox::Vector2 *value) const override
Assigns the value to the given pointer. The value gets converted if the types don&#39;t match...
Definition: MultiTypeValue.h:81
virtual void exportData(uint8_t *&mem) const override
saves data from the MT into the bytestream (mem) and increases the bytestream pointer by the size of ...
Definition: MultiTypeValue.h:136
virtual bool setValue(const double &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:117
MT_ValueBase is an almost pure virtual baseclass of MT_Value<T>, which holds the value of the MultiTy...
Definition: MultiType.h:171
virtual bool getValue(char *value) const =0
void loadAndIncrease(T *&variable, uint8_t *&mem)
reads the objectID of a pointer out of the bytestream and increases the mem pointer ...
Definition: Serialise.h:55
virtual bool setValue(const long &value) override
Assigns the value by converting it to T.
Definition: MultiTypeValue.h:112