Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement s4u_enforce
[simgrid.git] / include / xbt / asserts.hpp
1 /* Copyright (c) 2016-2022. 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 <exception>
10 #include <simgrid/Exception.hpp>
11 #include <xbt/log.h>
12
13 namespace simgrid {
14 namespace xbt {
15
16 /**
17  * @brief Those are the SimGrid version of the good ol' assert macro.
18  *
19  * You can pass them a format message and arguments, just as if it where a printf.
20  *
21  * If the statement evaluates to false, then a simgrid::AsertionError is thrown.
22  * This is identical to the xbt_assert macro, except that an exception is thrown instead of calling abort().
23  *
24  * Unlike the standard assert, s4u_enforce is never disabled, even if the macro NDEBUG is defined at compile time.
25  * Note however that this macro should *not* be used with a condition that has side effects, since the exception can be
26  * caught and ignored.
27  */
28 /** @brief The condition which failed will be displayed.
29     @hideinitializer  */
30 #define s4u_enforce(...) \
31   _XBT_IF_ONE_ARG(_s4u_enforce_ARG1, _s4u_enforce_ARGN, __VA_ARGS__)(__VA_ARGS__)
32 #define _s4u_enforce_ARG1(cond) _s4u_enforce_ARGN((cond), "Assertion %s failed", #cond)
33 #define _s4u_enforce_ARGN(cond, ...)                                                                                   \
34   do {                                                                                                                 \
35     if (!(cond)) {                                                                                                     \
36       throw simgrid::AssertionError(__VA_ARGS__);                                                                 \
37     }                                                                                                                  \
38   } while (0)
39
40
41 } // namespace xbt
42 } // namespace simgrid
43
44 #endif