Welcome Guest Search | Active Topics | Log In | Register

How I do "cast" class references in Squirrel/Sqrat?
#1 Posted : Thursday, May 19, 2016 10:08:31 AM(UTC)
Rank: Member

Groups: Registered
Joined: 5/19/2016(UTC)
Posts: 15
Location: UK

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)

I'm an experienced programmer, but a complete newbie to Squirrel and Sqrat.

I've successfully got a vm working, bound a class to it (using sqrat), and accessed a C++ created object of that class from a script.

Not a big deal I know! (though I did have to change sqratallocater.h to use the C++ 11 is_default_constructible to get it to compile on VS2013).

Now looking forwards I'm wondering how I'll achieve something, but it's such a standard use case I would hope it's pretty simple.

I have inherited classes, the usual sort of thing

class Bar

class Foo:public Bar

class Fun:public Bar

Which of course I can bind to Squirrel using Sqrat.

I also have functions along the lines of

Bar* getBar();

Which I assume I can also bind to squirrel (As I said, complete squirrel newbie here).

At which point I assume I'll be able to do something along the lines of

my_bar = getBar();

inside squirrel.

but often I will want to be able to identify the derived class and access the object as that class.

What in C++ would read

Foo* my_foo = dynamic_cast<Foo*>(my_bar);
if (my_foo)
    // Do Foo specific stuff here

How do I do the same sort of thing from a Squirrel script?
#2 Posted : Friday, May 20, 2016 9:20:59 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 128
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
By heart I am not sure if instanceof iterates through inheritance. If it does, this would suffice:

if (bar instanceof foo)


if it does not iterate, you still could do it yourself, like in this example:

class foo
  function getbase() { try { return base.getbase() ;} catch(x) { return base ; }}

if (bar.getbase() instanceof foo)


Be aware, in this special case the test will fail if bar actually is a foo instance itself, you would want to write

if ((bar instanceof foo) || (bar.getbase() instanceof foo))

Note this all might be bullshit, I am typing by heart without making sure I'm right :-)

#3 Posted : Sunday, May 22, 2016 2:47:27 PM(UTC)
Rank: Member

Groups: Registered
Joined: 12/5/2010(UTC)
Posts: 20
Location: Roslindale, MA

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
I would first start questioning why you would need to do this, since down-casting is typically an indication that your design could use some work.

Regardless, I'll attempt to answer your question assuming you know why you're doing what you're doing. I use SqPlus, so you'll have to translate to Sqrat.

One technique you could use is to create a static casting function for each inheritance object:


static Foo* Foo:SafeCastNative(NativeObject *pObj)
    return dynamic_cast<Foo*>(pObj);

static Foo* Foo:SafeCastScript(SquirrelObject sqObject)
    if (sqObject.GetType() == OT_INSTANCE)
        return (Foo*)sqObject.GetInstanceUP(ClassType<Foo>::type());
    return NULL;

Then bind the SafeCastScript:


RegisterGlobal(SafeCastScript, "CastToFoo");

In Squirrel, you'll do something like this (assuming you're working with a Bar object):

local Foo = CastToFoo(this);

But again, I really don't think you need this kind of thing. With native polymorphism set up correctly, and the right bindings to squirrel, you should be able to avoid using this scenario.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.166 seconds.