rcpputils  master
C++ API providing common utilities and data structures.
endian.hpp
Go to the documentation of this file.
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
24 #ifndef RCPPUTILS__ENDIAN_HPP_
25 #define RCPPUTILS__ENDIAN_HPP_
26 
27 // TODO(anyone) replace this macro when the appropriate C++20 value lands.
28 #if !defined(RCPPUTILS_NO_STD_ENDIAN) && (__cplusplus <= 201703L)
29 #define RCPPUTILS_NO_STD_ENDIAN
30 #endif
31 
32 #if !defined(RCPPUTILS_NO_STD_ENDIAN)
33 #include <type_traits>
34 namespace rcpputils
35 {
36 using std::endian;
37 } // namespace rcpputils
38 #define RCPPUTILS_HAVE_STD_ENDIAN 1
39 #endif // __cplusplus > 201703L
40 
41 #ifndef RCPPUTILS_HAVE_STD_ENDIAN
42 #ifdef __has_include
43 # if __has_include(<endian.h>)
44 # include <endian.h>
45 # endif
46 #endif
47 
48 namespace rcpputils
49 {
58 enum class endian
59 {
60 #ifdef _WIN32
61  little = 0,
62  big = 1,
63  native = little
64 #else
65  little = __ORDER_LITTLE_ENDIAN__,
66  big = __ORDER_BIG_ENDIAN__,
67  native = __BYTE_ORDER__
68 #endif
69 };
70 } // namespace rcpputils
71 #endif // RCPPUTILS_HAVE_STD_ENDIAN
72 #endif // RCPPUTILS__ENDIAN_HPP_
rcpputils
Definition: asserts.hpp:37
rcpputils::endian::native
@ native
rcpputils::endian::big
@ big
rcpputils::endian::little
@ little
rcpputils::endian
endian
Type traits for defining the endianness at compile-time.
Definition: endian.hpp:58