hide ec_node structure
[protos/libecoli.git] / include / ecoli_assert.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * @defgroup assert Assert
7  * @{
8  *
9  * @brief Assertion helpers
10  *
11  * Helpers to check at runtime if a condition is true, or otherwise
12  * either abort (exit program) or return an error.
13  */
14
15 #ifndef ECOLI_ASSERT_
16 #define ECOLI_ASSERT_
17
18 #include <stdbool.h>
19
20 /**
21  * Abort if the condition is false.
22  *
23  * If expression is false this macro will prints an error message to
24  * standard error and terminates the program by calling abort(3).
25  *
26  * @param expr
27  *   The expression to be checked.
28  * @param args
29  *   The format string, optionally followed by other arguments.
30  */
31 #define ec_assert_print(expr, args...) \
32         __ec_assert_print(expr, #expr, args)
33
34 /* internal */
35 void __ec_assert_print(bool expr, const char *expr_str,
36                 const char *format, ...);
37
38 /**
39  * Check a condition or return.
40  *
41  * If the condition is true, do nothing. If it is false, set
42  * errno and return the specified value.
43  *
44  * @param cond
45  *   The condition to test.
46  * @param ret
47  *   The value to return.
48  * @param err
49  *   The errno to set.
50  */
51 #define EC_CHECK_ARG(cond, ret, err) do {                               \
52                 if (!(cond)) {                                          \
53                         errno = err;                                    \
54                         return ret;                                     \
55                 }                                                       \
56         } while(0)
57
58 #endif
59
60 /** @} */