Skip to content

Commit 9d8d952

Browse files
feat(utils): add not_null utility with source_location support
1 parent c4e6f9f commit 9d8d952

1 file changed

Lines changed: 102 additions & 14 deletions

File tree

wheel_utils/include/wheel_utils/logging.hxx

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,117 @@
22
#define LOGGING_HXX
33
#include "__wheel_utils_config.hxx"
44

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
88

99
WHEEL_UTILS_NAMESPACE
1010

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
1413

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"
1744
#endif
1845

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";
2285

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();
2588

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
27113

114+
#if defined(WHEEL_DEBUG)
115+
#if CURRENT_CPP_VERSION >= 202002L
28116
#define DEBUG_PRINT(msg) \
29117
do { \
30118
const auto fmt = std::format("[DEBUG] [{}:{}] {}", __LINE__, WHEEL_BASE_NAME(__FILE__), msg);\

0 commit comments

Comments
 (0)