[luabind] Calling C++ functions in luawith objects as arguments
Brought to you by:
arvidn,
daniel_wallin
From: <ch...@t-...> - 2011-02-18 07:33:22
|
<html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body><font face="arial,helvetica,sans-serif" color="#000000"> <p><span style="font-size: 11pt; color: black; font-family: Arial;">Hello folks,<br /><br />i'm new to Lua/Luabind, i'm trying to call some C++ functions from a library with lua and i hope you can give me some help.<br /><br />The C++ functions are in a namespace:<br /><br />namespace ImageProcessor<br />{<br /> void Invert(const CByteImage *pInputImage, CByteImage *pOutputImage)<br /> {<br /> //Function declaration here!<br /> }</span></p> <p><span style="font-size: 11pt; color: black; font-family: Arial;"> void Canny(const CByteImage *pInputImage, CByteImage *pOutputImage, int nLowThreshold, int nHighThreshold)<br /> {<br /> //Function declaration here!.<br /> }</span></p> <p style="margin-bottom: 12pt;"><span style="font-size: 11pt; color: black; font-family: Arial;"> void Canny(const CByteImage *pInputImage, CVec2dArray &resultPoints, CVec2dArray &resultDirections, int nLowThreshold, int nHighThreshold)<br /> {<br /> //Function declaration here!.<br /> }<br /> ...<br /> ... More functions here<br /> ...<br />}<br /><br /><br />As you can see, the functions take several arguments, for example objects of the class CByteImage.<br /><br />In my C++ Code, I create these objects of the class CByteImage: </span></p> <p><span style="font-size: 11pt; color: black; font-family: Arial;">CByteImage *m_pInImage;<br />CByteImage *m_pOutImage;<br />m_pInImage = </span><span style="font-size: 11pt; color: blue; font-family: Arial;">new</span><span style="font-size: 11pt; color: black; font-family: Arial;"> CByteImage(int width, int height, CByteImage::eGrayScale);<br />m_pOutImage = </span><span style="font-size: 11pt; color: blue; font-family: Arial;">new</span><span style="font-size: 11pt; color: black; font-family: Arial;"> CByteImage(int width, int height, CByteImage::ImageType);<br /><br />Later, i call for example the first two functions in my C++ code like this:<br /><br />ImageProcessor::Invert(m_pInImage, m_pOutImage);<br />ImageProcessor::Canny(m_pInImage, m_pOutImage, 50, 50);<br /><br /><br />Now i want to call these two C++ functions in lua, and that's the point where my problems begin:<br />Nicolas told me, that i need to bind the functions in the namespace where they are defined. </span></p> <p><span style="font-size: 11pt; color: black; font-family: Arial;">Now the thing with the objects. I don’t really know how to handle these things. Do I have to bind the whole class CByteImage with the objects that I use as arguments of my function? Do I have to use the “user definded types” thing to tell lua how to handle these data types? Do I use pointers and make them available for lua?</span></p> <p><span style="font-size: 11pt; color: black; font-family: Arial;">What I’ve done so far:<br /><br />lua_State* LuaState = lua_open();<br />// open the new state with luabind <br />luabind::open(LuaState);<br />// export the functions we want <br />luabind::module(LuaState) <br />[<br />// First Arg: Name of the function in Lua, Second Arg: Function in C++ <br /> luabind::def("Invert", &ImageProcessor::Invert) <br />//I don't know exactly how to bind the arguments of the Canny function <br />luabind::def("Canny", &ImageProcessor::Canny, (void), (const CByteImage *), (CByteImage *), (int), (int)),<br /><br />//Binding of the CByteClass<br />luabind::class_<CByteImage>(</span><span style="font-size: 11pt; color: #a31515; font-family: Arial;">"CByteImage"</span><span style="font-size: 11pt; color: black; font-family: Arial;">)<br />.def(luabind::constructor<>()) <br />//Not sure how to binb objects<br />luabind::object global_vars = luabind::globals();<br />global_vars["a"] = a;<br /><br />];</span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">Now I have thought that I could call these two C++ functions in lua like this:</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">--Lua</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">test = Invert(<span style="color: black;">m_pInImage</span>, <span style="color: black;">m_pOutImage</span>)</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">test2 = Canny(<span style="color: black;">m_pInImage</span>, <span style="color: black;">m_pOutImage</span>, 50, 50)</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">Again, is this correct or how do I have to do this?</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">Thank you very much for your help</font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3"> </font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="mso-bidi-font-size: 11.0pt;"><font size="3">Greetz Janik</font></span></p> <br /></font></body></html> |