Blog > Blog > Why SLB::Class sucks, and why it doesn't

Why SLB::Class sucks, and why it doesn't

Posted by Hamcha on August 24, 2011


So, as I said earlier, I'm using SLB to wrap C++ classes into Lua so I can use them and do stuff without having to do dirty workarounds.

It worked just fine until today.

Today I had to pass a class pointer to a method and it didn't work as I thought.

I have a basic reference counting for nearly all of my classes that will be used with class pointers, it's cool, it works and does its job just fine.


There is a Class::Create(); method that creates an instance and adds 1 to the internal reference counter of the created instance, when I want to pass the pointer to a class I just do it and call a Retain() method that increases the internal reference counter to indicate that now the pointer is used in two different places.
When I stop using the instance from one place, I just call a Release() method (usually in the Dispose() method of the class) that decreases the reference counter and when it becomes zero I just delete the pointer.

The Create() methods works great on SLB and proves that SLB::Class is able to manage class pointers greatly.

The problem came when I need to pass this pointer as an argument for a method.
I don't know why, but there is no default way to retrieve the default pointer.

So, what I've done is do some ugly workaround, said "screw SLB::Class" and made all the wrapped functions static and decided to return pointers not a s SLB::Classes but as 64bit integers (which are the memory addresses), so, instead of having a code like:


mi = Mission::Create()
cut = Cutscene.create()
cut.setSomething(something)
cut.setSomethingelese("poop")
mi.addCutscene(cut)
Game.setCurrentMission(mi)

I have something like:
mi = Mission::Create()
cut = Cutscene.create()
Cutscene.setSomething(cut,something)
Cutscene.setSomethingelese(cut,"poop")
Mission.addCutscene(mi,cut)
Game.setCurrentMission(mi)

It's not that bad actually, I'm fine with it, but it annoys me.

In fact, I think there is another solution, feel free to try it and let me know if it works for you.
The basic idea is to do everything using SLB::Classes and making a function that returns an integer (which would be the memory address) such as: 


unsigned long long Class::getPtr()
{
    return (unsigned long long)this;
}

I have not tested it and I don't feel like trying it since I've been searching for something that works for the past 2 days but if you're searching for a cleaner solution, go try it, though I don't guarantee that it'll work.

Otherwise you'll have to deal with code like this:


void Mission::LuaEncounter_Add(ptr _m, ptr _ep)
{
    EncounterEvent* _e;
    _e = (EncounterEvent*)_ep;
    _e->setPlayerCompany(((Mission*)_m)->workingCompany);
    ((Mission*)_m)->Events.push(_e);
}

And if you think that is clean, then you probably have a messed up mind.

Grammar Nazis are welcome

English is not my native language and I'm looking forward to improve so if you happen to find some mistakes please point them out!

Comments:

Frenchie

This is way more hlefpul than anything else I've looked at.
Jayna

Very true! Makes a change to see somneoe spell it out like that. :)
Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.