VSM C++ SDK
Vehicle Specific Modules SDK
endian.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
12 #ifndef VSM_ENDIAN_H_
13 #define VSM_ENDIAN_H_
14 
15 #include <ugcs/vsm/defs.h>
16 
17 #include <stdint.h>
18 
19 namespace ugcs {
20 namespace vsm {
21 
22 namespace internal {
23 
25 static const uint16_t endianness = 0x0102;
26 
27 }
28 
30 constexpr bool
32 {
33  return *reinterpret_cast<const uint8_t *>(&internal::endianness) == 0x02;
34 }
35 
37 constexpr bool
39 {
40  return !Is_system_le();
41 }
42 
43 //XXX ifdef GCC
45 #define VSM_BSWAP16(x) (((x) >> 8) | ((x) << 8))
46 
47 #define VSM_BSWAP32(x) __builtin_bswap32(x)
48 
49 #define VSM_BSWAP64(x) __builtin_bswap64(x)
50 
52 template <typename T>
53 constexpr T
55 {
56  return x;
57 }
58 
64 template <typename T>
65 constexpr T
67 {
68  return Is_system_be() ? x : VSM_BSWAP16(x);
69 }
70 
76 template <typename T>
77 constexpr T
79 {
80  return Is_system_be() ? x : VSM_BSWAP32(x);
81 }
82 
88 template <typename T>
89 constexpr T
91 {
92  return Is_system_be() ? x : VSM_BSWAP64(x);
93 }
94 
96 template <typename T>
97 constexpr T
99 {
100  return x;
101 }
102 
108 template <typename T>
109 constexpr T
111 {
112  return Is_system_le() ? x : VSM_BSWAP16(x);
113 }
114 
120 template <typename T>
121 constexpr T
123 {
124  return Is_system_le() ? x : VSM_BSWAP32(x);
125 }
126 
132 template <typename T>
133 constexpr T
135 {
136  return Is_system_le() ? x : VSM_BSWAP64(x);
137 }
138 
140 template <typename T>
141 constexpr T
143 {
144  return x;
145 }
146 
153 template <typename T>
154 constexpr T
156 {
157  return Convert_be_16(x);
158 }
159 
166 template <typename T>
167 constexpr T
169 {
170  return Convert_be_32(x);
171 }
172 
179 template <typename T>
180 constexpr T
182 {
183  return Convert_be_64(x);
184 }
185 
188 #define __VSM_BO_INT(type_size) int ## type_size ## _t
189 #define __VSM_BO_UINT(type_size) uint ## type_size ## _t
190 
191 #define _VSM_DEF_BO_CONV(type_size, type_name) \
192  \
193  constexpr type_name \
194  Ntoh(type_name x) \
195  { \
196  return Convert_nh_ ## type_size (x); \
197  } \
198  \
199  \
200  constexpr type_name \
201  Hton(type_name x) \
202  { \
203  return Convert_nh_ ## type_size (x); \
204  } \
205  \
206  \
207  constexpr type_name \
208  Le(type_name x) \
209  { \
210  return Convert_le_ ## type_size (x); \
211  } \
212  \
213  \
214  constexpr type_name \
215  Be(type_name x) \
216  { \
217  return Convert_be_ ## type_size (x); \
218  } \
219 
220 #define VSM_DEF_BO_CONV(type_size) \
221  _VSM_DEF_BO_CONV(type_size, __VSM_BO_INT(type_size)) \
222  _VSM_DEF_BO_CONV(type_size, __VSM_BO_UINT(type_size))
223 
226 VSM_DEF_BO_CONV(8)
227 VSM_DEF_BO_CONV(16)
228 VSM_DEF_BO_CONV(32)
229 VSM_DEF_BO_CONV(64)
230 
232 constexpr float
233 Hton(float x)
234 {
235  //XXX
236  return x;
237 }
238 
240 constexpr float
241 Ntoh(float x)
242 {
243  //XXX
244  return x;
245 }
246 
248 constexpr double
249 Hton(double x)
250 {
251  //XXX
252  return x;
253 }
254 
256 constexpr double
257 Ntoh(double x)
258 {
259  //XXX
260  return x;
261 }
262 
264 constexpr float
265 Le(float x)
266 {
267  //XXX
268  return x;
269 }
270 
272 constexpr float
273 Be(float x)
274 {
275  //XXX
276  return x;
277 }
278 
280 constexpr double
281 Le(double x)
282 {
283  //XXX
284  return x;
285 }
286 
288 constexpr double
289 Be(double x)
290 {
291  //XXX
292  return x;
293 }
294 
295 #ifndef NO_DOXYGEN
296 namespace internal {
297 
299 class Le_converter {
300 public:
301  template <typename T>
302  static constexpr T
303  Convert(T value)
304  {
305  return Le(value);
306  }
307 };
308 
310 class Be_converter {
311 public:
312  template <typename T>
313  static constexpr T
314  Convert(T value)
315  {
316  return Be(value);
317  }
318 };
319 
320 } /* namespace internal */
321 #endif
322 
324 template <typename T, class Converter>
325 class Bo_value {
326 public:
331  Bo_value(T value = 0):
332  value(Converter::Convert(value))
333  {}
334 
339  Bo_value &
340  operator =(T value)
341  {
342  this->value = Converter::Convert(value);
343  return *this;
344  }
345 
350  operator T() const
351  {
352  return Converter::Convert(value);
353  }
354 
359  T
360  Get() const
361  {
362  return Converter::Convert(value);
363  }
364 
372  static T
373  Get(const void* buffer)
374  {
375  return *static_cast<const Bo_value*>(buffer);
376  }
377 
384  static void
385  Set(void* buffer, const T value)
386  {
387  *(static_cast<Bo_value*>(buffer)) = value;
388  }
389 
390 private:
392  T value;
393 } __PACKED;
394 
398 template <typename T>
400 
404 template <typename T>
406 
407 // @{
415 typedef Le_value<float> Le_float;
417 // @}
418 
419 
420 // @{
428 typedef Be_value<float> Be_float;
430 // @}
431 
432 } /* namespace vsm */
433 } /* namespace ugcs */
434 
435 #endif /* VSM_ENDIAN_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Le_value< int8_t > Le_int8
Standard primitive types for little-endian byte order.
Definition: endian.h:409
#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:373
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:155
#define VSM_BSWAP16(x)
Swap bytes in 16-bits integer value.
Definition: endian.h:45
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:134
constexpr T Convert_be_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:54
Common preprocessor definitions.
Helper class for byte-order-dependent value representation.
Definition: endian.h:325
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:110
#define VSM_BSWAP32(x)
Swap bytes in 32-bits integer value.
Definition: endian.h:47
constexpr bool Is_system_be()
Check if the system is big-endian.
Definition: endian.h:38
constexpr int8_t Hton(int8_t x)
Convert value from host to network byte order.
Definition: endian.h:226
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:78
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:168
constexpr bool Is_system_le()
Check if the system is little-endian.
Definition: endian.h:31
constexpr T Convert_le_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:98
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:122
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:66
Bo_value & operator=(T value)
Assign new value.
Definition: endian.h:304
Be_value< int8_t > Be_int8
Standard primitive types for big-endian byte order.
Definition: endian.h:422
Bo_value(T value=0)
Construct value.
Definition: endian.h:331
constexpr T Convert_nh_8(T x)
Stub for easier conversion functions generalization.
Definition: endian.h:142
#define VSM_BSWAP64(x)
Swap bytes in 64-bits integer value.
Definition: endian.h:49
constexpr int8_t Ntoh(int8_t x)
Convert value from network to host byte order.
Definition: endian.h:226
constexpr int8_t Le(int8_t x)
Convert value from LE to host byte order and vice versa.
Definition: endian.h:226
static void Set(void *buffer, const T value)
Save value given in host order to byte buffer.
Definition: endian.h:385
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:181
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:90
constexpr int8_t Be(int8_t x)
Convert value from BE to host byte order and vice versa.
Definition: endian.h:226
T Get() const
Get the value of underlying type.
Definition: endian.h:360