[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby and C++

SteveD

1/18/2006 4:16:00 PM

hey everyone,
im trying to write a script as well as a program each of which can
communicate with one another. the script should be able to have access
to the class in my c++ code. just a simple class with a couple of
variable and some accessors and modifiers for them. i want to be able
to create an object of that class type in the script and call the
classes functions in order to manipulate the data. at this point i can
create an object in the script and it seems that i can modify the object
through the set methods, but when i call the get method for that
function it get an error saying it is expecting the class type as the
return type(i assume thats what the error means.. "in 'getInt': wrong
type - expected DummieClass")
for all of my set methods i return self. for the get methods i return a
VALUE object which should be holding the value of the class member i am
working with.

********************************************************************
c++ code:

static VALUE _wrap_get_int(VALUE self)
{
GET_MY_CLASS(self); //a macro that returns an instance pointer of
my class

//this function call returns a VALUE after converting the c++ int
return GetRubyInt(pDummie->getInt());
}

*********************************************************************
ruby script:

obj = SimpleExt::Dummie.new(12,3.4, "my String")
obj.setInt(100)
x = obj.getInt()

print x

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

it seems to be crashing out when it gets to the line x = obj.getInt().
can anyone tell me whats wrong with this, or am i going about this the
wrong way? im thinking that i may have to change my c++ code around,
but i dont know. if anyone could lend some adive i would appreciate it.
thanks
steveD









--
Posted via http://www.ruby-....


5 Answers

Mark Volkmann

1/18/2006 4:24:00 PM

0

I can't speak to the specific problem you're running into. However, I
can recommend using SWIG for this sort of thing. I think it makes this
kind of integration much easier. I created a presentation on this that
you can read at http://www.ociweb.com/jnb/jnbJa.... It mostly
focuses on using SWIG to invoke C++ from Java, but at the end there is
a Ruby example.

On 1/18/06, SteveD <focusproductions@comcast.net> wrote:
> hey everyone,
> im trying to write a script as well as a program each of which can
> communicate with one another. the script should be able to have access
> to the class in my c++ code. just a simple class with a couple of
> variable and some accessors and modifiers for them. i want to be able
> to create an object of that class type in the script and call the
> classes functions in order to manipulate the data. at this point i can
> create an object in the script and it seems that i can modify the object
> through the set methods, but when i call the get method for that
> function it get an error saying it is expecting the class type as the
> return type(i assume thats what the error means.. "in 'getInt': wrong
> type - expected DummieClass")
> for all of my set methods i return self. for the get methods i return a
> VALUE object which should be holding the value of the class member i am
> working with.
>
> ********************************************************************
> c++ code:
>
> static VALUE _wrap_get_int(VALUE self)
> {
> GET_MY_CLASS(self); //a macro that returns an instance pointer of
> my class
>
> //this function call returns a VALUE after converting the c++ int
> return GetRubyInt(pDummie->getInt());
> }
>
> *********************************************************************
> ruby script:
>
> obj = SimpleExt::Dummie.new(12,3.4, "my String")
> obj.setInt(100)
> x = obj.getInt()
>
> print x
>
> ***********************************************************************
>
> it seems to be crashing out when it gets to the line x = obj.getInt().
> can anyone tell me whats wrong with this, or am i going about this the
> wrong way? im thinking that i may have to change my c++ code around,
> but i dont know. if anyone could lend some adive i would appreciate it.
> thanks
> steveD
>
>
>
>
>
>
>
>
>
> --
> Posted via http://www.ruby-....
>
>


--
R. Mark Volkmann
Partner, Object Computing, Inc.


Joe Van Dyk

1/18/2006 4:25:00 PM

0

On 1/18/06, SteveD <focusproductions@comcast.net> wrote:
> hey everyone,
> im trying to write a script as well as a program each of which can
> communicate with one another. the script should be able to have access
> to the class in my c++ code. just a simple class with a couple of
> variable and some accessors and modifiers for them. i want to be able
> to create an object of that class type in the script and call the
> classes functions in order to manipulate the data. at this point i can
> create an object in the script and it seems that i can modify the object
> through the set methods, but when i call the get method for that
> function it get an error saying it is expecting the class type as the
> return type(i assume thats what the error means.. "in 'getInt': wrong
> type - expected DummieClass")
> for all of my set methods i return self. for the get methods i return a
> VALUE object which should be holding the value of the class member i am
> working with.
>
> ********************************************************************
> c++ code:
>
> static VALUE _wrap_get_int(VALUE self)
> {
> GET_MY_CLASS(self); //a macro that returns an instance pointer of
> my class
>
> //this function call returns a VALUE after converting the c++ int
> return GetRubyInt(pDummie->getInt());
> }
>
> *********************************************************************
> ruby script:
>
> obj = SimpleExt::Dummie.new(12,3.4, "my String")
> obj.setInt(100)
> x = obj.getInt()
>
> print x
>
> ***********************************************************************
>
> it seems to be crashing out when it gets to the line x = obj.getInt().
> can anyone tell me whats wrong with this, or am i going about this the
> wrong way? im thinking that i may have to change my c++ code around,
> but i dont know. if anyone could lend some adive i would appreciate it.

It would help if you could post the entire code. Or at least, some
sample code that exhibits the same behavior.


Asfand Yar Qazi

1/19/2006 12:14:00 PM

0

SteveD wrote:
> hey everyone,
<snip>

I suggest ignoring the SWIG suggestion - SWIG just overcomplicates
things - one more layer where things can go wrong. With Ruby's easy
to use interface, it is not needed.

If you could give some more details of how your code works (i.e. list
the code :-) I could help you. There seems to either be a problem
with the GET_MY_CLASS macro, or the GetRubyInt method. But without
more details - I dunno.

--
Entry in RollerCoaster Tycoon 2 readme.txt file:
RollerCoaster Tycoon2 must be played on a video card capable of
640x480 screen resolution at a bit depth setting of 256 bits.
And the proof that playing too many strategy games causes loss of
humour: http://tinyurl...

Paul Brannan

1/19/2006 6:00:00 PM

0

On Thu, Jan 19, 2006 at 01:16:14AM +0900, SteveD wrote:
> classes functions in order to manipulate the data. at this point i can
> create an object in the script and it seems that i can modify the object
> through the set methods, but when i call the get method for that
> function it get an error saying it is expecting the class type as the
> return type(i assume thats what the error means.. "in 'getInt': wrong
> type - expected DummieClass")

I suspect that it is the GET_MY_CLASS line that is raising the
exception, so this probably has nothing to do with your return value.

As someone else pointed out, posting a complete minimal solution that
reproduces the problem along with the exact output and the expected
output is usually helpful in tracking these things down (plus sometimes
when I put something like that together, I find the problem before I
even post!)

Paul




igor.sarzisartori@gmail.com

1/20/2006 8:10:00 PM

0

Asfand Yar Qazi wrote:
> SteveD wrote:
> > hey everyone,
> <snip>
>
> I suggest ignoring the SWIG suggestion - SWIG just overcomplicates
> things - one more layer where things can go wrong. With Ruby's easy
> to use interface, it is not needed.
>
Why swig is not ok? In my small experience of exporting a lot of C/C++
to ruby it was a simple task of editing a simple i_file with:

%module call_myclass
%{
#include "MyClassheader.h"
%}
%include "MyClassheader.h"

the wrapper created is a huge file, but I don't have any problems.