[6131] | 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 | |
---|
[7266] | 37 | #include "network/NetworkPrereqs.h" |
---|
| 38 | |
---|
| 39 | #include <loki/TypeTraits.h> |
---|
| 40 | |
---|
[6131] | 41 | #include "util/Serialise.h" |
---|
| 42 | #include "core/CorePrereqs.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox{ |
---|
[7284] | 45 | |
---|
[6131] | 46 | // These functions implement loading / saving / etc. for pointer types |
---|
[7284] | 47 | |
---|
[6131] | 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 | } |
---|
[7284] | 53 | |
---|
[6131] | 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 | } |
---|
[7284] | 60 | |
---|
[6131] | 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 | } |
---|
[7284] | 70 | |
---|
[6131] | 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 |
---|
[7163] | 77 | return variable == variable->getSynchronisable(*(uint32_t*)(mem)); |
---|
| 78 | } |
---|
[7284] | 79 | |
---|
[10624] | 80 | // These functions implement loading / saving / etc. for StrongPtr<T> |
---|
[7284] | 81 | |
---|
[7163] | 82 | /** @brief returns the size of the objectID needed to synchronise the pointer */ |
---|
[10624] | 83 | template <class T> inline uint32_t returnSize( const StrongPtr<T>& ) |
---|
[7163] | 84 | { |
---|
| 85 | return sizeof(uint32_t); |
---|
| 86 | } |
---|
[7284] | 87 | |
---|
[7163] | 88 | /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ |
---|
[10624] | 89 | template <class T> inline void loadAndIncrease( const StrongPtr<T>& variable, uint8_t*& mem ) |
---|
[7163] | 90 | { |
---|
| 91 | // *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); |
---|
[10624] | 92 | *const_cast<typename Loki::TypeTraits<StrongPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); |
---|
[7163] | 93 | mem += returnSize( variable ); |
---|
| 94 | } |
---|
[7284] | 95 | |
---|
[7163] | 96 | /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ |
---|
[10624] | 97 | template <class T> inline void saveAndIncrease( const StrongPtr<T>& variable, uint8_t*& mem ) |
---|
[7163] | 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 | } |
---|
[7284] | 105 | |
---|
[7163] | 106 | /** @brief checks whether the objectID of the variable is the same as in the bytestream */ |
---|
[10624] | 107 | template <class T> inline bool checkEquality( const StrongPtr<T>& variable, uint8_t* mem ) |
---|
[7163] | 108 | { |
---|
| 109 | if ( variable.get() ) |
---|
| 110 | return *(uint32_t*)(mem) == variable->getObjectID(); |
---|
| 111 | else |
---|
[6131] | 112 | return *(uint32_t*)(mem) == OBJECTID_UNKNOWN; |
---|
| 113 | } |
---|
[7284] | 114 | |
---|
[7163] | 115 | // These functions implement loading / saving / etc. for WeakPtr<T> |
---|
[7284] | 116 | |
---|
[7163] | 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 | } |
---|
[7284] | 122 | |
---|
[7163] | 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) )); |
---|
[10624] | 127 | *const_cast<typename Loki::TypeTraits<WeakPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); |
---|
[7163] | 128 | mem += returnSize( variable ); |
---|
| 129 | } |
---|
[7284] | 130 | |
---|
[7163] | 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 | } |
---|
[7284] | 140 | |
---|
[7163] | 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 | } |
---|
[6131] | 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | #endif |
---|