OregonCore  revision be9e804-git
Your Favourite TBC server
ByteConverter.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_BYTECONVERTER_H
19 #define OREGON_BYTECONVERTER_H
20 
21 /*
22  ByteConverter reverse your byte order. This is used
23  for cross platform where they have different endians.
24  */
25 
26 #include<Platform/Define.h>
27 #include<algorithm>
28 
29 namespace ByteConverter
30 {
31 template<size_t T>
32 inline void convert(char* val)
33 {
34  std::swap(*val, *(val + T - 1));
35  convert < T - 2 > (val + 1);
36 }
37 
38 template<> inline void convert<0>(char*) {}
39 template<> inline void convert<1>(char*) {} // ignore central byte
40 
41 template<typename T> inline void apply(T* val)
42 {
43  convert<sizeof(T)>((char*)(val));
44 }
45 }
46 
47 #if OREGON_ENDIAN == OREGON_BIGENDIAN
48 template<typename T> inline void EndianConvert(T& val)
49 {
50  ByteConverter::apply<T>(&val);
51 }
52 template<typename T> inline void EndianConvertReverse(T&) { }
53 #else
54 template<typename T> inline void EndianConvert(T&) { }
55 template<typename T> inline void EndianConvertReverse(T& val)
56 {
57  ByteConverter::apply<T>(&val);
58 }
59 #endif
60 
61 template<typename T> void EndianConvert(T*); // will generate link error
62 template<typename T> void EndianConvertReverse(T*); // will generate link error
63 
64 inline void EndianConvert(uint8&) { }
65 inline void EndianConvert( int8&) { }
66 inline void EndianConvertReverse(uint8&) { }
67 inline void EndianConvertReverse( int8&) { }
68 
69 #endif
70 
void EndianConvert(T &val)
Definition: ByteConverter.h:48
void apply(T *val)
Definition: ByteConverter.h:41
ACE_INT8 int8
Definition: Define.h:69
ACE_UINT8 uint8
Definition: Define.h:73
void EndianConvertReverse(T &)
Definition: ByteConverter.h:52
void convert(char *val)
Definition: ByteConverter.h:32
void convert< 0 >(char *)
Definition: ByteConverter.h:38
void convert< 1 >(char *)
Definition: ByteConverter.h:39