VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
endian.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
12 #ifndef _UGCS_VSM_ENDIAN_H_
13 #define _UGCS_VSM_ENDIAN_H_
14 
15 #include <ugcs/vsm/defs.h>
16 
17 #include <stdint.h>
18 
19 // __BIG_ENDIAN__ and __LITTLE_ENDIAN__ are defined in some gcc versions
20 // only, probably depending on the architecture. Try to use endian.h if
21 // the gcc way fails - endian.h also doesn't seem to be available on all
22 // platforms.
23 #if defined(__BIG_ENDIAN__) || __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
24  #define _UGCS_VSM_BIG_ENDIAN_
25 #else /* __BIG_ENDIAN__ */
26  #if defined(__LITTLE_ENDIAN__) || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
27  #else
28  #ifdef BSD
29  #include <sys/endian.h>
30  #else
31  #include <endian.h>
32  #endif
33  #if __BYTE_ORDER == __BIG_ENDIAN
34  #define _UGCS_VSM_BIG_ENDIAN_
35  #elif __BYTE_ORDER == __LITTLE_ENDIAN
36  #else
37  #error "Failed to determine endianness!"
38  #endif /* __BYTE_ORDER */
39  #endif /* __LITTLE_ENDIAN__ */
40 #endif /* __BIG_ENDIAN__ */
41 
42 namespace ugcs {
43 namespace vsm {
44 
46 constexpr bool
48 {
49 #ifdef _UGCS_VSM_BIG_ENDIAN_
50  return false;
51 #else
52  return true;
53 #endif
54 }
55 
57 constexpr bool
59 {
60  return !Is_system_le();
61 }
62 
63 // XXX ifdef GCC
65 #define VSM_BSWAP16(x) (((x) >> 8) | ((x) << 8))
66 
67 #define VSM_BSWAP32(x) __builtin_bswap32(x)
68 
69 #define VSM_BSWAP64(x) __builtin_bswap64(x)
70 
72 template <typename T>
73 constexpr T
75 {
76  return x;
77 }
78 
84 template <typename T>
85 constexpr T
87 {
88  return Is_system_be() ? x : VSM_BSWAP16(x);
89 }
90 
96 template <typename T>
97 constexpr T
99 {
100  return Is_system_be() ? x : VSM_BSWAP32(x);
101 }
102 
108 template <typename T>
109 constexpr T
111 {
112  return Is_system_be() ? x : VSM_BSWAP64(x);
113 }
114 
116 template <typename T>
117 constexpr T
119 {
120  return x;
121 }
122 
128 template <typename T>
129 constexpr T
131 {
132  return Is_system_le() ? x : VSM_BSWAP16(x);
133 }
134 
140 template <typename T>
141 constexpr T
143 {
144  return Is_system_le() ? x : VSM_BSWAP32(x);
145 }
146 
152 template <typename T>
153 constexpr T
155 {
156  return Is_system_le() ? x : VSM_BSWAP64(x);
157 }
158 
160 template <typename T>
161 constexpr T
163 {
164  return x;
165 }
166 
173 template <typename T>
174 constexpr T
176 {
177  return Convert_be_16(x);
178 }
179 
186 template <typename T>
187 constexpr T
189 {
190  return Convert_be_32(x);
191 }
192 
199 template <typename T>
200 constexpr T
202 {
203  return Convert_be_64(x);
204 }
205 
208 #define __VSM_BO_INT(type_size) int ## type_size ## _t
209 #define __VSM_BO_UINT(type_size) uint ## type_size ## _t
210 
211 #define _VSM_DEF_BO_CONV(type_size, type_name) \
212  \
213  constexpr type_name \
214  Ntoh(type_name x) \
215  { \
216  return Convert_nh_ ## type_size (x); \
217  } \
218  \
219  \
220  constexpr type_name \
221  Hton(type_name x) \
222  { \
223  return Convert_nh_ ## type_size (x); \
224  } \
225  \
226  \
227  constexpr type_name \
228  Le(type_name x) \
229  { \
230  return Convert_le_ ## type_size (x); \
231  } \
232  \
233  \
234  constexpr type_name \
235  Be(type_name x) \
236  { \
237  return Convert_be_ ## type_size (x); \
238  } \
239 
240 #define VSM_DEF_BO_CONV(type_size) \
241  _VSM_DEF_BO_CONV(type_size, __VSM_BO_INT(type_size)) \
242  _VSM_DEF_BO_CONV(type_size, __VSM_BO_UINT(type_size))
243 
246 VSM_DEF_BO_CONV(8)
247 VSM_DEF_BO_CONV(16)
248 VSM_DEF_BO_CONV(32)
249 VSM_DEF_BO_CONV(64)
250 
252 constexpr float
253 Hton(float x)
254 {
255  // XXX
256  return x;
257 }
258 
260 constexpr float
261 Ntoh(float x)
262 {
263  // XXX
264  return x;
265 }
266 
268 constexpr double
269 Hton(double x)
270 {
271  // XXX
272  return x;
273 }
274 
276 constexpr double
277 Ntoh(double x)
278 {
279  // XXX
280  return x;
281 }
282 
284 constexpr float
285 Le(float x)
286 {
287  // XXX
288  return x;
289 }
290 
292 constexpr float
293 Be(float x)
294 {
295  // XXX
296  return x;
297 }
298 
300 constexpr double
301 Le(double x)
302 {
303  // XXX
304  return x;
305 }
306 
308 constexpr double
309 Be(double x)
310 {
311  // XXX
312  return x;
313 }
314 
315 #ifndef NO_DOXYGEN
316 namespace internal {
317 
319 class Le_converter {
320 public:
321  template <typename T>
322  static constexpr T
323  Convert(T value)
324  {
325  return Le(value);
326  }
327 };
328 
330 class Be_converter {
331 public:
332  template <typename T>
333  static constexpr T
334  Convert(T value)
335  {
336  return Be(value);
337  }
338 };
339 
340 } /* namespace internal */
341 #endif
342 
344 template <typename T, class Converter>
345 class Bo_value {
346 public:
351  Bo_value(T value = 0):
352  value(Converter::Convert(value))
353  {}
354 
359  Bo_value &
360  operator =(T value)
361  {
362  this->value = Converter::Convert(value);
363  return *this;
364  }
365 
370  operator T() const
371  {
372  return Converter::Convert(value);
373  }
374 
379  T
380  Get() const
381  {
382  return Converter::Convert(value);
383  }
384 
392  static T
393  Get(const void* buffer)
394  {
395  return *static_cast<const Bo_value*>(buffer);
396  }
397 
404  static void
405  Set(void* buffer, const T value)
406  {
407  *(static_cast<Bo_value*>(buffer)) = value;
408  }
409 
410 private:
412  T value;
413 } __PACKED;
414 
418 template <typename T>
420 
424 template <typename T>
426 
427 // @{
435 typedef Le_value<float> Le_float;
437 // @}
438 
439 
440 // @{
448 typedef Be_value<float> Be_float;
450 // @}
451 
452 } /* namespace vsm */
453 } /* namespace ugcs */
454 
455 #endif /* _UGCS_VSM_ENDIAN_H_ */
Le_value< int8_t > Le_int8
Standard primitive types for little-endian byte order.
Definition: endian.h:429
#define __PACKED
Pack structure or class, i.e.
Definition: defs.h:35
static T Get(const void *buffer)
Interpret byte buffer as a storage for underlying type and return host byte order value...
Definition: endian.h:393
constexpr T Convert_nh_16(T x)
Convert 16 bits value byte order from network to host byte order and vice versa.
Definition: endian.h:175
#define VSM_BSWAP16(x)
Swap bytes in 16-bits integer value.
Definition: endian.h:65
constexpr T Convert_le_64(T x)
Convert 64 bits value byte order from LE to host byte order and vice versa.
Definition: endian.h:154
constexpr T Convert_be_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:74
Common preprocessor definitions.
Helper class for byte-order-dependent value representation.
Definition: endian.h:345
constexpr T Convert_le_16(T x)
Convert 16 bits value byte order from LE to host byte order and vice versa.
Definition: endian.h:130
#define VSM_BSWAP32(x)
Swap bytes in 32-bits integer value.
Definition: endian.h:67
Bo_value & operator=(T value)
Assign new value.
Definition: endian.h:360
constexpr bool Is_system_be()
Check if the system is big-endian.
Definition: endian.h:58
constexpr int8_t Hton(int8_t x)
Convert value from host to network byte order.
Definition: endian.h:246
VSM definitions to work with byte order.
constexpr T Convert_be_32(T x)
Convert 32 bits value byte order from BE to host byte order and vice versa.
Definition: endian.h:98
constexpr T Convert_nh_32(T x)
Convert 32 bits value byte order from network to host byte order and vice versa.
Definition: endian.h:188
constexpr bool Is_system_le()
Check if the system is little-endian.
Definition: endian.h:47
constexpr T Convert_le_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:118
constexpr T Convert_le_32(T x)
Convert 32 bits value byte order from LE to host byte order and vice versa.
Definition: endian.h:142
constexpr T Convert_be_16(T x)
Convert 16 bits value byte order from BE to host byte order and vice versa.
Definition: endian.h:86
Be_value< int8_t > Be_int8
Standard primitive types for big-endian byte order.
Definition: endian.h:442
Bo_value(T value=0)
Construct value.
Definition: endian.h:351
constexpr T Convert_nh_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:162
#define VSM_BSWAP64(x)
Swap bytes in 64-bits integer value.
Definition: endian.h:69
constexpr int8_t Ntoh(int8_t x)
Convert value from network to host byte order.
Definition: endian.h:246
constexpr int8_t Le(int8_t x)
Convert value from LE to host byte order and vice versa.
Definition: endian.h:246
static void Set(void *buffer, const T value)
Save value given in host order to byte buffer.
Definition: endian.h:405
constexpr T Convert_nh_64(T x)
Convert 64 bits value byte order from network to host byte order and vice versa.
Definition: endian.h:201
constexpr T Convert_be_64(T x)
Convert 64 bits value byte order from BE to host byte order and vice versa.
Definition: endian.h:110
T value
Stored value (in wire byte order).
Definition: endian.h:376
constexpr int8_t Be(int8_t x)
Convert value from BE to host byte order and vice versa.
Definition: endian.h:246
T Get() const
Get the value of underlying type.
Definition: endian.h:380