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

Discussion in 'Ruby' started by Lyes Amazouz, Aug 20, 2008.

  1. Lyes Amazouz

    Lyes Amazouz Guest

    [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
    ===========
     
    Lyes Amazouz, Aug 20, 2008
    #1
    1. Advertising

  2. Lyes Amazouz

    Alex Fenton Guest

    Re: mapping betwing a standard type and a user defined type withSWIG

    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
     
    Alex Fenton, Aug 20, 2008
    #2
    1. Advertising

  3. Lyes Amazouz

    Lyes Amazouz Guest

    Re: mapping betwing a standard type and a user defined type withSWIG

    [Note: parts of this message were removed to make it a legal post.]

    On Wed, Aug 20, 2008 at 4:51 PM, Alex Fenton <>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
    ===========
     
    Lyes Amazouz, Aug 20, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    291
  2. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  3. Alona
    Replies:
    1
    Views:
    449
    Joe Kesselman
    Apr 5, 2007
  4. Replies:
    1
    Views:
    506
    Sion Arrowsmith
    Jul 10, 2008
  5. Lyes Amazouz
    Replies:
    2
    Views:
    227
    Lyes Amazouz
    Nov 14, 2008
Loading...

Share This Page