|
2 | 2 | #define LOGGING_HXX |
3 | 3 | #include "__wheel_utils_config.hxx" |
4 | 4 |
|
5 | | -#include <iostream> |
6 | | -#include <format> |
7 | | -#include <cstring> |
| 5 | +#include <iostream> // IWYU pragma: keep |
| 6 | +#include <format> // IWYU pragma: keep |
| 7 | +#include <cstring> // IWYU pragma: keep |
8 | 8 |
|
9 | 9 | WHEEL_UTILS_NAMESPACE |
10 | 10 |
|
11 | | -#if defined(WHEEL_DEBUG) |
12 | | - #if defined(_MSVC_LANG) |
13 | | - #define CURRENT_CPP_VERSION _MSVC_LANG |
| 11 | + #if defined(_MSVC_LANG) |
| 12 | + #define CURRENT_CPP_VERSION _MSVC_LANG |
14 | 13 |
|
15 | | - #else |
16 | | - #define CURRENT_CPP_VERSION __cplusplus |
| 14 | +#else |
| 15 | + #define CURRENT_CPP_VERSION __cplusplus |
| 16 | +#endif |
| 17 | + |
| 18 | +#if defined(__clang__) || defined(__GNUC__) |
| 19 | + #define WHEEL_ALWAYS_INLINE __attribute__((always_inline)) inline |
| 20 | + |
| 21 | +#elif defined(_MSC_VER) |
| 22 | + #define WHEEL_ALWAYS_INLINE __forceinline |
| 23 | +#else |
| 24 | + #warning "WHEEL_ALWAYS_INLINE macro is not supported on this compiler. Peformance may be degraded." |
| 25 | +#endif |
| 26 | + |
| 27 | + #if defined(_WIN32) |
| 28 | + #define WHEEL_BASE_NAME(path) ((std::strrchr(path, '\\')) ? std::strrchr(path, '\\') + 1 : path) |
| 29 | + |
| 30 | + #else |
| 31 | + #define WHEEL_BASE_NAME(path) ((std::strrchr(path, '/')) ? std::strrchr(path, '/') + 1 : path) |
| 32 | +#endif |
| 33 | + |
| 34 | +#if !defined (WHEEL_NO_FAIL_FAST) |
| 35 | + #define NOT_NULL_CONTEXT void |
| 36 | + |
| 37 | +#else |
| 38 | + #define NOT_NULL_CONTEXT bool |
| 39 | +#endif |
| 40 | + |
| 41 | +#if defined(WHEEL_ASSERTION) |
| 42 | + #if CURRENT_CPP_VERSION < 202002L && defined (WHEEL_SOURCE_LOCATION) |
| 43 | + #error "WHEEL_SOURCE_LOCATION only supports C++ version >= 20" |
17 | 44 | #endif |
18 | 45 |
|
19 | | - #if CURRENT_CPP_VERSION >= 202002L |
20 | | - #if defined(_WIN32) |
21 | | - #define WHEEL_BASE_NAME(path) ((std::strrchr(path, '\\')) ? std::strrchr(path, '\\') + 1 : path) |
| 46 | + #if defined (WHEEL_SOURCE_LOCATION) |
| 47 | + #include <type_traits> |
| 48 | + #include <source_location> |
| 49 | + |
| 50 | + template<typename T> |
| 51 | + requires ::std::is_pointer_v<T> |
| 52 | + WHEEL_ALWAYS_INLINE void not_null(T ptr, const std::source_location current = std::source_location::current()) { |
| 53 | + if (ptr == nullptr) [[unlikely]] { |
| 54 | + const auto info_fmt = ::std::format( |
| 55 | + "[NULL_CRITICAL] [{}:{}:{}]", |
| 56 | + current.file_name(), current.function_name(), |
| 57 | + current.line() |
| 58 | + ); |
| 59 | + ::std::cerr << ::std::format("{} Null pointer is not allowed in this context.\n", info_fmt); |
| 60 | + ::std::abort(); |
| 61 | + } |
| 62 | + } |
| 63 | + #endif |
| 64 | + |
| 65 | + #if defined (WHEEL_EXPERIMENT) |
| 66 | + template<typename T> |
| 67 | + struct is_pointer { |
| 68 | + static const bool value = false; |
| 69 | + }; |
| 70 | + |
| 71 | + template<typename T> |
| 72 | + struct is_pointer<T*> { |
| 73 | + static const bool value = true; |
| 74 | + }; |
| 75 | + |
| 76 | + template<typename T> |
| 77 | + inline constexpr bool is_pointer_value = is_pointer<T>::value; |
| 78 | + |
| 79 | + template<typename T> |
| 80 | + WHEEL_ALWAYS_INLINE NOT_NULL_CONTEXT not_null(T ptr) { |
| 81 | + static_assert(is_pointer_value<T> == true, "T must be a pointer"); |
| 82 | + |
| 83 | + if (ptr == nullptr) { |
| 84 | + ::std::cerr << "Null pointer is not allowed in this context.\n"; |
22 | 85 |
|
23 | | - #else |
24 | | - #define WHEEL_BASE_NAME(path) ((std::strrchr(path, '/')) ? std::strrchr(path, '/') + 1 : path) |
| 86 | + #if !defined (WHEEL_NO_FAIL_FAST) |
| 87 | + ::std::abort(); |
25 | 88 |
|
26 | | - #endif |
| 89 | + #else |
| 90 | + return false; |
| 91 | + #endif |
| 92 | + } |
| 93 | + |
| 94 | + #if defined (WHEEL_NO_FAIL_FAST) |
| 95 | + return true; |
| 96 | + #endif |
| 97 | + } |
| 98 | + #endif |
| 99 | + |
| 100 | + #if CURRENT_CPP_VERSION >= 202002L && !defined(WHEEL_SOURCE_LOCATION) && !defined (WHEEL_EXPERIMENT) |
| 101 | + #include <type_traits> |
| 102 | + |
| 103 | + template<typename T> |
| 104 | + requires ::std::is_pointer_v<T> |
| 105 | + WHEEL_ALWAYS_INLINE void not_null(T ptr) { |
| 106 | + if (ptr == nullptr) [[unlikely]] { |
| 107 | + ::std::cerr << "Null pointer is not allowed in this context.\n"; |
| 108 | + ::std::abort(); |
| 109 | + } |
| 110 | + } |
| 111 | + #endif |
| 112 | +#endif |
27 | 113 |
|
| 114 | +#if defined(WHEEL_DEBUG) |
| 115 | + #if CURRENT_CPP_VERSION >= 202002L |
28 | 116 | #define DEBUG_PRINT(msg) \ |
29 | 117 | do { \ |
30 | 118 | const auto fmt = std::format("[DEBUG] [{}:{}] {}", __LINE__, WHEEL_BASE_NAME(__FILE__), msg);\ |
|
0 commit comments