node_any: add a C api to match strvec attributes
[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  * Assert API
7  *
8  * Helpers to check at runtime if a condition is true, or otherwise
9  * either abort (exit program) or return an error.
10  */
11
12 #ifndef ECOLI_ASSERT_
13 #define ECOLI_ASSERT_
14
15 #include <stdbool.h>
16
17 /**
18  * Abort if the condition is false.
19  *
20  * If expression is false this macro will prints an error message to
21  * standard error and terminates the program by calling abort(3).
22  *
23  * @param expr
24  *   The expression to be checked.
25  * @param args
26  *   The format string, optionally followed by other arguments.
27  */
28 #define ec_assert_print(expr, args...) \
29         __ec_assert_print(expr, #expr, args)
30
31 /* internal */
32 void __ec_assert_print(bool expr, const char *expr_str,
33                 const char *format, ...);
34
35 /**
36  * Check a condition or return.
37  *
38  * If the condition is true, do nothing. If it is false, set
39  * errno and return the specified value.
40  *
41  * @param cond
42  *   The condition to test.
43  * @param ret
44  *   The value to return.
45  * @param err
46  *   The errno to set.
47  */
48 #define EC_CHECK_ARG(cond, ret, err) do {                               \
49                 if (!(cond)) {                                          \
50                         errno = err;                                    \
51                         return ret;                                     \
52                 }                                                       \
53         } while(0)
54
55 #endif