Jump to content
  • Advertisement
Sign in to follow this  
Prinsessa

Template idea

This topic is 1062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by Prinsessa

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!