06.12.2013, 14:09
(
Последний раз редактировалось Emmet_; 09.12.2013 в 20:10.
)
variadic.inc
Disclaimer
This is for ADVANCED variadic function manipulation ONLY! I would suggest that you use y_va from YSI for simply formatting variadic functions.
I needed this for something I was working on related to PAWN, but I just gave up on it. I accidentally wrote this include, so I thought I would finish it up and release it!
Introduction
Variadic functions are a feature in most scripting languages, where a scripter can specify an indefinite amount of arguments towards a function. However, in PAWN, this isn't possible without playing around with the assembly (later versions of PAWN have dealt with this), but this include introduces a brand new way of handling them!
More information
Have you ever wrote a function like this before?
And then you figured out that you can't do it this way! The reason being is because variadic parameters don't have a referable location in ordinary PAWN code, so assembly is required, but that can be very complicated and one mistake could corrupt the entire script.
However, this include allows scripters to dynamically push arguments and format them. The pushed arguments are stored within an array by address, so they can be accessed later with the corresponding functions.
Example
Using variadic.inc:
You can also use "@variadic[]" with CallLocalFunction and such to call local functions with the corresponding pushed arguments.
Alternatively, you can also use "stock", but make sure you place "..." after the parameters.
String arguments
You can retrieve string arguments easily.
Getting:
Setting:
Functions
Obsolete functions
These functions are obsolete since version 2. However, I am simply keeping them for another feature I'm adding to this include, so the best thing to do is ignore them.
Alternatively, you can use @variadic[] instead of these functions - but it's up to you.
Notes
- I do recommend that you use y_va for this - this is for advanced variadic function manipulation!
- This has been fully tested, but there might be problems. If so, report them!
Download
Pastebin
Disclaimer
This is for ADVANCED variadic function manipulation ONLY! I would suggest that you use y_va from YSI for simply formatting variadic functions.
I needed this for something I was working on related to PAWN, but I just gave up on it. I accidentally wrote this include, so I thought I would finish it up and release it!
Introduction
Variadic functions are a feature in most scripting languages, where a scripter can specify an indefinite amount of arguments towards a function. However, in PAWN, this isn't possible without playing around with the assembly (later versions of PAWN have dealt with this), but this include introduces a brand new way of handling them!
More information
Have you ever wrote a function like this before?
pawn Код:
printex(str[], ...)
{
new
output[128];
format(output, sizeof(output), str, ...);
return print(output);
}
However, this include allows scripters to dynamically push arguments and format them. The pushed arguments are stored within an array by address, so they can be accessed later with the corresponding functions.
Example
Using variadic.inc:
pawn Код:
#include <a_samp>
#include <variadic>
var MyPrint(str[])
{
new
output[128];
format(output, sizeof(output), str, @variadic[1]);
return print(output);
}
Alternatively, you can also use "stock", but make sure you place "..." after the parameters.
String arguments
You can retrieve string arguments easily.
Getting:
pawn Код:
stock MyFunction(a[], b[])
{
new
addr = GetVariadicAddress(0); // Address of "a[]".
print(GetVariadicString(addr)); // Print the result.
}
pawn Код:
stock MyFunction(a[], b[])
{
new
addr = GetVariadicAddress(1); // Address of "b[]".
SetVariadicString(addr, "Hi there.");
print(GetVariadicString(addr)); // Print the result.
}
pawn Код:
// Get the address of a function argument.
native GetVariadicAddress(argument);
// Get the string from the specified argument address. You can return it or store it inside "dest".
native GetVariadicString(address, dest[] = "", size = sizeof(dest));
// Set the string from the specified argument address to the specified string.
native SetVariadicString(address, str[]);
// Checks if an argument is packed.
native IsArgumentPacked(address);
// Gets the length of a variadic string.
native GetVariadicLength(address);
// Delete an argument from the specified argument address.
native DeleteArgument(address);
// Popped the pushed arguments.
native PopArguments();
These functions are obsolete since version 2. However, I am simply keeping them for another feature I'm adding to this include, so the best thing to do is ignore them.
pawn Код:
// Push all arguments from "start" to "end" (obsolete).
native PushArguments(start, end);
// "Format" the variadic arguments.
native VariadicFormat(output[], len, const str[]);
// Call a local function with the variadic arguments.
native CallLocalVariadic(function[], const specifiers[]);
// Call a remote function with the variadic arguments.
native CallRemoteVariadic(function[], const specifiers[]);
Notes
- I do recommend that you use y_va for this - this is for advanced variadic function manipulation!
- This has been fully tested, but there might be problems. If so, report them!
Download
Pastebin