Maipa's Standard Library Extension 1.5.6
mstd
Loading...
Searching...
No Matches
assert_base.hpp
1/*
2 * mstd - Maipa's Standard Library
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/mstd/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _MSTD_ASSERT_BASE_HPP_
12 #define _MSTD_ASSERT_BASE_HPP_
13
14 #include <mstd/config.hpp>
15
17_MSTD_WARNING("this is only available for c++17 and greater!");
18 #else
19
21 #include <source_location>
22 #endif
23
24 #include <fmt/format.h>
25 #include <filesystem>
26 #include <string_view>
27
28 #include <mstd/function_view.hpp>
29
30 #pragma region DEBUGBREAK
31 #if _DEBUG
32 #ifdef _WIN32
33 #define WIN32_LEAN_AND_MEAN
34 #define NOMINMAX
35 #include <crtdbg.h>
36 #include <Windows.h>
37 #define _MSTD_DEBUGBREAK() __debugbreak()
38 #elif defined(__linux__) || defined(__APPLE__)
39 #include <csignal>
40 #define _MSTD_DEBUGBREAK() __builtin_trap()
41 #else
42 #include <cstdlib>
43 #define _MSTD_DEBUGBREAK() std::abort()
44 #endif
45 #else
46 #define _MSTD_DEBUGBREAK() ((void)0)
47 #endif
48 #pragma endregion
49
50 #pragma region ASSERT_DEFINES
52 #define _MSTD_SOURCE_LOCATION_ARG const std::source_location& location
53 #define _MSTD_GET_SOURCE_FILE_NAME location.file_name()
54 #define _MSTD_GET_SOURCE_FUNC_NAME location.function_name()
55 #define _MSTD_GET_SOURCE_LINE location.line()
56 #define _MSTD_GET_SOURCE_COLUMN location.column()
57 #define _MSTD_PASS_SOURCE_LOCATION location
58 #define _MSTD_GET_CURRENT_SOURCE_LOCATION ::std::source_location::current()
59 #else
60 #define _MSTD_SOURCE_LOCATION_ARG const std::string_view &file_name, size_t line, const std::string_view &func_name
61 #define _MSTD_GET_SOURCE_FILE_NAME file_name.data()
62 #define _MSTD_GET_SOURCE_FUNC_NAME func_name.data()
63 #define _MSTD_GET_SOURCE_LINE line
64 #define _MSTD_GET_SOURCE_COLUMN 0
65 #define _MSTD_PASS_SOURCE_LOCATION file_name, line, func_name
66 #define _MSTD_GET_CURRENT_SOURCE_LOCATION __FILE__, __LINE__, __FUNCTION__
67 #endif
68 #pragma endregion // ASSERT_DEFINES
69
70 #pragma region ASSERT_BASE
71 #define MSTD_STOP_ASSERT_BASE(expression, log_error_func, ...)
72 do {
73 if (!(expression)) {
74 if (::mstd::stop_assert_handler(#expression, log_error_func,
75 _MSTD_GET_CURRENT_SOURCE_LOCATION __VA_OPT__(, ) __VA_ARGS__)) {
77 }
78 }
79 } while (0)
80
81 #define MSTD_LOG_ASSERT_BASE(expression, log_error_func, ...)
82 if (!(expression))
83 ::mstd::log_assert_handler(#expression, log_error_func, _MSTD_GET_CURRENT_SOURCE_LOCATION __VA_OPT__(, ) __VA_ARGS__)
84
85 #define MSTD_EMPTY_ASSERT_BASE(expression, log_error_func, ...) ((void)0)
86 #pragma endregion
87
88 #pragma region FUNCTIONS_DEFINES
89
90namespace mstd {
91 #pragma region FORMAT_LOG
92
94 const std::string_view message = "") noexcept {
95 const std::string shortFile = std::filesystem::path(_MSTD_GET_SOURCE_FILE_NAME).filename().string();
96 std::string fullMessage = fmt::format("Assertion failed [{}:{}:{},{}]: {}", shortFile, _MSTD_GET_SOURCE_FUNC_NAME,
98
99 if (!message.empty()) { fullMessage += fmt::format("\nmessage: {}", message); }
100 return fullMessage;
101 }
102
103 #pragma endregion // FORMAT_LOG
104
105 #pragma region REPORT_TO_SYSTEM
106 #if _DEBUG
108 #ifdef _WIN32
110 message.data()) == 1;
111 #else
112 return true;
113 #endif // _WIN32
114 }
115 #endif
116 #pragma endregion // REPORT_TO_SYSTEM
117
118 #pragma region ASSERT_HANDLER_NO_MSG
119
120 inline void log_assert_handler(const std::string_view expression,
121 const mstd::function_view<void(const std::string_view)>& errorLogger, _MSTD_SOURCE_LOCATION_ARG) noexcept {
122 const std::string msg = format_log(expression, _MSTD_PASS_SOURCE_LOCATION);
123 errorLogger(msg);
124 }
125
126 inline bool stop_assert_handler(const std::string_view expression,
127 const mstd::function_view<void(const std::string_view)>& errorLogger, _MSTD_SOURCE_LOCATION_ARG) noexcept {
128 const std::string msg = format_log(expression, _MSTD_PASS_SOURCE_LOCATION);
129 errorLogger(msg);
130 #if _DEBUG
131 return report_to_system(_MSTD_PASS_SOURCE_LOCATION, msg);
132 #else
133 return true;
134 #endif
135 }
136
137 #pragma endregion // ASSERT_HANDLER_NO_MSG
138
139 #pragma region ASSERT_HANDLER_MSG
140
141 template<class... Args>
142 inline void log_assert_handler(const std::string_view expression,
143 const mstd::function_view<void(const std::string_view)>& errorLogger, _MSTD_SOURCE_LOCATION_ARG,
144 fmt::format_string<Args...> fmtFormat, Args&&... args) noexcept {
148 }
149
150 template<class... Args>
151 inline bool stop_assert_handler(const std::string_view expression,
152 const mstd::function_view<void(const std::string_view)>& errorLogger, _MSTD_SOURCE_LOCATION_ARG,
153 fmt::format_string<Args...> fmtFormat, Args&&... args) noexcept {
157 #if _DEBUG
159 #else
160 return true;
161 #endif
162 }
163
164 #pragma endregion // ASSERT_HANDLER_MSG
165} // namespace mstd
166
167 #pragma endregion
168
169 #endif
170#endif
#define _MSTD_GET_SOURCE_FILE_NAME
Definition assert_base.hpp:53
#define _MSTD_GET_SOURCE_LINE
Definition assert_base.hpp:55
#define _MSTD_GET_SOURCE_FUNC_NAME
Definition assert_base.hpp:54
#define _MSTD_GET_SOURCE_COLUMN
Definition assert_base.hpp:56
#define _MSTD_GET_CURRENT_SOURCE_LOCATION
Definition assert_base.hpp:58
#define _MSTD_PASS_SOURCE_LOCATION
Definition assert_base.hpp:57
#define _MSTD_DEBUGBREAK()
Definition assert_base.hpp:46
#define _MSTD_SOURCE_LOCATION_ARG
Definition assert_base.hpp:52
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_HAS_CXX20
Definition config.hpp:52
Definition arithmetic_types.hpp:23
void log_assert_handler(const std::string_view expression, const mstd::function_view< void(const std::string_view)> &errorLogger, _MSTD_SOURCE_LOCATION_ARG, fmt::format_string< Args... > fmtFormat, Args &&... args) noexcept
Definition assert_base.hpp:142
bool stop_assert_handler(const std::string_view expression, const mstd::function_view< void(const std::string_view)> &errorLogger, _MSTD_SOURCE_LOCATION_ARG, fmt::format_string< Args... > fmtFormat, Args &&... args) noexcept
Definition assert_base.hpp:151
std::string format_log(const std::string_view expression, _MSTD_SOURCE_LOCATION_ARG, const std::string_view message="") noexcept
Definition assert_base.hpp:93
void log_assert_handler(const std::string_view expression, const mstd::function_view< void(const std::string_view)> &errorLogger, _MSTD_SOURCE_LOCATION_ARG) noexcept
Definition assert_base.hpp:120
bool stop_assert_handler(const std::string_view expression, const mstd::function_view< void(const std::string_view)> &errorLogger, _MSTD_SOURCE_LOCATION_ARG) noexcept
Definition assert_base.hpp:126