🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Provide a generic funcdef for application functions

Started by
3 comments, last by Solokiller 4 years, 3 months ago

I have a use case for a generic funcdef where all functions should be accepted for the parameter.

This is the code that requires it:

https://github.com/Solokiller/AngelscriptUtils/blob/7a3a71ab613d74b8d315e2a1c30dc2c8930540d5/include/AngelscriptUtils/ScriptAPI/Scheduler.h

https://github.com/Solokiller/AngelscriptUtils/blob/7a3a71ab613d74b8d315e2a1c30dc2c8930540d5/src/AngelscriptUtils/ScriptAPI/Scheduler.cpp

Basically i have a scheduler class that can execute a function at a later point in time. The caller passes in a function handle to do this. Right now that function handle parameter is a ?& in parameter type, but this means there is no compile time checking going on, and object methods can only be passed in by creating a funcdef and converting the method to that.

I propose adding a new parameter type similar to ?& in that allows an application function to take any function or object method as though the parameter type were a compatible funcdef:

funcdef@ parameterName

Used in a function or method declaration like this:

void MyFunction(funcdef@ myFunctionParameter)

The C++ function would simply take an asIScriptFunction pointer:

void MyCPPFunction(asIScriptFunction* myFunctionParameter)
{
//Use it, call Release when done
}

Generic calls would work the same way as they do now (i assume my code is wrong because i'm using GetArgAddress instead of GetArgObject, but it still works).

Script calls would handle it like this:

bool ACompareFunction(SomeClass@ lhs, SomeClass@ rhs) {…}
class Foo
{
    void SomeOtherMethod(const string& in text) {…}
}
MyFunction(ACompareFunction); //Implicit conversion to funcdef
Foo foo;
MyFunction(funcdef(foo.SomeOtherMethod)); //Explicit conversion to the any funcdef type

This would eliminate the need to perform runtime type checking and makes it easier for script authors to see if their code will compile or not.

It should also be possible for any existing funcdef to implicitly convert to the any funcdef type.

Advertisement

Solokiller said:
I propose adding a new parameter type similar to ?& in that allows an application function to take any function or object method as though the parameter type were a compatible funcdef:

I've been thinking in similar lines too. Not specifically for funcdefs, but to have the possibility of adding restrictions to the var type ? to only accept certain types, for example, only primitives, only handles, only non-handles, etc. The reason is the same as you state, to have better compile time type validations.

In your case you still cannot avoid the runtime checks though, since you need to match the function with the parameter list. You just skip the first validation to make sure the func arg is actually a function.

I've added this suggestion to the to-do list for a potential future addition.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

@WitchLord Sounds good. I think you only need to match the parameter list for overloaded functions though? So you only need a funcdef to perform exact matching in that case. Most use cases involve non-overloaded functions so it would still be very helpful and the syntax would be simple enough to use.

This topic is closed to new replies.

Advertisement