[Orxonox-commit 186] r2861 - in branches/netp2/src: network/synchronisable util
scheusso at orxonox.net
scheusso at orxonox.net
Sat Mar 28 23:19:06 CET 2009
Author: scheusso
Date: 2009-03-28 22:19:06 +0000 (Sat, 28 Mar 2009)
New Revision: 2861
Added:
branches/netp2/src/util/Serialise.h
Modified:
branches/netp2/src/network/synchronisable/SynchronisableSpecialisations.cc
branches/netp2/src/network/synchronisable/SynchronisableVariable.h
branches/netp2/src/util/MultiType.h
branches/netp2/src/util/MultiTypeValue.h
Log:
- moved serialise functions from SynchronisableVariableSpecialisation.cc
to util/Serialise.h
- made MultiType serialisable (use functions importData/exportData or
operator << / >> )
Modified: branches/netp2/src/network/synchronisable/SynchronisableSpecialisations.cc
===================================================================
--- branches/netp2/src/network/synchronisable/SynchronisableSpecialisations.cc 2009-03-27 18:11:30 UTC (rev 2860)
+++ branches/netp2/src/network/synchronisable/SynchronisableSpecialisations.cc 2009-03-28 22:19:06 UTC (rev 2861)
@@ -70,10 +70,10 @@
registerVariable(variable.x, mode, cb, bidirectional);
registerVariable(variable.y, mode, cb, bidirectional);
}
- template <> void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional)
- {
- registerVariable( (const ColourValue&)variable, mode, cb, bidirectional);
- }
+// template <> void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional)
+// {
+// registerVariable( (const ColourValue&)variable, mode, cb, bidirectional);
+// }
template <> void Synchronisable::registerVariable( const Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional)
{
Modified: branches/netp2/src/network/synchronisable/SynchronisableVariable.h
===================================================================
--- branches/netp2/src/network/synchronisable/SynchronisableVariable.h 2009-03-27 18:11:30 UTC (rev 2860)
+++ branches/netp2/src/network/synchronisable/SynchronisableVariable.h 2009-03-28 22:19:06 UTC (rev 2861)
@@ -20,7 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author:
- * Oliver Scheuss, (C) 2008
+ * Oliver Scheuss
* Co-authors:
* ...
*
@@ -34,6 +34,7 @@
#include <string>
#include <cassert>
+#include "util/Serialise.h"
#include "core/Core.h"
#include "core/CoreIncludes.h"
#include "network/synchronisable/NetworkCallback.h"
@@ -78,10 +79,10 @@
virtual inline uint32_t getSize(uint8_t mode);
virtual inline void* getReference(){ return (void *)&this->variable_; }
protected:
- inline bool checkEquality(uint8_t* mem);
- inline void setAndIncrease(uint8_t*& mem);
- inline void getAndIncrease(uint8_t*& mem);
- inline uint32_t returnSize();
+// inline bool checkEquality(uint8_t* mem);
+// inline void loadAndIncrease(uint8_t*& mem);
+// inline void saveAndIncrease(uint8_t*& mem);
+// inline uint32_t returnSize();
T& variable_;
uint8_t mode_;
@@ -125,8 +126,8 @@
{
if ( state_ == this->mode_ )
{
- getAndIncrease( mem );
- return returnSize();
+ saveAndIncrease( this->variable_, mem );
+ return returnSize( this->variable_ );
}
else
return 0;
@@ -142,11 +143,11 @@
// check whether we need to consider a callback
if ( this->callback_ != 0 )
{
- if( forceCallback || !checkEquality( mem ) )
+ if( forceCallback || !checkEquality( this->variable_, mem ) )
callback = true;
}
// write the data
- setAndIncrease( mem );
+ loadAndIncrease( this->variable_, mem );
// now do a callback if neccessary
if ( callback )
NetworkCallbackManager::triggerCallback( this->callback_ );
@@ -155,71 +156,71 @@
template <class T> inline uint32_t SynchronisableVariable<T>::getSize(uint8_t mode)
{
if ( mode == this->mode_ )
- return returnSize();
+ return returnSize( this->variable_ );
else
return 0;
}
- template <> _NetworkExport uint32_t SynchronisableVariable<const bool>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const bool>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const bool>::getAndIncrease(uint8_t*& mem);
+ /*template <> _NetworkExport uint32_t SynchronisableVariable<const bool>::returnSize();
+ template <> _NetworkExport void SynchronisableVariable<const bool>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const bool>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const bool>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned char>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const unsigned char>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const unsigned char>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned char>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned char>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const unsigned char>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const short>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const short>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const short>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const short>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const short>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const short>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned short>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const unsigned short>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const unsigned short>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned short>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned short>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const unsigned short>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const int>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const int>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const int>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const int>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const int>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const int>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned int>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const unsigned int>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const unsigned int>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned int>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned int>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const unsigned int>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const long>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const long>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const long>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const long>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned long>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const unsigned long>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const unsigned long>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned long>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned long>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const unsigned long>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const long long>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const long long>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const long long>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long long>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long long>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const long long>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const unsigned long long>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const unsigned long long>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const unsigned long long>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const float>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const float>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const float>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const float>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const float>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const float>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const double>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const double>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const double>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const double>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const double>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const double>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const long double>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const long double>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const long double>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long double>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const long double>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const long double>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const std::string>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const std::string>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const std::string>::getAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const std::string>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const std::string>::saveAndIncrease(uint8_t*& mem);
template <> _NetworkExport bool SynchronisableVariable<const std::string>::checkEquality(uint8_t* mem);
template <> _NetworkExport uint32_t SynchronisableVariable<const Degree>::returnSize();
- template <> _NetworkExport void SynchronisableVariable<const Degree>::setAndIncrease(uint8_t*& mem);
- template <> _NetworkExport void SynchronisableVariable<const Degree>::getAndIncrease(uint8_t*& mem);
- template <> _NetworkExport bool SynchronisableVariable<const Degree>::checkEquality(uint8_t* mem);
+ template <> _NetworkExport void SynchronisableVariable<const Degree>::loadAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport void SynchronisableVariable<const Degree>::saveAndIncrease(uint8_t*& mem);
+ template <> _NetworkExport bool SynchronisableVariable<const Degree>::checkEquality(uint8_t* mem);*/
@@ -248,7 +249,7 @@
*static_cast<uint8_t*>(mem) = varReference_;
mem += sizeof(this->varReference_);
// now write the content
- SynchronisableVariable<T>::getAndIncrease( mem );
+ saveAndIncrease( this->variable_, mem );
// mem += SynchronisableVariable<T>::getSize();
return SynchronisableVariableBidirectional::getSize(mode);
}
@@ -267,9 +268,9 @@
else{
// apply data
mem += sizeof(varReference_);
- if ( SynchronisableVariableBidirectional<T>::checkEquality( mem )==true )
+ if ( checkEquality( this->variable_, mem )==true )
{
- mem += SynchronisableVariable<T>::getSize( mode );
+ mem += getSize( mode );
return;
}
else
@@ -291,7 +292,7 @@
{
this->varReference_ = *static_cast<uint8_t*>(mem);
mem += sizeof(varReference_);
- if ( SynchronisableVariable<T>::checkEquality( mem ) == false )
+ if ( checkEquality( this->variable_, mem ) == false )
{
// value changed so remark for callback
if ( this->callback_ != 0 )
@@ -300,7 +301,7 @@
}
}
// now write the data
- SynchronisableVariable<T>::setAndIncrease(mem);
+ loadAndIncrease(this->variable_, mem);
// now do a callback if neccessary
if ( callback )
NetworkCallbackManager::triggerCallback( this->callback_ );
@@ -309,12 +310,12 @@
template <class T> inline uint32_t SynchronisableVariableBidirectional<T>::getSize(uint8_t mode)
{
- return SynchronisableVariable<T>::returnSize() + sizeof(varReference_);
+ return returnSize( this->variable_ ) + sizeof(varReference_);
}
}
-#include "network/synchronisable/SynchronisableVariableSpecialisations.h"
+//#include "network/synchronisable/SynchronisableVariableSpecialisations.h"
#endif
Modified: branches/netp2/src/util/MultiType.h
===================================================================
--- branches/netp2/src/util/MultiType.h 2009-03-27 18:11:30 UTC (rev 2860)
+++ branches/netp2/src/util/MultiType.h 2009-03-28 22:19:06 UTC (rev 2861)
@@ -222,6 +222,10 @@
virtual operator orxonox::Degree() const = 0;
virtual void toString(std::ostream& outstream) const = 0;
+
+ virtual void importData( uint8_t*& mem )=0;
+ virtual void exportData( uint8_t*& mem ) const=0;
+ virtual uint8_t getSize() const=0;
MT_Type type_; //!< The type of the current value
bool bHasDefaultValue_; //!< True if the last conversion wasn't successful
@@ -319,6 +323,15 @@
/** @brief Returns true if the current type is T. */
template <typename T> inline bool isType() const { return false; } // Only works for specialized values - see below
std::string getTypename() const;
+
+ /** @brief Saves the value of the MT to a bytestream (pointed at by mem) and increases mem pointer by size of MT */
+ inline void importData(uint8_t*& mem) { assert(sizeof(MT_Type)<=8); *(uint8_t*)(mem) = this->getType(); mem+=sizeof(uint8_t); this->value_->importData(mem); }
+ /** @brief Loads the value of the MT from a bytestream (pointed at by mem) and increases mem pointer by size of MT */
+ inline void exportData(uint8_t*& mem) { assert(sizeof(MT_Type)<=8); this->setType(*(uint8_t*)mem); mem+=sizeof(uint8_t); this->value_->exportData(mem); }
+ /** @brief Saves the value of the MT to a bytestream and increases pointer to bytestream by size of MT */
+ inline uint8_t*& operator << (uint8_t*& mem) { importData(mem); return mem; }
+ /** @brief Loads the value of the MT to a bytestream and increases pointer to bytestream by size of MT */
+ inline void operator >> (uint8_t*& mem) { exportData(mem); }
/** @brief Checks whether the value is a default one. */
bool hasDefaultValue() const { return this->value_->hasDefaultValue(); }
Modified: branches/netp2/src/util/MultiTypeValue.h
===================================================================
--- branches/netp2/src/util/MultiTypeValue.h 2009-03-27 18:11:30 UTC (rev 2860)
+++ branches/netp2/src/util/MultiTypeValue.h 2009-03-28 22:19:06 UTC (rev 2861)
@@ -39,6 +39,8 @@
#include "UtilPrereqs.h"
#include "MathConvert.h"
#include "MultiType.h"
+#include "Serialise.h"
+#include <cassert>
namespace orxonox
{
@@ -146,9 +148,118 @@
/** @brief Puts the current value on the stream */
inline void toString(std::ostream& outstream) const { outstream << this->value_; }
+
+ /** @brief loads data from the bytestream (mem) into the MT and increases the bytestream pointer by the size of the data */
+ inline void importData( uint8_t*& mem ) { loadAndIncrease( /*(const T&)*/this->value_, mem ); }
+ /** @brief saves data from the MT into the bytestream (mem) and increases the bytestream pointer by the size of the data */
+ inline void exportData( uint8_t*& mem ) const { saveAndIncrease( /*(const T&)*/this->value_, mem ); }
+ /** @brief returns the size of the data that would be saved by exportData */
+ inline uint8_t getSize() const { return returnSize( this->value_ ); }
T value_; //!< The stored value
};
+
+ // Import / Export specialisation
+ // ColourValue
+ template <> inline void MT_Value<ColourValue>::importData( uint8_t*& mem )
+ {
+ loadAndIncrease( this->value_.r, mem );
+ loadAndIncrease( this->value_.g, mem );
+ loadAndIncrease( this->value_.b, mem );
+ loadAndIncrease( this->value_.a, mem );
+ }
+ template <> inline void MT_Value<ColourValue>::exportData( uint8_t*& mem ) const
+ {
+ saveAndIncrease( this->value_.r, mem );
+ saveAndIncrease( this->value_.g, mem );
+ saveAndIncrease( this->value_.b, mem );
+ saveAndIncrease( this->value_.a, mem );
+ }
+ template <> inline uint8_t MT_Value<ColourValue>::getSize() const
+ {
+ return 4*returnSize(this->value_.r);
+ }
+ // Ogre::Quaternion
+ template <> inline void MT_Value<Ogre::Quaternion>::importData( uint8_t*& mem )
+ {
+ loadAndIncrease( this->value_.x, mem );
+ loadAndIncrease( this->value_.y, mem );
+ loadAndIncrease( this->value_.z, mem );
+ loadAndIncrease( this->value_.w, mem );
+ }
+ template <> inline void MT_Value<Ogre::Quaternion>::exportData( uint8_t*& mem ) const
+ {
+ saveAndIncrease( this->value_.x, mem );
+ saveAndIncrease( this->value_.y, mem );
+ saveAndIncrease( this->value_.z, mem );
+ saveAndIncrease( this->value_.w, mem );
+ }template <> inline uint8_t MT_Value<Ogre::Quaternion>::getSize() const
+ {
+ return 4*returnSize(this->value_.x);
+ }
+ // Ogre::Vector2
+ template <> inline void MT_Value<Ogre::Vector2>::importData( uint8_t*& mem )
+ {
+ loadAndIncrease( this->value_.x, mem );
+ loadAndIncrease( this->value_.y, mem );
+ }
+ template <> inline void MT_Value<Ogre::Vector2>::exportData( uint8_t*& mem ) const
+ {
+ saveAndIncrease( this->value_.x, mem );
+ saveAndIncrease( this->value_.y, mem );
+ }
+ template <> inline uint8_t MT_Value<Ogre::Vector2>::getSize() const
+ {
+ return 2*returnSize(this->value_.x);
+ }
+ // Ogre::Vector3
+ template <> inline void MT_Value<Ogre::Vector3>::importData( uint8_t*& mem )
+ {
+ loadAndIncrease( this->value_.x, mem );
+ loadAndIncrease( this->value_.y, mem );
+ loadAndIncrease( this->value_.z, mem );
+ }
+ template <> inline void MT_Value<Ogre::Vector3>::exportData( uint8_t*& mem ) const
+ {
+ saveAndIncrease( this->value_.x, mem );
+ saveAndIncrease( this->value_.y, mem );
+ saveAndIncrease( this->value_.z, mem );
+ }
+ template <> inline uint8_t MT_Value<Ogre::Vector3>::getSize() const
+ {
+ return 3*returnSize(this->value_.x);
+ }
+ // Ogre::Vector4
+ template <> inline void MT_Value<Ogre::Vector4>::importData( uint8_t*& mem )
+ {
+ loadAndIncrease( this->value_.x, mem );
+ loadAndIncrease( this->value_.y, mem );
+ loadAndIncrease( this->value_.z, mem );
+ loadAndIncrease( this->value_.w, mem );
+ }
+ template <> inline void MT_Value<Ogre::Vector4>::exportData( uint8_t*& mem ) const
+ {
+ saveAndIncrease( this->value_.x, mem );
+ saveAndIncrease( this->value_.y, mem );
+ saveAndIncrease( this->value_.z, mem );
+ saveAndIncrease( this->value_.w, mem );
+ }
+ template <> inline uint8_t MT_Value<Ogre::Vector4>::getSize() const
+ {
+ return 4*returnSize(this->value_.x);
+ }
+ template <> inline void MT_Value<void*>::importData( uint8_t*& mem )
+ {
+ assert(0);
+ }
+ template <> inline void MT_Value<void*>::exportData( uint8_t*& mem ) const
+ {
+ assert(0);
+ }
+ template <> inline uint8_t MT_Value<void*>::getSize() const
+ {
+ assert(0); return 0;
+ }
}
#endif /* _MultiTypeValue_H__ */
Added: branches/netp2/src/util/Serialise.h
===================================================================
--- branches/netp2/src/util/Serialise.h (rev 0)
+++ branches/netp2/src/util/Serialise.h 2009-03-28 22:19:06 UTC (rev 2861)
@@ -0,0 +1,477 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Oliver Scheuss
+ * Co-authors:
+ * ...
+ *
+ */
+
+/**
+ @file
+ @brief Functions to serialise most of the types/classed used in Orxonox
+*/
+
+#ifndef _Serialise_H__
+#define _Serialise_H__
+
+#include <cstring>
+#include "util/Math.h"
+
+namespace orxonox{
+
+// general template declaration
+
+ /** @brief returns the size of the variable in a datastream */
+ template <class T> inline uint32_t returnSize( const T& );
+ /** @brief loads the value of a variable out of the bytestream and increases the mem pointer */
+ template <class T> inline void loadAndIncrease( const T&, uint8_t*& );
+ /** @brief saves the value of a variable into the bytestream and increases the mem pointer */
+ template <class T> inline void saveAndIncrease( const T&, uint8_t*& );
+ /** @brief checks whether the variable of type T is the same as in the bytestream */
+ template <class T> inline bool checkEquality( const T&, uint8_t* );
+
+// =================== Template specialisation stuff =============
+
+// =========== bool
+
+ template <> inline uint32_t returnSize( const bool& variable )
+ {
+ return sizeof(uint8_t);
+ }
+
+ template <> inline void loadAndIncrease( const bool& variable, uint8_t*& mem )
+ {
+ *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const bool& variable, uint8_t*& mem )
+ {
+ *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const bool& variable, uint8_t* mem )
+ {
+ return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
+ }
+
+// =========== char
+
+ template <> inline uint32_t returnSize( const char& variable )
+ {
+ return sizeof(uint8_t);
+ }
+
+ template <> inline void loadAndIncrease( const char& variable, uint8_t*& mem )
+ {
+ *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const char& variable, uint8_t*& mem )
+ {
+ *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const char& variable, uint8_t* mem )
+ {
+ return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
+ }
+
+// =========== unsigned char
+
+ template <> inline uint32_t returnSize( const unsigned char& variable )
+ {
+ return sizeof(uint8_t);
+ }
+
+ template <> inline void loadAndIncrease( const unsigned char& variable, uint8_t*& mem )
+ {
+ *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const unsigned char& variable, uint8_t*& mem )
+ {
+ *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const unsigned char& variable, uint8_t* mem )
+ {
+ return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
+ }
+
+// =========== short
+
+ template <> inline uint32_t returnSize( const short& variable )
+ {
+ return sizeof(int16_t);
+ }
+
+ template <> inline void loadAndIncrease( const short& variable, uint8_t*& mem )
+ {
+ *(short*)( &variable ) = *(int16_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const short& variable, uint8_t*& mem )
+ {
+ *(int16_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const short& variable, uint8_t* mem )
+ {
+ return *(int16_t*)(mem) == static_cast<int16_t>(variable);
+ }
+
+// =========== unsigned short
+
+ template <> inline uint32_t returnSize( const unsigned short& variable )
+ {
+ return sizeof(uint16_t);
+ }
+
+ template <> inline void loadAndIncrease( const unsigned short& variable, uint8_t*& mem )
+ {
+ *(unsigned short*)( &variable ) = *(uint16_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const unsigned short& variable, uint8_t*& mem )
+ {
+ *(uint16_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const unsigned short& variable, uint8_t* mem )
+ {
+ return *(uint16_t*)(mem) == variable;
+ }
+
+// =========== int
+
+ template <> inline uint32_t returnSize( const int& variable )
+ {
+ return sizeof(int32_t);
+ }
+
+ template <> inline void loadAndIncrease( const int& variable, uint8_t*& mem )
+ {
+ *(int *)( &variable ) = *(int32_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const int& variable, uint8_t*& mem )
+ {
+ *(int32_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const int& variable, uint8_t* mem )
+ {
+ return *(int32_t*)(mem) == variable;
+ }
+
+// =========== unsigned int
+
+ template <> inline uint32_t returnSize( const unsigned int& variable )
+ {
+ return sizeof(uint32_t);
+ }
+
+ template <> inline void loadAndIncrease( const unsigned int& variable, uint8_t*& mem )
+ {
+ *(unsigned int*)( &variable ) = *(uint32_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const unsigned int& variable, uint8_t*& mem )
+ {
+ *(uint32_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const unsigned int& variable, uint8_t* mem )
+ {
+ return *(uint32_t*)(mem) == variable;
+ }
+
+// =========== long
+
+ template <> inline uint32_t returnSize( const long& variable )
+ {
+ return sizeof(int32_t);
+ }
+
+ template <> inline void loadAndIncrease( const long& variable, uint8_t*& mem )
+ {
+ *(long*)( &variable ) = *(int32_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const long& variable, uint8_t*& mem )
+ {
+ *(int32_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const long& variable, uint8_t* mem )
+ {
+ return *(int32_t*)(mem) == variable;
+ }
+
+// =========== unsigned long
+
+ template <> inline uint32_t returnSize( const unsigned long& variable )
+ {
+ return sizeof(uint32_t);
+ }
+
+ template <> inline void loadAndIncrease( const unsigned long& variable, uint8_t*& mem )
+ {
+ *(unsigned long*)( &variable ) = *(uint32_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const unsigned long& variable, uint8_t*& mem )
+ {
+ *(uint32_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const unsigned long& variable, uint8_t* mem )
+ {
+ return *(uint32_t*)(mem) == variable;
+ }
+
+// =========== long long
+
+ template <> inline uint32_t returnSize( const long long& variable )
+ {
+ return sizeof(int64_t);
+ }
+
+ template <> inline void loadAndIncrease( const long long& variable, uint8_t*& mem )
+ {
+ *(long long*)( &variable ) = *(int64_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const long long& variable, uint8_t*& mem )
+ {
+ *(int64_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const long long& variable, uint8_t* mem )
+ {
+ return *(int64_t*)(mem) == variable;
+ }
+
+// =========== unsigned long long
+
+ template <> inline uint32_t returnSize( const unsigned long long& variable )
+ {
+ return sizeof(uint64_t);
+ }
+
+ template <> inline void loadAndIncrease( const unsigned long long& variable, uint8_t*& mem )
+ {
+ *(unsigned long long*)( &variable ) = *(uint64_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const unsigned long long& variable, uint8_t*& mem )
+ {
+ *(uint64_t*)(mem) = variable;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const unsigned long long& variable, uint8_t* mem )
+ {
+ return *(uint64_t*)(mem) == variable;
+ }
+
+// =========== float
+
+ template <> inline uint32_t returnSize( const float& variable )
+ {
+ return sizeof(uint32_t);
+ }
+
+ template <> inline void loadAndIncrease( const float& variable, uint8_t*& mem )
+ {
+ *(uint32_t*)( &variable ) = *(uint32_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const float& variable, uint8_t*& mem )
+ {
+ *(uint32_t*)(mem) = *(uint32_t*)( &variable );
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const float& variable, uint8_t* mem )
+ {
+ return *(uint32_t*)(mem) == *(uint32_t*)( &variable );
+ }
+
+// =========== double
+
+ template <> inline uint32_t returnSize( const double& variable )
+ {
+ return sizeof(uint64_t);
+ }
+
+ template <> inline void loadAndIncrease( const double& variable, uint8_t*& mem )
+ {
+ *(uint64_t*)( &variable ) = *(uint64_t*)(mem);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const double& variable, uint8_t*& mem )
+ {
+ *(uint64_t*)(mem) = *(uint64_t*)( &variable );
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const double& variable, uint8_t* mem )
+ {
+ return *(uint64_t*)(mem) == *(uint64_t*)( &variable );
+ }
+
+// =========== long double
+
+ template <> inline uint32_t returnSize( const long double& variable )
+ {
+ return sizeof(uint64_t);
+ }
+
+ template <> inline void loadAndIncrease( const long double& variable, uint8_t*& mem )
+ {
+ double temp;
+ memcpy(&temp, mem, sizeof(uint64_t));
+ *(long double*)( &variable ) = static_cast<const long double>(temp);
+ mem += returnSize( variable );
+ }
+
+ template <> inline void saveAndIncrease( const long double& variable, uint8_t*& mem )
+ {
+ double temp = static_cast<double>(variable);
+ memcpy(mem, &temp, sizeof(uint64_t));
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const long double& variable, uint8_t* mem )
+ {
+ double temp = static_cast<double>(variable);
+ return memcmp(&temp, mem, sizeof(uint64_t))==0;
+ }
+
+// =========== string
+
+ template <> inline uint32_t returnSize( const std::string& variable )
+ {
+ return variable.length()+1;
+ }
+
+ template <> inline void saveAndIncrease( const std::string& variable, uint8_t*& mem )
+ {
+ memcpy(mem, variable.c_str(), variable.length()+1);
+ mem += variable.length()+1;
+ }
+
+ template <> inline void loadAndIncrease( const std::string& variable, uint8_t*& mem )
+ {
+ *(std::string*)( &variable ) = std::string((const char *)mem);
+ mem += variable.length()+1;
+ }
+
+ template <> inline bool checkEquality( const std::string& variable, uint8_t* mem )
+ {
+ return std::string((const char*)mem)==variable;
+ }
+
+// =========== Degree
+
+ template <> inline uint32_t returnSize( const Degree& variable )
+ {
+ return sizeof(Ogre::Real);
+ }
+
+ template <> inline void saveAndIncrease( const Degree& variable, uint8_t*& mem )
+ {
+ Ogre::Real r = variable.valueDegrees();
+ memcpy(mem, &r, returnSize( variable ));
+ mem += returnSize( variable );
+ }
+
+ template <> inline void loadAndIncrease( const Degree& variable, uint8_t*& mem )
+ {
+ Ogre::Real* r = (Ogre::Real*)mem;
+ (Degree&)variable = *r;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const Degree& variable, uint8_t* mem )
+ {
+ Ogre::Real* r = (Ogre::Real*)mem;
+ return variable==Degree(*r);
+ }
+
+// =========== Radian
+
+ template <> inline uint32_t returnSize( const Radian& variable )
+ {
+ return sizeof(Ogre::Real);
+ }
+
+ template <> inline void saveAndIncrease( const Radian& variable, uint8_t*& mem )
+ {
+ Ogre::Real r = variable.valueRadians();
+ memcpy(mem, &r, returnSize( variable ));
+ mem += returnSize( variable );
+ }
+
+ template <> inline void loadAndIncrease( const Radian& variable, uint8_t*& mem )
+ {
+ Ogre::Real* r = (Ogre::Real*)mem;
+ (Radian&)variable = *r;
+ mem += returnSize( variable );
+ }
+
+ template <> inline bool checkEquality( const Radian& variable, uint8_t* mem )
+ {
+ Ogre::Real* r = (Ogre::Real*)mem;
+ return variable==Degree(*r);
+ }
+
+
+}
+
+
+#endif
More information about the Orxonox-commit
mailing list