c++squirrel

c++ sqrat binding methods though a wrapper


I am attempting to develop a wrapper for my Squirrel VM and SQRat binding

The normal way of binding a class is to call:

Class<MyClass> myClass(vm,"myClass");
myClass.Func(_SC("Foo"), &MyClass::Foo);

Where the class im trying to bind looks like so:

class MyClass {
public:
    void Foo() { cout << bar; }
    int bar;
};

Now iv Wrapped squirrel into a helper class that handles the machine state and errors called "SqEnvironment"

class SqEnvironment
{
public:
    SqEnvironment(unsigned int stacksize); //set up enviroment add callbacks ext.
template<class T>
SqurrelClass<T> bindClass(string classname);
HSQUIRRELVM v;
}

template<class T>
SqurrelClass<T> SqEnvironment::bindClass(string classname)
{
    return SqurrelClass<T>(classname,v);
}

Calling the bindclass method in the main, looks like so:

int main(int argc, char* argv[])
{
    SqEnvironment e(1024);

    SqurrelClass<MyClass> myclass = e.bindClass<MyClass>("MyClass");
    myclass.bindFunction(&MyClass::Foo,"Foo"); //No idea.

    cin.get();
    return 0;
}

I just cant quite get the implementation down on the bind method.

template<class T>
class SqurrelClass
{
public:
    SqurrelClass(std::string classname, HSQUIRRELVM v);
    void bindFunction(void (T::*mymethod), std::string name); //Error   C2182   'mymethod': illegal use of type 'void'

    ~SqurrelClass();

private:
    Sqrat::Class<T>* myClass;
};

template<class T>
inline SqurrelClass<T>::SqurrelClass(std::string classname, HSQUIRRELVM v)
{
    myClass = new Sqrat::Class<T>(v, classname.c_str());
    Sqrat::RootTable(v).Bind(classname.c_str(), *myClass);
}

template<class T>
inline SqurrelClass<T>::~SqurrelClass()
{
    delete myClass;
}

Now iv seen: c++ passing class method as argument to a class method with templates

And this is SUPER close, but it was solved by adding a instance of the class to the method call, allowing direct de-refrenceing of the class's method.

But I cant exactly do that... An instance is never made that I have access to.

So how would I go about doing something like this?


Solution

  • I'm dumb.

    Forgot to add the () to the end of the argument;

    method needs to be:

    void bindFunction(void (T::*mymethod)(), std::string name);
    

    then additionally for variables its:

    template<typename J>
    void bindVariable(J T::*myvar, std::string variable);