🎉 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!

Template idea

Started by
3 comments, last by Prinsessa 8 years, 8 months ago

I know full, real templates are hard to do for a lot of reasons (I've read the threads), but I had some sort of idea last night so I thought I'd present it and ask anyway.

Wouldn't it be plausible to emulate templates in such a way that you could register a templated function and then the C++ callback would receive the template types either as asIObjectType * or something that tells us what kind of primitive it is, and then we could do something with that?

Primitives aside for now, focusing on handles, it could look something like this in the AS:

ClassType @obj = getObjOf<ClassType>("by-this-name");

This function has been registered with the following AS signature:

T @getObjOf<T>(const string &inout)

In order to call this C++ function:

asIScriptObject *callbackGetObjOf(asIObjectType *templateArg, const std::string &functionArg)

{

return findObjByNameAndType(templateArg, functionArg);

}

Or perhaps if you're okay with doing some manual work if you have some C++ types registered (let's say ClassType is a registered C++ type):

void *callbackGetObjOf(asIObjectType *templateArg, const std::string &functionArg)
{

// return ...;

}

Or instead of void *, perhaps a CScriptHandle with a ref or whatever...

This function would be able to return a class derived from ClassType or whatever since the return value would automatically be casted to whatever T specified in the AS code.

This is obviously not necessarily safe since a bad cast could cause a crash, and so the responsibility of using this would lie with the programmer, but at least it would be possible to use templates to some extent unless my idea cannot possibly work in reality. Maybe by activating some compiler flag or AS engine rule in order to enable this unsafe feature.

Advertisement

Template functions like this is something I've had in mind for quite some time, though not really something that I've prioritized to be implemented in AngelScript. (for some reason it wasn't already mentioned in the todo list, so I've added it now)

Your idea is perfectly valid, though I would use type ids instead of asIObjectType, so that not only object types can be represented but also primitives and handles.

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

Yeah, I remembered from looking around the docs the other day that there was something else besides asIObjectType but I couldn't remember what it was called.

Ah! I did indeed look at the todo list like yesterday to see if it was on there and got pretty sad to see it absent. Cool that you've added it!

I don't know how common the problem is, but my (component-based) C++ game engine uses templates a bunch to make things handy but since AS lacks templated functions I can't transfer the interface quite as elegantly to AS and have to resort to slightly bulkier solutions where I have to both tell the function (with a string) what type I want and then cast it anyway because I have to return the base class to AS. Stuff like that.

Good to hear that you think the idea possible to implement at least.

Until function templates are added, consider registering getObjOf<T> as a template type. It would allow you to use the precise syntax you're asking for, you'd just have to register a constructor from const string& and a cast to T@. It may not be the most efficient thing in the world but, unlike your current solution, it would be type-safe.

It's a member function in this case, unfortunately. Though I did actually play with the convoluted thought of doing something similar anyhow before dismissing the idea. :p

This topic is closed to new replies.

Advertisement