|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Function pointer as parameter of a function to export in LUA ?Hi,
In my C++ code I have this fonction to export in lua using luabind : inline void ScriptingApp::setCallBack(functionType func){_callbackFunc = func;} doing this declaration : luabind::class_<ScriptingApp>("ScriptingApp") .def("setCallBack", &ScriptingApp::setCallBack) The problem is that functionType is defined as a function pointer : typedef const char * mstring; typedef void (* functionType) (void, const int&, mstring); And that doesn't compile. I get this error :
I don't know if it is possible to use such function pointer as argument of a function to export in LUA ? If not maybe trying to see what is possible using functor instead of function pointer might work ? Thanks for any help, Aurélien MAIGNAN ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ luabind-user mailing list luabind-user@... https://lists.sourceforge.net/lists/listinfo/luabind-user |
|||||||||||||||||
|
|
Re: Function pointer as parameter of a function to export in LUA ?Aurélien MAIGNAN wrote:
> Hi, > > In my C++ code I have this fonction to export in lua using luabind : > > inline void ScriptingApp::setCallBack(functionType func){_callbackFunc = > func;} > > doing this declaration : > > luabind::class_<ScriptingApp>("ScriptingApp") > .def("setCallBack", &ScriptingApp::setCallBack) > > The problem is that functionType is defined as a function pointer : And the problem with that is that luabind cannot map this to one of the integral types in Lua, and neither can it map it to one of your defined classes. Out of curiosity, and because it might help in understanding why you want to this: How do you intend to call this function from your Lua script? Where is the parameter coming from that you intend to pass? If you want to pass a Lua function, that can be done by making the parameter a luabind::object&, something like this: static void foo(const luabind::object& bar, int i, const char * str) { try { bar(i, str); } catch (error& e) { // do your error handling here } } (this is untested; I just mixed together some bits from my own code) Enno. -- "Multiple exclamation marks [...] are a sure sign of a diseased mind." - Terry Pratchett, "Eric" (http://tinyurl.com/evan) ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ luabind-user mailing list luabind-user@... https://lists.sourceforge.net/lists/listinfo/luabind-user |
|||||||||||||||||
|
|
Re: Function pointer as parameter of a function to export in LUA ?Hi,
thanks for your reply. My intend was to do something like that : in C++ : typedef void (*functionType) (void*, int) [sorry I wrote void instead of void* in my first mail] class AClass { functionType _callbackFunc; void memberMethod(int); void memberMethodThatActLikeAListener(...); static void staticFunction(void* MyThis, int i) { AClass * Obj = reinterpret_cast<AClass*>(myThis); return Obj->memberMethod(i); }; void setCallBack(functionType func); functionType getCallBack(); }; inline void AClass ::setCallBack(functionType func) {_callbackFunc = func;} inline functionType AClass ::getCallBack() {return _callbackFunc;} AClass ::memberMethodThatActLikeAListener(...) { //do some stuff, and particularly : getCallBack()(this,id); } registering : luabind::class_<AClass >("AClass ") .def("setCallBack", &AClass ::setCallBack) .def("staticFunction", &AClass ::staticFunction) And in lua : AClass a; a:setCallBack(a:staticFunction); This is what I wanted to do. Up to now I manage to have another solution that act someway like this using what you suggested : in C++ : // the lua function to callback luabind::object _callbackFunc; void call_CallBack_Function(const int& id, mstring name); void setCallBack(luabind::object func); luabind::object getCallBack(); void AClass::call_CallBack_Function(const int& id, mstring name) { if(_callbackFunc && luabind::type(_callbackFunc) == LUA_TFUNCTION) { luabind::call_function<void>(_callbackFunc, id, name, this); } } and in LUA : --define the callback function function doUpdatePos(id,name,self) print "\nI update the position NOW!\n" self:updatePos(id,name) end function initCallback() sas:setCallBack(doUpdatePos) end so instead of doing all the job in C++ in manage the callback process going from C+++ to lua and then going back from lua to C++. Thanks, Aurélien MAIGNAN 2008/6/23 Enno Rehling <enno.rehling@...>:
------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ luabind-user mailing list luabind-user@... https://lists.sourceforge.net/lists/listinfo/luabind-user |
| Free Forum Powered by Nabble | Forum Help |