add meson support
[protos/libecoli.git] / include / ecoli_assert.h
diff --git a/include/ecoli_assert.h b/include/ecoli_assert.h
new file mode 100644 (file)
index 0000000..fcd2186
--- /dev/null
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
+ */
+
+/**
+ * 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 <stdbool.h>
+
+/**
+ * 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