X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=include%2Fecoli_assert.h;fp=include%2Fecoli_assert.h;h=fcd218628fc433b5cee60b6f59eb71487c0b9858;hb=18d03456d96f7a086a2ccc82ce97fcf056848d90;hp=0000000000000000000000000000000000000000;hpb=a1571d413d2acac5d4a4fbdf2e50b2d1a6da3aa6;p=protos%2Flibecoli.git diff --git a/include/ecoli_assert.h b/include/ecoli_assert.h new file mode 100644 index 0000000..fcd2186 --- /dev/null +++ b/include/ecoli_assert.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +/** + * Assert API + * + * Helpers to check at runtime if a condition is true, or otherwise + * either abort (exit program) or return an error. + */ + +#ifndef ECOLI_ASSERT_ +#define ECOLI_ASSERT_ + +#include + +/** + * Abort if the condition is false. + * + * If expression is false this macro will prints an error message to + * standard error and terminates the program by calling abort(3). + * + * @param expr + * The expression to be checked. + * @param args + * The format string, optionally followed by other arguments. + */ +#define ec_assert_print(expr, args...) \ + __ec_assert_print(expr, #expr, args) + +/* internal */ +void __ec_assert_print(bool expr, const char *expr_str, + const char *format, ...); + +/** + * Check a condition or return. + * + * If the condition is true, do nothing. If it is false, set + * errno and return the specified value. + * + * @param cond + * The condition to test. + * @param ret + * The value to return. + * @param err + * The errno to set. + */ +#define EC_CHECK_ARG(cond, ret, err) do { \ + if (!(cond)) { \ + errno = err; \ + return ret; \ + } \ + } while(0) + +#endif