Orxonox  0.0.5 Codename: Arcturus
Serialise.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  * Oliver Scheuss
24  * Co-authors:
25  * ...
26  *
27  */
28 
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 
49  template <class T> inline uint32_t returnSize( T*& variable )
50  {
51  return sizeof(uint32_t);
52  }
53 
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 
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 
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 
83  template <class T> inline uint32_t returnSize( const StrongPtr<T>& )
84  {
85  return sizeof(uint32_t);
86  }
87 
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 
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 
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 
118  template <class T> inline uint32_t returnSize( const WeakPtr<T>& variable )
119  {
120  return sizeof(uint32_t);
121  }
122 
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 
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 
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
Shared library macros, enums, constants and forward declarations for the network library ...
Functions to serialise most of the types/classed used in Orxonox.
Shared library macros, enums, constants and forward declarations for the core library ...
bool checkEquality(T *&variable, uint8_t *mem)
checks whether the objectID of the variable is the same as in the bytestream
Definition: Serialise.h:72
uint32_t returnSize(T *&variable)
returns the size of the objectID needed to synchronise the pointer
Definition: Serialise.h:49
A strong pointer which wraps a pointer to an object and keeps this object alive as long as the strong...
Definition: CorePrereqs.h:227
WeakPtr wraps a pointer to an object, which becomes nullptr if the object is deleted.
Definition: CorePrereqs.h:236
T * get() const
Returns the wrapped pointer as T*.
Definition: WeakPtr.h:154
T * get() const
Returns the wrapped pointer as T*.
Definition: StrongPtr.h:219
UnVolatile< typename UnConst< T >::Result >::Result UnqualifiedType
Definition: TypeTraits.h:256
Definition: TypeTraits.h:165
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
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
Definition: InputPrereqs.h:78
static constexpr uint32_t OBJECTID_UNKNOWN
Definition: CorePrereqs.h:71
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