Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/Serialise.h @ 6417

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

Merged presentation2 branch back to trunk.
Major new features:

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