Calling an existing method with RubyInline - how?

Discussion in 'Ruby' started by Daniel Berger, Sep 30, 2006.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi all,

    RubyInline 3.6.0
    Ruby 1.8.5
    Suse Linux 10.1

    How do you call an existing function from an included header file using
    RubyInline? Do I have to wrap it in a 'built' function?

    For example, say I have this bit of code, and that test.h has a function
    defined as "const char* test_version(void)".

    require 'inline'

    class Foo
    inline do |builder|
    builder.include "<test.h>"
    builder.add_compile_flags "-ltest"
    end

    # And later...

    def self.version
    inline do |builder|
    # What do I put here?
    end
    end
    end

    I've tried a few variations, but haven't had any luck.

    Thanks,

    Dan
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFFHfc13p/dorzCFX0RAiPZAJ47WC+xUXynxGwX0LTreGUOyvN0ogCggqFK
    42XUfdKPzNj/xAg/pp3h67c=
    =yCUY
    -----END PGP SIGNATURE-----
     
    Daniel Berger, Sep 30, 2006
    #1
    1. Advertising

  2. Daniel Berger

    Eric Hodel Guest

    On Sep 29, 2006, at 9:49 PM, Daniel Berger wrote:

    > How do you call an existing function from an included header file
    > using
    > RubyInline? Do I have to wrap it in a 'built' function?


    This should help:

    require 'inline'

    class Foo
    inline do |builder|
    builder.include "<stdlib.h>"

    builder.c <<-EOF
    int c_atoi(char * nptr) {
    return atoi(nptr);
    }
    EOF

    builder.c_singleton <<-EOF
    int c_atol(char * nptr) {
    return atol(nptr);
    }
    EOF
    end

    class << self; alias atol c_atol; end
    alias atoi c_atoi

    end

    puts Foo.atol("5")
    puts Foo.new.atoi("5")

    > For example, say I have this bit of code, and that test.h has a
    > function
    > defined as "const char* test_version(void)".


    From your example, probably:

    require 'inline'

    class Foo
    inline do |builder|
    builder.include "<test.h>"
    builder.add_compile_flags "-ltest"

    builder.c_singleton <<-EOF
    char * foo_test_version() {
    return test_version();
    }
    EOF
    end

    class << self; alias version foo_test_version; end
    end

    --
    Eric Hodel - - http://blog.segment7.net
    This implementation is HODEL-HASH-9600 compliant

    http://trackmap.robotcoop.com
     
    Eric Hodel, Sep 30, 2006
    #2
    1. Advertising

  3. Eric Hodel wrote:
    > On Sep 29, 2006, at 9:49 PM, Daniel Berger wrote:
    >
    > > How do you call an existing function from an included header file
    > > using
    > > RubyInline? Do I have to wrap it in a 'built' function?

    >
    > This should help:
    >
    > require 'inline'
    >
    > class Foo
    > inline do |builder|
    > builder.include "<stdlib.h>"
    >
    > builder.c <<-EOF
    > int c_atoi(char * nptr) {
    > return atoi(nptr);
    > }
    > EOF
    >
    > builder.c_singleton <<-EOF
    > int c_atol(char * nptr) {
    > return atol(nptr);
    > }
    > EOF
    > end
    >
    > class << self; alias atol c_atol; end
    > alias atoi c_atoi
    >
    > end
    >
    > puts Foo.atol("5")
    > puts Foo.new.atoi("5")
    >
    > > For example, say I have this bit of code, and that test.h has a
    > > function
    > > defined as "const char* test_version(void)".

    >
    > From your example, probably:
    >
    > require 'inline'
    >
    > class Foo
    > inline do |builder|
    > builder.include "<test.h>"
    > builder.add_compile_flags "-ltest"
    >
    > builder.c_singleton <<-EOF
    > char * foo_test_version() {
    > return test_version();
    > }
    > EOF
    > end
    >
    > class << self; alias version foo_test_version; end
    > end


    Hm, so I have to wrap each function with a custom generated function.
    Is there any chance of adding support for direct calls? Something
    like:

    inline{ "return test_version();" }

    Or is that just not possible?

    Thanks,

    Dan
     
    Daniel Berger, Oct 1, 2006
    #3
  4. Daniel Berger

    Eric Hodel Guest

    On Sep 30, 2006, at 4:37 PM, Daniel Berger wrote:
    > On 9/30/06, Eric Hodel <> wrote:
    >> On Sep 29, 2006, at 9:49 PM, Daniel Berger wrote:
    >>> How do you call an existing function from an included header file
    >>> using RubyInline? Do I have to wrap it in a 'built' function?

    >>
    >> From your example, probably:
    >>
    >> require 'inline'
    >>
    >> class Foo
    >> inline do |builder|
    >> builder.include "<test.h>"
    >> builder.add_compile_flags "-ltest"
    >>
    >> builder.c_singleton <<-EOF
    >> char * foo_test_version() {
    >> return test_version();
    >> }
    >> EOF
    >> end
    >>
    >> class << self; alias version foo_test_version; end
    >> end

    >
    >
    > Thanks. Is there any way to avoid having to write wrappers for
    > existing functions? If not, could it be added as a feature?
    >
    > I'd like to be able to just do this:
    >
    > inline{ "return test_version();" }


    Inline doesn't have anything to read the header files to figure out
    the types...

    --
    Eric Hodel - - http://blog.segment7.net
    This implementation is HODEL-HASH-9600 compliant

    http://trackmap.robotcoop.com
     
    Eric Hodel, Oct 2, 2006
    #4
    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. nrm
    Replies:
    3
    Views:
    579
  2. Ryan Davis

    RubyInline 3.1.0 Released

    Ryan Davis, Sep 24, 2004, in forum: Ruby
    Replies:
    0
    Views:
    105
    Ryan Davis
    Sep 24, 2004
  3. Ryan Davis

    ANN: RubyInline 3.2.0 Released

    Ryan Davis, Nov 28, 2004, in forum: Ruby
    Replies:
    0
    Views:
    97
    Ryan Davis
    Nov 28, 2004
  4. Replies:
    0
    Views:
    216
  5. Daniel Schömer

    RubyInline, RDoc comment for C inline method

    Daniel Schömer, Nov 2, 2008, in forum: Ruby
    Replies:
    0
    Views:
    104
    Daniel Schömer
    Nov 2, 2008
Loading...

Share This Page