Re: Calling C++ functions in luawith objects as arguments

Tony Kostanjsek <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
hi Janik,
start off slowly and test it step by step:
bind CByteImage and a simple debug method that dumps some info about your 
object, for example

struct CByteImage
{
int width;
int height;
CByteImage() {width=13; height=13;}
void print() {cout << width << " / " << height;}
};

module(luaState)
[
class_<CByteImage>("CByteImage")
.def("print", &CByteImage::print)
];

that should enable you to instantiate the image and see if the instance responds 
correctly.
in Lua:
local img = CByteImage()
img:print() -- note the use of the : operator for method call

IIRC luabind will allow you to use "img" as pointer, reference or instance.

then bind the processing functions (I added the namespace to the first module 
call so it's the same hierarchy as in C++):
(also, don't overload for now to keep it simple)

module(luaState, "ImageProcessor") 
[
  def("Invert", &ImageProcessor::Invert),
];

now you should be able to call it like this:
local inImg = CByteImage()
local outImg = CByteImage()
ImageProcessor.Invert(inImg, outImg) -- note the use of the . operator for 
calling a regular function within a namespace


Try to create and register the bindings in the correct order, i.e. CByteImage 
before ImageProcessor.Invert functions ... I remember vaguely having had some 
problems with luabind bailing with "unknown class" errors, but I'm not too sure 
anymore.

hth,
Tony

----- Ursprüngliche Mail ----
Von: "[email protected]" <[email protected]>
An: Francisco Ganacim <[email protected]>; [email protected]
Gesendet: Freitag, den 18. Februar 2011, 13:55:51 Uhr
Betreff: Re: [luabind] Calling C++ functions in luawith objects as arguments

hey francisco,

thanks for your reply, now i know how to overload functions in a correct way.
Nevertheless i still don't know how to expose the CByteImage type to lua. Do i 
have to use the "user defined type" converter? Or do i have to expose those 
objects?
And how can i realise this in my concrete application?

Ups, I've seen that there is a lot of HTML text of what i've posted this 
morning. So i will post my text from this morning again, this time in text form, 
so that it is readable.

Greets Janik

**************************************************************************************************************************

Posted this morning

Hello folks,

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.

The C++ functions are in a namespace:

namespace ImageProcessor
{
       void Invert(const CByteImage *pInputImage, CByteImage *pOutputImage)
        {
           //Function declaration here!
        }
        void Canny(const CByteImage *pInputImage, CByteImage *pOutputImage, int 
nLowThreshold, int nHighThreshold)
        {
            //Function declaration here!.
        }
        void Canny(const CByteImage *pInputImage, CVec2dArray &resultPoints, 
CVec2dArray &resultDirections, int nLowThreshold, int nHighThreshold)
        {
            //Function declaration here!.
        }
        ...
        ... More functions here
        ...
}


As you can see, the functions take several arguments, for example objects of the 
class CByteImage.

In my C++ Code, I create these objects of the class CByteImage:  
CByteImage *m_pInImage;
CByteImage *m_pOutImage;
m_pInImage = new CByteImage(int width, int height, CByteImage::eGrayScale);
m_pOutImage = new CByteImage(int width, int height, CByteImage::ImageType);

Later, i call for example the first two functions in my C++ code like this:

ImageProcessor::Invert(m_pInImage, m_pOutImage);
ImageProcessor::Canny(m_pInImage, m_pOutImage, 50, 50);


Now i want to call these two C++ functions in lua, and that's the point where my 
problems begin:
Nicolas told me, that i need to bind the functions in the namespace where they 
are defined. 

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?
What I’ve done so far:

lua_State* LuaState = lua_open();
// open the new state with luabind 
luabind::open(LuaState);
// export the functions we want 
luabind::module(LuaState) 
[
// First Arg: Name of the function in Lua, Second Arg: Function in C++ 
luabind::def("Invert", &ImageProcessor::Invert) 
luabind::def( "Canny", ( void (*) (const CByteImage*, CByteImage*, int, int) ) 
&ImageProcessor::Canny ); 


//Binding of the CByteClass
luabind::class_<CByteImage>("CByteImage")
.def(luabind::constructor<>()) 
//Not sure how to binb objects
luabind::object global_vars = luabind::globals();
global_vars["a"] = a;

];

Now I have thought that I could call these two C++ functions in lua like this:

--Lua

test = Invert(m_pInImage, m_pOutImage)
test2 = Canny(m_pInImage, m_pOutImage, 50, 50)

Again, is this correct or how do I have to do this?

Thank you very much for your help

Greetz Janik

-----Original-Nachricht-----
Subject: Re: [luabind] Calling C++ functions in luawith objects as arguments
Date: Fri, 18 Feb 2011 12:25:58 +0100
From: Francisco Ganacim <[email protected]>
To: "[email protected]" <[email protected]>, [email protected]



On Fri, Feb 18, 2011 at 9:24 AM, Francisco Ganacim <[email protected]> wrote:
Hi, 
// First Arg: Name of the function in Lua, Second Arg: Function in C++ 
luabind::def("Invert", &ImageProcessor::Invert) 
//I don't know exactly how to bind the arguments of the Canny function 
luabind::def("Canny", &ImageProcessor::Canny, (void), (const CByteImage *), 
(CByteImage *), (int), (int)),


have you tried 
luabind::def( "Canny", ( void (*) (const CByteImage*, CByteImage*, int, int) 
&ImageProcessor::Canny );
?

ops, one ")" missing...
luabind::def( "Canny", ( void (*) (const CByteImage*, CByteImage*, int, int) ) 
&ImageProcessor::Canny ); 

-- 
Francisco Ganacim



-- 
Francisco Ganacim




------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user




------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.