Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / xbt / asserts.hpp
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_XBT_ASSERTS_HPP
7 #define SIMGRID_XBT_ASSERTS_HPP
8
9 #include <simgrid/Exception.hpp>
10
11 /**
12  * @brief Those are the SimGrid version of the good ol' assert macro.
13  *
14  * You can pass them a format message and arguments, just as if it where a printf.
15  *
16  * If the statement evaluates to false, then a simgrid::AsertionError is thrown.
17  * This is identical to the xbt_assert macro, except that an exception is thrown instead of calling abort().
18  *
19  * Unlike the standard assert, xbt_enforce is never disabled, even if the macro NDEBUG is defined at compile time.
20  * Note however that this macro should *not* be used with a condition that has side effects, since the exception can be
21  * caught and ignored.
22  */
23 /** @brief The condition which failed will be displayed.
24     @hideinitializer  */
25 #define xbt_enforce(...) \
26   _XBT_IF_ONE_ARG(_xbt_enforce_ARG1, _xbt_enforce_ARGN, __VA_ARGS__)(__VA_ARGS__)
27 #define _xbt_enforce_ARG1(cond) _xbt_enforce_ARGN((cond), "Assertion %s failed", #cond)
28 #define _xbt_enforce_ARGN(cond, ...)                                                                                   \
29   do {                                                                                                                 \
30     if (not(cond)) {                                                                                                   \
31       throw simgrid::AssertionError(XBT_THROW_POINT, xbt::string_printf(__VA_ARGS__));                                 \
32     }                                                                                                                  \
33   } while (0)
34
35 #endif