2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_BYTECONVERTER_H
20 #define MANGOS_BYTECONVERTER_H
22 /** ByteConverter reverse your byte order. This is use
23 for cross platform where they have different endians.
26 #include<Platform/Define.h>
29 namespace ByteConverter
32 inline void convert(char *val
)
34 std::swap(*val
, *(val
+ T
- 1));
35 convert
<T
- 2>(val
+ 1);
38 template<> inline void convert
<0>(char *) {}
39 template<> inline void convert
<1>(char *) {} // ignore central byte
41 template<typename T
> inline void apply(T
*val
)
43 convert
<sizeof(T
)>((char *)(val
));
47 #if MANGOS_ENDIAN == MANGOS_BIGENDIAN
48 template<typename T
> inline void EndianConvert(T
& val
) { ByteConverter::apply
<T
>(&val
); }
49 template<typename T
> inline void EndianConvertReverse(T
&) { }
51 template<typename T
> inline void EndianConvert(T
&) { }
52 template<typename T
> inline void EndianConvertReverse(T
& val
) { ByteConverter::apply
<T
>(&val
); }
55 template<typename T
> void EndianConvert(T
*); // will generate link error
56 template<typename T
> void EndianConvertReverse(T
*); // will generate link error
58 inline void EndianConvert(uint8
&) { }
59 inline void EndianConvert( int8
&) { }
60 inline void EndianConvertReverse(uint8
&) { }
61 inline void EndianConvertReverse( int8
&) { }