| 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 |  *      Oliver Scheuss | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |     @file | 
|---|
| 31 |     @brief Functions to serialise pointers to objects that inherit from Synchronisable | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #ifndef _NetworkSerialise_H__ | 
|---|
| 35 | #define _NetworkSerialise_H__ | 
|---|
| 36 |  | 
|---|
| 37 | #include "network/NetworkPrereqs.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include <loki/TypeTraits.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include "util/Serialise.h" | 
|---|
| 42 | #include "core/CorePrereqs.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox{ | 
|---|
| 45 |  | 
|---|
| 46 |     // These functions implement loading / saving / etc. for pointer types | 
|---|
| 47 |  | 
|---|
| 48 |     /** @brief returns the size of the objectID needed to synchronise the pointer */ | 
|---|
| 49 |     template <class T> inline uint32_t returnSize( T*& variable ) | 
|---|
| 50 |     { | 
|---|
| 51 |       return sizeof(uint32_t); | 
|---|
| 52 |     } | 
|---|
| 53 |  | 
|---|
| 54 |     /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ | 
|---|
| 55 |     template <class T> inline void loadAndIncrease( T*& variable, uint8_t*& mem ) | 
|---|
| 56 |     { | 
|---|
| 57 |         *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); | 
|---|
| 58 |         mem += returnSize( variable ); | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |     /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ | 
|---|
| 62 |     template <class T> inline void saveAndIncrease( T*& variable, uint8_t*& mem ) | 
|---|
| 63 |     { | 
|---|
| 64 |         if ( variable ) | 
|---|
| 65 |             *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); | 
|---|
| 66 |         else | 
|---|
| 67 |             *(uint32_t*)(mem) = OBJECTID_UNKNOWN; | 
|---|
| 68 |         mem += returnSize( variable ); | 
|---|
| 69 |     } | 
|---|
| 70 |  | 
|---|
| 71 |     /** @brief checks whether the objectID of the variable is the same as in the bytestream */ | 
|---|
| 72 |     template <class T> inline  bool checkEquality( T*& variable, uint8_t* mem ) | 
|---|
| 73 |     { | 
|---|
| 74 |         if ( variable ) | 
|---|
| 75 |             return *(uint32_t*)(mem) == variable->getObjectID(); | 
|---|
| 76 |         else | 
|---|
| 77 |             return variable == variable->getSynchronisable(*(uint32_t*)(mem)); | 
|---|
| 78 |     } | 
|---|
| 79 |  | 
|---|
| 80 |     // These functions implement loading / saving / etc. for StrongPtr<T> | 
|---|
| 81 |  | 
|---|
| 82 |     /** @brief returns the size of the objectID needed to synchronise the pointer */ | 
|---|
| 83 |     template <class T> inline uint32_t returnSize( const StrongPtr<T>& ) | 
|---|
| 84 |     { | 
|---|
| 85 |         return sizeof(uint32_t); | 
|---|
| 86 |     } | 
|---|
| 87 |  | 
|---|
| 88 |     /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ | 
|---|
| 89 |     template <class T> inline void loadAndIncrease( const StrongPtr<T>& variable, uint8_t*& mem ) | 
|---|
| 90 |     { | 
|---|
| 91 | //         *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); | 
|---|
| 92 |         *const_cast<typename Loki::TypeTraits<StrongPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); | 
|---|
| 93 |         mem += returnSize( variable ); | 
|---|
| 94 |     } | 
|---|
| 95 |  | 
|---|
| 96 |     /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ | 
|---|
| 97 |     template <class T> inline void saveAndIncrease( const StrongPtr<T>& variable, uint8_t*& mem ) | 
|---|
| 98 |     { | 
|---|
| 99 |         if ( variable.get() ) | 
|---|
| 100 |             *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); | 
|---|
| 101 |         else | 
|---|
| 102 |             *(uint32_t*)(mem) = OBJECTID_UNKNOWN; | 
|---|
| 103 |         mem += returnSize( variable ); | 
|---|
| 104 |     } | 
|---|
| 105 |  | 
|---|
| 106 |     /** @brief checks whether the objectID of the variable is the same as in the bytestream */ | 
|---|
| 107 |     template <class T> inline  bool checkEquality( const StrongPtr<T>& variable, uint8_t* mem ) | 
|---|
| 108 |     { | 
|---|
| 109 |         if ( variable.get() ) | 
|---|
| 110 |             return *(uint32_t*)(mem) == variable->getObjectID(); | 
|---|
| 111 |         else | 
|---|
| 112 |             return *(uint32_t*)(mem) == OBJECTID_UNKNOWN; | 
|---|
| 113 |     } | 
|---|
| 114 |  | 
|---|
| 115 |     // These functions implement loading / saving / etc. for WeakPtr<T> | 
|---|
| 116 |  | 
|---|
| 117 |     /** @brief returns the size of the objectID needed to synchronise the pointer */ | 
|---|
| 118 |     template <class T> inline uint32_t returnSize( const WeakPtr<T>& variable ) | 
|---|
| 119 |     { | 
|---|
| 120 |         return sizeof(uint32_t); | 
|---|
| 121 |     } | 
|---|
| 122 |  | 
|---|
| 123 |     /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ | 
|---|
| 124 |     template <class T> inline void loadAndIncrease( const WeakPtr<T>& variable, uint8_t*& mem ) | 
|---|
| 125 |     { | 
|---|
| 126 |         //         *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); | 
|---|
| 127 |         *const_cast<typename Loki::TypeTraits<WeakPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); | 
|---|
| 128 |         mem += returnSize( variable ); | 
|---|
| 129 |     } | 
|---|
| 130 |  | 
|---|
| 131 |     /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ | 
|---|
| 132 |     template <class T> inline void saveAndIncrease( const WeakPtr<T>& variable, uint8_t*& mem ) | 
|---|
| 133 |     { | 
|---|
| 134 |         if ( variable.get() ) | 
|---|
| 135 |             *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); | 
|---|
| 136 |         else | 
|---|
| 137 |             *(uint32_t*)(mem) = OBJECTID_UNKNOWN; | 
|---|
| 138 |         mem += returnSize( variable ); | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     /** @brief checks whether the objectID of the variable is the same as in the bytestream */ | 
|---|
| 142 |     template <class T> inline  bool checkEquality( const WeakPtr<T>& variable, uint8_t* mem ) | 
|---|
| 143 |     { | 
|---|
| 144 |         if ( variable.get() ) | 
|---|
| 145 |             return *(uint32_t*)(mem) == variable->getObjectID(); | 
|---|
| 146 |         else | 
|---|
| 147 |             return *(uint32_t*)(mem) == OBJECTID_UNKNOWN; | 
|---|
| 148 |     } | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 |  | 
|---|
| 152 | #endif | 
|---|