LibNRG
0.0.1
Networking for Real-time Games library
All
Classes
Files
Functions
Variables
Friends
nrg_endian.h
Go to the documentation of this file.
1
/*
2
LibNRG - Networking for Real-time Games
3
4
Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5
6
This software is provided 'as-is', without any express or implied
7
warranty. In no event will the authors be held liable for any damages
8
arising from the use of this software.
9
10
Permission is granted to anyone to use this software for any purpose,
11
including commercial applications, and to alter it and redistribute it
12
freely, subject to the following restrictions:
13
14
1. The origin of this software must not be misrepresented; you must not
15
claim that you wrote the original software. If you use this software
16
in a product, an acknowledgment in the product documentation would be
17
appreciated but is not required.
18
2. Altered source versions must be plainly marked as such, and must not be
19
misrepresented as being the original software.
20
3. This notice may not be removed or altered from any source distribution.
21
*/
25
#ifndef NRG_ENDIAN_H
26
#define NRG_ENDIAN_H
27
/* adapted from http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c */
28
29
#if defined(__BYTE_ORDER__)
30
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN
31
#define NRG_BIG_ENDIAN 1
32
#endif
33
#elif defined(BYTE_ORDER)
34
#if BYTE_ORDER == BIG_ENDIAN
35
#define NRG_BIG_ENDIAN 1
36
#endif
37
#else
38
#warning Assuming host is little endian.
39
#endif
40
41
namespace
nrg {
42
44
template
<
typename
T>
45
static
inline
T swapbytes(
const
T& data){
46
T ret;
47
const
char
* src =
reinterpret_cast<
const
char
*
>
(&data) +
sizeof
(T) - 1;
48
char
* dst =
reinterpret_cast<
char
*
>
(&ret);
49
50
for
(
int
i =
sizeof
(T); i > 0; --i){
51
*dst++ = *src--;
52
}
53
54
return
ret;
55
}
56
58
template
<
typename
T>
59
static
inline
T ntoh(
const
T& data){
60
#ifdef NRG_BIG_ENDIAN
61
return
data;
62
#else
63
return
swapbytes(data);
64
#endif
65
}
66
68
template
<
typename
T>
69
static
inline
T hton(
const
T& data){
70
return
nrg::ntoh(data);
71
}
72
73
}
74
#endif
75
include
nrg_endian.h
Generated on Mon Sep 29 2014 17:20:36 for LibNRG by
1.8.6