1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_BYTECONVERTER_H |
---|
22 | #define TRINITY_BYTECONVERTER_H |
---|
23 | |
---|
24 | /** ByteConverter reverse your byte order. This is use |
---|
25 | for cross platform where they have different endians. |
---|
26 | */ |
---|
27 | |
---|
28 | #include<Platform/Define.h> |
---|
29 | #include<algorithm> |
---|
30 | |
---|
31 | namespace ByteConverter |
---|
32 | { |
---|
33 | template<size_t T> |
---|
34 | inline void convert(char *val) |
---|
35 | { |
---|
36 | std::swap(*val, *(val + T - 1)); |
---|
37 | convert<T - 2>(val + 1); |
---|
38 | } |
---|
39 | |
---|
40 | template<> inline void convert<0>(char *) {} |
---|
41 | template<> inline void convert<1>(char *) {} // ignore central byte |
---|
42 | |
---|
43 | template<typename T> inline void apply(T *val) |
---|
44 | { |
---|
45 | convert<sizeof(T)>((char *)(val)); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | #if TRINITY_ENDIAN == TRINITY_BIGENDIAN |
---|
50 | template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); } |
---|
51 | #else |
---|
52 | template<typename T> inline void EndianConvert(T&) { } |
---|
53 | #endif |
---|
54 | |
---|
55 | template<typename T> inline void EndianConvert(T*) { } |
---|
56 | inline void EndianConvert(uint8&) { } |
---|
57 | inline void EndianConvert( int8&) { } |
---|
58 | |
---|
59 | #endif |
---|