Skip to content

Commit 9b9261c

Browse files
committed
make utki::assert() do nothing in release build
1 parent e768f21 commit 9b9261c

2 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/utki/debug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const std::string_view colored_error_string = "\033[1;31merror\033[0m";
5454
const std::string_view uncolored_error_string = "error";
5555
} // namespace
5656

57-
void utki::assert(
57+
void utki::assert_always(
5858
bool condition,
5959
const std::function<void(std::ostream&)>& print,
6060
const utki::source_location& source_location

src/utki/debug.hpp

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ struct source_location {
166166

167167
namespace utki {
168168

169-
void assert(
169+
/**
170+
* @brief Assert that condion is true.
171+
* If the condition is true, then nothing happens.
172+
* If the condition is false, it will print the assertion failure message along with the information string
173+
* provided by the passed in function and then abort the program.
174+
* @param condition - condition to check for being true.
175+
* @param print - fucntion providing information string to print in case the assertion fails.
176+
* @param source_location - location of the assert() invocation in the source code.
177+
*/
178+
void assert_always(
170179
bool condition,
171180
const std::function<void(std::ostream&)>& print,
172181
const utki::source_location& source_location
@@ -175,6 +184,49 @@ void assert(
175184
#endif
176185
);
177186

187+
/**
188+
* @brief Assert that condion is true.
189+
* In debug build this function invokes assert_always().
190+
* In non-debug build this fuction does nothing.
191+
* @param condition - condition to check for being true.
192+
* @param print - fucntion providing information string to print in case the assertion fails.
193+
* @param source_location - location of the assert() invocation in the source code.
194+
*/
195+
#ifdef DEBUG
196+
inline void assert(
197+
bool condition,
198+
const std::function<void(std::ostream&)>& print,
199+
const utki::source_location& source_location
200+
# if CFG_CPP >= 20
201+
= std_source_location::current()
202+
# endif
203+
)
204+
{
205+
assert_always(
206+
condition, //
207+
print,
208+
source_location
209+
);
210+
}
211+
#else
212+
inline void assert(
213+
bool condition,
214+
const std::function<void(std::ostream&)>& print,
215+
const utki::source_location& source_location
216+
# if CFG_CPP >= 20
217+
= std_source_location::current()
218+
# endif
219+
)
220+
{}
221+
#endif
222+
223+
/**
224+
* @brief Assert that condion is true.
225+
* In debug build this function invokes assert_always().
226+
* In non-debug build this fuction does nothing.
227+
* @param condition - condition to check for being true.
228+
* @param source_location - location of the assert() invocation in the source code.
229+
*/
178230
inline void assert(
179231
bool condition,
180232
const utki::source_location& source_location
@@ -183,7 +235,11 @@ inline void assert(
183235
#endif
184236
)
185237
{
186-
utki::assert(condition, nullptr, source_location);
238+
utki::assert(
239+
condition, //
240+
nullptr,
241+
source_location
242+
);
187243
}
188244

189245
// MSVC compiler gives warning about implicit conversion of pointer to bool,
@@ -199,7 +255,11 @@ void assert(
199255
#endif
200256
)
201257
{
202-
assert(p != nullptr, print, source_location);
258+
assert(
259+
p != nullptr, //
260+
print,
261+
source_location
262+
);
203263
}
204264

205265
template <class object_type>
@@ -211,7 +271,11 @@ void assert(
211271
#endif
212272
)
213273
{
214-
assert(p != nullptr, nullptr, source_location);
274+
assert(
275+
p != nullptr, //
276+
nullptr,
277+
source_location
278+
);
215279
}
216280

217281
// smart pointers do not have implicit conversion to bool, so we need to define
@@ -227,7 +291,11 @@ void assert(
227291
#endif
228292
)
229293
{
230-
assert(p != nullptr, print, source_location);
294+
assert(
295+
p != nullptr, //
296+
print,
297+
source_location
298+
);
231299
}
232300

233301
template <class object_type>
@@ -239,7 +307,11 @@ void assert(
239307
#endif
240308
)
241309
{
242-
assert(p != nullptr, nullptr, source_location);
310+
assert(
311+
p != nullptr, //
312+
nullptr,
313+
source_location
314+
);
243315
}
244316

245317
template <class object_type>
@@ -252,7 +324,11 @@ void assert(
252324
#endif
253325
)
254326
{
255-
assert(p != nullptr, print, source_location);
327+
assert(
328+
p != nullptr, //
329+
print,
330+
source_location
331+
);
256332
}
257333

258334
template <class object_type>
@@ -264,7 +340,11 @@ void assert(
264340
#endif
265341
)
266342
{
267-
assert(p != nullptr, nullptr, source_location);
343+
assert(
344+
p != nullptr, //
345+
nullptr,
346+
source_location
347+
);
268348
}
269349

270350
// std::function does not have implicit conversion to bool, so we need to define
@@ -280,7 +360,11 @@ void assert(
280360
#endif
281361
)
282362
{
283-
assert(p != nullptr, print, source_location);
363+
assert(
364+
p != nullptr, //
365+
print,
366+
source_location
367+
);
284368
}
285369

286370
template <class func_type>
@@ -292,7 +376,11 @@ void assert(
292376
#endif
293377
)
294378
{
295-
assert(p != nullptr, nullptr, source_location);
379+
assert(
380+
p != nullptr, //
381+
nullptr,
382+
source_location
383+
);
296384
}
297385

298386
} // namespace utki

0 commit comments

Comments
 (0)