Odil
A C++11 library for the DICOM standard
src
odil
endian.h
Go to the documentation of this file.
1
/*************************************************************************
2
* odil - Copyright (C) Universite de Strasbourg
3
* Distributed under the terms of the CeCILL-B license, as published by
4
* the CEA-CNRS-INRIA. Refer to the LICENSE file or to
5
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
6
* for details.
7
************************************************************************/
8
9
#ifndef _05d00816_25d0_41d1_9768_afd39f0503da
10
#define _05d00816_25d0_41d1_9768_afd39f0503da
11
12
#define ODIL_SWAP \
13
auto source = reinterpret_cast<char const *>(&value); \
14
auto const end = source + sizeof(value); \
15
T result; \
16
auto destination = reinterpret_cast<char *>(&result) + sizeof(result) - 1; \
17
while(source != end) \
18
{ \
19
*destination = *source; \
20
++source; \
21
--destination; \
22
}
23
24
namespace
odil
25
{
26
27
enum class
ByteOrdering
28
{
29
LittleEndian,
30
BigEndian
31
};
32
33
ByteOrdering
get_endianness
();
34
35
static
ByteOrdering
const
byte_ordering{
get_endianness
()};
36
37
template
<
typename
T>
38
T
host_to_big_endian
(T
const
& value)
39
{
40
if
(byte_ordering == ByteOrdering::LittleEndian)
41
{
42
ODIL_SWAP
43
return
result;
44
}
45
else
46
{
47
return
value;
48
}
49
}
50
51
template
<
typename
T>
52
T
host_to_little_endian
(T
const
& value)
53
{
54
if
(byte_ordering ==
ByteOrdering::BigEndian
)
55
{
56
ODIL_SWAP
57
return
result;
58
}
59
else
60
{
61
return
value;
62
}
63
}
64
65
template
<
typename
T>
66
T
big_endian_to_host
(T
const
& value)
67
{
68
if
(byte_ordering == ByteOrdering::LittleEndian)
69
{
70
ODIL_SWAP
71
return
result;
72
}
73
else
74
{
75
return
value;
76
}
77
}
78
79
template
<
typename
T>
80
T
little_endian_to_host
(T
const
& value)
81
{
82
if
(byte_ordering ==
ByteOrdering::BigEndian
)
83
{
84
ODIL_SWAP
85
return
result;
86
}
87
else
88
{
89
return
value;
90
}
91
}
92
93
}
94
95
#undef ODIL_SWAP
96
97
#endif // _05d00816_25d0_41d1_9768_afd39f0503da
odil::ByteOrdering
ByteOrdering
Definition:
endian.h:28
odil::ByteOrdering::LittleEndian
@ LittleEndian
ODIL_SWAP
#define ODIL_SWAP
Definition:
endian.h:12
odil
Definition:
Association.h:25
odil::little_endian_to_host
T little_endian_to_host(T const &value)
Definition:
endian.h:80
odil::host_to_little_endian
T host_to_little_endian(T const &value)
Definition:
endian.h:52
odil::big_endian_to_host
T big_endian_to_host(T const &value)
Definition:
endian.h:66
odil::get_endianness
ByteOrdering get_endianness()
odil::host_to_big_endian
T host_to_big_endian(T const &value)
Definition:
endian.h:38
Generated by
1.8.20