TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ByteConverter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_BYTECONVERTER_H
20 #define TRINITY_BYTECONVERTER_H
21 
26 #include "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 TRINITY_ENDIAN == TRINITY_BIGENDIAN
48 template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); }
49 template<typename T> inline void EndianConvertReverse(T&) { }
50 template<typename T> inline void EndianConvertPtr(void* val) { ByteConverter::apply<T>(val); }
51 template<typename T> inline void EndianConvertPtrReverse(void*) { }
52 #else
53 template<typename T> inline void EndianConvert(T&) { }
54 template<typename T> inline void EndianConvertReverse(T& val) { ByteConverter::apply<T>(&val); }
55 template<typename T> inline void EndianConvertPtr(void*) { }
56 template<typename T> inline void EndianConvertPtrReverse(void* val) { ByteConverter::apply<T>(val); }
57 #endif
58 
59 template<typename T> void EndianConvert(T*); // will generate link error
60 template<typename T> void EndianConvertReverse(T*); // will generate link error
61 
62 inline void EndianConvert(uint8&) { }
63 inline void EndianConvert( int8&) { }
64 inline void EndianConvertReverse(uint8&) { }
65 inline void EndianConvertReverse( int8&) { }
66 
67 #endif
68 
int8_t int8
Definition: Define.h:148
void EndianConvertPtrReverse(void *)
Definition: ByteConverter.h:51
void EndianConvert(T &val)
Definition: ByteConverter.h:48
void apply(T *val)
Definition: ByteConverter.h:41
void EndianConvertReverse(T &)
Definition: ByteConverter.h:49
Definition: ByteConverter.h:29
void EndianConvertPtr(void *val)
Definition: ByteConverter.h:50
void convert(char *val)
Definition: ByteConverter.h:32
uint8_t uint8
Definition: Define.h:152
void convert< 0 >(char *)
Definition: ByteConverter.h:38
void convert< 1 >(char *)
Definition: ByteConverter.h:39