Passing arguments to a function
#1

Hello,

Maybe someone will know better way of doing this:

Код:
m_query(const query[], const callback[] = "", const format[] = "", {Float, _}:...)
{
	new
		args = numargs();

	if(args > 3)
	{
                switch(args)
                {
                        case 4: mysql_tquery(gSQL, query, callback, format, getarg(3));
                        case 5: mysql_tquery(gSQL, query, callback, format, getarg(3), getarg(4));
                        case 6: mysql_tquery(gSQL, query, callback, format, getarg(3), getarg(4), getarg(5));
                        case 7: mysql_tquery(gSQL, query, callback, format, getarg(3), getarg(4), getarg(5), getarg(6));
                        case 8: mysql_tquery(gSQL, query, callback, format, getarg(3), getarg(4), getarg(5), getarg(6), getarg(7));
                        case 9: mysql_tquery(gSQL, query, callback, format, getarg(3), getarg(4), getarg(5), getarg(6), getarg(7), getarg(8));
                 }
	}
	else
	{
		mysql_tquery(gSQL, query);
	}
}
There is one big downside - I have to write new line for every possible argument count. I can't use #define in this place.
Reply
#2

Why would you even need this? That's basically a function that calls another function.
Reply
#3

Use y_va
Reply
#4

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Why would you even need this? That's basically a function that calls another function.
I know. All I want to do is to get rid of gSQL variable in m_query functions. In my case, gSQL variable is static, and it can't be visible in other files. That is why define is not an option. There is no difference for me which variable for gSQL I will use, static or new. I am just curious if code above can be rewriten to be more universal.

Quote:
Originally Posted by Dayvison_
Посмотреть сообщение
Use y_va
thanks.
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
So I actually rewrote y_va just yesterday! Doing what you want there has always been awkward and there were several solutions from macros up to y_va, but they all had downsides. I'll write a bit more about this later, but in short:

1) Macros are fragile and prone to breaking code without great care. They also bloat your code and have length limits.

2) Explicit assembly requires knowing assembly.

3) y_va needs wrappers for every function you would want - "format" was wrapped (as "va_format") but if you wanted something else you needed assembly again - "mysql_tquery" wasn't wrapped.

4) Your method I've not seen before, but it won't work with strings or arrays.

As I said though, if you have the very newest version of YSI, I've written a new "___" macro (triple underscore, like the "..." triple dot (elipses)):

PHP код:
m_query(const query[], const callback[] = "", const format[] = ""GLOBAL_TAG_TYPES:...)
{
    switch (
numargs())
    {
        case 
02:
            
// Probably bad somehow.
            
return;
        case 
1:
            
mysql_tquery(gSQLquery);
        default:
            
mysql_tquery(gSQLquerycallbackformat___(3));
    }

Using "GLOBAL_TAG_TYPES" just means you can pass many different types of data in, not just floats and integers, but use "{Float, _}" if you prefer. "___(3)" means "all remaining parameters after the third one".
thanks, it is working as I want.
Issue not related to the topic, y_va include gives warnings on lines 336 and 428.
Код:
CodeScanGetMatchDisasm(m, dctx, len);
CodeScanGetMatchDisasm(m, dctx, -12);
function declared as
Код:
stock CodeScanGetMatchDisasm(csm[CodeScanner], ctx[DisasmContext]) {
	// Doesn't do any decompilation, just gets the information for decompiling
	// the currently found match.
	ctx[DisasmContext_end_ip] = 0,
	ctx[DisasmContext_start_ip] = ctx[DisasmContext_nip] = ctx[DisasmContext_cip] = csm[CodeScanMatch_cip];
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)