CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUEndian.h
1 //
2 // CUEndian.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This header includes several inline functions to force data into "network"
6 // (or big-endian) order. This guarantees that serialized binary data is the
7 // same across all platforms.
8 //
9 // All of the functions in this header are idempotent. To decode a previously
10 // encoded piece of data, use the function again.
11 //
12 // CUGL MIT License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 11/28/16
33 //
34 #ifndef __CU_SENDIAN_H__
35 #define __CU_SENDIAN_H__
36 #include <SDL/SDL.h>
37 
38 namespace cugl {
39 
53 SDL_FORCE_INLINE Uint16 marshall(Sint16 value) {
54 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
55  return (Sint16)SDL_Swap16((Uint16)value);
56 #else
57  return value;
58 #endif
59 }
60 
74 SDL_FORCE_INLINE Uint16 marshall(Uint16 value) {
75 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
76  return SDL_Swap16(value);
77 #else
78  return value;
79 #endif
80 }
81 
95 SDL_FORCE_INLINE Uint32 marshall(Sint32 value) {
96 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
97  return (Sint32)SDL_Swap32((Uint32)value);
98 #else
99  return value;
100 #endif
101 }
102 
116 SDL_FORCE_INLINE Uint32 marshall(Uint32 value) {
117 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
118  return SDL_Swap32(value);
119 #else
120  return value;
121 #endif
122 }
123 
137 SDL_FORCE_INLINE Uint64 marshall(Sint64 value) {
138 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
139  return (Sint64)SDL_Swap64((Uint64)value);
140 #else
141  return value;
142 #endif
143 }
144 
158 SDL_FORCE_INLINE Uint64 marshall(Uint64 value) {
159 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
160  return SDL_Swap64(value);
161 #else
162  return value;
163 #endif
164 }
165 
179 SDL_FORCE_INLINE float marshall(float value) {
180 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
181  union
182  {
183  float f;
184  Uint32 ui32;
185  } swapper;
186  swapper.f = value;
187  swapper.ui32 = SDL_Swap32(swapper.ui32);
188  return swapper.f;
189 #else
190  return value;
191 #endif
192 }
193 
207 SDL_FORCE_INLINE double marshall(double value) {
208 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
209  union
210  {
211  double d;
212  Uint64 ui64;
213  } swapper;
214  swapper.d = value;
215  swapper.ui64 = SDL_Swap64(swapper.ui64);
216  return swapper.d;
217 #else
218  return value;
219 #endif
220 }
221 
222 }
223 #endif /* __CU_ENDIAN_H__ */