[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

SWIG, Ruby, C and typemaps

Jeff Sack

1/11/2007 10:18:00 PM


Hello all. I am seeking some advice on using SWIG to wrap some native
C code provided to me (I don't have the ability to change it).
Basically, I have something like this:

The definition of one of the problematic methods I am trying to wrap
looks something like this. This is in my SWIG interface file (.i
file):

int open_file(ResponseObject **resp, const char *fname);

And the normal way of using this is as follows:

ResponseObject *reponsObj;
const char *filename="input_file";

if(open_file(&reponsObj, filename)!=0)
{
// handle non-zero error code
}
else
{
// success!
}


I think the most natural way of accessing this method might be
something like this:

resp = open_file('input_file')

In this scenario, resp is a Ruby object representing ResponseObject,
and the original int return value is now mapped to a conditionally
thrown exception. If open_file returns 0, then no exception would be
thrown, otherwise, the Ruby method open_file would throw an exception
which wraps the int return code.

Thoughts? I know there is some SWIG/typemap magic that can do this,
but so far I'm a bit stumped. Can someone offer some advice? I can
provide more details if needed.

Thanks so much in advance for your help. Much appreciated.

-Jeff

4 Answers

Alex Fenton

1/12/2007 9:43:00 AM

0

Jeff Sack wrote:

> The definition of one of the problematic methods I am trying to wrap
> looks something like this. This is in my SWIG interface file (.i
> file):
>
> int open_file(ResponseObject **resp, const char *fname);

....

> I think the most natural way of accessing this method might be
> something like this:
>
> resp = open_file('input_file')

You'lll first want a typemap to tell SWIG to convert one ruby input (the string filename) into the two parameters C is expecting. Along the lines of (untested)

%typemap(in,numinputs=1) (ResponseObject **resp, const char *fname) {
$1 = new ResponseObject();
$2 = rb_str_new2(fname);
}

Then you'll need another typemap to treat the resp argument as a ruby return value, and convert it to a ruby wrapper round the C object.

%typemap(argout) (ResponseObject **resp) {
$result = SWIG_NewPointerObj($1, $1_descriptor, 1);
}

hth
alex

dennism3

3/21/2012 7:51:00 PM

0

In article
<17005859.87.1332320969695.JavaMail.geo-discussion-forums@yner4>, syvyn11
<syvyn11@gmail.com> wrote:

>>On Tuesday, March 20, 2012 5:55:25 PM UTC-4, Dennis M wrote:
>>BTW Scott Walker and his fellow corrupt rightwing henchmen are history in
>>Wisconsin (thanks Ed), even with all of the out of state money behind them.
>
>Never mind that Walker's reforms are working to save money and jobs in
>Wisconsin.

Looks like the people who live there don't agree with this mantra.

Walker sees the writing on the wall anyway, he was recently saying how he
could make much more money "in the private sector."

If he even had the few brain cells Sarah Palin has he'd voluntarily resign
before he gets thrown out of office through the biggest recall in US
history.

Not Sure

3/22/2012 2:46:00 AM

0

On Mar 20, 2:55 pm, denni...@dennism3.invalid (Dennis M) wrote:
> In article
> <f7e7817e-a799-4840-8d9b-e3c6281ff...@ow8g2000pbc.googlegroups.com>,
>
> wolfagain <w...@provide.net> wrote:
> >So all that RANTING like a fool for Union Rights in Wisconsin and Ohio
> >was just Ed's way of thanking union employees for all that Cash. He
> >claimed he gave it all to charity...BUT records indicate he gave only
> >100K of it to charity.
>
> >Who ELSE is Big Mouth Ed getting cash from???!
>
> Let's see those "records" you're talking about. If he didn't give all of it
> to charity he probably gave it to the union or some other political group
> he's aligned with.
>
> BTW Scott Walker and his fellow corrupt rightwing henchmen are history in
> Wisconsin (thanks Ed),

After the last "recall" humiliated the hysterical dipshits on the
left, this one should be even funnier. See you in the summer when
Walker's stronger than ever :)

even with all of the out of state money behind them.

Ubiquitous

3/24/2012 10:21:00 AM

0

dennism3@dennism3.invalid wrote:

>If he even had the few brain cells Sarah Palin

Ad hominem noted. Get back to us when you have a real argument to make.

--
"Admittedly, conservatives give as good as they get. The difference
between us and [leftists] is that we can argue as well as inveigh.
They can only hurl invectives." -- Don Feder