Re: returning an object in XS
by davido (Cardinal) on Feb 29, 2012 at 03:52 UTC
|
There needs to be a typemap that will explain to Perl how to bind a rect * to a Perl structure of some sort. There are several CPAN modules that help to simplify the process. The one I'm most familiar with is Inline::CPP. But I'm highly biased.
The gist of it is that you can create a C++ class that maps to a Perl class. The constructor becomes new(). The destructor becomes DESTROY(), and so on. The docs for Inline::C, Inline::C-Cookbook, and Inline::CPP are required reading in getting started with it. But it's fun (if you're slightly masochistic) once you get the hang of it.
Inline::Struct is compatible with C (ie, doesn't require C++), and will bind C structures to Perl objects too.
There was a time when Inline::CPP wasn't installing on the vast majority of systems out there. Over the last few months we've gotten it to the point that there's only one class of smoke testers that are still having trouble with it (NetBSD -- Any NetBSD pros get in touch with me so we can try to figure it out). Most other common platforms are able to install it and use it. There's also an email list where people can answer questions about the Inline modules: inline@perl.org. See http://lists.perl.org for details.
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
I should mention that if you happen to be one of the unfortunate few for whom it doesn't install, download the latest dev release tarball, unpack it, and perform the mantra. Often the dev releases include tweaks undergoing smoke testing to 'PASS' additional platforms.
| [reply] |
|
|
|
|
Re: returning an object in XS
by ikegami (Patriarch) on Feb 29, 2012 at 07:18 UTC
|
By the way, *result = r; is wrong. You're writing to unallocated memory. You need to allocate a rect first, and place a pointer to it in result. That also means the code calling your function will have to free it, which makes it wholly inappropriate for an XS function to return this.
Sorry, don't know how to call C++ code from C, so I can't help you more than that.
| [reply] [d/l] [select] |
|
|
Hi Ikegami,
After I fixed it, it passed but then it gives me another error shown below
*** glibc detected *** free(): invalid next size (fast): 0x000000000089aaa0 ***
Abort
I don't know what happen, would you please help?
The code is posted below. By the way, is that possible you can scan thru my code to see if I am doing what I supposed to do?
Thanks Ikehami,
MODULE = nDB PACKAGE = geom
REQUIRE: 1.925
SV*
get_bbox(rects)
SV* rects
INIT:
rect* result;
rect** arrRects;
I32 nRects = 0;
SV* sv;
SV* rv = newRV_noinc(sv);
if (rv == NULL)
warn("RV has not been created sucessfully!\n");
SV** ssv;
double minx,miny,maxx,maxy,tmp_minx,tmp_miny,tmp_maxx,tmp_maxy;
int n;
if ((!SvROK(rects))||(SvTYPE(SvRV(rects)) != SVt_PVAV)||((nRects =
+av_len((AV*)SvRV(rects))) < 0)) {
XSRETURN_UNDEF;
}
arrRects = (rect**)safemalloc(sizeof(rect*)*(nRects));
for (n=0;n<=nRects;n++) {
ssv = av_fetch((AV*)SvRV(rects), n, 0);
if (ssv != NULL) {
arrRects[n] = (rect*)malloc(sizeof(rect));
if (arrRects[n] == NULL) {
warn("get_bbox: unable to malloc rect\n");
} else {
arrRects[n] = (rect*)SvIV((SV*)SvRV(*ssv));
}
}
}
CODE:
minx = (*arrRects)->x1();
miny = (*arrRects)->y1();
maxx = (*arrRects)->x2();
maxy = (*arrRects)->y2();
for (n=1;n<=nRects;n++) {
tmp_minx = (*(arrRects+n))->x1();
tmp_miny = (*(arrRects+n))->y1();
tmp_maxx = (*(arrRects+n))->x2();
tmp_maxy = (*(arrRects+n))->y2();
if (tmp_minx < minx)
minx = tmp_minx;
if (tmp_miny < miny)
miny = tmp_miny;
if (tmp_maxx > maxx)
maxx = tmp_maxx;
if (tmp_maxy > maxy)
maxy = tmp_maxy;
}
result = (rect*)malloc(sizeof(rect*));
rect r(minx,miny,maxx,maxy);
*result = r;
sv_setref_iv(rv,"rect",(IV) result);
RETVAL = rv;
OUTPUT:
RETVAL
| [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
Re: returning an object in XS
by BrowserUk (Patriarch) on Feb 29, 2012 at 03:52 UTC
|
rect r(minx,miny,maxx,maxy);
What language is that?
It doesn't look like valid C to me? r() is a function taking 4 doubles, but what is the rect prefix doing?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
| [reply] |
|
|
|
|
|
|
|
| [reply] |
|
|
that was just my class rect constructor.
Is that a macro?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] |