MRPT  2.0.0
reverse_bytes.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
12 #include <mrpt/core/Clock.h>
13 #include <cstdint>
14 
15 namespace mrpt
16 {
17 /** \addtogroup mrpt_core_grp
18  * @{ */
19 
20 /** Reverse the order of the bytes of a given type (useful for transforming btw
21  * little/big endian) */
22 void reverseBytesInPlace(bool& v_in_out);
23 void reverseBytesInPlace(uint8_t& v_in_out);
24 void reverseBytesInPlace(int8_t& v_in_out);
25 void reverseBytesInPlace(uint16_t& v_in_out);
26 void reverseBytesInPlace(int16_t& v_in_out);
27 void reverseBytesInPlace(uint32_t& v_in_out);
28 void reverseBytesInPlace(int32_t& v_in_out);
29 void reverseBytesInPlace(uint64_t& v_in_out);
30 void reverseBytesInPlace(int64_t& v_in_out);
31 void reverseBytesInPlace(float& v_in_out);
32 void reverseBytesInPlace(double& v_in_out);
33 void reverseBytesInPlace(long double& v_in_out);
34 void reverseBytesInPlace(std::chrono::time_point<mrpt::Clock>& v_in_out);
35 
36 /** Reverse the order of the bytes of a given type (useful for transforming btw
37  * little/big endian) */
38 template <class T>
39 inline void reverseBytes(const T& v_in, T& v_out)
40 {
41  v_out = v_in;
42  reverseBytesInPlace(v_out);
43 }
44 
45 template <class T>
46 inline T reverseBytes(const T& v_in)
47 {
48  T v_out = v_in;
49  reverseBytesInPlace(v_out);
50  return v_out;
51 }
52 
53 template <class T>
54 inline T toNativeEndianness(const T& v_in)
55 {
56 #if MRPT_IS_BIG_ENDIAN
57  T v_out = v_in;
58  reverseBytesInPlace(v_out);
59  return v_out;
60 #else
61  return v_in;
62 #endif
63 }
64 
65 template <
66  typename enum_t,
67  typename underlying_t = typename std::underlying_type<enum_t>::type>
68 inline void reverseBytesInPlace_enum(enum_t& v)
69 {
70  underlying_t v_out = static_cast<underlying_t>(v);
71  reverseBytesInPlace(v_out);
72  v = static_cast<enum_t>(v_out);
73 }
74 
75 /** @} */
76 } // namespace mrpt
void reverseBytes(const T &v_in, T &v_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
Definition: reverse_bytes.h:39
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
T toNativeEndianness(const T &v_in)
Definition: reverse_bytes.h:54
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void reverseBytesInPlace_enum(enum_t &v)
Definition: reverse_bytes.h:68



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020