Odil
A C++11 library for the DICOM standard
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 #include "odil/odil.h"
13 
14 #define ODIL_SWAP \
15  auto source = reinterpret_cast<char const *>(&value); \
16  auto const end = source + sizeof(value); \
17  T result; \
18  auto destination = reinterpret_cast<char *>(&result) + sizeof(result) - 1; \
19  while(source != end) \
20  { \
21  *destination = *source; \
22  ++source; \
23  --destination; \
24  }
25 
26 namespace odil
27 {
28 
30 {
32  BigEndian
33 };
34 
36 
38 
39 template<typename T>
40 T host_to_big_endian(T const & value)
41 {
43  {
44  ODIL_SWAP
45  return result;
46  }
47  else
48  {
49  return value;
50  }
51 }
52 
53 template<typename T>
54 T host_to_little_endian(T const & value)
55 {
57  {
58  ODIL_SWAP
59  return result;
60  }
61  else
62  {
63  return value;
64  }
65 }
66 
67 template<typename T>
68 T big_endian_to_host(T const & value)
69 {
71  {
72  ODIL_SWAP
73  return result;
74  }
75  else
76  {
77  return value;
78  }
79 }
80 
81 template<typename T>
82 T little_endian_to_host(T const & value)
83 {
85  {
86  ODIL_SWAP
87  return result;
88  }
89  else
90  {
91  return value;
92  }
93 }
94 
95 }
96 
97 #undef ODIL_SWAP
98 
99 #endif // _05d00816_25d0_41d1_9768_afd39f0503da
#define ODIL_SWAP
Definition: endian.h:14
Definition: Association.h:25
ByteOrdering const byte_ordering
T host_to_little_endian(T const &value)
Definition: endian.h:54
T big_endian_to_host(T const &value)
Definition: endian.h:68
ByteOrdering get_endianness()
ByteOrdering
Definition: endian.h:30
T host_to_big_endian(T const &value)
Definition: endian.h:40
T little_endian_to_host(T const &value)
Definition: endian.h:82
#define ODIL_API
Definition: odil.h:28