Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > mapping betwing a standard type and a user defined type with SWIG

Reply
Thread Tools

mapping betwing a standard type and a user defined type with SWIG

 
 
Lyes Amazouz
Guest
Posts: n/a
 
      08-20-2008
[Note: parts of this message were removed to make it a legal post.]

Hello!

I've wrote a C library where I redefined (with typedef) the "char" and I
gave it the name "my_char". Every things worked well when I gave constant
strings to my wrapped functions which takes a "my_char *" as argument (then
it is equivalent to give a char * argument). But when I have given a String
variable to one of my functions (I initialised a variable var = "" and gave
it to the finction), it returned me this:

Expected argument 3 of type my_char *, but got String "" (TypeError)

How can I do to tell SWIG that the "my_char" type is euivalent to the "char"
type? May I have a solution to make work my functions

thank you

--
===========
| Lyes Amazouz
| USTHB, Algiers
===========

 
Reply With Quote
 
 
 
 
Alex Fenton
Guest
Posts: n/a
 
      08-20-2008
Lyes Amazouz wrote:

> I've wrote a C library where I redefined (with typedef) the "char" and I
> gave it the name "my_char". Every things worked well when I gave constant
> strings to my wrapped functions which takes a "my_char *" as argument (then
> it is equivalent to give a char * argument). But when I have given a String
> variable to one of my functions (I initialised a variable var = "" and gave
> it to the finction), it returned me this:
>
> Expected argument 3 of type my_char *, but got String "" (TypeError)
>
> How can I do to tell SWIG that the "my_char" type is euivalent to the "char"
> type? May I have a solution to make work my functions


You are passing a C function that expects a 'my_char*' a Ruby string,
which in C has the type VALUE.

You need to apply a typemap that tells SWIG how to translate a ruby
object into my_char*. Something roughly like:

%typemap(in) my_char* "$1 = (my_char*)STR2CSTR($input);"

This is the most basic conversion; a good typemap would probably also
verify the ruby class of the passed-in argument, perhaps using SWIG's
%typemap(check)

Without wishing to be rude, this is fairly basic SWIG stuff. You might
want to have another look at the manual. It is dense but quite
comprehensive. In particular:

http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn37
http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn41

alex
 
Reply With Quote
 
 
 
 
Lyes Amazouz
Guest
Posts: n/a
 
      08-20-2008
[Note: parts of this message were removed to make it a legal post.]

On Wed, Aug 20, 2008 at 4:51 PM, Alex Fenton <(E-Mail Removed)>wrote:

> Lyes Amazouz wrote:
>
> I've wrote a C library where I redefined (with typedef) the "char" and I
>> gave it the name "my_char". Every things worked well when I gave constant
>> strings to my wrapped functions which takes a "my_char *" as argument
>> (then
>> it is equivalent to give a char * argument). But when I have given a
>> String
>> variable to one of my functions (I initialised a variable var = "" and
>> gave
>> it to the finction), it returned me this:
>>
>> Expected argument 3 of type my_char *, but got String "" (TypeError)
>>
>> How can I do to tell SWIG that the "my_char" type is euivalent to the
>> "char"
>> type? May I have a solution to make work my functions
>>

>
> You are passing a C function that expects a 'my_char*' a Ruby string, which
> in C has the type VALUE.
>
> You need to apply a typemap that tells SWIG how to translate a ruby object
> into my_char*. Something roughly like:
>
> %typemap(in) my_char* "$1 = (my_char*)STR2CSTR($input);"
>
> This is the most basic conversion; a good typemap would probably also
> verify the ruby class of the passed-in argument, perhaps using SWIG's
> %typemap(check)
>
> Without wishing to be rude, this is fairly basic SWIG stuff. You might want
> to have another look at the manual. It is dense but quite comprehensive. In
> particular:
>
> http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn37
> http://www.swig.org/Doc1.3/Ruby.html#Ruby_nn41
>
> alex
>
>

Hello

Thank you for your help, I will try this.




--
===========
| Lyes Amazouz
| USTHB, Algiers
===========

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mapping C long unsigned integer with swig for ruby Lyes Amazouz Ruby 2 11-14-2008 05:47 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
SWIG (Python) - "no constructor defined" for concrete class Stodge Python 1 04-21-2008 11:30 AM
SWIG: name 'new_doubleArray' is not defined alexandre_irrthum@yahoo.com Python 0 05-17-2006 01:10 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments