[Include] variadic.inc - Variadic functions!
#1

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?

pawn Код:
printex(str[], ...)
{
    new
        output[128];

    format(output, sizeof(output), str, ...);

    return print(output);
}
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:

pawn Код:
#include <a_samp>
#include <variadic>

var MyPrint(str[])
{
    new
        output[128];

    format(output, sizeof(output), str, @variadic[1]);
    return print(output);
}
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:

pawn Код:
stock MyFunction(a[], b[])
{
    new
        addr = GetVariadicAddress(0); // Address of "a[]".

    print(GetVariadicString(addr)); // Print the result.
}
Setting:

pawn Код:
stock MyFunction(a[], b[])
{
    new
        addr = GetVariadicAddress(1); // Address of "b[]".

    SetVariadicString(addr, "Hi there.");

    print(GetVariadicString(addr)); // Print the result.
}
Functions
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();
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.

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[]);
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
Reply


Messages In This Thread
variadic.inc - Variadic functions! - by Emmet_ - 06.12.2013, 14:09
Re: variadic.inc - Variadic functions! - by iZN - 06.12.2013, 17:33
Re: variadic.inc - Variadic functions! - by PT - 06.12.2013, 20:16
Re: variadic.inc - Variadic functions! - by Emmet_ - 07.12.2013, 03:27
Re: variadic.inc - Variadic functions! - by Niko_boy - 07.12.2013, 04:01
Re: variadic.inc - Variadic functions! - by Emmet_ - 07.12.2013, 05:17
Re: variadic.inc - Variadic functions! - by Emmet_ - 09.12.2013, 19:49
Re: variadic.inc - Variadic functions! - by kristo - 14.03.2016, 15:04
Re: variadic.inc - Variadic functions! - by Pottus - 27.05.2017, 19:11
Re: variadic.inc - Variadic functions! - by Dice_ - 28.05.2017, 19:35
Re: variadic.inc - Variadic functions! - by DRIFT_HUNTER - 28.05.2017, 19:48
Re: variadic.inc - Variadic functions! - by Pottus - 28.05.2017, 20:26
Re: variadic.inc - Variadic functions! - by kristo - 29.05.2017, 17:14
Re: variadic.inc - Variadic functions! - by KinG7 - 29.05.2017, 17:26
Re: variadic.inc - Variadic functions! - by Pottus - 29.05.2017, 22:31

Forum Jump:


Users browsing this thread: 1 Guest(s)