Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/libraries/network/synchronisable/Serialise.h @ 6964

Last change on this file since 6964 was 6964, checked in by scheusso, 14 years ago

fix in ClassID (thanks reto)
fix in Rocket
small improvements in model
weakptr synchronisable

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