![Quote](images/misc/quote_icon.png)
Originally Posted by
ChrisW67
Anything you do with va_list arguments is going to be a kludge and prone to breakage.
Sure the "solution" is a Kludge, yes, but strangely it seems partially portable the same code worked on Windows XP and Linux w/o problems... I cannot garantue it could work in other platform as Mac OSX or totally different CPU as ARM, Power PC...
![Quote](images/misc/quote_icon.png)
Originally Posted by
ChrisW67
If you cannot alter the interface of the existing function why not
add another with a sane (less insane) interface:
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, char **buttonNames);
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, char **buttonNames);
To copy to clipboard, switch view to plain text mode
The function is indeed safe it seems to don't have a way to found when args end but in reality it has; it is declared as an always inline function using the GCC extensions __builtin_va_arg_pack() and more importantly __builtin_va_arg_len().
The real function (that no one should call it directly!) is this:
void messagebox_aux(json_object **json, char *iconType, char *title, char *msg, int showTime, [B]int n_buttons[/B], ...
/* char *btn1, char *btn2, ... */ );
void messagebox_aux(json_object **json, char *iconType, char *title, char *msg, int showTime, [B]int n_buttons[/B], ...
/* char *btn1, char *btn2, ... */ );
To copy to clipboard, switch view to plain text mode
where n_buttons are the number of va_args calculated using the macro __builtin_va_arg_len().
So I obtain a partial overloaded function in C as I could call it as:
messagebox(&json, "alert", title, msg, showTime); // This an alert with it has no buttons
messagebox(&json, "alert", title, msg, showTime); // This an alert with it has no buttons
To copy to clipboard, switch view to plain text mode
that translates to:
messagebox_aux(&json, "alert", title, msg, showTime, 0); // no args and n_buttons is 0!
messagebox_aux(&json, "alert", title, msg, showTime, 0); // no args and n_buttons is 0!
To copy to clipboard, switch view to plain text mode
or as:
messagebox(&json, "alert", title, msg, showTime, "OK"); // This an alert with only a button called "OK"
messagebox(&json, "alert", title, msg, showTime, "OK"); // This an alert with only a button called "OK"
To copy to clipboard, switch view to plain text mode
that translates to:
messagebox_aux(&json, "alert", title, msg, showTime, 1, "OK"); // an arg so n_buttons is 1 and is called "OK"
messagebox_aux(&json, "alert", title, msg, showTime, 1, "OK"); // an arg so n_buttons is 1 and is called "OK"
To copy to clipboard, switch view to plain text mode
Your version could solve the problem in Qt maybe but I need to create the array before calling the function, right?
I think I could not do this:
messagebox(&json, "alert", title, msg, showTime, {"OK", "Discard", "Cancel", "..."});
messagebox(&json, "alert", title, msg, showTime, {"OK", "Discard", "Cancel", "..."});
To copy to clipboard, switch view to plain text mode
But I would have to do this:
char *arr[] = {"OK", "Discard", "Cancel", "..."};
messagebox(&json, "alert", title, msg, showTime, arr);
char *arr[] = {"OK", "Discard", "Cancel", "..."};
messagebox(&json, "alert", title, msg, showTime, arr);
To copy to clipboard, switch view to plain text mode
... and I cannot call it w/o the last arg, right? It wouldn't compile...
In any case in C I'm pretty sure cannot declare an array in a function declaration, right?
![Quote](images/misc/quote_icon.png)
Originally Posted by
ChrisW67
To avoid repeating yourself you can change the implementation of the the va_list version to build a suitable list of strings for the new implementation. Your legacy C code keeps calling the va_list version and your new code uses the new interface.
In the end yes I was forced to create a version that takes a va_list as this Kludge creates a va_list from a QVector not a "..." and it seems they have "forgotten" a method to pass from a va_list to a "..." there's only the other way around... so in the end the list of calls is this:
always inline void messagebox(...);
always inline void message_box_aux(...); // creates the va_list using the va_start() macro
vmessagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, int n_buttons, va_list buttons)
always inline void messagebox(...);
always inline void message_box_aux(...); // creates the va_list using the va_start() macro
vmessagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, int n_buttons, va_list buttons)
To copy to clipboard, switch view to plain text mode
![Quote](images/misc/quote_icon.png)
Originally Posted by
ChrisW67
Incidentally, how does the existing C code call this function? Surely it faces exactly the same problem.
The C code calls the first version and Qt calls the last one after the QVector -> va_list ugly hack... by the way I seem to have found a bug in the __bultin_va_arg_pack() thing as, for convenience, I have created other proxy functions to create the various types of MessageBox and they are always inline and use __bultin_va_arg_pack() in any call until they arrive to vmessagebox but n_buttons is always 0... when args are presents in the first function call, too! It seems to not work well if called too much times...
I fear I'm forced to create MACROS but they are really unsafe as don't check the args types...
Oh well if only I could use Qt / C++ for all my applications... plain C in 2013 is so limiting really...
If I had to this in Qt/C++? I could create 3 methods using overloading:
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime);
void messagebox
(json_object
**json,
char *iconType,
char *title,
char *msg,
int showTime,
QString button
);
void messagebox
(json_object
**json,
char *iconType,
char *title,
char *msg,
int showTime,
QStringList button
);
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime);
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, QString button);
void messagebox(json_object **json, char *iconType, char *title, char *msg, int showTime, QStringList button);
To copy to clipboard, switch view to plain text mode
all would be more easy, intuitive, without hacks or unportable GCC extensions... but my PM say "C++ is ugly and slow (?), our society write ANSI C... K&R and better if C90 compliant"!
So annoying
Bookmarks