Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial3/src/libraries/network/synchronisable/Serialise.h @ 10904

Last change on this file since 10904 was 8706, checked in by dafrick, 13 years ago

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
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#include "core/CoreIncludes.h"
44#include "core/BaseObject.h" // remove this if circular dependencies in BaseObject/SmartPtr are fixed
45//#include "core/SmartPtr.h"
46
47namespace orxonox{
48
49    // These functions implement loading / saving / etc. for pointer types
50
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    }
56
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    }
63
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    }
73
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
80            return variable == variable->getSynchronisable(*(uint32_t*)(mem));
81    }
82
83    // These functions implement loading / saving / etc. for SmartPtr<T>
84
85    /** @brief returns the size of the objectID needed to synchronise the pointer */
86    template <class T> inline uint32_t returnSize( const SmartPtr<T>& )
87    {
88        return sizeof(uint32_t);
89    }
90
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    }
98
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    }
108
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
115            return *(uint32_t*)(mem) == OBJECTID_UNKNOWN;
116    }
117
118    // These functions implement loading / saving / etc. for WeakPtr<T>
119
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    }
125
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    }
133
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    }
143
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    }
152}
153
154
155#endif
Note: See TracBrowser for help on using the repository browser.