Infinite parameters with defines
#1

How would I make a define that accept 'n' number of parameters?

pawn Код:
#define my_keyword%9my_function(%1, ...?); {print("hello");}
What would this be replaced by?
pawn Код:
...?

// use it like
main()
{
     my_keyword my_function("OnPlayerKill", infinite_params_here...);
}
Reply
#2

Simply
pawn Код:
#define fancy%9\32;voideater(%0,%1) Hello there! First parameter is %0, the rest is %1
Reply
#3

Hi there, thanks for the reply.

I tried something like this -
pawn Код:
#include <a_samp>
#include <ppg>
   
#define fancy%9\32;voideater(%0,%1) print("Hello there! First parameter is "#%0", the rest is "#%1"")

main() {
    fancy voideater("hello", "wow", "cow", "bar");
}
Could you tell me why it gives a warning? And what's the method to get each argument? I mean I know getarg and numarg but I am absolutely clueless about using them here!

Thanks!
Reply
#4

Ah, I might've misunderstood your question then. The preprocessor simply substitutes text, it's not a smart creature. So, your example turned into
pawn Код:
print("Hello there! First parameter is hello, the rest is wow", "cow", "bar")
and print accepts only single argument (in contrary to printf). Check this out:
pawn Код:
#include <a_samp>
   
#define fancy%9\32;voideater(%0,%1) printf("Hello there, the result is: "#%0,%1)

main() {
    fancy voideater("hello %s, %s, and %s", "wow", "cow", "bar");
}
Also, old playground ( http://slice-vps.nl/ppg/ ) has "Show preprocessed output" - it's really awesome macro resolver. Try it!
Reply
#5

Thanks, great!

One last question to bother you -

How would you use variables in this case?

pawn Код:
new gVar[ 500 char ] = { 0 , ... } ;

#define DEF_ACCEPTING_INFI_PARAMS(..) ---- print all variables passed to it

main()
{
     DEF_ACCEPTING_INFI_PARAMS(gVar{0}, gVar{1}, .... gVar{499});
}
Thanks!
Reply
#6

This isn't so easy if you don't have formatting string (the one with placeholders) So you can:
1. Pass the formatting string yourself
pawn Код:
#define DEF_ACCEPTING_INFI_PARAMS(%0,%1) printf("Some additonal text before. " #%0,%1)

main()
{
     DEF_ACCEPTING_INFI_PARAMS("%d, %d and %d", gVar{0}, gVar{1}, gVar{2});
}
Not so handy if you want to dump 500 variables.
2. Create predefined macros for each number of arguments. Not so handy as well - limit to 10 variables, and you have 10 diffrent macros for one thing.
3. Custom variable arguments function.

I'd go with variable arguments function (with y_va from YSI 3 for synthetic sugar [the one from 4 isn't in working state yet]).
Reply
#7

Aah. I was looking at this include and was looking at the defines. All that "Aa:Ab:Ac" didn't make sense to me!

Anyways, thank you!
Reply
#8

Those "Ab:Ac:" macros only work with variable declarations, when you have more information about the variables in the text (such as their tag and size). However, they are well documented in the pre-processor tutorial. Your problem is that you are thinking of "%0" and "%1" as parameters, as indicated by your topic title - forget that now. They are completely different things and you need to not try and apply anything you know about function parameters to them. They are JUST placeholders. They say "I'm here now, but something else will be shortly".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)