Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding singleton in SLB

Tags:

c++

singleton

lua

I have singleton class and I would like to bind it for lua to use. I'm using SLB (Simple Lua Binder). I really have no idea how to do this. All my ideas just dont work. Anyone?

void Logger::export_class_to_lua(SLB::Manager *m) {
    SLB::Class< Logger, SLB::Instance::NoCopyNoDestroy >("Logger",m)
        .set("getInstance",&Logger::getInstance)
        .set("log",&Logger::log)
        .set("info",&Logger::info)
        .set("warning",&Logger::warning)
        .set("error",&Logger::error)
        .set("fatal",&Logger::fatal);
}
like image 521
Łukasz Stalmach Avatar asked May 15 '26 22:05

Łukasz Stalmach


1 Answers

Using your code do:

void Logger::export_class_to_lua(SLB::Manager *m) {
    SLB::Class< Logger, SLB::Instance::NoCopyNoDestroy >("Logger",m)
        //.set("getInstance",&Logger::getInstance) // OMIT THIS
        .set("log",&Logger::log)
        .set("info",&Logger::info)
        .set("warning",&Logger::warning)
        .set("error",&Logger::error)
        .set("fatal",&Logger::fatal);

    // Next we set global variable within LUA to access the Singleton
    SLB::setGlobal<Logger*>(&(*lua_State), getInstance(), "logger");
}

lua_State will be a pointer to whatever lua_State you created. "logger" is the name of the object/class/variable you use within LUA to access the Singleton.

So for example; inside of LUA you would do:

logger:log("Logging some information.")
logger:error("An error has occured.")

Assuming your log and error functions take const char* or something.

like image 104
thomas Avatar answered May 18 '26 11:05

thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!