Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [ANN] Typelib: a C/C++ type/value manipulation library, and dynamicfunction calls

Thread Tools

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

Sylvain Joyeux
Posts: n/a
Typelib 1.0rc1 is out !
Author: Sylvain Joyeux <(E-Mail Removed)>
Project page:
C++ library:
Ruby bindings:

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 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 ="", registry)

# This searches the test_function in 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 =
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 = :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 !

Reply With Quote
Marcin Raczkowski
Posts: n/a

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

I heope it's as good as it looks

Reply With Quote

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
Are system calls sometimes costlier than library calls? Richard Tobin C Programming 24 11-11-2007 08:52 AM
Voipdiscount Outgoing Calls with Sipgate Incoming Calls on a Linksys PAP2 Giganews UK VOIP 27 10-09-2006 11:28 PM
ods calls business object then method calls ta with output params andy6 ASP .Net 2 06-09-2006 01:54 AM
MoVoIP - FREE MOBILE Inetrnet Phone Calls - FREE Internet Phone Calls ubifone VOIP 0 07-29-2005 04:31 PM
Sequence of constructor calls and destructor calls. Honne Gowda A C++ 2 10-31-2003 09:31 AM