| [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" |
|---|
| [7163] | 43 | #include "core/CoreIncludes.h" |
|---|
| [7284] | 44 | #include "core/BaseObject.h" // remove this if circular dependencies in BaseObject/SmartPtr are fixed |
|---|
| 45 | //#include "core/SmartPtr.h" |
|---|
| [6131] | 46 | |
|---|
| 47 | namespace orxonox{ |
|---|
| [7284] | 48 | |
|---|
| [6131] | 49 | // These functions implement loading / saving / etc. for pointer types |
|---|
| [7284] | 50 | |
|---|
| [6131] | 51 | /** @brief returns the size of the objectID needed to synchronise the pointer */ |
|---|
| 52 | template <class T> inline uint32_t returnSize( T*& variable ) |
|---|
| 53 | { |
|---|
| 54 | return sizeof(uint32_t); |
|---|
| 55 | } |
|---|
| [7284] | 56 | |
|---|
| [6131] | 57 | /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ |
|---|
| 58 | template <class T> inline void loadAndIncrease( T*& variable, uint8_t*& mem ) |
|---|
| 59 | { |
|---|
| 60 | *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); |
|---|
| 61 | mem += returnSize( variable ); |
|---|
| 62 | } |
|---|
| [7284] | 63 | |
|---|
| [6131] | 64 | /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ |
|---|
| 65 | template <class T> inline void saveAndIncrease( T*& variable, uint8_t*& mem ) |
|---|
| 66 | { |
|---|
| 67 | if ( variable ) |
|---|
| 68 | *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); |
|---|
| 69 | else |
|---|
| 70 | *(uint32_t*)(mem) = OBJECTID_UNKNOWN; |
|---|
| 71 | mem += returnSize( variable ); |
|---|
| 72 | } |
|---|
| [7284] | 73 | |
|---|
| [6131] | 74 | /** @brief checks whether the objectID of the variable is the same as in the bytestream */ |
|---|
| 75 | template <class T> inline bool checkEquality( T*& variable, uint8_t* mem ) |
|---|
| 76 | { |
|---|
| 77 | if ( variable ) |
|---|
| 78 | return *(uint32_t*)(mem) == variable->getObjectID(); |
|---|
| 79 | else |
|---|
| [7163] | 80 | return variable == variable->getSynchronisable(*(uint32_t*)(mem)); |
|---|
| 81 | } |
|---|
| [7284] | 82 | |
|---|
| [7163] | 83 | // These functions implement loading / saving / etc. for SmartPtr<T> |
|---|
| [7284] | 84 | |
|---|
| [7163] | 85 | /** @brief returns the size of the objectID needed to synchronise the pointer */ |
|---|
| [8706] | 86 | template <class T> inline uint32_t returnSize( const SmartPtr<T>& ) |
|---|
| [7163] | 87 | { |
|---|
| 88 | return sizeof(uint32_t); |
|---|
| 89 | } |
|---|
| [7284] | 90 | |
|---|
| [7163] | 91 | /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ |
|---|
| 92 | template <class T> inline void loadAndIncrease( const SmartPtr<T>& variable, uint8_t*& mem ) |
|---|
| 93 | { |
|---|
| 94 | // *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); |
|---|
| 95 | *const_cast<typename Loki::TypeTraits<SmartPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); |
|---|
| 96 | mem += returnSize( variable ); |
|---|
| 97 | } |
|---|
| [7284] | 98 | |
|---|
| [7163] | 99 | /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ |
|---|
| 100 | template <class T> inline void saveAndIncrease( const SmartPtr<T>& variable, uint8_t*& mem ) |
|---|
| 101 | { |
|---|
| 102 | if ( variable.get() ) |
|---|
| 103 | *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); |
|---|
| 104 | else |
|---|
| 105 | *(uint32_t*)(mem) = OBJECTID_UNKNOWN; |
|---|
| 106 | mem += returnSize( variable ); |
|---|
| 107 | } |
|---|
| [7284] | 108 | |
|---|
| [7163] | 109 | /** @brief checks whether the objectID of the variable is the same as in the bytestream */ |
|---|
| 110 | template <class T> inline bool checkEquality( const SmartPtr<T>& variable, uint8_t* mem ) |
|---|
| 111 | { |
|---|
| 112 | if ( variable.get() ) |
|---|
| 113 | return *(uint32_t*)(mem) == variable->getObjectID(); |
|---|
| 114 | else |
|---|
| [6131] | 115 | return *(uint32_t*)(mem) == OBJECTID_UNKNOWN; |
|---|
| 116 | } |
|---|
| [7284] | 117 | |
|---|
| [7163] | 118 | // These functions implement loading / saving / etc. for WeakPtr<T> |
|---|
| [7284] | 119 | |
|---|
| [7163] | 120 | /** @brief returns the size of the objectID needed to synchronise the pointer */ |
|---|
| 121 | template <class T> inline uint32_t returnSize( const WeakPtr<T>& variable ) |
|---|
| 122 | { |
|---|
| 123 | return sizeof(uint32_t); |
|---|
| 124 | } |
|---|
| [7284] | 125 | |
|---|
| [7163] | 126 | /** @brief reads the objectID of a pointer out of the bytestream and increases the mem pointer */ |
|---|
| 127 | template <class T> inline void loadAndIncrease( const WeakPtr<T>& variable, uint8_t*& mem ) |
|---|
| 128 | { |
|---|
| 129 | // *const_cast<typename Loki::TypeTraits<T*>::UnqualifiedType*>(&variable) = dynamic_cast<T*>(variable->getSynchronisable( *(uint32_t*)(mem) )); |
|---|
| 130 | *const_cast<typename Loki::TypeTraits<SmartPtr<T> >::UnqualifiedType*>(&variable) = orxonox_cast<T*>(T::getSynchronisable(*(uint32_t*)(mem))); |
|---|
| 131 | mem += returnSize( variable ); |
|---|
| 132 | } |
|---|
| [7284] | 133 | |
|---|
| [7163] | 134 | /** @brief saves the objectID of a pointer into the bytestream and increases the mem pointer */ |
|---|
| 135 | template <class T> inline void saveAndIncrease( const WeakPtr<T>& variable, uint8_t*& mem ) |
|---|
| 136 | { |
|---|
| 137 | if ( variable.get() ) |
|---|
| 138 | *(uint32_t*)(mem) = static_cast<uint32_t>(variable->getObjectID()); |
|---|
| 139 | else |
|---|
| 140 | *(uint32_t*)(mem) = OBJECTID_UNKNOWN; |
|---|
| 141 | mem += returnSize( variable ); |
|---|
| 142 | } |
|---|
| [7284] | 143 | |
|---|
| [7163] | 144 | /** @brief checks whether the objectID of the variable is the same as in the bytestream */ |
|---|
| 145 | template <class T> inline bool checkEquality( const WeakPtr<T>& variable, uint8_t* mem ) |
|---|
| 146 | { |
|---|
| 147 | if ( variable.get() ) |
|---|
| 148 | return *(uint32_t*)(mem) == variable->getObjectID(); |
|---|
| 149 | else |
|---|
| 150 | return *(uint32_t*)(mem) == OBJECTID_UNKNOWN; |
|---|
| 151 | } |
|---|
| [6131] | 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | #endif |
|---|