I want to create a define to parse function signature and using Boost Preprocessor create something like this:
MY_DEFINE std::string fun(int t, float b)
{
or at least:
MY_DEFINE(std::string)(fun)(int t, float b)
{
that would generate:
class fun_in
{
int t;
float b;
}
class fun_out
{
std::string value;
}
void my_fun_wrapper(int t, float b)
{
}
std::string fun(int t, float b)
{
my_fun_wrapper(t, b);
for each function with that define.
Is it possible to create such define wrapper for function of N
incoming arguments and any return type via Boost Preprocessor?
Well, the preprocessor can't parse tokens without pre-telling it. So you will need to use a lot more parenthesis instead. Here's what it would look like:
Here's how to create the macro using boost(I assume you are familiar with its preprocessor library). First is to define some macros for handling parenthesis, because boost doesn't handle sequences with commas at all:
Next, you need to handle the args in three different ways. First, is to output them as member variable(like
int a; float b;
). Then as functions arguments(like(int a, float b)
). Then finally as forward arguments to pass along to the other function(like(a, b)
).Next we create a
DETAIL_DEFINE
macro that take three parameters. The first is the name of the function, the arguments, and then the return value. This will produce the classes and functions, like you want:Finally, the
DEFINE
macro will parse out all the parenthesis and pass them to theDETAIL_DEFINE
macro:So now when you write:
It should output:
Note that this won't work in MSVC, there are workarounds though. Also, you need to compile with the
-DBOOST_PP_VARIADICS=1
.