[ANN] Typelib: a C/C++ type/value manipulation library, and dynamicfunction calls

Discussion in 'Ruby' started by Sylvain Joyeux, Feb 20, 2008.

  1. Typelib 1.0rc1 is out !
    Author: Sylvain Joyeux <>
    Project page: http://sourceforge.net/projects/typelib
    C++ library: http://typelib.sourceforge.net/html/
    Ruby bindings: http://typelib.sourceforge.net/html/ruby

    Typelib is a C++ library which allows introspection on data types
    and data values. Its value model is based on the C type model. The
    library allows to load definition from various type description files
    (including plain C), build types programmatically, create and manipulate
    values defined using these types.

    The Ruby bindings to the C++ Typelib library allow to represent and
    easily manipulate C types and in-memory values from within Ruby. Based
    on that capability, the bindings offer a nice way to dynamically
    interface with C shared libraries. The dynamic function calls are using
    the dyncall library, to get rid of the many Ruby/DL bugs and
    limitations, and most importantly its lack of maintainership. The
    dyncall source code is provided in the Typelib source packages.

    == Example

    Let‘s assume that a C dynamic library called libtest.so contains a
    function with the following prototype: int test_function(SimpleStruct*).
    The SimpleStruct type is defined in a test_header.h file which looks
    like this:

    typedef struct SimpleStruct {
    int a;
    double d;
    } SimpleStruct;

    Using Typelib, a Ruby script can interface with this library this way:

    require 'typelib'
    include Typelib

    # Load all types defined in the C file my_header.h
    registry = Registry.import('test_header.h')

    # Get a Library object for the target shared library, and get
    # a function handle in this library. +registry+ is used as the
    # type registry to access functions in this library
    lib = Library.open("libtest.so", registry)

    # This searches the test_function in libtest.so. Note that MyStruct
    # must be
    # already defined in +registry+ (this is the case here). See the
    # documentation of Typelib::Function for the various ways to define
    # ruby/C
    # function bindings.
    test_function = lib.find('test_function').

    # Get the Ruby description of the MyStruct type
    my_struct = registry.get('MyStruct')

    # Create an uninitialized MyStruct parameter
    arg = my_struct.new
    arg.a = 10
    arg.b = 20.35498

    # and call the function, getting the integer return value
    result = test_function[struct]

    # It is also possible to use named parameters
    struct = my_struct.new :a => 10, :b => 20
    # .. or to even create the structure argument on the fly
    result = test_function[ :a => 10, :b => 20 ]

    # In fact, the value of this MyStruct argument is not used by
    # test_function:
    # This argument is used as a buffer for output values. This can be
    # set up
    # in Typelib with:
    test_function = registry.find('test_function').
    result, struct = test_function[]

    # If the value was both used by +test_function+ and modified by it,
    # we
    # would have used the following:
    test_function = registry.find('test_function').
    result, arg = test_function[arg]

    # ... which can be useful if you want Typelib to build the object
    # on-the-fly
    result, struct = test_function[:a => 10, :b => 20]

    # Then, getting the returned values is as simple as
    puts struct.a
    puts struct.b

    Enjoy !
    Sylvain Joyeux, Feb 20, 2008
    1. Advertisements

  2. Re: [ANN] Typelib: a C/C++ type/value manipulation library, anddynamic function calls


    I have to inspect it, but i was going to do exactly same thing ! :)

    I heope it's as good as it looks
    Marcin Raczkowski, Feb 20, 2008
    1. Advertisements

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:
  2. Paul McGuire

    win32com typelib difficulty

    Paul McGuire, May 24, 2004, in forum: Python
    Paul McGuire
    May 24, 2004
  3. Alexander Eisenhuth

    Howto access a enumeration in a COM TypeLib

    Alexander Eisenhuth, Jun 21, 2005, in forum: Python
    Alexander Eisenhuth
    Jun 21, 2005
  4. Vince C.
    Vince C.
    Jan 15, 2004
  5. Thomas Born
    Thomas Born
    Feb 9, 2004

Share This Page