TrinityCore
ByteConverter.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_BYTECONVERTER_H
19#define TRINITY_BYTECONVERTER_H
20
25#include "Define.h"
26
28{
29 template<size_t T>
30 inline void convert(char *val)
31 {
32 char tmp = *val;
33 *val = *(val + T - 1);
34 *(val + T - 1) = tmp;
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
48template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); }
49template<typename T> inline void EndianConvertReverse(T&) { }
50template<typename T> inline void EndianConvertPtr(void* val) { ByteConverter::apply<T>(val); }
51template<typename T> inline void EndianConvertPtrReverse(void*) { }
52#else
53template<typename T> inline void EndianConvert(T&) { }
54template<typename T> inline void EndianConvertReverse(T& val) { ByteConverter::apply<T>(&val); }
55template<typename T> inline void EndianConvertPtr(void*) { }
56template<typename T> inline void EndianConvertPtrReverse(void* val) { ByteConverter::apply<T>(val); }
57#endif
58
59template<typename T> void EndianConvert(T*); // will generate link error
60template<typename T> void EndianConvertReverse(T*); // will generate link error
61
62inline void EndianConvert(uint8&) { }
63inline void EndianConvert( int8&) { }
64inline void EndianConvertReverse(uint8&) { }
65inline void EndianConvertReverse( int8&) { }
66
67#endif
void EndianConvertReverse(T &)
Definition: ByteConverter.h:49
void EndianConvertPtr(void *val)
Definition: ByteConverter.h:50
void EndianConvertPtrReverse(void *)
Definition: ByteConverter.h:51
void EndianConvert(T &val)
Definition: ByteConverter.h:48
uint8_t uint8
Definition: Define.h:144
int8_t int8
Definition: Define.h:140
void convert< 1 >(char *)
Definition: ByteConverter.h:39
void convert(char *val)
Definition: ByteConverter.h:30
void apply(T *val)
Definition: ByteConverter.h:41
void convert< 0 >(char *)
Definition: ByteConverter.h:38