Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/SuperOrxoBros_HS18/src/libraries/util/Serialise.h @ 12175

Last change on this file since 12175 was 12175, checked in by siramesh, 5 years ago

Super Orxo Bros (Sidharth Ramesh, Nisa Balta, Jeff Ren)

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