Passing arguments to overloaded functions

Are you developing an External using the LiveCode Externals SDK?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Passing arguments to overloaded functions

Post by n.allan » Sat Feb 02, 2008 2:03 pm

In my external I have an overloaded function which can acccept anything from 1 to 5 arguments.

I would like to be able to use the same function call in runrev but the external should choose the correct function depending on the amount of args passed.

go functionA("arg1")
go functionA("arg1","arg2") //etc...

without throwing an exception.

What is the most efficient way of handling such a situation?

Currently I am doing a case select on p_argument_count in the external and writing all 5 "versions" of the function.

Is there a better or faster/more efficient way?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Post by mwieder » Sat May 17, 2008 1:51 am

That's the way I'd think of coding it, too. You can try handing your argument list to a C++ set of overloaded functions, but I think the overhead would end up slowing things down and complicating the code. I think you'll find it hard to get faster than a switch statement.

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Sat May 17, 2008 5:03 pm

Thanks for the reply, up until now I saw nothing but tumbleweed passing by.

What I eventually did to save on coding was:

Supposing an external C++ function accepted 5 integer arguments but needed at least 3. I checked to see if we had been passed enough arguments.

if (p_argument_count < 3)
{
r_err = true
r_result = strdup("Not enough params"); etc....

if we had enough arguments then initialize all 5 to null

int arg1 = NULL;
int arg2 = NULL;
int arg3 = NULL;
int arg4 = NULL;
int arg5 = NULL;

Then initialize each depending on the amount of arguments passed from rev

if (p_argument_count ==5) // if this statement is true then set arg5
{
arg5 = atoi(p_arguments[4]);
}

if (p_argument count >= 4) // if this statement is true then set arg4
{
arg4 = atoi(p_arguments[3]);
}

if (p_argument_count >= 3) // This statement should be true so set the rest
{
arg3 = atoi(p_arguments[2]);
arg2 = atoi(p_arguments[1]);
arg1 = atoi(p_arguments[0]);
}

//then do the nasty with the resulting variables

SomeCFunction(arg1,arg2,arg3,arg4,arg5)

I'm not sure about freeing memory and stuff like that yet as I'm still learning but it will be OK for my purpose as I'm the only person using the app.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Post by mwieder » Sat May 17, 2008 8:37 pm

Yeah, sorry bout that. I'm not much of a web-forum guy myself - only check in here every six months or so.

At any rate, your code looks good, and as you point out your second check for > 3 probably isn't necessary. There's nothing in what you posted that would require freeing memory (the engine takes care of the strdup'd string itself), so I think you're doing fine.

The one thing I would caution about is setting r_err to true. That will work, but may be more dramatic than you need for a given situation. The problem is that if an external function returns a false value in r_err then script execution will stop at that point with an error dialog. Setting the result value to false or to an error message instead would probably suffice in most cases, and then you would handle the error in the script that called the external.

Post Reply

Return to “Building Externals”