Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed excess white space at the end of lines.

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