[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Calling C++ class methods from Lua (newbie)
- From: rob <lua@...>
- Date: Mon, 10 Oct 2005 19:24:23 +0100
Henderson, Michael D wrote:
Hi. I'm using the 5.1-alpha package. I am changing C++ programs to use
configuration files written in Lua. It is a joy to see a future where
I am not maintaining many different custom parsers :)
You could look at luabind or tolua++, both allow you to deal directly
with c++ classes.
This may not be the best approach, but I'm trying to make the lua
script call directly into the C++ class that maintains the application
environment. The parser was written in C++, so it carried around a
context pointer and would call the methods directly
(context->SetHostName(someString)). I've created a C/Lua function (in
this example int fLua_SetHostName(lua_State *L);) that knows how to
call the C++ class to set the host name.
I'm able to retrieve the string, but I'm having difficulty
understanding how to deal with the class pointer. I'm wondering how to
pass in the context. I can create a global external to the function
(ApplicationEnvironment *context;) but some of our applications are
threaded and need multiple contexts.
I would like to push a pointer to the context on to the Lua stack, but
I'm not at all sure how to push it on and pull it off. Do I create a
lua_State for each thread? Would I (should I) push the context onto
the stack, or would I be better off putting it in a table? I think
that tables would be more flexible. Either way, how can I handle C++
pointers on the stack or in tables?