qxLib
asserts.h
Go to the documentation of this file.
1 /**
2 
3  @file asserts.h
4  @author Khrapov
5  @date 27.12.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8  @section qx_assert_table Assert Types Comparison Table
9 
10  | Assertion type | Crash on failure | Remains in shipping (can have side effects) | Returns condition | Description |
11  |:--------------:|:----------------:|:-------------------------------------------:|:-----------------:|-------------|
12  | QX_ASSERT | + | - | - | Fast fail, similar to `assert()` |
13  | QX_VERIFY | + | + | - | Critical places: allocation errors, security checks, encryption, anti-cheat, etc. |
14  | QX_EXPECT | - | + | + | Defensive programming |
15  | QX_ENSURE | - | - | - | Possibly heavy checks that will not stay in a fast build |
16 
17 **/
18 #pragma once
19 
21 
22 #include <qx/asserts/asserts.inl>
23 
24 /**
25  @brief Use this as a condition in any macro above to indicate that this part of your code must never be executed
26 **/
27 #define QX_NO_ENTRY !"No entry"
28 
29 /**
30  @brief Use this as a condition in any macro above to indicate that this part of your code is not ready yet
31 **/
32 #define QX_NOT_IMPLEMENTED !"Not implemented"
33 
34 /**
35  @brief Verifies that the condition is true
36  @details QX_ASSERT macros generate fatal failures and disappear in shipping. See @ref qx_assert_table for details.
37  @param condition - a condition to check. if false, assert fails
38  @param category - a category to use for logging and reporting
39  @param ... - optional user message and its format args. the format string should be without QXT
40 **/
41 #define QX_ASSERT_C(condition, category, ...) _QX_ASSERT_C(condition, category, ##__VA_ARGS__)
42 
43 /**
44  @brief Verifies that the condition is true
45  @details QX_ASSERT macros generate fatal failures and disappear in shipping. See @ref qx_assert_table for details.
46  @param condition - a condition to check. if false, assert fails
47  @param ... - optional user message and its format args. the format string should be without QXT
48 **/
49 #define QX_ASSERT(condition, ...) QX_ASSERT_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
50 
51 /**
52  @brief Verifies that the condition is true
53  @details QX_VERIFY macros generate fatal failures and remain in shipping. See @ref qx_assert_table for details.
54  @param condition - a condition to check. if false, assert fails
55  @param category - a category to use for logging and reporting
56  @param ... - optional user message and its format args. the format string should be without QXT
57 **/
58 #define QX_VERIFY_C(condition, category, ...) \
59  _QX_COMMON_ASSERT( \
60  condition, \
61  category, \
62  qx::assert_type::verify, \
63  _QX_ASSERT_AFTER_DEBUG_BREAK_FATAL, \
64  void, \
65  ##__VA_ARGS__)
66 
67 /**
68  @brief Verifies that the condition is true
69  @details QX_VERIFY macros generate fatal failures and remain in shipping. See @ref qx_assert_table for details.
70  @param condition - a condition to check. if false, assert fails
71  @param ... - optional user message and its format args. the format string should be without QXT
72 **/
73 #define QX_VERIFY(condition, ...) QX_VERIFY_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
74 
75 /**
76  @brief Verifies that the condition is true
77  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
78  @param condition - a condition to check. if false, assert fails
79  @param category - a category to use for logging and reporting
80  @param ... - optional user message and its format args. the format string should be without QXT
81  @retval - condition
82 **/
83 #define QX_EXPECT_C(condition, category, ...) \
84  _QX_COMMON_ASSERT( \
85  condition, \
86  category, \
87  qx::assert_type::expect, \
88  _QX_ASSERT_AFTER_DEBUG_BREAK_NON_FATAL, \
89  bool, \
90  ##__VA_ARGS__)
91 
92 /**
93  @brief Verifies that the condition is true
94  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
95  @param condition - a condition to check. if false, assert fails
96  @param ... - optional user message and its format args. the format string should be without QXT
97  @retval - condition
98 **/
99 #define QX_EXPECT(condition, ...) QX_EXPECT_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
100 
101 /**
102  @brief Verifies that the condition is true and calls continue; otherwise
103  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
104  @param condition - a condition to check. if false, assert fails
105  @param category - a category to use for logging and reporting
106  @param ... - optional user message and its format args. the format string should be without QXT
107 **/
108 #define QX_EXPECT_CONTINUE_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, continue, ##__VA_ARGS__)
109 
110 /**
111  @brief Verifies that the condition is true and calls continue; otherwise
112  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
113  @param condition - a condition to check. if false, assert fails
114  @param ... - optional user message and its format args. the format string should be without QXT
115 **/
116 #define QX_EXPECT_CONTINUE(condition, ...) QX_EXPECT_CONTINUE_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
117 
118 /**
119  @brief Verifies that the condition is true and calls break; otherwise
120  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
121  @param condition - a condition to check. if false, assert fails
122  @param category - a category to use for logging and reporting
123  @param ... - optional user message and its format args. the format string should be without QXT
124 **/
125 #define QX_EXPECT_BREAK_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, break, ##__VA_ARGS__)
126 
127 /**
128  @brief Verifies that the condition is true and calls break; otherwise
129  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
130  @param condition - a condition to check. if false, assert fails
131  @param ... - optional user message and its format args. the format string should be without QXT
132 **/
133 #define QX_EXPECT_BREAK(condition, ...) QX_EXPECT_BREAK_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
134 
135 /**
136  @brief Verifies that the condition is true and calls return; otherwise
137  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
138  @param condition - a condition to check. if false, assert fails
139  @param category - a category to use for logging and reporting
140  @param ... - optional user message and its format args. the format string should be without QXT
141 **/
142 #define QX_EXPECT_RETURN_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, return, ##__VA_ARGS__)
143 
144 /**
145  @brief Verifies that the condition is true and calls return; otherwise
146  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
147  @param condition - a condition to check. if false, assert fails
148  @param ... - optional user message and its format args. the format string should be without QXT
149 **/
150 #define QX_EXPECT_RETURN(condition, ...) QX_EXPECT_RETURN_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
151 
152 /**
153  @brief Verifies that the condition is true and calls return return_value; otherwise
154  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
155  @param condition - a condition to check. if false, assert fails
156  @param category - a category to use for logging and reporting
157  @param return_value - a value to return in case of failure
158  @param ... - optional user message and its format args. the format string should be without QXT
159 **/
160 #define QX_EXPECT_RETURN_CT(condition, category, return_value, ...) \
161  _QX_EXPECT_ACTION(condition, category, return return_value, ##__VA_ARGS__)
162 
163 /**
164  @brief Verifies that the condition is true and calls return return_value; otherwise
165  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
166  @param condition - a condition to check. if false, assert fails
167  @param return_value - a value to return in case of failure
168  @param ... - optional user message and its format args. the format string should be without QXT
169 **/
170 #define QX_EXPECT_RETURN_T(condition, return_value, ...) \
171  QX_EXPECT_RETURN_CT(condition, QX_GET_FILE_CATEGORY(), return_value, ##__VA_ARGS__)
172 
173 /**
174  @brief Verifies that the condition is true and calls co_return; otherwise
175  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
176  @param condition - a condition to check. if false, assert fails
177  @param category - a category to use for logging and reporting
178  @param ... - optional user message and its format args. the format string should be without QXT
179 **/
180 #define QX_EXPECT_CO_RETURN_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, co_return, ##__VA_ARGS__)
181 
182 /**
183  @brief Verifies that the condition is true and calls co_return; otherwise
184  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
185  @param condition - a condition to check. if false, assert fails
186  @param ... - optional user message and its format args. the format string should be without QXT
187 **/
188 #define QX_EXPECT_CO_RETURN(condition, ...) QX_EXPECT_CO_RETURN_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
189 
190 /**
191  @brief Verifies that the condition is true and calls co_return return_value; otherwise
192  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
193  @param condition - a condition to check. if false, assert fails
194  @param category - a category to use for logging and reporting
195  @param return_value - a value to return in case of failure
196  @param ... - optional user message and its format args. the format string should be without QXT
197 **/
198 #define QX_EXPECT_CO_RETURN_CT(condition, category, return_value, ...) \
199  _QX_EXPECT_ACTION(condition, category, co_return return_value, ##__VA_ARGS__)
200 
201 /**
202  @brief Verifies that the condition is true and calls co_return return_value; otherwise
203  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
204  @param condition - a condition to check. if false, assert fails
205  @param return_value - a value to return in case of failure
206  @param ... - optional user message and its format args. the format string should be without QXT
207 **/
208 #define QX_EXPECT_CO_RETURN_T(condition, return_value, ...) \
209  QX_EXPECT_CO_RETURN_CT(condition, QX_GET_FILE_CATEGORY(), return_value, ##__VA_ARGS__)
210 
211 /**
212  @brief Verifies that the condition is true
213  @details QX_ENSURE macros generate nonfatal failures and disappear in shipping. See @ref qx_assert_table for details.
214  @param condition - a condition to check. if false, assert fails
215  @param category - a category to use for logging and reporting
216  @param ... - optional user message and its format args. the format string should be without QXT
217 **/
218 #define QX_ENSURE_C(condition, category, ...) _QX_ENSURE_C(condition, category, ##__VA_ARGS__)
219 
220 /**
221  @brief Verifies that the condition is true
222  @details QX_ENSURE macros generate nonfatal failures and disappear in shipping. See @ref qx_assert_table for details.
223  @param condition - a condition to check. if false, assert fails
224  @param ... - optional user message and its format args. the format string should be without QXT
225 **/
226 #define QX_ENSURE(condition, ...) QX_ENSURE_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)