Binding singleton in SLB - c++

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);
}

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.

Related

In C++, coding for Arduino, how can I save a function to a variable and call it later via the variable?

I want to save a function I've made to a string variable and call it later via the variable - something like this:
String hello = "helloWorld();";
void loop()
{
hello;
}
void helloWorld()
{
//does functional things...
}
This is a super basic idea of what I want to make happen.
I'm making a game and want a certain function (different every time) to be stored in a variable to be played back later.
To declare a function pointer, you need to know its signature.
If your function helloWorld does not take parameters and returns nothing (void
), a variable to store that could be defined as
void (*myFunc)() = helloWorld;
void loop()
{
myFunc();
}
The only Arduino specific is that usually the required function declaration is done for you secretly behind the scenes.

Getting raw pointer from shared_ptr to pass it to function that requires raw

Ok first off I'm very new to C++ so apologies if my understanding is poor. I'll try explain myself as best I can. What I have is I am using a library function that returns a std::shared_ptr<SomeObject>, I then have a different library function that takes a raw pointer argument (more specifically node-addon-api Napi::External<T>::New(Napi::Env env, T *data) static function). I want to create a Napi::External object using my std::shared_ptr. What I am currently doing is this:
{
// ...
std::shared_ptr<SomeObject> pSomeObject = something.CreateSomeObject();
auto ext = Napi::External<SomeObject>::New(info.Env(), pSomeObject.get());
auto instance = MyNapiObjectWrapper::Create({ ext });
return instance;
}
But I am worried this will run into memory issues.
My pSomeObject only exists in the current scope, so I imagine what should happen is after the return, it's reference count will drop to 0 and the SomeObject instance it points to will be destroyed and as such I will have issues with the instance I return which uses this object. However I have been able to run this code and call functions on SomeObject from my instance, so I'm thinking maybe my understanding is wrong.
My question is what should I do when given a shared pointer but I need to work off a raw pointer because of other third party library requirements? One option that was proposed to me was make a deep copy of the object and create a pointer to that
If my understanding on any of this is wrong please correct me, as I said I'm quite new to C++.
============================
Edit:
So I was missing from my original post info about ownership and what exactly this block is. The block is an instance method for an implementation I have for a Napi::ObjectWrap instance. This instance method needs to return an Napi::Object which will be available to the caller in node.js. I am using Napi::External as I need to pass a sub type of Napi::Value to the constructor New function when creating the Napi:Object I return, and I need the wrapped SomeObject object in the external which I extract in my MyNapiObjectWrapper constructor like so:
class MyNapiObjectWrapper
{
private:
SomeObject* someObject;
static Napi::FunctionReference constructor; // ignore for now
public:
static void Init(Napi::Env env) {...}
MyNapiObjectWrapper(const CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// My original code to match the above example
this->someObject = info[0].As<const Napi::External<SomeObject>>().Data();
}
DoSomething()
{
this->someObject->DoSomething();
}
}
I have since come to realise I can pass the address of the shared pointer when creating my external and use it as follows
// modified first sample
{{
// ...
std::shared_ptr<SomeObject> pSomeObject = something.CreateSomeObject();
auto ext = Napi::External<SomeObject>::New(info.Env(), &pSomeObject);
auto instance = MyNapiObjectWrapper::Create({ ext });
return instance;
}
// modified second sample
class MyNapiObjectWrapper
{
private:
std::shared_ptr<SomeObject> someObject;
static Napi::FunctionReference constructor; // ignore for now
public:
static void Init(Napi::Env env) {...}
MyNapiObjectWrapper(const CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// My original code to match the above example
this->someObject =
*info[0].As<const Napi::External<std::shared_ptr<SomeObject>>>().Data();
}
DoSomething()
{
this->someObject->DoSomething();
}
}
So now I am passing a pointer to a shared_ptr to create my Napi::External, my question now though is this OK? Like I said at the start I'm new to c++ but this seems like a bit of a smell. However I tested it with some debugging and could see the reference count go up, so I'm thinking I'm in the clear???
Here the important part of the documentation:
The Napi::External template class implements the ability to create a Napi::Value object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
So you need to ensure that the object passed by data to Napi::External Napi::External::New exits until the Napi::External<T> object is destructed.
So the code that you have shown is not correct.
What you could do is to pass a Finalize callback to the New function:
static Napi::External Napi::External::New(napi_env env,
T* data,
Finalizer finalizeCallback);
And use a lambda function as Finalize, that lambda could hold a copy through the capture to the shared pointer allowing to keep the shared pointer alive until finalize is called.
std::shared_ptr<SomeObject> pSomeObject = something.CreateSomeObject();
auto ext = Napi::External<SomeObject>::New(
info.Env(),
pSomeObject.get(),
[pSomeObject](Env /*env*/, SomeObject* data) {});

Registering a class' function in LuaBridge, then calling it via a lua script?

Whenever I run the program, it prints nothing to the console.
This works fine if I don't use a class and just do it all in main().
The moment I put it in a class, I start having problems.
What am I doing wrong?
using namespace luabridge;
myClass::myClass()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L).beginClass<myClass>("myClass").addFunction("printMessage", &myClass::printMessage).endClass();
luaL_dofile(L, "test.lua");
lua_pcall(L, 0, 0, 0);
}
void myClass::printMessage(const std::string& s)
{
std::cout << s << std::endl;
}
My lua script "test.lua"
I've tried
c = myClass()
c:printMessage("You can call C++ functions from Lua!")
and
myClass:printMessage("You can call C++ functions from Lua!")
and
printMessage("You can call C++ functions from Lua!")
There are three options to do what you want:
If you want to construct a myClass instance in lua, and use it like your first example, you need to also export a constructor after the beginClass:
.addConstructor <void (*) (void)> ()
Construct a myClass instance in C++, and then pass it to lua using some other function. Then lua may access it like:
myClassInstance:printMessage("Hello")
Make printMessage static, and export it with the following:
.addStaticFunction("printMessage", &myClass::printMessage)
Then you can call it in lua using:
myClass.printMessage("Hello")
Notice the difference between . and : in the calls. . accesses like a static and : accesses like an instance.

Get the calling object or method in d

Somewhat related to my previous question here
Is there a way to get the calling Object from within a function or method in d?
example:
class Foo
{
public void bar()
{
auto ci = whoCalledMe();
// ci should be something that points me to baz.qux, _if_ baz.qux made the call
}
}
class Baz
{
void qux()
{
auto foo = new Foo();
foo.bar();
}
}
Questions:
Does something like whoCalledMe exist? and if so, what is it called?
if something does exist, can it be used at compile time (in a template) and if so, how?
Alternatively;
is it possible to get access to the call stack at runtime? like with php's debug_backtrace?
To expand on what CyberShadow said, since you can get the fully qualified name of the function by using __FUNCTION__, you can also get the function as a symbol using a mixin:
import std.stdio;
import std.typetuple;
void callee(string file=__FILE__, int line=__LINE__, string func=__FUNCTION__)()
{
alias callerFunc = TypeTuple!(mixin(func))[0];
static assert(&caller == &callerFunc);
callerFunc(); // will eventually overflow the stack
}
void caller()
{
callee();
}
void main()
{
caller();
}
The stack will overflow here since these two functions end up calling each other recursively indefinitely.
It's not directly possible to get information about your "caller". You might have some luck getting the address from the call stack, but this is a low-level operation and depends on things such as whether your program was compiled with stack frames. After you have the address, you could in theory convert it to a function name and line number, provided debugging symbols are available for your program's binary, but (again) this is highly platform-specific and depends on the toolchain used to compile your program.
As an alternative, you might find this helpful:
void callee(string file=__FILE__, int line=__LINE__, string func=__FUNCTION__)()
{
writefln("I was called by %s, which is in %s at line %d!", func, file, line);
}
void caller()
{
// Thanks to IFTI, we can call the function as usual.
callee();
}
But note that you can't use this trick for non-final class methods, because every call to the function will generate a new template instance (and the compiler needs to know the address of all virtual methods of a class beforehand).
Finding the caller is something debuggers do and generally requires having built the program with symbolic debug information switches turned on. Reading the debug info to figure this out is highly system dependent and is pretty advanced.
The exception unwinding mechanism also finds the caller, but those tables are not generated for functions that don't need them, and the tables do not include the name of the function.

Cannot call member function without object = C++

I am brushing up again and I am getting an error:
Cannot call member function without object.
I am calling like:
FxString text = table.GetEntry(obj->GetAlertTextID());
FxUChar outDescription1[ kCP_DEFAULT_STRING_LENGTH ];
IC_Utility::CP_StringToPString(text, &outDescription1[0] );
The line: IC_Utility::CP_StringToPString(text, &outDescription1[0] ); is getting the error
My function is:
void IC_Utility::CP_StringToPString( FxString& inString, FxUChar *outString)
{
}
I know it has to be something simple I am missing.
If you've written the CP_StringToPString function, you need to declare it static:
static void IC_Utility::CP_StringToPString( FxString& inString, FxUChar *outString)
Alternatively, if it's a function in third-party code, you need to declare an IC_Utility object to call it on:
IC_Utility u;
u.CP_StringToPString(text, &outDescription1[0] );
Your method isn't static, and so it must be called from an instance (sort of like the error is saying). If your method doesn't require access to any other instance variables or methods, you probably just want to declare it static. Otherwise, you'll have to obtain the correct instance and execute the method on that instance.
You have to declare the function with the 'static' keyword:
class IC_Utility {
static void CP_StringToPString( FxString& inString, FxUChar *outString);
You need to declare the function static in your class declaration. e.g.
class IC_Utility {
// ...
static void CP_StringToPString(FxString& inString, FxUChar *outString);
// ...
};
"static" is the right answer. or, you can pass it a NULL "this" pointer if it's not used in the function:
((IC_Utility*)NULL)->CP_StringToPString(...);