May 29, 2019
[func_wrap_macro.c]
// gcc -g -Wall -o func_wrap_macro func_wrap_macro.c
#include <stdio.h>
// External function examples.
// Actually, it will be "read", "send" and so on.
static int sample_func1( char c )
{
return( 999 );
}
static void sample_func2( int n )
{
return;
}
// Function wrapping macros.
// Use them in your codes where the external functions are called.
#define FUNC_WRAP(func_param) FUNC_WRAP_##func_param
#define FUNC_WRAP_VOID(func_param) FUNC_WRAP_VOID_##func_param
// Example of function wrapper declarating macros.
// It should be enough with these two.
// You can customize the implementation.
// For example, time-measurement function calls can be added.
#define DECLARE_WRAP( type, func, var, param ) \
type (FUNC_WRAP_##func)var { \
type ret = (func)param; \
printf("%s-%d\n", #func, ret); \
return ret; \
}
#define DECLARE_WRAP_VOID( func, var, param ) \
void (FUNC_WRAP_VOID_##func)var { \
(func)param; \
printf("%s\n", #func); \
return; \
}
// Function wrapper declarations.
// Put them for the number of external functions you will use.
DECLARE_WRAP( int, sample_func1, (char c), (c))
DECLARE_WRAP_VOID( sample_func2, (int n), (n))
// Example of use.
int main()
{
char tmpC = 0;
int tmpN = 1;
int ret = 0;
ret = FUNC_WRAP(sample_func1(tmpC));
FUNC_WRAP_VOID(sample_func2(tmpN));
printf("ret = %d\n", ret);
return( 0 );
}