cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
endian.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdio>
4
5// Use C++20 endian check if available, otherwise fallback (e.g., linux
6// specific)
7#if __cplusplus >= 202002L && defined(__has_include) && __has_include(<bit>)
8// --- C++20 Standard Way (Ideal) ---
9# include <bit>
10# define CPP_TOOLBOX_ENDIAN_NAMESPACE std
11// Define helper macros based on standard enum
12constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE =
13 (std::endian::native == std::endian::little);
14constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG =
15 (std::endian::native == std::endian::big);
16
17#else
18// --- Pre-C++20 Fallback ---
19
20// Define the enum class locally if std::endian isn't available
21// We primarily need the values for comparison logic below.
23{
24enum class endian
25{
26 little,
27 big
28};
29} // namespace cpp_toolbox::detail
30# define CPP_TOOLBOX_ENDIAN_NAMESPACE cpp_toolbox::detail
31
32# if defined(_WIN32) || defined(_WIN64)
33 // Windows (MSVC, MinGW) is almost always little-endian on supported
34 // architectures (x86, x64, ARM)
35# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::little
36constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = true;
37constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = false;
38
39# elif defined(__APPLE__) && defined(__MACH__)
40 // macOS (Clang) on supported architectures (x86_64, arm64) is little-endian
41// Include <machine/endian.h> or rely on compiler builtins if available
42# include <machine/endian.h> // Standard macOS header for BYTE_ORDER etc.
43 // Check __BYTE_ORDER__ defensively, but default to little if check fails?
44 // Common case is LE.
45# if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
46# warning \
47 "Detected Big Endian macOS? This is unusual. Verify target architecture."
48# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::big
49constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = false;
50constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = true;
51# else
52# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::little
53constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = true;
54constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = false;
55# endif
56
57# elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
58 && defined(__ORDER_BIG_ENDIAN__)
59 // Common Linux/GCC/Clang way using predefined macros
60# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
61# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::little
62constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = true;
63constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = false;
64# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
65# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::big
66constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = false;
67constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = true;
68# else
69# error \
70 "Unknown machine endianness detected (__BYTE_ORDER__ defined but not LE/BE)."
71constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = false; // Avoid compilation error
72constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = false;
73# endif
74
75# else
76 // --- Fallback/Unknown ---
77# warning \
78 "Could not reliably determine platform endianness. Assuming Little Endian. Define CPP_TOOLBOX_FORCE_ENDIANNESS if necessary."
79 // Default assumption: Little Endian (most common desktop/mobile platform)
80# define CPP_TOOLBOX_NATIVE_ENDIAN_ENUM cpp_toolbox::detail::endian::little
81constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE = true;
82constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG = false;
83// Consider adding a compile error here if strictness is required:
84// #error "Cannot determine platform endianness. Please define
85// CPP_TOOLBOX_FORCE_ENDIANNESS (little or big)."
86
87# endif // Platform checks
88
89#endif // C++20 check
constexpr bool CPP_TOOLBOX_NATIVE_IS_LITTLE
Definition endian.hpp:81
constexpr bool CPP_TOOLBOX_NATIVE_IS_BIG
Definition endian.hpp:82
Definition endian.hpp:23
endian
Definition endian.hpp:25