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 
23 
24 /**
25  @brief Verifies that the condition is true
26  @details QX_ASSERT macros generate fatal failures and disappear in shipping. See @ref qx_assert_table for details.
27  @param condition - a condition to check. if false, assert fails
28  @param category - a category to use for logging and reporting
29  @param ... - optional user message and its format args. the format string should be without QXT
30 **/
31 #define QX_ASSERT_C(condition, category, ...) _QX_ASSERT_C(condition, category, ##__VA_ARGS__)
32 
33 /**
34  @brief Verifies that the condition is true
35  @details QX_ASSERT macros generate fatal failures and disappear in shipping. See @ref qx_assert_table for details.
36  @param condition - a condition to check. if false, assert fails
37  @param ... - optional user message and its format args. the format string should be without QXT
38 **/
39 #define QX_ASSERT(condition, ...) QX_ASSERT_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
40 
41 /**
42  @brief Verifies that the condition is true
43  @details QX_VERIFY macros generate fatal failures and remain in shipping. See @ref qx_assert_table for details.
44  @param condition - a condition to check. if false, assert fails
45  @param category - a category to use for logging and reporting
46  @param ... - optional user message and its format args. the format string should be without QXT
47 **/
48 #define QX_VERIFY_C(condition, category, ...) \
49  _QX_COMMON_ASSERT( \
50  condition, \
51  category, \
52  qx::assert_type::verify, \
53  _QX_ASSERT_AFTER_DEBUG_BREAK_FATAL, \
54  void, \
55  ##__VA_ARGS__)
56 
57 /**
58  @brief Verifies that the condition is true
59  @details QX_VERIFY macros generate fatal failures and remain in shipping. See @ref qx_assert_table for details.
60  @param condition - a condition to check. if false, assert fails
61  @param ... - optional user message and its format args. the format string should be without QXT
62 **/
63 #define QX_VERIFY(condition, ...) QX_VERIFY_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
64 
65 /**
66  @brief Verifies that the condition is true
67  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
68  @param condition - a condition to check. if false, assert fails
69  @param category - a category to use for logging and reporting
70  @param ... - optional user message and its format args. the format string should be without QXT
71  @retval - condition
72 **/
73 #define QX_EXPECT_C(condition, category, ...) \
74  _QX_COMMON_ASSERT( \
75  condition, \
76  category, \
77  qx::assert_type::expect, \
78  _QX_ASSERT_AFTER_DEBUG_BREAK_NON_FATAL, \
79  bool, \
80  ##__VA_ARGS__)
81 
82 /**
83  @brief Verifies that the condition is true
84  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
85  @param condition - a condition to check. if false, assert fails
86  @param ... - optional user message and its format args. the format string should be without QXT
87  @retval - condition
88 **/
89 #define QX_EXPECT(condition, ...) QX_EXPECT_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
90 
91 /**
92  @brief Verifies that the condition is true and calls continue; otherwise
93  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
94  @param condition - a condition to check. if false, assert fails
95  @param category - a category to use for logging and reporting
96  @param ... - optional user message and its format args. the format string should be without QXT
97 **/
98 #define QX_EXPECT_CONTINUE_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, continue, ##__VA_ARGS__)
99 
100 /**
101  @brief Verifies that the condition is true and calls continue; otherwise
102  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
103  @param condition - a condition to check. if false, assert fails
104  @param ... - optional user message and its format args. the format string should be without QXT
105 **/
106 #define QX_EXPECT_CONTINUE(condition, ...) QX_EXPECT_CONTINUE_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
107 
108 /**
109  @brief Verifies that the condition is true and calls break; otherwise
110  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
111  @param condition - a condition to check. if false, assert fails
112  @param category - a category to use for logging and reporting
113  @param ... - optional user message and its format args. the format string should be without QXT
114 **/
115 #define QX_EXPECT_BREAK_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, break, ##__VA_ARGS__)
116 
117 /**
118  @brief Verifies that the condition is true and calls break; otherwise
119  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
120  @param condition - a condition to check. if false, assert fails
121  @param ... - optional user message and its format args. the format string should be without QXT
122 **/
123 #define QX_EXPECT_BREAK(condition, ...) QX_EXPECT_BREAK_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
124 
125 /**
126  @brief Verifies that the condition is true and calls return; otherwise
127  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
128  @param condition - a condition to check. if false, assert fails
129  @param category - a category to use for logging and reporting
130  @param ... - optional user message and its format args. the format string should be without QXT
131 **/
132 #define QX_EXPECT_RETURN_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, return, ##__VA_ARGS__)
133 
134 /**
135  @brief Verifies that the condition is true and calls return; otherwise
136  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
137  @param condition - a condition to check. if false, assert fails
138  @param ... - optional user message and its format args. the format string should be without QXT
139 **/
140 #define QX_EXPECT_RETURN(condition, ...) QX_EXPECT_RETURN_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
141 
142 /**
143  @brief Verifies that the condition is true and calls return return_value; otherwise
144  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
145  @param condition - a condition to check. if false, assert fails
146  @param category - a category to use for logging and reporting
147  @param return_value - a value to return in case of failure
148  @param ... - optional user message and its format args. the format string should be without QXT
149 **/
150 #define QX_EXPECT_RETURN_CT(condition, category, return_value, ...) \
151  _QX_EXPECT_ACTION(condition, category, return return_value, ##__VA_ARGS__)
152 
153 /**
154  @brief Verifies that the condition is true and calls return return_value; otherwise
155  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
156  @param condition - a condition to check. if false, assert fails
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_T(condition, return_value, ...) \
161  QX_EXPECT_RETURN_CT(condition, QX_GET_FILE_CATEGORY(), return_value, ##__VA_ARGS__)
162 
163 /**
164  @brief Verifies that the condition is true and calls co_return; 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 category - a category to use for logging and reporting
168  @param ... - optional user message and its format args. the format string should be without QXT
169 **/
170 #define QX_EXPECT_CO_RETURN_C(condition, category, ...) _QX_EXPECT_ACTION(condition, category, co_return, ##__VA_ARGS__)
171 
172 /**
173  @brief Verifies that the condition is true and calls co_return; otherwise
174  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
175  @param condition - a condition to check. if false, assert fails
176  @param ... - optional user message and its format args. the format string should be without QXT
177 **/
178 #define QX_EXPECT_CO_RETURN(condition, ...) QX_EXPECT_CO_RETURN_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)
179 
180 /**
181  @brief Verifies that the condition is true and calls co_return return_value; otherwise
182  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
183  @param condition - a condition to check. if false, assert fails
184  @param category - a category to use for logging and reporting
185  @param return_value - a value to return in case of failure
186  @param ... - optional user message and its format args. the format string should be without QXT
187 **/
188 #define QX_EXPECT_CO_RETURN_CT(condition, category, return_value, ...) \
189  _QX_EXPECT_ACTION(condition, category, co_return return_value, ##__VA_ARGS__)
190 
191 /**
192  @brief Verifies that the condition is true and calls co_return return_value; otherwise
193  @details QX_EXPECT macros generate nonfatal failures and remain in shipping. See @ref qx_assert_table for details.
194  @param condition - a condition to check. if false, assert fails
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_T(condition, return_value, ...) \
199  QX_EXPECT_CO_RETURN_CT(condition, QX_GET_FILE_CATEGORY(), return_value, ##__VA_ARGS__)
200 
201 /**
202  @brief Verifies that the condition is true
203  @details QX_ENSURE macros generate nonfatal failures and disappear in shipping. See @ref qx_assert_table for details.
204  @param condition - a condition to check. if false, assert fails
205  @param category - a category to use for logging and reporting
206  @param ... - optional user message and its format args. the format string should be without QXT
207 **/
208 #define QX_ENSURE_C(condition, category, ...) _QX_ENSURE_C(condition, category, ##__VA_ARGS__)
209 
210 /**
211  @brief Verifies that the condition is true
212  @details QX_ENSURE macros generate nonfatal failures and disappear in shipping. See @ref qx_assert_table for details.
213  @param condition - a condition to check. if false, assert fails
214  @param ... - optional user message and its format args. the format string should be without QXT
215 **/
216 #define QX_ENSURE(condition, ...) QX_ENSURE_C(condition, QX_GET_FILE_CATEGORY(), ##__VA_ARGS__)