Horizon Official Technical Documentation
ByteConverter.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef ByteConverter_h
31#define ByteConverter_h
32
33#include <algorithm>
34#include <cstdint>
35
37{
38 template<size_t T>
39 inline void convert(char *val)
40 {
41 std::swap(*val, *(val + T - 1));
42 convert<T - 2>(val + 1);
43 }
44
45 template<> inline void convert<0>(char *) { }
46 template<> inline void convert<1>(char *) { } // ignore central byte
47
48 template<typename T> inline void apply(T *val)
49 {
50 convert<sizeof(T)>((char *)(val));
51 }
52}
53
54#if 0 // Big Endian
55template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); }
56template<typename T> inline void EndianConvertReverse(T&) { }
57template<typename T> inline void EndianConvertPtr(void* val) { ByteConverter::apply<T>(val); }
58template<typename T> inline void EndianConvertPtrReverse(void*) { }
59#else // Little Endian
60template<typename T> inline void EndianConvert(T&) { }
61template<typename T> inline void EndianConvertReverse(T& val) { ByteConverter::apply<T>(&val); }
62template<typename T> inline void EndianConvertPtr(void*) { }
63template<typename T> inline void EndianConvertPtrReverse(void* val) { ByteConverter::apply<T>(val); }
64#endif
65
66template<typename T> void EndianConvert(T*); // will generate link error
67template<typename T> void EndianConvertReverse(T*); // will generate link error
68
69inline void EndianConvert(uint8_t &) { }
70inline void EndianConvert(int8_t &) { }
71inline void EndianConvertReverse(uint8_t &) { }
72inline void EndianConvertReverse(int8_t &) { }
73
74
75#endif /* ByteConverter_h */
void EndianConvertReverse(T &val)
Definition: ByteConverter.hpp:61
void EndianConvertPtr(void *)
Definition: ByteConverter.hpp:62
void EndianConvert(T &)
Definition: ByteConverter.hpp:60
void EndianConvertPtrReverse(void *val)
Definition: ByteConverter.hpp:63
Definition: ByteConverter.hpp:37
void convert< 1 >(char *)
Definition: ByteConverter.hpp:46
void convert(char *val)
Definition: ByteConverter.hpp:39
void apply(T *val)
Definition: ByteConverter.hpp:48
void convert< 0 >(char *)
Definition: ByteConverter.hpp:45