Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/libraries/util/Serialise.h @ 7073

Last change on this file since 7073 was 6746, checked in by rgrieder, 14 years ago

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

  • Property svn:eol-style set to native
File size: 19.5 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 most of the types/classed used in Orxonox
32*/
33
34#ifndef _Serialise_H__
35#define _Serialise_H__
36
37#include "UtilPrereqs.h"
38
39#include <cstring>
40#include "Math.h"
41#include "mbool.h"
42
43namespace orxonox{
44
45    /** @brief returns the size of the variable in a datastream */
46    template <class T> inline uint32_t returnSize( const T& variable );
47    /** @brief loads the value of a variable out of the bytestream and increases the mem pointer */
48    template <class T> inline void loadAndIncrease( const T& variable, uint8_t*& mem );
49    /** @brief saves the value of a variable into the bytestream and increases the mem pointer */
50    template <class T> inline void saveAndIncrease( const T& variable, uint8_t*& mem );
51    /** @brief checks whether the variable of type T is the same as in the bytestream */
52    template <class T> inline bool checkEquality( const T& variable, uint8_t* mem );
53
54// =================== Template specialisation stuff =============
55
56// =========== bool
57
58    template <> inline uint32_t returnSize( const bool& variable )
59    {
60        return sizeof(uint8_t);
61    }
62
63    template <> inline void loadAndIncrease( const bool& variable, uint8_t*& mem )
64    {
65        *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
66        mem += returnSize( variable );
67    }
68
69    template <> inline void saveAndIncrease( const bool& variable, uint8_t*& mem )
70    {
71        *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
72        mem += returnSize( variable );
73    }
74
75    template <> inline bool checkEquality( const bool& variable, uint8_t* mem )
76    {
77        return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
78    }
79
80// =========== char
81
82    template <> inline uint32_t returnSize( const char& variable )
83    {
84        return sizeof(uint8_t);
85    }
86
87    template <> inline void loadAndIncrease( const char& variable, uint8_t*& mem )
88    {
89        *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
90        mem += returnSize( variable );
91    }
92
93    template <> inline void saveAndIncrease( const char& variable, uint8_t*& mem )
94    {
95        *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
96        mem += returnSize( variable );
97    }
98
99    template <> inline bool checkEquality( const char& variable, uint8_t* mem )
100    {
101        return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
102    }
103
104// =========== unsigned char
105
106    template <> inline uint32_t returnSize( const unsigned char& variable )
107    {
108        return sizeof(uint8_t);
109    }
110
111    template <> inline void loadAndIncrease( const unsigned char& variable, uint8_t*& mem )
112    {
113        *(uint8_t*)( &variable ) = *static_cast<uint8_t*>(mem);
114        mem += returnSize( variable );
115    }
116
117    template <> inline void saveAndIncrease( const unsigned char& variable, uint8_t*& mem )
118    {
119        *static_cast<uint8_t*>(mem) = *(uint8_t*)( &variable );
120        mem += returnSize( variable );
121    }
122
123    template <> inline bool checkEquality( const unsigned char& variable, uint8_t* mem )
124    {
125        return *static_cast<uint8_t*>(mem) == *(uint8_t*)( &variable );
126    }
127
128// =========== short
129
130    template <> inline uint32_t returnSize( const short& variable )
131    {
132        return sizeof(int16_t);
133    }
134
135    template <> inline void loadAndIncrease( const short& variable, uint8_t*& mem )
136    {
137        *(short*)( &variable ) = *(int16_t*)(mem);
138        mem += returnSize( variable );
139    }
140
141    template <> inline void saveAndIncrease( const short& variable, uint8_t*& mem )
142    {
143        *(int16_t*)(mem) = variable;
144        mem += returnSize( variable );
145    }
146
147    template <> inline bool checkEquality( const short& variable, uint8_t* mem )
148    {
149        return *(int16_t*)(mem) == static_cast<int16_t>(variable);
150    }
151
152// =========== unsigned short
153
154    template <> inline uint32_t returnSize( const unsigned short& variable )
155    {
156        return sizeof(uint16_t);
157    }
158
159    template <> inline void loadAndIncrease( const unsigned short& variable, uint8_t*& mem )
160    {
161        *(unsigned short*)( &variable ) = *(uint16_t*)(mem);
162        mem += returnSize( variable );
163    }
164
165    template <> inline void saveAndIncrease( const unsigned short& variable, uint8_t*& mem )
166    {
167        *(uint16_t*)(mem) = variable;
168        mem += returnSize( variable );
169    }
170
171    template <> inline bool checkEquality( const unsigned short& variable, uint8_t* mem )
172    {
173        return *(uint16_t*)(mem) == variable;
174    }
175
176// =========== int
177
178    template <> inline uint32_t returnSize( const int& variable )
179    {
180        return sizeof(int32_t);
181    }
182
183    template <> inline void loadAndIncrease( const int& variable, uint8_t*& mem )
184    {
185        *(int *)( &variable ) = *(int32_t*)(mem);
186        mem += returnSize( variable );
187    }
188
189    template <> inline void saveAndIncrease( const int& variable, uint8_t*& mem )
190    {
191        *(int32_t*)(mem) = variable;
192        mem += returnSize( variable );
193    }
194
195    template <> inline bool checkEquality( const int& variable, uint8_t* mem )
196    {
197        return *(int32_t*)(mem) == variable;
198    }
199
200// =========== unsigned int
201
202    template <> inline uint32_t returnSize( const unsigned int& variable )
203    {
204        return sizeof(uint32_t);
205    }
206
207    template <> inline void loadAndIncrease( const unsigned int& variable, uint8_t*& mem )
208    {
209        *(unsigned int*)( &variable ) = *(uint32_t*)(mem);
210        mem += returnSize( variable );
211    }
212
213    template <> inline void saveAndIncrease( const unsigned int& variable, uint8_t*& mem )
214    {
215        *(uint32_t*)(mem) = variable;
216        mem += returnSize( variable );
217    }
218
219    template <> inline bool checkEquality( const unsigned int& variable, uint8_t* mem )
220    {
221        return *(uint32_t*)(mem) == variable;
222    }
223
224// =========== long
225
226    template <> inline uint32_t returnSize( const long& variable )
227    {
228        return sizeof(int32_t);
229    }
230
231    template <> inline void loadAndIncrease( const long& variable, uint8_t*& mem )
232    {
233        *(long*)( &variable ) = *(int32_t*)(mem);
234        mem += returnSize( variable );
235    }
236
237    template <> inline void saveAndIncrease( const long& variable, uint8_t*& mem )
238    {
239        *(int32_t*)(mem) = variable;
240        mem += returnSize( variable );
241    }
242
243    template <> inline bool checkEquality( const long& variable, uint8_t* mem )
244    {
245        return *(int32_t*)(mem) == variable;
246    }
247
248// =========== unsigned long
249
250    template <> inline uint32_t returnSize( const unsigned long& variable )
251    {
252        return sizeof(uint32_t);
253    }
254
255    template <> inline void loadAndIncrease( const unsigned long& variable, uint8_t*& mem )
256    {
257        *(unsigned long*)( &variable ) = *(uint32_t*)(mem);
258        mem += returnSize( variable );
259    }
260
261    template <> inline void saveAndIncrease( const unsigned long& variable, uint8_t*& mem )
262    {
263        *(uint32_t*)(mem) = variable;
264        mem += returnSize( variable );
265    }
266
267    template <> inline bool checkEquality( const unsigned long& variable, uint8_t* mem )
268    {
269        return *(uint32_t*)(mem) == variable;
270    }
271
272// =========== long long
273
274    template <> inline uint32_t returnSize( const long long& variable )
275    {
276        return sizeof(int64_t);
277    }
278
279    template <> inline void loadAndIncrease( const long long& variable, uint8_t*& mem )
280    {
281        *(long long*)( &variable ) = *(int64_t*)(mem);
282        mem += returnSize( variable );
283    }
284
285    template <> inline void saveAndIncrease( const long long& variable, uint8_t*& mem )
286    {
287        *(int64_t*)(mem) = variable;
288        mem += returnSize( variable );
289    }
290
291    template <> inline bool checkEquality( const long long& variable, uint8_t* mem )
292    {
293        return *(int64_t*)(mem) == variable;
294    }
295
296// =========== unsigned long long
297
298    template <> inline uint32_t returnSize( const unsigned long long& variable )
299    {
300        return sizeof(uint64_t);
301    }
302
303    template <> inline void loadAndIncrease( const unsigned long long& variable, uint8_t*& mem )
304    {
305        *(unsigned long long*)( &variable ) = *(uint64_t*)(mem);
306        mem += returnSize( variable );
307    }
308
309    template <> inline void saveAndIncrease( const unsigned long long& variable, uint8_t*& mem )
310    {
311        *(uint64_t*)(mem) = variable;
312        mem += returnSize( variable );
313    }
314
315    template <> inline bool checkEquality( const unsigned long long& variable, uint8_t* mem )
316    {
317        return *(uint64_t*)(mem) == variable;
318    }
319
320// =========== float
321
322    template <> inline uint32_t returnSize( const float& variable )
323    {
324        return sizeof(uint32_t);
325    }
326
327    template <> inline void loadAndIncrease( const float& variable, uint8_t*& mem )
328    {
329        *(uint32_t*)( &variable ) = *(uint32_t*)(mem);
330        mem += returnSize( variable );
331    }
332
333    template <> inline void saveAndIncrease( const float& variable, uint8_t*& mem )
334    {
335        *(uint32_t*)(mem) = *(uint32_t*)( &variable );
336        mem += returnSize( variable );
337    }
338
339    template <> inline bool checkEquality( const float& variable, uint8_t* mem )
340    {
341        return *(uint32_t*)(mem) == *(uint32_t*)( &variable );
342    }
343
344// =========== double
345
346    template <> inline uint32_t returnSize( const double& variable )
347    {
348        return sizeof(uint64_t);
349    }
350
351    template <> inline void loadAndIncrease( const double& variable, uint8_t*& mem )
352    {
353        *(uint64_t*)( &variable ) = *(uint64_t*)(mem);
354        mem += returnSize( variable );
355    }
356
357    template <> inline void saveAndIncrease( const double& variable, uint8_t*& mem )
358    {
359        *(uint64_t*)(mem) = *(uint64_t*)( &variable );
360        mem += returnSize( variable );
361    }
362
363    template <> inline bool checkEquality( const double& variable, uint8_t* mem )
364    {
365        return *(uint64_t*)(mem) == *(uint64_t*)( &variable );
366    }
367
368// =========== long double
369
370    template <> inline uint32_t returnSize( const long double& variable )
371    {
372        return sizeof(uint64_t);
373    }
374
375    template <> inline void loadAndIncrease( const long double& variable, uint8_t*& mem )
376    {
377        double temp;
378        memcpy(&temp, mem, sizeof(uint64_t));
379        *(long double*)( &variable ) = static_cast<long double>(temp);
380        mem += returnSize( variable );
381    }
382
383    template <> inline void saveAndIncrease( const long double& variable, uint8_t*& mem )
384    {
385        double temp = static_cast<double>(variable);
386        memcpy(mem, &temp, sizeof(uint64_t));
387        mem += returnSize( variable );
388    }
389
390    template <> inline bool checkEquality( const long double& variable, uint8_t* mem )
391    {
392        double temp = static_cast<double>(variable);
393        return memcmp(&temp, mem, sizeof(uint64_t))==0;
394    }
395
396// =========== string
397
398    template <> inline uint32_t returnSize( const std::string& variable )
399    {
400        return variable.length()+1;
401    }
402
403    template <> inline void saveAndIncrease( const std::string& variable, uint8_t*& mem )
404    {
405        memcpy(mem, variable.c_str(), variable.length()+1);
406        mem += variable.length()+1;
407    }
408
409    template <> inline void loadAndIncrease( const std::string& variable, uint8_t*& mem )
410    {
411        *(std::string*)( &variable ) = (const char *)mem;
412        mem += variable.length()+1;
413    }
414
415    template <> inline bool checkEquality( const std::string& variable, uint8_t* mem )
416    {
417        //return std::string((const char*)mem)==variable;
418        return (const char*)mem==variable;
419    }
420
421// =========== Degree
422
423    template <> inline uint32_t returnSize( const Degree& variable )
424    {
425        return sizeof(Ogre::Real);
426    }
427
428    template <> inline void saveAndIncrease( const Degree& variable, uint8_t*& mem )
429    {
430        Ogre::Real r = variable.valueDegrees();
431        memcpy(mem, &r, returnSize( variable ));
432        mem += returnSize( variable );
433    }
434
435    template <> inline void loadAndIncrease( const Degree& variable, uint8_t*& mem )
436    {
437        Ogre::Real* r = (Ogre::Real*)mem;
438        (Degree&)variable = *r;
439        mem += returnSize( variable );
440    }
441
442     template <> inline bool checkEquality( const Degree& variable, uint8_t* mem )
443    {
444        Ogre::Real* r = (Ogre::Real*)mem;
445        return variable==Degree(*r);
446    }
447
448// =========== Radian
449
450    template <> inline uint32_t returnSize( const Radian& variable )
451    {
452        return sizeof(Ogre::Real);
453    }
454
455    template <> inline void saveAndIncrease( const Radian& variable, uint8_t*& mem )
456    {
457        Ogre::Real r = variable.valueRadians();
458        memcpy(mem, &r, returnSize( variable ));
459        mem += returnSize( variable );
460    }
461
462    template <> inline void loadAndIncrease( const Radian& variable, uint8_t*& mem )
463    {
464        Ogre::Real* r = (Ogre::Real*)mem;
465        (Radian&)variable = *r;
466        mem += returnSize( variable );
467    }
468
469    template <> inline bool checkEquality( const Radian& variable, uint8_t* mem )
470    {
471        Ogre::Real* r = (Ogre::Real*)mem;
472        return variable==Degree(*r);
473    }
474
475    // =========== Vector2
476
477    template <> inline uint32_t returnSize( const Vector2& variable )
478    {
479        return returnSize( variable.x )+returnSize( variable.y );
480    }
481
482    template <> inline void saveAndIncrease( const Vector2& variable, uint8_t*& mem )
483    {
484        saveAndIncrease( variable.x, mem );
485        saveAndIncrease( variable.y, mem );
486    }
487
488    template <> inline void loadAndIncrease( const Vector2& variable, uint8_t*& mem )
489    {
490        loadAndIncrease( variable.x, mem );
491        loadAndIncrease( variable.y, mem );
492    }
493
494    template <> inline bool checkEquality( const Vector2& variable, uint8_t* mem )
495    {
496        return checkEquality(variable.x, mem) && checkEquality(variable.y, mem+returnSize(variable.x));
497    }
498
499    // =========== Vector3
500
501    template <> inline uint32_t returnSize( const Vector3& variable )
502    {
503        return returnSize( variable.x )+returnSize( variable.y )+returnSize( variable.z );
504    }
505
506    template <> inline void saveAndIncrease( const Vector3& variable, uint8_t*& mem )
507    {
508        saveAndIncrease( variable.x, mem );
509        saveAndIncrease( variable.y, mem );
510        saveAndIncrease( variable.z, mem );
511    }
512
513    template <> inline void loadAndIncrease( const Vector3& variable, uint8_t*& mem )
514    {
515        loadAndIncrease( variable.x, mem );
516        loadAndIncrease( variable.y, mem );
517        loadAndIncrease( variable.z, mem );
518    }
519
520    template <> inline bool checkEquality( const Vector3& variable, uint8_t* mem )
521    {
522        return checkEquality(variable.x, mem) && checkEquality(variable.y, mem+returnSize(variable.x)) &&
523            checkEquality(variable.z, mem+returnSize(variable.x)+returnSize(variable.y));
524    }
525
526    // =========== Vector4
527
528    template <> inline uint32_t returnSize( const Vector4& variable )
529    {
530        return returnSize( variable.w )+returnSize( variable.x )+returnSize( variable.y )+returnSize( variable.z );
531    }
532
533    template <> inline void saveAndIncrease( const Vector4& variable, uint8_t*& mem )
534    {
535        saveAndIncrease( variable.w, mem );
536        saveAndIncrease( variable.x, mem );
537        saveAndIncrease( variable.y, mem );
538        saveAndIncrease( variable.z, mem );
539    }
540
541    template <> inline void loadAndIncrease( const Vector4& variable, uint8_t*& mem )
542    {
543        loadAndIncrease( variable.w, mem );
544        loadAndIncrease( variable.x, mem );
545        loadAndIncrease( variable.y, mem );
546        loadAndIncrease( variable.z, mem );
547    }
548
549    template <> inline bool checkEquality( const Vector4& variable, uint8_t* mem )
550    {
551        return checkEquality(variable.w, mem) && checkEquality(variable.x, mem+returnSize(variable.w)) &&
552            checkEquality(variable.y, mem+returnSize(variable.w)+returnSize(variable.x)) &&
553            checkEquality(variable.z, mem+returnSize(variable.w)+returnSize(variable.x)+returnSize(variable.y));
554    }
555
556    // =========== Quaternion
557
558    template <> inline uint32_t returnSize( const Quaternion& variable )
559    {
560        return returnSize( variable.w )+returnSize( variable.x )+returnSize( variable.y )+returnSize( variable.z );
561    }
562
563    template <> inline void saveAndIncrease( const Quaternion& variable, uint8_t*& mem )
564    {
565        saveAndIncrease( variable.w, mem );
566        saveAndIncrease( variable.x, mem );
567        saveAndIncrease( variable.y, mem );
568        saveAndIncrease( variable.z, mem );
569    }
570
571    template <> inline void loadAndIncrease( const Quaternion& variable, uint8_t*& mem )
572    {
573        loadAndIncrease( variable.w, mem );
574        loadAndIncrease( variable.x, mem );
575        loadAndIncrease( variable.y, mem );
576        loadAndIncrease( variable.z, mem );
577    }
578
579    template <> inline bool checkEquality( const Quaternion& variable, uint8_t* mem )
580    {
581        return checkEquality(variable.w, mem) && checkEquality(variable.x, mem+returnSize(variable.w)) &&
582            checkEquality(variable.y, mem+returnSize(variable.w)+returnSize(variable.x)) &&
583            checkEquality(variable.z, mem+returnSize(variable.w)+returnSize(variable.x)+returnSize(variable.y));
584    }
585
586    // =========== ColourValue
587
588    template <> inline uint32_t returnSize( const ColourValue& variable )
589    {
590        return returnSize( variable.r )+returnSize( variable.g )+returnSize( variable.b )+returnSize( variable.a );
591    }
592
593    template <> inline void saveAndIncrease( const ColourValue& variable, uint8_t*& mem )
594    {
595        saveAndIncrease( variable.r, mem );
596        saveAndIncrease( variable.g, mem );
597        saveAndIncrease( variable.b, mem );
598        saveAndIncrease( variable.a, mem );
599    }
600
601    template <> inline void loadAndIncrease( const ColourValue& variable, uint8_t*& mem )
602    {
603        loadAndIncrease( variable.r, mem );
604        loadAndIncrease( variable.g, mem );
605        loadAndIncrease( variable.b, mem );
606        loadAndIncrease( variable.a, mem );
607    }
608
609    template <> inline bool checkEquality( const ColourValue& variable, uint8_t* mem )
610    {
611        return checkEquality(variable.r, mem) && checkEquality(variable.g, mem+returnSize(variable.r)) &&
612            checkEquality(variable.b, mem+returnSize(variable.r)+returnSize(variable.g)) &&
613            checkEquality(variable.a, mem+returnSize(variable.r)+returnSize(variable.g)+returnSize(variable.b));
614    }
615
616    // =========== mbool
617
618    template <> inline uint32_t returnSize( const mbool& variable )
619    {
620        return returnSize( (unsigned char&)((mbool&)variable).getMemory() );
621    }
622
623    template <> inline void saveAndIncrease( const mbool& variable, uint8_t*& mem )
624    {
625        saveAndIncrease( (unsigned char&)((mbool&)variable).getMemory(), mem );
626    }
627
628    template <> inline void loadAndIncrease( const mbool& variable, uint8_t*& mem )
629    {
630        loadAndIncrease( (unsigned char&)((mbool&)variable).getMemory(), mem );
631    }
632
633    template <> inline bool checkEquality( const mbool& variable, uint8_t* mem )
634    {
635        return checkEquality( (unsigned char&)((mbool&)variable).getMemory(), mem );
636    }
637}
638
639
640#endif
Note: See TracBrowser for help on using the repository browser.