root/trunk/src/framework/Utilities/ByteConverter.h @ 2

Revision 2, 1.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_BYTECONVERTER_H
20#define MANGOS_BYTECONVERTER_H
21
22/** ByteConverter reverse your byte order.  This is use
23    for cross platform where they have different endians.
24 */
25
26#include<Platform/Define.h>
27#include<algorithm>
28
29namespace 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 MANGOS_ENDIAN == MANGOS_BIGENDIAN
48template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); }
49#else
50template<typename T> inline void EndianConvert(T&) { }
51#endif
52
53template<typename T> inline void EndianConvert(T*) { }
54inline void EndianConvert(uint8&) { }
55inline void EndianConvert( int8&) { }
56
57#endif
Note: See TracBrowser for help on using the browser.