luabind: cannot access global variable - c++

I have a C++ class that I want to give access to in the lua script through a global variable, however when I try to use it I get the following error:
terminate called after throwing an instance of 'luabind::error'
what(): lua runtime error
baz.lua:3: attempt to index global 'foo' (a nil value)Aborted (core dumped)
My Lua script (baz.lua) looks like this:
-- baz.lua
frames = 0
bar = foo:createBar()
function baz()
frames = frames + 1
bar:setText("frame: " .. frames)
end
And I'm made a simple and short (as well as I could) main.cpp that recreates this problem:
#include <memory>
#include <iostream>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <boost/ref.hpp>
#include <luabind/luabind.hpp>
class bar
{
public:
static void init(lua_State *L)
{
using luabind::module;
using luabind::class_;
module(L)
[
class_<bar>("bar")
.def("setText", &bar::setText)
];
}
void setText(const std::string &text)
{
std::cout << text << std::endl;
}
};
class foo
{
public:
foo() :
L(luaL_newstate())
{
int ret = luaL_dofile(L, "baz.lua");
if (ret != 0) {
std::cout << lua_tostring(L, -1);
}
luabind::open(L);
using luabind::module;
using luabind::class_;
module(L)
[
class_<foo>("bar")
.def("createBar", &foo::createBar)
];
bar::init(L);
luabind::globals(L)["foo"] = boost::ref(*this);
}
boost::reference_wrapper<bar> createBar()
{
auto b = std::make_shared<bar>();
bars_.push_back(b);
return boost::ref(*b.get());
}
void baz()
{
luabind::call_function<void>(L, "baz");
}
private:
lua_State *L;
std::vector<std::shared_ptr<bar>> bars_;
};
int main()
{
foo f;
while (true) {
f.baz();
}
}
This is compiled with:
g++ -std=c++11 -llua -lluabind main.cpp
I've discovered that if I put the bar = foo:createBar() into the baz() function then it doesn't error, so I assume I'm not initialising the globals in the global namespace correctly? Am I missing a luabind function that I need to call before I am able to do this? Or is this just not possible at all...
Thanks!

You're running baz.lua before you register any globals. Put the dofile command after registering your bindings.
The sequence is as follows:
You invoke foo's constructor in C++, which
creates a Lua state
runs lua.baz
registers your bindings
then in c++ you call f.baz.

Related

How to update a C++ object from Lua

How can I define a function in Lua which will update an object passed to it from C++ (perhaps by a pointer or reference?). I am using sol2, and would like to update the m_ data member of the Foo object f in the following C++ source code:
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include <iostream>
struct Foo
{
int m_ = 0;
void bar() { std::cout << "hello\n;"; }
};
int main(int, char*[])
{
sol::state lua;
lua.open_libraries(sol::lib::base);
sol::load_result script1 = lua.load_file("test1.lua");
sol::function fx = lua["f"];
Foo f;
fx(f);
assert(4 == f.x);
return 0;
}
... my lua script currently looks like this:
function f(x)
x:m_ = 4
end

C++ Segmentation Fault when Assigning Class Member Variable

I have a function "testfunc()" within a class "Test" under a namespace "TEST_NS".
In the main program, I've created a pointer to "Test" and would like to point to the "testfunc()" function. No problem...
I declare a private variable "foo" as a member of "Test", and the "testfunc()" function attempts to change the variable. This results in a segmentation fault.
Why does this happen?
I've created a simple example to show my issue. The segfault happens when I set foo = 1; in test.cpp
Here are the files:
main.cpp:
#include <iostream>
#include "test.h"
using namespace std;
int main () {
TEST_NS::Test * test;
test->testfunc();
return 0;
}
test.h:
#include <iostream>
namespace TEST_NS
{
class Test
{
int foo;
public:
Test();
~Test();
void testfunc();
};
}
test.cpp:
#include <iostream>
#include "test.h"
using namespace TEST_NS;
Test::Test() { }
Test::~Test() { }
void Test::testfunc()
{
std::cout << "This is testfunc()!" << std::endl;
foo = 1;
}
The code was compiled with
g++ main.cpp test.cpp -o test
And run with
./test
You need to instantiate an object of your class; try
TEST_NS::Test * test = new TEST_NS::Test();
test->testfunc();
Your main() function needs to change; the preferred way to write it is
int main () {
TEST_NS::Test test;
test.testfunc();
return 0;
}
If you really want to use a pointer (why?), then you should write:
int main () {
auto test = new TEST_NS::Test();
test->testfunc();
delete test;
return 0;
}
notice the addition of delete test;. But this is bad practice as it creates the potential for memory leaks, especially as more code is added (or if testfunc() throws an exception). If you really need to use pointers, it's much better to use std::unique_ptr<>.
int main () {
auto test = std::make_unique<TEST_NS::Test>();
test->testfunc();
return 0;
}
notice that this also gets rid of the explicit new which is generally a good idea, as well as the delete that had to be added; plus, it's exception-safe.
There is very little reason for "naked" new or delete in modern C++. Note that you can still get a "raw" pointer by using get(), although dereferencing would be preferred
void testfunc(TEST_NS::Test& test)
{
test.testfunc();
}
// ...
testfunc(*test);

How to invoke a C++ function after main() finishes

I am developing a C++ tool that should run transparent to main program. That is: if user simply links the tool to his program the tool will be activated. For that I need to invoke two functions, function a(), before main() gets control and function b() after main() finishes.
I can easily do the first by declaring a global variable in my program and have it initialize by return code of a(). i.e
int v = a() ;
but I cannot find a way to call b() after main() finishes?
Does any one can think of a way to do this?
The tool runs on windows, but I'd rather not use any OS specific calls.
Thank you, George
Use RAII, with a and b called in constructor/destructor.
class MyObj {
MyObj()
{
a();
};
~MyObj()
{
b();
};
};
Then just have an instance of MyObj outside the scope of main()
MyObj obj;
main()
{
...
}
Some things to note.
This is bog-standard C++ and will work on any platform
You can use this without changing ANY existing source code, simply by having your instance of MyObj in a separate compilation unit.
While it will run before and after main(), any other objects constructed outside main will also run at this time. And you have little control of the order
of your object's construction/destruction, among those others.
SOLUTION IN C:
have a look at atexit:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void bye(void)
{
printf("That was all, folks\n");
}
int main(void)
{
long a;
int i;
a = sysconf(_SC_ATEXIT_MAX);
printf("ATEXIT_MAX = %ld\n", a);
i = atexit(bye);
if (i != 0) {
fprintf(stderr, "cannot set exit function\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
http://linux.die.net/man/3/atexit
this still implies however that you have access to your main and you can add the atexit call. If you have no access to the main and you cannot add this function call I do not think there is any option.
EDIT:
SOLUTION IN C++:
as sudgested there is a c++ equivalent from std. I simply paste in here an example which i copied from the link available just below the code:
#include <iostream>
#include <cstdlib>
void atexit_handler_1()
{
std::cout << "at exit #1\n";
}
void atexit_handler_2()
{
std::cout << "at exit #2\n";
}
int main()
{
const int result_1 = std::atexit(atexit_handler_1);
const int result_2 = std::atexit(atexit_handler_2);
if ((result_1 != 0) or (result_2 != 0)) {
std::cerr << "Registration failed\n";
return EXIT_FAILURE;
}
std::cout << "returning from main\n";
return EXIT_SUCCESS;
}
http://en.cppreference.com/w/cpp/utility/program/atexit
Isn't any global variable constructed before main and destructed afterward? I made a test struct whose constructor is called before main and the destructor afterward.
#include <iostream>
struct Test
{
Test() { std::cout << "Before main..." << std::endl; }
~Test() { std::cout << "After main..." << std::endl; }
};
Test test;
int main()
{
std::cout << "Returning now..." << std::endl;
return 0;
}
If you're happy to stick with a single compiler and non-standard C/C++, then GCC's __attribute__((constructor)) and __attribute__((destructor)) might be of use:
#include <stdio.h>
void __attribute__((constructor)) ctor()
{
printf("Before main()\n");
}
void __attribute__((destructor)) dtor()
{
printf("After main()\n");
}
int main()
{
printf("main()\n");
return 0;
}
Result:
Before main()
main()
After main()
Alternatively to the destructor, you can use atexit() in a similar manner - in C++, you do not need to have access to main() to register atexit there. You can do that as well it in your a() - for example:
void b(void) {
std::cout << "Exiting.\n";
}
int a(void) {
std::cout << "Starting.\n";
atexit(b);
return 0;
}
// global in your module
int i = a();
That being said, I'd also prefer the global C++ class object, which will call the b() stuff in its destructor.

Function calls with class members?

Before I present the code which is found at the bottom of this post I would like to talk about the issue and the fix's that I do not desire. Okay basically I've created a GUI from scratch sort of and one requirement I wanted for this was allow components to have their own click executions so if i click a button or tab etc.. It would call Component->Execute(); Well normally you would do something like a switch statement of ids and if that components ID equaled n number then it would perform this action. Well that seemed kinda dumb to me and I thought there has to be a better way. I eventually tried to incorporate a feature in JAVA where you would do like Component.AddActionListener(new ActionListener( public void execute(ActionEvent ae) { })); or something like that and I thought that this feature has to be possible in C++. I eventually came across storing void functions into a variable in which could be executed at any time and modified at any time. However I hadn't noticed an issue and that was this only worked with static functions. So below you'll see my problem. I've patched the problem by using a pointer to SomeClass however this would mean having an individual function call for every class type is there no way to store a function callback to a non-static class member without doing the below strategy? and instead doing a strategy like the commented out code?
//Main.cpp
#include <iostream> //system requires this.
#include "SomeClass.h"
void DoSomething1(void)
{
std::cout << "We Called Static DoSomething1\n";
}
void DoSomething2(void)
{
std::cout << "We Called Static DoSomething2\n";
}
int main()
{
void (*function_call2)(SomeClass*);
void (*function_call)() = DoSomething1; //This works No Problems!
function_call(); //Will Call the DoSomething1(void);
function_call = DoSomething2; //This works No Problems!
function_call(); //Will Call the DoSomething2(void);
SomeClass *some = new SomeClass(); //Create a SomeClass pointer;
function_call = SomeClass::DoSomething3; //Static SomeClass::DoSomething3();
function_call(); //Will Call the SomeClass::DoSomething3(void);
//function_call = some->DoSomething4; //Non-Static SomeClass::DoSomething4 gives an error.
//function_call(); //Not used because of error above.
function_call2 = SomeClass::DoSomething5; //Store the SomeClass::DoSomething(SomeClass* some);
function_call2(some); //Call out SomeClass::DoSomething5 which calls on SomeClass::DoSomething4's non static member.
system("pause");
return 0;
}
//SomeClass.hpp
#pragma once
#include <iostream>
class SomeClass
{
public:
SomeClass();
~SomeClass();
public:
static void DoSomething3(void);
void DoSomething4(void);
static void DoSomething5(SomeClass* some);
};
//SomeClass.cpp
#include "SomeClass.h"
SomeClass::SomeClass(void)
{
}
SomeClass::~SomeClass(void)
{
}
void SomeClass::DoSomething3(void)
{
std::cout << "We Called Static DoSomething3\n";
}
void SomeClass::DoSomething4(void)
{
std::cout << "We Called Non-Static DoSomething4\n";
}
void SomeClass::DoSomething5(SomeClass *some)
{
some->DoSomething4();
}
Secondary Fix for what I'll do not an exact answer I wanted but it meets my needs for now along with allowing additional features which would have become overly complicate had this not existed.
//Component.hpp
#pragma once
#include <iostream>
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#include "Constants.hpp"
#include "ScreenState.hpp"
#include "ComponentType.hpp"
using namespace std;
class Component
{
static void EMPTY(void) { }
static void EMPTY(int i) { }
public:
Component(void)
{
callback = EMPTY;
callback2 = EMPTY;
callback_id = -1;
}
Component* SetFunction(void (*callback)())
{
this->callback = callback;
return this;
}
Component* SetFunction(void (*callback2)(int), int id)
{
this->callback_id = id;
this->callback2 = callback2;
return this;
}
void execute(void)
{
callback();
callback2(callback_id);
}
}
The syntax for pointers-to-member-functions is as follows:
struct Foo
{
void bar(int, int);
void zip(int, int);
};
Foo x;
void (Foo::*p)(int, int) = &Foo::bar; // pointer
(x.*p)(1, 2); // invocation
p = &Foo::zip;
(x.*p)(3, 4); // invocation
Mind the additional parentheses in the function invocation, which is needed to get the correct operator precedence. The member-dereference operator is .* (and there's also ->* from an instance pointer).

How to call a function which belongs to a class in C++?

I am trying to call the function hello which belongs to the class program1
#include <iostream>
using namespace std;
class program1
{
program1();
~program1();
hello();
}
program1::hello()
{
cout<<"hello";
}
int main()
{
program1.hello(); //Call it like a normal function...
cin.get();
}
Names inside a class are private by default.
class program1 {
public:
program1();
~program1();
void hello() ;
};
// ...
int main(int, char **) {
program1 myProgram;
myProgram.hello();
return 0;
}
Alternatively, you can invoke a method on a temporary:
int main(int, char **) {
program1().hello();
return 0;
}
but that's probably for later in the semester.
you forgot to create an object:
int main()
{
program1 p1;
p1.hello();
}
Class definition should end with ;
Secondly, you need to instantiate class to call members on it. ( i.e., creation of an object for the class )
In C++, methods should have return type.
program1::hello(); // method should have a return type.
class members and methods are private by default, which means you cannot access them outside the class-scope.
So, the class definition should be -
class program1
{
public: // Correction 1
program1();
~program1();
void hello(); // Correction 2
};
void program1::hello() // Need to place return type here too.
{
cout<<"hello";
}
Now on creation of object for class program1, it's method hello() can be called on it.
This version is edited. (make sure you include all the body of the methods)
#include <iostream>
using namespace std;
class program1
{
public: // To allow outer access
program1();
~program1();
void hello(); // The void
}; // Don't miss this semicolon
void program1::hello() // The void
{
cout<<"hello";
}
int main()
{
program1 prog; // Declare an obj
prog.hello(); //Call it like a normal function...
cin.get();
}
I noticed that you left out return type for your hello() function.
If you want to call hello() as a member function, then as suggested you should create an object to it.
program1 prog;
prog.hello();
If you want to call it without an object, the you should use static function.
class program1
{
public: // To allow outer access
program1();
~program1();
static void hello(); // The void
}
then you can call it this way:
program1::hello();
Therefore the working code should be this way:
#include <iostream>
using namespace std;
class program1 {
public:
void hello();
}; // Don't miss this semicolon
class program2 {
public:
void static hello(); // to demonstrate static function
}; // Don't miss this semicolon
void program1::hello() {
cout << "Hello, I'm program1." << endl;
}
void program2::hello() {
cout << "Hello, I'm program2." << endl;
}
int main(void) {
program1 prog1;
prog1.hello(); // Non-static function requires object
program2::hello(); // Static function doesn't
return 0; // Return 0
}