Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/synchronisable/Serialise.h @ 7268

Last change on this file since 7268 was 7266, checked in by rgrieder, 14 years ago

Moved Loki library files to separate loki folder in externals.
Also added TypeManip.h (now used in Convert.h) and static_check.h.

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