Until it's available as an engine feature, I've taken a stab at implementing the templated type-of object add_on as mentioned here. Note that this requires at least rev 2043 for an engine bugfix.
I register it into a custom namespace as follows:
engine->SetDefaultNamespace("myns");
RegisterScriptType(engine);
engine->SetDefaultNamespace("");
New Types
myns::typeid
class Foo {}
Foo foo;
void main()
{
myns::typeid t = myns::typeof(foo);
// implicitly convertible to a string
print("type of t is: " + t);
if ( t == myns::type<Foo>() )
print("matches");
}
myns::type<T>
class Foo {}
void main()
{
myns::type<Foo> typeOfFoo;
myns::type<int> typeOfInt;
// implicitly convertible to a string (or a myns::typeid for comparisons)
print("Type of Foo is: " + typeOfFoo);
print("Type of int is: " + typeOfInt);
}
New global functions
myns::typeid myns::typeof()
interface Base {}
class Derived : Base {}
Derived gDerived;
Base@ gBase = @gDerived;
void main()
{
myns::type<Base> typeOfBase;
myns::type<Derived> typeOfDerived;
print( myns::typeof(gDerived) == typeOfBase ? "yes" : "no" ); // no
print( myns::typeof(gDerived) == typeOfDerived ? "yes" : "no" ); // yes
print( myns::typeof(gBase) == typeOfBase ? "yes" : "no" ); // yes
print( myns::typeof(gBase) == typeOfDerived ? "yes" : "no" ); // no
myns::mynsSprite@ anim = ...;
if ( myns::typeof(anim) == myns::type<myns::Animation2d>() )
print("It's an animation");
}
string myns::classname()
class Base
{
Base() { print("constructing a: " + myns::classname(this)); }
}
class Derived : Base
{
Derived() { print("constructing a: " + myns::classname(this)); }
}
void main()
{
Derived derived;
// "constructing a: Base"
// "constructing a: Derived"
}