convert a C/C++ array to a Ruby array

E

elathan

Hello!

While writting a Ruby extension, consider the prototype:

double * foo(void);

I would like, optimally, to return a Ruby array with floats, but the problem is that I don't know
the length of the C array of doubles which foo() returns.

Is there a way to accomplish such a thing? If not, how should the Right (TM) Ruby
extension treat such a prototype?

I am compiling with g++, so I can use all the functionality C++ provides.

Regards,
Elias
 
N

nobu.nokada

Hi,

At Thu, 8 Jan 2004 18:03:15 +0900,
While writting a Ruby extension, consider the prototype:

double * foo(void);

I would like, optimally, to return a Ruby array with floats, but the problem is that I don't know
the length of the C array of doubles which foo() returns.

How do you use it in C at all?
 
E

elathan

Quoting (e-mail address removed):
How do you use it in C at all?

Actually it is part of the functionallity a C++ Class embeds:

Foo::Foo(int n, double *f);
double * Foo::Get();

Okay, I can grab 'n' from the constructor and use it as the length
of the Ruby/C array, but I wanted to make sure that there is no
general way to procceed without messing with the details of the
C/C++ code.

Regards,
Elias
 
N

nobu.nokada

Hi,

At Thu, 8 Jan 2004 18:46:21 +0900,
Actually it is part of the functionallity a C++ Class embeds:

Foo::Foo(int n, double *f);
double * Foo::Get();

Okay, I can grab 'n' from the constructor and use it as the length
of the Ruby/C array, but I wanted to make sure that there is no
general way to procceed without messing with the details of the
C/C++ code.

If you can get the length from Foo, you can use it.

What do you really intend, pseudo array?
 
E

elathan

Quoting (e-mail address removed):
If you can get the length from Foo, you can use it.

Yes I know. The problem is that I am creating Ruby bindings for a large
framework (more than 50 C++ classes) and the bindings code is automagically
generated by reading the C++ prototypes, using a handmade Ruby
parser/compiler.

The generated code is quite readable and can be altered by a human, but
I want to eliminate as much as I can the human intervation, so as the majority of the
C++ prototypes to be compiled in the Ruby API without human editing.
What do you really intend, pseudo array?

If I was doing this by myself, I would create a Ruby array of length Foo::n and
populate it with the double(s) Foo::Get() returns.

But this requires some magick, i.e. to understand further the functionality of Class Foo;
not just read the prototypes and translate them using an application. So, I asked
here if someone knows a better way...

I checked SWIG and it returns a special SWIG object in that case...

Regards,
Elias
 
P

Phil Tomson

Quoting (e-mail address removed):

Yes I know. The problem is that I am creating Ruby bindings for a large
framework (more than 50 C++ classes) and the bindings code is automagically
generated by reading the C++ prototypes, using a handmade Ruby
parser/compiler.

The generated code is quite readable and can be altered by a human, but
I want to eliminate as much as I can the human intervation, so as the majority of the
C++ prototypes to be compiled in the Ruby API without human editing.
I checked SWIG and it returns a special SWIG object in that case...


I'm just curious, why didn't you use SWIG for this? Sounds sort of
like you're recreating SWIG.

Phil
 
E

Elias Athanasopoulos

I'm just curious, why didn't you use SWIG for this? Sounds sort of
like you're recreating SWIG.

Because SWIG doesn't produce readable by human C++ code. Also, there
are things in the extension that I prefer to port to Ruby in a different
way from the one SWIG uses.

I know that using SWIG's internal scripting language can do almost
everything I do, but I already know C++ and it is easiest to do it
in my way. I don't recreate SWIG. My auto-generated code is specific
for my extension and the compiler I create is less than 500 lines of
Ruby code (I don't parse the prototypes, because the original C++ code
has RTTI related functions i.e. 'bring me the arguments foo() takes
as input').

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top