MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
bit.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, 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
15#pragma once
16
18
19#if defined(__has_include)
20#if __has_include(<bit>) && (!defined(_MSVC_LANG) || _MSVC_LANG >= 202002L)
21
22// Prioritize using std::endian from C++20.
23#include <bit> // IWYU pragma: export
24
25#endif // __has_include(<bit>) && (!defined(_MSVC_LANG) || _MSVC_LANG >= 202002L)
26#elif defined(_WIN32)
27// Forward-compatibility with STL: https://github.com/microsoft/STL/blob/vs-2019-16.5/stl/inc/bit#L26
28#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
29// Predefined compiler macros: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
30#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L
31// Fallback to POSIX Issue 8: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html
32#include <endian.h>
33#elif defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
34// Fallback to OpenBSD/FreeBSD/etc.: https://www.man7.org/linux/man-pages/man3/endian.3.html
35// _BSD_SOURCE for compatibility with glibc 2.19 and older.
36#include <sys/endian.h>
37#endif
38
39namespace bsoncxx {
40namespace detail {
41
42// Equivalent to `std::endian` in C++20.
43enum class endian {
44#if defined(__cpp_lib_endian) && __cpp_lib_endian >= 201907L
45 little = static_cast<int>(std::endian::little),
46 big = static_cast<int>(std::endian::big),
47 native = static_cast<int>(std::endian::native)
48#elif defined(_WIN32)
49 // STL: https://github.com/microsoft/STL/blob/vs-2019-16.5/stl/inc/bit#L26
50 little = 0,
51 big = 1,
52 native = little,
53#elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)
54 // Predefined compiler macros for GCC and Clang.
55 little = __ORDER_LITTLE_ENDIAN__,
56 big = __ORDER_BIG_ENDIAN__,
57 native = __BYTE_ORDER__,
58#elif defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(BYTE_ORDER)
59 // Fallback to POSIX and OpenBSD/FreeBSD/etc.
60 little = LITTLE_ENDIAN,
61 big = BIG_ENDIAN,
62 native = BYTE_ORDER,
63#else
64#error "Could not determine the byte order for the target compiler architecture"
65#endif
66};
67
68} // namespace detail
69} // namespace bsoncxx
70
72
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
The top-level namespace within which all bsoncxx library entities are declared.