odil
endian.h
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 <boost/detail/endian.hpp>
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 
29 enum class ByteOrdering
30 {
31  LittleEndian,
32  BigEndian
33 };
34 
35 template<typename T>
36 T host_to_big_endian(T const & value)
37 {
38 #ifdef BOOST_LITTLE_ENDIAN
39  ODIL_SWAP
40 
41  return result;
42 #else
43  return value;
44 #endif
45 }
46 
47 template<typename T>
48 T host_to_little_endian(T const & value)
49 {
50 #ifdef BOOST_BIG_ENDIAN
51  ODIL_SWAP
52 
53  return result;
54 #else
55  return value;
56 #endif
57 }
58 
59 template<typename T>
60 T big_endian_to_host(T const & value)
61 {
62 #ifdef BOOST_LITTLE_ENDIAN
63  ODIL_SWAP
64 
65  return result;
66 #else
67  return value;
68 #endif
69 }
70 
71 template<typename T>
72 T little_endian_to_host(T const & value)
73 {
74 #ifdef BOOST_BIG_ENDIAN
75  ODIL_SWAP
76 
77  return result;
78 #else
79  return value;
80 #endif
81 }
82 
83 }
84 
85 #undef ODIL_SWAP
86 
87 #endif // _05d00816_25d0_41d1_9768_afd39f0503da
Definition: Association.cpp:39