AdsLib
Loading...
Searching...
No Matches
AdsLib
wrap_endian.h
1
// SPDX-License-Identifier: MIT
6
#pragma once
7
8
#include <cstddef>
9
#include <cstdint>
10
11
namespace
bhf
12
{
13
namespace
ads
14
{
15
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
16
template
<
class
T>
17
inline
T letoh(
const
void
* buffer)
18
{
19
const
auto
bytes =
reinterpret_cast<
const
uint8_t*
>
(buffer);
20
T result = 0;
21
for
(
size_t
i = 0; i <
sizeof
(T); ++i) {
22
result += (bytes[i] << (8 * i));
23
}
24
return
result;
25
}
26
#else
27
template
<
class
T>
28
inline
T letoh(
const
void
* buffer)
29
{
30
return
*
reinterpret_cast<
const
T*
>
(buffer);
31
}
32
#endif
33
34
template
<
class
T>
35
inline
T letoh(
const
T& value)
36
{
37
return
letoh<T>(
reinterpret_cast<
const
uint8_t*
>
(&value));
38
}
39
40
template
<
class
T>
41
T htole(
const
T value)
42
{
43
return
letoh(value);
44
}
45
}
46
}
bhf
Definition:
AdsDef.h:21
Generated on Fri Dec 16 2022 11:37:25 for AdsLib by
1.9.5