Is it possible to distract this logic with a macro?

I have thousands of shells of functions that actually do the same logic, for example:

// a, b, ... are variable length parameters of different type

void API_Wrapper(hHandle, a, b, ..)
{
if (hHandle)
   return remote_API(hHandle, a, b, ..);
else
   return API(a, b, ..);
}

I want to use a macro to reuse if-else logic, so I can just implement a function like this:

void API_Wrapper(hHandle, a, b, ..)
{
    API_CALL(api_name, hHandle, a, b, ..); // API_CALL is a macro
}

I did not come up with a good way. (Note: I could solve this with ... and __va_args__, but this extension is not supported by the compiler we are currently using)

Has anyone ever encountered the same problem and any idea?

+3
source share
4 answers

Another trick without variable macros:

#define API_CALL(hHandle, api_name, arguments) if (hHandle) return remote_##api_name arguments; else return api_name arguments;

void API_Wrapper(int hHandle, int a, double b, char c)
{
            API_CALL(hHandle, api_name, (a, b, c));
}

What will happen:

void API_Wrapper(int hHandle, int a, double b, char c)
{
     if (hHandle) return remote_api_name (a, b, c); else return api_name (a, b, c);;
}
+1
source

API_CALL0, API_CALL1 ..:) -, API_Wrapper, btw

0
#define API_CALL(api_name, hHandle, ...) if (hHandle) remote##api_name(hHandle, __VA_ARGS__); else api_name(hHandle, __VA_ARGS__);

void API_Wrapper(int hHandle, int a, double b, char c)
{
            API_CALL(api_name, hHandle, a, b, c);
}

void API_Wrapper(int hHandle, int a, double b, char c)
{
     if (hHandle) remoteapi_name(hHandle, a, b, c); else api_name(hHandle, a, b, c);;
}
0

Using BOOST_PP_SEQ_XXXX, you can write your wrapper table in a way similar to this:

WRAP_API (myfunc1, param (int, k) param (double, r) param (char *, s))
                               ^ ^
                               ^ blank ^ blank

WRAP_API (myfunc2, param (int, k))

....

without variable macros, you can use a space-separated list, which is considered as one macro parameter + some preprocessing technologies encapsulated in boost pp

0
source

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


All Articles