-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IPC commands with multiple arguments #20
Comments
Thanks for the detailed analysis. I hadn't done much testing with multiple arguments as I did not have a need to use them. But this is definitely unintuitive in its current state and does not really work with the C Macro because of the way the pre-processor works. There seem two be two main problems with adding multiple commands:
I think I've come up with a somewhat elegant/hacky solution that solves both of those problems without the need for disabling any compiler warnings that could identify potentially bad/buggy code. The following macro should work for multi-argument IPC Command definitions: #define IPCCOMMAND_MULT(FUNC, ARGC, ...) \
{ #FUNC, {.array_param=FUNC }, ARGC, (ArgType[ARGC])__VA_ARGS__ } Now the only thing I don't like about this solution, is I believe the pre-processor is treating the first argument as Now to fix this, we could do the following #define IPCCOMMAND_MULT(FUNC, ARGC, ...) \
{ #FUNC, {.array_param=FUNC }, ARGC, (ArgType[ARGC]){__VA_ARGS__} } and then drop the braces around types for multi-argument commands. However, this makes the We could alter the #define IPCCOMMAND(FUNC, ARGC, TYPES) \
{ #FUNC, {FUNC }, ARGC, (ArgType[ARGC]){TYPES} } and add braces around IPCCOMMAND( quit, 1, ARG_TYPE_NONE ), Now the main issue with altering the IPCCOMMAND macro is that it would break configs for everyone since everyone would need to remove the braces around the types. Though the macros would work better and more elegantly. So it looks like it will be a trade-off between a bit uglier code and making a config-breaking patch update. What do you think? |
As for the last option that works with single arguments for me, but it doesn't work with multiple arguments. All in all I think that we shouldn't worry too much about breaking existing configs. Now, if I keep the pragma of ignoring incompatible pointer types then option 2 works fine for both cases (single and multi param, and named IPCCOMMAND rather than IPCCOMMAND_MULT). If we do end up with two different macros depending on whether it is a single argument or multiple, then it makes sense taking this a step further and drop the count for the single param. Example: #define IPCCOMMAND(FUNC, TYPE) \
{ #FUNC, {.single_param=FUNC }, 1, (ArgType[1]){TYPE} }
#define IPCCOMMANDS(FUNC, ARGC, ...) \
{ #FUNC, {.array_param=FUNC }, ARGC, (ArgType[ARGC]){__VA_ARGS__} } Then having config like this: static IPCCommand ipccommands[] = {
IPCCOMMANDS( customfunc, 8, ARG_TYPE_SINT, ARG_TYPE_STR, ARG_TYPE_SINT, ARG_TYPE_SINT, ARG_TYPE_SINT, ARG_TYPE_SINT, ARG_TYPE_SINT, ARG_TYPE_SINT ),
IPCCOMMAND( incnmaster, ARG_TYPE_SINT ),
}; As you said you never had any practical use for commands handling multiple arguments and that will be the case for most people. Personally I just have some very specific use cases for this. I think even if you have to add an IPC command with multiple arguments manually instead of using a macro it would be fine as it is such a rare case. It would just good to have an example of how to do so should one need it. |
I thought I'd make some notes about IPC commands with multiple arguments as it is not particularly intuitive and is likely going to confuse some people. Or perhaps I am just doing something wrong.
The various compilation errors may also depend on the compiler used, not sure.
In any case after some trial end errors I ended up with a function like this:
The first thing I tried which I also feel is most intuitive is to add it like this in the ipcommands array:
However that gives me a compilation error in relation to passing more arguments than the macro expects (using the
gcc
compiler).While I didn't expect it to work I tried defining the ArgType separately and use that to work around the macro issue
and adding it like this:
but this led to an error regarding that the cast specifies the array type.
The last thing I tried was to skip the macro entirely and add the IPC command manually:
This still adds a warning as the function signature does not match the signature of the single param function, but it still compiles fine.
and executes fine:
Output:
and finally I was able to silence the warning during compilation by adding these pragmas:
I hope this is useful to someone and let me know if there is a better way of doing this.
The text was updated successfully, but these errors were encountered: