framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
endian.hpp
Go to the documentation of this file.
1 // Copyright (C) 2012 iwg molw5
2 // For conditions of distribution and use, see copyright notice in COPYING
3 
17 #pragma once
18 
21 
26 #define FRAMEWORK_BIG_ENDIAN 1
27 
31 #define FRAMEWORK_LITTLE_ENDIAN 2
32 
36 #define FRAMEWORK_PDP_ENDIAN 3
37 
38 #ifndef FRAMEWORK_HOST_ENDIANNESS
39  // TODO: This test is not sufficint to guarentee the existance to endian.h - improve if possible
40  #ifdef __unix__
41  #include <endian.h>
42  #if __BYTE_ORDER == __LITTLE_ENDIAN
43  #define FRAMEWORK_HOST_ENDIANNESS FRAMEWORK_LITTLE_ENDIAN
44  #elif __BYTE_ORDER == __BIG_ENDIAN
45  #define FRAMEWORK_HOST_ENDIANNESS FRAMEWORK_BIG_ENDIAN
46  #elif __BYTE_ORDER == __PDP_ENDIAN
47  #define FRAMEWORK_HOST_ENDIANNESS FRAMEWORK_PDP_ENDIAN
48  #else
49  #error "Host endianness not supported"
50  #endif
51  #else
52  #error "Platform not supported";
53  #endif
54 #endif
55 
56 #ifndef FRAMEWORK_HOST_FLOAT_ENDIANNESS
57  // TODO: This test is not sufficint to guarentee the existance to endian.h - improve if possible
58  #ifdef __unix__
59  #include <endian.h>
60  #if __FLOAT_WORD_ORDER == __LITTLE_ENDIAN
61  #define FRAMEWORK_HOST_FLOAT_ENDIANNESS FRAMEWORK_LITTLE_ENDIAN
62  #elif __FLOAT_WORD_ORDER == __BIG_ENDIAN
63  #define FRAMEWORK_HOST_FLOAT_ENDIANNESS FRAMEWORK_BIG_ENDIAN
64  #elif __FLOAT_WORD_ORDER == __PDP_ENDIAN
65  #define FRAMEWORK_HOST_FLOAT_ENDIANNESS FRAMEWORK_PDP_ENDIAN
66  #else
67  #error "Host endianness not supported"
68  #endif
69  #else
70  #error "Platform not supported";
71  #endif
72 #endif
73 
74 // A compatible FRAMEWORK_HOST_ENDIANNESS value must be provided
75 #if FRAMEWORK_HOST_ENDIANNESS == FRAMEWORK_BIG_ENDIAN
76 #elif FRAMEWORK_HOST_ENDIANNESS == FRAMEWORK_LITTLE_ENDIAN
77 #elif FRAMEWORK_HOST_ENDIANNESS == FRAMEWORK_PDP_ENDIAN
78 #else
79  #error "Invalid FRAMEWORK_HOST_ENDIANNESS definition"
80 #endif
81 
82 // A compatible FRAMEWORK_HOST_FLOAT_ENDIANNESS value must be provided
83 #if FRAMEWORK_HOST_FLOAT_ENDIANNESS == FRAMEWORK_BIG_ENDIAN
84 #elif FRAMEWORK_HOST_FLOAT_ENDIANNESS == FRAMEWORK_LITTLE_ENDIAN
85 #elif FRAMEWORK_HOST_FLOAT_ENDIANNESS == FRAMEWORK_PDP_ENDIAN
86 #else
87  #error "Invalid FRAMEWORK_HOST_FLOAT_ENDIANNESS definition"
88 #endif
89 
90 namespace framework
91 {
98  enum class byte_order
99  {
100  little_endian = FRAMEWORK_LITTLE_ENDIAN,
101  big_endian = FRAMEWORK_BIG_ENDIAN,
102  pdp_endian = FRAMEWORK_PDP_ENDIAN,
103  host_endian = FRAMEWORK_HOST_ENDIANNESS
104  };
105 
106  namespace detail
107  {
108  template <typename T, typename Enabler = void>
109  struct little_to_host_impl;
110 
111  template <typename T, typename Enabler = void>
112  struct host_to_little_impl;
113 
114  template <typename T, typename Enabler = void>
115  struct big_to_host_impl;
116 
117  template <typename T, typename Enabler = void>
118  struct host_to_big_impl;
119 
120  template <typename T, typename Enabler = void>
121  struct pdp_to_host_impl;
122 
123  template <typename T, typename Enabler = void>
124  struct host_to_pdp_impl;
125 
126  template <typename T>
127  using reduce = typename std::remove_cv <typename std::remove_reference <T>::type>::type;
128  }
129 
130  template <typename T>
131  detail::reduce <T> little_to_host (T&& x)
132  {
133  return detail::little_to_host_impl <detail::reduce <T>>::run(std::forward <T> (x));
134  }
135 
136  template <typename T>
137  detail::reduce <T> host_to_little (T&& x)
138  {
139  return detail::host_to_little_impl <detail::reduce <T>>::run(std::forward <T> (x));
140  }
141 
142  template <typename T>
143  detail::reduce <T> big_to_host (T&& x)
144  {
145  return detail::big_to_host_impl <detail::reduce <T>>::run(std::forward <T> (x));
146  }
147 
148  template <typename T>
149  detail::reduce <T> host_to_big (T&& x)
150  {
151  return detail::host_to_big_impl <detail::reduce <T>>::run(std::forward <T> (x));
152  }
153 
154  template <typename T>
155  detail::reduce <T> pdp_to_host (T&& x)
156  {
157  return detail::pdp_to_host_impl <detail::reduce <T>>::run(std::forward <T> (x));
158  }
159 
160  template <typename T>
161  detail::reduce <T> host_to_pdp (T&& x)
162  {
163  return detail::host_to_pdp_impl <detail::reduce <T>>::run(std::forward <T> (x));
164  }
165 }
166 
167 #include <framework/common/endian.inl>
168 
169 // Note - these definitions need to be placed at the end of the file to suppress doxygen warnings.
170 // Removing the definitions below with #undef failed.
171 #ifdef DOXYGEN
172  #error "Documentation only"
173 
182  #define FRAMEWORK_HOST_ENDIANNESS
183 
192  #define FRAMEWORK_HOST_FLOAT_ENDIANNESS
193 #endif