From edbcecb272fe268c07abd3e92f4a93efa56ed64b Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 16 Dec 2020 11:06:11 +0100 Subject: [PATCH] Add support for else condition in M_LET_IF. Fix also a bug in computing unique variable per line --- m-core.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/m-core.h b/m-core.h index 1f297b36..a925136a 100644 --- a/m-core.h +++ b/m-core.h @@ -3141,19 +3141,23 @@ m_core_parse2_enum (const char str[], const char **endptr) /* Declare a variable, initialize it, continue if the initialization succeeds, and clears the variable afterwards. - Otherwise, stop the execution. - M_LET_IF(init code, test code, clear code) { code using the variable } + Otherwise, stop the execution and execute else_code if defined. + M_LET_IF(init code, test code, clear code[, else_code]) { code using the variable } Example: M_LET_IF(void * p = malloc(100), p!=0, free(p)) { // code using p } // Here p is free */ -#define M_LET_IF(init, test, clear) \ - M_LET_IF_INTERNAL(init, test, clear, m_var_ ## __LINE__) +#define M_LET_IF(init, test, ...) \ + M_IF_NARGS_EQ1(__VA_ARGS__) \ + ( \ + M_LET_IF_INTERNAL (init, test, __VA_ARGS__, (void)0, M_C(m_var_, __LINE__)), \ + M_LET_IF_INTERNAL (init, test, __VA_ARGS__, M_C(m_var_, __LINE__)) \ + ) -#define M_LET_IF_INTERNAL(init, test, clear, cont) \ +#define M_LET_IF_INTERNAL(init, test, clear, else_a, cont) \ for(bool cont = true; cont; cont = false) \ - for( init ; cont && (test) ; (clear)) \ + for( init ; cont && ( (test) || (else_a, false) ); (clear)) \ for( (void) 0; cont; cont = false) \