Is there a way to provide a single macro function to return values ​​of different types, including nothing?

I created the following macros to lock the mutex and return (from the function in which this macro is called) in the event of a failed lock attempt. Currently, I have narrowed it down to two macros - one for returning from functions that return a value, regardless of type, and another for returning from functions that don't return anything (i.e. Void).

The code outside the macros (see below) is for illustration only and has very little to do with the actual production code in which the macros will be used.

#define MUTEX_LOCK()\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return;\ }\ } #define MUTEX_LOCK_RVAL(err_val)\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return err_val;\ }\ } void vfunc() { printf("\nIn vfunc()\n"); MUTEX_LOCK(); printf("\nOut of vfunc()\n"); } UINT16 uint16func() { printf("\nIn uint16func()\n"); MUTEX_LOCK_RVAL(0); printf("\nOut of uint16func()\n"); return 9; } CHAR* errstr = "Hoo boy!"; CHAR* strfunc() { printf("\nIn strfunc()\n"); MUTEX_LOCK_RVAL(errstr); printf("\nOut of strfunc()\n"); return NULL; } 

Is there a way to reduce them to a single macro that can be used in functions that return a value, as well as void.

+5
source share
3 answers

To make it compatible with ANSI, I define a return macro and another simple character that evaluates to null and clearly shows that it is zero. I.e:.

 #define VOID_RET //This nulls the return value #define MUTEX_LOCK(err_val)\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return err_val;\ }\ } void *mutex = NULL; void vfunc(void) { printf("\nIn vfunc()\n"); MUTEX_LOCK(VOID_RET); printf("\nOut of vfunc()\n"); } UINT16 uint16func(void) { printf("\nIn uint16func()\n"); MUTEX_LOCK(0); printf("\nOut of uint16func()\n"); return 9; } CHAR* errstr = "Hoo boy!"; CHAR* strfunc(void) { printf("\nIn strfunc()\n"); MUTEX_LOCK(errstr); printf("\nOut of strfunc()\n"); return NULL; } 

I tested this code under C11 and C99.

+4
source

As far as I can tell from the documentation, you can remove a macro function that does not accept an argument, because you are not required to pass an argument to a macro function that expects arguments.
From the GNU GCC Documentation :

You can leave the macro arguments blank; this is not a mistake for the preprocessor [...]


For a few arguments, this is interesting:

You cannot completely ignore arguments; if a macro takes two arguments, there must be exactly one comma at the top level of its argument list.

With these examples:

 min(, b) ==> (( ) < (b) ? ( ) : (b)) min(a, ) ==> ((a ) < ( ) ? (a ) : ( )) min(,) ==> (( ) < ( ) ? ( ) : ( )) min((,),) ==> (((,)) < ( ) ? ((,)) : ( )) 
+2
source

The only solution I can imagine is the following:

 #define MUTEX_LOCK( err_val )\ {\ {\ if (pthread_mutex_lock(&mutex) != 0)\ {\ printf("Failed to lock mutex.\n");\ return err_val;\ }\ }\ } int test_int() { MUTEX_LOCK( 1 ); return 0; } void test_void() { MUTEX_LOCK( ; ); return; } 
+2
source

Source: https://habr.com/ru/post/1239622/


All Articles