//SWIG <-> Ruby - call by refrence for value types

View: New views
3 Messages — Rating Filter:   Alert me  

//SWIG <-> Ruby - call by refrence for value types

by Sören Freudiger-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi @all
I'm pretty new to SWIG and Ruby and have a question.

--------------------
C++:
class A {
public:
 foo(int& var){ var+=10;}
};
--------------------

--------------------
Ruby:
var = 10
a = Example::A.new()
a.foo(var)
--------------------

results in: error --> Expected argument 1 of type int &, but got Fixnum 10
(TypeError)

How can I fix that problem. Our C++ code I have to write a ruby interface
for uses a huge number of methods like that...

Best regards,
SirAnn


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: //SWIG <-> Ruby - call by refrence for value types

by James17 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Sören Freudiger-2 wrote:
--------------------
C++:
class A {
public:
 foo(int& var){ var+=10;}
};
--------------------
Ruby:
var = 10
a = Example::A.new()
a.foo(var)
--------------------

results in: error --> Expected argument 1 of type int &, but got Fixnum 10
(TypeError)
You are trying to pass an integer value when the C++ function is asking for an integer reference.  What to do about it probably depends upon what the integer is used for in the function... but it will come down to defining a typemap to convert a Ruby value properly.

Re: //SWIG <-> Ruby - call by refrence for value types

by Sören Freudiger-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Okay, I got it. Somehow.

My example.i (just for a global foo-function):
----------------------------
/* File : example.i */
%module example
%{
#include "example.h"
%}
%include "typemaps.i"
void foo(int& INOUT);
----------------------------

example.h
----------------------------
void foo(int& val) { val +=10; }
----------------------------

run.rb
----------------------------
require 'example'
var = 7
var = Example::foo(var)
p var
----------------------------

Works fine. Is there a way to map ALL "int&" to "int& INOUT" automatically?

Something like " %apply int& {int& INOUT)}"???
That would simplifying the rest. Only if no INOUT is wished the interface has to be described in ".i"

Best,
SirAnn



-----Ursprüngliche Nachricht-----
Von: James17 [mailto:james.d.masters@...]
Gesendet: Montag, 9. November 2009 16:27
An: swig-user@...
Betreff: Re: [Swig-user] //SWIG <-> Ruby - call by refrence for value types




Sören Freudiger-2 wrote:

>
> --------------------
> C++:
> class A {
> public:
>  foo(int& var){ var+=10;}
> };
> --------------------
> Ruby:
> var = 10
> a = Example::A.new()
> a.foo(var)
> --------------------
>
> results in: error --> Expected argument 1 of type int &, but got Fixnum 10
> (TypeError)
>

You are trying to pass an integer value when the C++ function is asking for
an integer reference.  What to do about it probably depends upon what the
integer is used for in the function... but it will come down to defining a
typemap to convert a Ruby value properly.
--
View this message in context: http://old.nabble.com/--SWIG-%3C-%3E-Ruby---call-by-refrence-for-value-types-tp26244076p26268232.html
Sent from the swig-user mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user