Jump to content
  • Advertisement
Sign in to follow this  
iraxef

Templated function in script

This topic is 1498 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 have a lot of code which looks like this:

MyDerivedType@ foo = cast<MyDerivedType>(database.FindObject("foo"));

...

MyOtherDerivedType@ bar = cast< MyOtherDerivedType >(database.FindObject("bar"));

Is there any existing way to have something like a templated function? The syntax I'd like to have is (for example):

MyDerivedType@ foo = database.FindObject<MyDerivedType>("foo");

Thank you.

Share this post


Link to post
Share on other sites
Advertisement

In that case will be useful to pass somehow classes as argument.

bool FindObject(class c)

{

  for( int i = 0; i < objects.length(); i++ )

    if( cast< c >( objects[ i ] ) !is null )

      return true;

  return false;

}

 

Or do more flexibly system, like this:

type - new class for storing information about type.

class MyClass

class MyClass : MyDerivedClass

MyClass obj;

type myType1 = typeof( MyClass );

type myType2 = typeof( obj );

type myDerivedType = typeof( MyDerivedClass );

Comparsion with operators ==, !=, <, >, <=, >= and is (equal to <=):

myType1 X myType2; true: ==, <=, >=, is; false: !=, <, >;

myType1 X derivedObj; true: !=, <=, <, is; false: ==, >, >=;

derivedObj X myType1; true: !=, >=, >; false: ==, <, <=, is;

This is can be implemented for now, if will be added possibility to pass class as parameter.

I can pass class name as string and resolve it in runtime (typeof("MyClass")), but this is not good practice, need checking in compile time.

Edited by cvet

Share this post


Link to post
Share on other sites

I think maybe i can do it for now with templates, something like this:

type t = typeof<MyClass>().get();

where typeof is template class with get function returning instance of type class.

Share this post


Link to post
Share on other sites

It's on my to-do list to implement support for template functions/methods. Though it will probably take a while before I get to it.

 

Until the template functions/methods are supported perhaps you might consider using the variable argument type. With it you would be able to have a syntax like this:

 

MyDerivedType@ foo;
database.FindObject("foo", @foo);

...

MyOtherDerivedType@ bar;
database.FindObject("bar", @bar);

Share this post


Link to post
Share on other sites

That's a nice step in the right direction! Thank you.

 

Now you just need a variable return type :)

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!