# 20 SWIG and Ruby

- Preliminaries
  - Running SWIG
  - Getting the right header files
  - Compiling a dynamic module
  - Using your module
  - Static linking
  - Compilation of C++ extensions
- Building Ruby Extensions under Windows 95/NT
  - Running SWIG from Developer Studio
- The Ruby-to-C/C++ Mapping
  - Modules
  - Functions
  - Variable Linking
  - Constants
  - Pointers
  - Structures
  - C++ classes
  - C++ inheritance
  - C++ overloaded functions
  - C++ Operators
  - C++ namespaces
  - C++ templates
  - C++ Smart Pointers
- Input and output parameters
- Simple exception handling
- Typemaps
  - What is a typemap?
  - Ruby typemaps
  - Typemap variables
  - Useful Functions
    - C Datatypes to Ruby Objects
    - Ruby Objects to C Datatypes
    - Macros for VALUE
    - Exceptions
    - Iterators
  - Typemap Examples
  - Converting a Ruby array to a char **
  - Collecting arguments in a hash
  - Pointer handling
    - Ruby Datatype Wrapping
- Operator overloading
  - An example (putting everything together)
  - Expanding the example
  - STL Vector to Ruby Array
- Advanced Topics
  - Creating Multi-Module Packages
  - Defining Aliases
  - Predicate Methods
  - Specifying Mixin Modules
  - Interacting with Ruby's Garbage Collector

This chapter describes SWIG's support of Ruby.

## 20.1 Preliminaries

SWIG 1.3 is known to work with Ruby 1.6 and 1.7. Given the choice, you should use the latest stable version (1.6) of Ruby. You should also determine if your system supports shared libraries and dynamic loading. SWIG will work with or without dynamic loading, but the compilation process will vary.

This chapter covers most SWIG features, but in less depth than is found in earlier chapters. At the very least, make sure you also read the "SWIG Basics" chapter. It is also assumed that the reader has a basic understanding of Ruby.

### 20.1.1 Running SWIG

To build a Ruby module, run SWIG using the `-ruby` option:

>     $ swig -ruby example.i

If building a C++ extension, add the `-c++` option:

>     $ swig -c++ -ruby example.i

This creates a file `example_wrap.c` (`example_wrap.cxx` if compiling a C++ extension) that contains all of the code needed to build a Ruby extension module. To finish building the module, you need to compile this file and link it with the rest of your program.

### 20.1.2 Getting the right header files

In order to compile the wrapper code, the compiler needs the `ruby.h` header file. This file is usually contained in a directory such as

>     /usr/local/lib/ruby/1.6/i686-linux/ruby.h

The exact location may vary on your machine, but the above location is typical. If you are not entirely sure where Ruby is installed, you can run Ruby to find out. For example:

>     $ ruby -e 'puts $:.join("\n")'
>     /usr/local//lib/ruby/site_ruby/1.6
>     /usr/local//lib/ruby/site_ruby/1.6/i686-linux
>     /usr/local//lib/ruby/site_ruby
>     /usr/local//lib/ruby/1.6
>     /usr/local//lib/ruby/1.6/i686-linux
>     .

### 20.1.3 Compiling a dynamic module

Ruby extension modules are typically compiled into shared libraries that the interpreter loads dynamically at runtime. Since the exact commands for doing this vary from platform to platform, your best bet is to follow the steps described in the `README.EXT` file from the Ruby distribution:

1. Create a file called `extconf.rb` that looks like the following:

   >     require 'mkmf'
   >     create_makefile('example')

2. Type the following to build the extension:

   >     $ ruby extconf.rb
   >     $ make
   >     $ make install

Of course, there is the problem that mkmf does not work correctly on all platforms, e.g, HPUX. If you need to add your own make rules to the file that `extconf.rb` produces, you can add this:

>     open("Makefile", "a") { |mf|
>        puts <<EOM
>        # Your make rules go here
>        EOM
>     }

to the end of the `extconf.rb` file. If for some reason you don't want to use the standard approach, you'll need to determine the correct compiler and linker flags for your build platform. For example, a typical sequence of commands for the Linux operating system would look something like this:

>     $ swig -ruby example.i
>     $ gcc -c example.c
>     $ gcc -c example_wrap.c -I/usr/local/lib/ruby/1.6/i686-linux
>     $ gcc -shared example.o example_wrap.o -o example.so

For other platforms it may be necessary to compile with the `-fPIC` option to generate position-independent code. If in doubt, consult the manual pages for your compiler and linker to determine the correct set of options. You might also check the SWIG Wiki for additional information.

### 20.1.4 Using your module

Ruby module names must be capitalized, but the convention for Ruby feature names is to use lowercase names. So, for example, the Etc extension module is imported by requiring the etc feature:

    # The feature name begins with a lowercase letter...
    require 'etc'

    # ... but the module name begins with an uppercase letter
    puts "Your login name: #{Etc.getlogin}"

To stay consistent with this practice, you should always specify a lowercase module name with SWIG's `%module` directive. SWIG will automatically correct the resulting Ruby module name for your extension. So for example, a SWIG interface file that begins with:

    %module example

will result in an extension module using the feature name "example" and Ruby module name "Example".

### 20.1.5 Static linking

An alternative approach to dynamic linking is to rebuild the Ruby interpreter with your extension module added to it. In the past, this approach was sometimes necessary due to limitations in dynamic loading support on certain machines. However, the situation has improved greatly over the last few years and you should not consider this approach unless there is really no other option.

The usual procedure for adding a new module to Ruby involves finding the Ruby source, adding an entry to the `ext/Setup` file, adding your directory to the list of extensions in the file, and finally rebuilding Ruby.

### 20.1.6 Compilation of C++ extensions

On most machines, C++ extension modules should be linked using the C++ compiler. For example:

>     $ swig -c++ -ruby example.i
>     $ g++ -c example.cxx
>     $ g++ -c example_wrap.cxx -I/usr/local/lib/ruby/1.6/i686-linux
>     $ g++ -shared example.o example_wrap.o -o example.so

## 20.2 Building Ruby Extensions under Windows 95/NT

Building a SWIG extension to Ruby under Windows 95/NT is roughly similar to the process used with Unix. Normally, you will want to produce a DLL that can be loaded into the Ruby interpreter. For all recent versions of Ruby, the procedure described above (i.e. using an `extconf.rb` script) will work with Windows as well; you should be able to build your code into a DLL by typing:

>     C:\swigtest> ruby extconf.rb
>     C:\swigtest> nmake
>     C:\swigtest> nmake install

The remainder of this section covers the process of compiling SWIG-generated Ruby extensions with Microsoft Visual C++ 6 (i.e. within the Developer Studio IDE, instead of using the command line tools). In order to build extensions, you may need to download the source distribution to the Ruby package, as you will need the Ruby header files.

### 20.2.1 Running SWIG from Developer Studio

If you are developing your application within Microsoft developer studio, SWIG can be invoked as a custom build option. The process roughly follows these steps :

- Open up a new workspace and use the AppWizard to select a DLL project.
- Add both the SWIG interface file (the .i file), any supporting C files, and the name of the wrapper file that will be created by SWIG (i.e.. `example_wrap.c`). Note : If using C++, choose a different suffix for the wrapper file such as `example_wrap.cxx`. Don't worry if the wrapper file doesn't exist yet--Developer Studio will keep a reference to it around.
- Select the SWIG interface file and go to the settings menu. Under settings, select the "Custom Build" option.
- Enter "SWIG" in the description field.
- Enter "`swig -ruby -o $(ProjDir)\$(InputName)_wrap.c $(InputPath)`" in the "Build command(s) field". You may have to include the path to swig.exe.
- Enter "`$(ProjDir)\$(InputName)_wrap.c`" in the "Output files(s) field".
- Next, select the settings for the entire project and go to the C/C++ tab and select the Preprocessor category. Add NT=1 to the Preprocessor definitions. This must be set else you will get compilation errors. Also add IMPORT to the preprocessor definitions, else you may get runtime errors. Also add the include directories for your Ruby installation under "Additional include directories".
- Next, select the settings for the entire project and go to the Link tab and select the General category. Set the name of the output file to match the name of your Ruby module (i.e.. example.dll). Next add the Ruby library file to your link libraries under Object/Library modules. For example "mswin32-ruby16.lib. You also need to add the path to the library under the Input tab - Additional library path.
- Build your project.

Now, assuming all went well, SWIG will be automatically invoked when you build your project. Any changes made to the interface file will result in SWIG being automatically invoked to produce a new version of the wrapper file. To run your new Ruby extension, simply run Ruby and use the `require` command as normal. For example if you have this ruby file run.rb:

>     # file: run.rb
>     require 'Example'

>     # Call a c function
>     print "Foo = ", Example.Foo, "\n"

Ensure the dll just built is in your path or current directory, then run the Ruby script from the DOS/Command prompt:

>     C:\swigtest> ruby run.rb
>     Foo = 3.0

## 20.3 The Ruby-to-C/C++ Mapping

This section describes the basics of how SWIG maps C or C++ declarations in your SWIG interface files to Ruby constructs.

### 20.3.1 Modules

The SWIG `%module` directive specifies the name of the Ruby module. If you specify:

>     %module example

then everything is wrapped into a Ruby module named `Example` that is nested directly under the global module. You can specify a more deeply nested module by specifying the fully-qualified module name in quotes, e.g.

>     %module "Foo::Bar::Spam"

When choosing a module name, do not use the same name as a built-in Ruby command or standard module name, the results may be unpredictable.

### 20.3.2 Functions

Global functions are wrapped as Ruby module methods. For example, given the SWIG interface file `example.i`:

>     %module example

>     int fact(int n);

and C source file `example.c`:

>     int fact(int n) {
>       if (n == 0)
>         return 1;
>       return (n * fact(n-1));
>     }

SWIG will generate a method fact in the Example module that can be used like so:

>     $ irb
>     irb(main):001:0> require 'example'
>     true
>     irb(main):002:0> Example.fact(4)
>     24

### 20.3.3 Variable Linking

C/C++ global variables are wrapped as a pair of singleton methods for the module: one to get the value of the global variable and one to set it. For example, the following SWIG interface file declares two global variables:

>     // SWIG interface file with global variables
>     %module example
>     ...
>     extern int    variable1;
>     extern double Variable2;
>     ...

Now look at the Ruby interface:

>     $ irb
>     irb(main):001:0> require 'Example'
>     true
>     irb(main):002:0> Example.variable1 = 2
>     2
>     irb(main):003:0> Example.Variable2 = 4 * 10.3
>     41.2
>     irb(main):004:0> Example.Variable2
>     41.2

If you make an error in variable assignment, you will receive an error message. For example:

>     irb(main):005:0> Example.Variable2 = "hello"
>     TypeError: no implicit conversion to float from string
>       from (irb):5:in `Variable2='
>       from (irb):5

If a variable is declared as `const`, it is wrapped as a read-only variable. Attempts to modify its value will result in an error.

To make ordinary variables read-only, you can also use the `%immutable` directive. For example:

>     %immutable;
>     extern char *path;
>     %mutable;

The `%immutable` directive stays in effect until it is explicitly disabled using `%mutable`.

### 20.3.4 Constants

C/C++ constants are wrapped as module constants initialized to the appropriate value. To create a constant, use `#define` or the `%constant` directive. For example:

>     #define PI 3.14159
>     #define VERSION "1.0"

>     %constant int FOO = 42;
>     %constant const char *path = "/usr/local";

>     const int BAR = 32;

Remember to use the :: operator in Ruby to get at these constant values, e.g.

>     $ irb
>     irb(main):001:0> require 'Example'
>     true
>     irb(main):002:0> Example::PI
>     3.14159

### 20.3.5 Pointers

"Opaque" pointers to arbitrary C/C++ types (i.e. types that aren't explicitly declared in your SWIG interface file) are wrapped as data objects. So, for example, consider a SWIG interface file containing only the declarations:

>     Foo *get_foo();
>     void set_foo(Foo *foo);

For this case, the get_foo() method returns an instance of an internally generated Ruby class:

>     irb(main):001:0> foo = Example::get_foo()
>     #<SWIG::TYPE_p_Foo:0x402b1654>

A `NULL` pointer is always represented by the Ruby `nil` object.

### 20.3.6 Structures

C/C++ structs are wrapped as Ruby classes, with accessor methods (i.e. "getters" and "setters") for all of the struct members. For example, this struct declaration:

>     struct Vector {
>         double x, y;
>     };

gets wrapped as a `Vector` class, with Ruby instance methods `x`, `x=`, `y` and `y=`. These methods can be used to access structure data from Ruby as follows:

>     $ irb
>     irb(main):001:0> require 'Example'
>     true
>     irb(main):002:0> f = Example::Vector.new
>     #<Example::Vector:0x4020b268>
>     irb(main):003:0> f.x = 10
>     nil
>     irb(main):004:0> f.x
>     10.0

Similar access is provided for unions and the public data members of C++ classes.

`const` members of a structure are read-only. Data members can also be forced to be read-only using the `%immutable` directive (in C++, `private` may also be used). For example:

>     struct Foo {
>        ...
>        %immutable;
>        int x;        /* Read-only members */
>        char *name;
>        %mutable;
>        ...
>     };

When `char *` members of a structure are wrapped, the contents are assumed to be dynamically allocated using `malloc` or `new` (depending on whether or not SWIG is run with the `-c++` option). When the structure member is set, the old contents will be released and a new value created. If this is not the behavior you want, you will have to use a typemap (described shortly).

Array members are normally wrapped as read-only. For example, this code:

>     struct Foo {
>        int x[50];
>     };

produces a single accessor function like this:

>     int *Foo_x_get(Foo *self) {
>         return self->x;
>     };

If you want to set an array member, you will need to supply a "memberin" typemap described in the section on typemaps. As a special case, SWIG does generate code to set array members of type `char` (allowing you to store a Ruby string in the structure).

When structure members are wrapped, they are handled as pointers. For example,

>     struct Foo {
>        ...
>     };

>     struct Bar {
>        Foo f;
>     };

generates accessor functions such as this:

>     Foo *Bar_f_get(Bar *b) {
>         return &b->f;
>     }

>     void Bar_f_set(Bar *b, Foo *val) {
>         b->f = *val;
>     }

### 20.3.7 C++ classes

Like structs, C++ classes are wrapped by creating a new Ruby class of the same name with accessor methods for the public class member data. Additionally, public member functions for the class are wrapped as Ruby instance methods, and public static member functions are wrapped as Ruby singleton methods. So, given the C++ class declaration:

>     class List {
>     public:
>       List();
>       ~List();
>       int  search(char *item);
>       void insert(char *item);
>       void remove(char *item);
>       char *get(int n);
>       int  length;
>       static void print(List *l);
>     };

SWIG would create a `List` class with:

- instance methods search, insert, remove, and get;
- instance methods length and length= (to get and set the value of the length data member); and,
- a print singleton method for the class.

In Ruby, these functions are used as follows:

>     require 'Example'

>     l = Example::List.new

>     l.insert("Ale")
>     l.insert("Stout")
>     l.insert("Lager")
>     Example.print(l)
>     l.length()
>     ----- produces the following output
>     Lager
>     Stout
>     Ale
>     3

### 20.3.8 C++ inheritance

The SWIG type-checker is fully aware of C++ inheritance. Therefore, if you have classes like this:

>     class Parent {
>         ...
>     };

>     class Child : public Parent {
>         ...
>     };

those classes are wrapped into a hierarchy of Ruby classes that reflect the same inheritance structure. All of the usual Ruby utility methods work normally:

>     irb(main):001:0> b = Bar.new
>     #<Bar:0x4016efd4>
>     irb(main):002:0> b.instance_of? Bar
>     true
>     irb(main):003:0> b.instance_of? Foo
>     false
>     irb(main):004:0> b.is_a? Bar
>     true
>     irb(main):005:0> b.is_a? Foo
>     true
>     irb(main):006:0> Bar < Foo
>     true
>     irb(main):007:0> Bar > Foo
>     false

Furthermore, if you have a function like this:

>     void spam(Parent *f);

then the function `spam()` accepts `Parent`* or a pointer to any class derived from `Parent`.

The Ruby module for SWIG currently doesn't support multiple inheritance (although this may change before the final SWIG 2.0 release). This doesn't mean that you can't wrap C++ classes which are derived from multiple bas classes, it simply means that only the first base class listed is considered. So if your SWIG interface file contains a declaration like this:

>     class Derived : public Base1, public Base2
>     {
>         ...
>     };

then the resulting Ruby class (`Derived`) will only consider `Base1` as its superclass. It won't inherit any of `Base2`'s member functions or data and it won't recognize `Base2` as an "ancestor" of `Derived` (i.e. the is_a? relationship would fail). For any additional base classes, you'll see a warning message like:

>     example.i:5: Warning(802): Warning for Derived: Base Base2 ignored. Multiple inheritance is not supported in Ruby.

### 20.3.9 C++ overloaded functions

C++ overloaded functions, methods, and constructors are mostly supported by SWIG. For example, if you have two functions like this:

>     void foo(int);
>     void foo(char *c);

You can use them in Ruby in a straightforward manner:

>     irb(main):001:0> foo(3)           # foo(int)
>     irb(main):002:0> foo("Hello")     # foo(char *c)

Similarly, if you have a class like this,

>     class Foo {
>     public:
>         Foo();
>         Foo(const Foo &);
>         ...
>     };

you can write Ruby code like this:

>     irb(main):001:0> f = Foo.new          # Create a Foo
>     irb(main):002:0> g = Foo.new(f)       # Copy f

Overloading support is not quite as flexible as in C++. Sometimes there are methods that SWIG can't disambiguate. For example:

>     void spam(int);
>     void spam(short);

or

>     void foo(Bar *b);
>     void foo(Bar &b);

If declarations such as these appear, you will get a warning message like this:

>     example.i:12: Warning(509): Overloaded spam(short) is shadowed by spam(int) at example.i:11.

To fix this, you either need to ignore or rename one of the methods. For example:

>     %rename(spam_short) spam(short);
>     ...
>     void spam(int);
>     void spam(short);   // Accessed as spam_short

or

>     %ignore spam(short);
>     ...
>     void spam(int);
>     void spam(short);   // Ignored

SWIG resolves overloaded functions and methods using a disambiguation scheme that ranks and sorts declarations according to a set of type-precedence rules. The order in which declarations appear in the input does not matter except in situations where ambiguity arises--in this case, the first declaration takes precedence.

Please refer to the "SWIG and C++" chapter for more information about overloading.

### 20.3.10 C++ Operators

For the most part, overloaded operators are handled automatically by SWIG and do not require any special treatment on your part. So if your class declares an overloaded addition operator, e.g.

>     class Complex {
>         ...
>         Complex operator+(Complex &);
>         ...
>     };

the resulting Ruby class will also support the addition (+) method correctly.

For cases where SWIG's built-in support is not sufficient, C++ operators can be wrapped using the `%rename` directive (available on SWIG 1.3.10 and later releases). All you need to do is give the operator the name of a valid Ruby identifier. For example:

>     %rename(add_complex) operator+(Complex &, Complex &);
>     ...
>     Complex operator+(Complex &, Complex &);

Now, in Ruby, you can do this:

>     a = Example::Complex.new(2, 3)
>     b = Example::Complex.new(4, -1)
>     c = Example.add_complex(a, b)

More details about wrapping C++ operators into Ruby operators is discussed in the section on operator overloading.

### 20.3.11 C++ namespaces

SWIG is aware of C++ namespaces, but namespace names do not appear in the module nor do namespaces result in a module that is broken up into submodules or packages. For example, if you have a file like this,

>     %module example

>     namespace foo {
>        int fact(int n);
>        struct Vector {
>            double x,y,z;
>        };
>     };

it works in Ruby as follows:

>     irb(main):001:0> require 'example'
>     true
>     irb(main):002:0> Example.fact(3)
>     6
>     irb(main):003:0> v = Example::Vector.new
>     #<Example::Vector:0x4016f4d4>
>     irb(main):004:0> v.x = 3.4
>     3.4
>     irb(main):004:0> v.y
>     0.0

If your program has more than one namespace, name conflicts (if any) can be resolved using `%rename` For example:

>     %rename(Bar_spam) Bar::spam;

>     namespace Foo {
>         int spam();
>     }

>     namespace Bar {
>         int spam();
>     }

If you have more than one namespace and your want to keep their symbols separate, consider wrapping them as separate SWIG modules. For example, make the module name the same as the namespace and create extension modules for each namespace separately. If your program utilizes thousands of small deeply nested namespaces each with identical symbol names, well, then you get what you deserve.

### 20.3.12 C++ templates

C++ templates don't present a huge problem for SWIG. However, in order to create wrappers, you have to tell SWIG to create wrappers for a particular template instantiation. To do this, you use the `%template` directive. For example:

>     %module example
>     %{
>     #include "pair.h"
>     %}

>     template<class T1, class T2>
>     struct pair {
>        typedef T1 first_type;
>        typedef T2 second_type;
>        T1 first;
>        T2 second;
>        pair();
>        pair(const T1&, const T2&);
>       ~pair();
>     };

>     %template(Pairii) pair<int,int>;

In Ruby:

>     irb(main):001:0> require 'example'
>     true
>     irb(main):002:0> p = Example::Pairii.new(3, 4)
>     #<Example:Pairii:0x4016f4df>
>     irb(main):003:0> p.first
>     3
>     irb(main):004:0> p.second
>     4

Obviously, there is more to template wrapping than shown in this example. More details can be found in the SWIG and C++ chapter. Some more complicated examples will appear later.

### 20.3.13 C++ Smart Pointers

In certain C++ programs, it is common to use classes that have been wrapped by so-called "smart pointers." Generally, this involves the use of a template class that implements `operator->()` like this:

>     template<class T> class SmartPtr {
>        ...
>        T *operator->();
>        ...
>     }

Then, if you have a class like this,

>     class Foo {
>     public:
>          int x;
>          int bar();
>     };

A smart pointer would be used in C++ as follows:

>     SmartPtr<Foo> p = CreateFoo();   // Created somehow (not shown)
>     ...
>     p->x = 3;                        // Foo::x
>     int y = p->bar();                // Foo::bar

To wrap this in Ruby, simply tell SWIG about the `SmartPtr` class and the low-level `Foo` object. Make sure you instantiate `SmartPtr` using `%template` if necessary. For example:

>     %module example
>     ...
>     %template(SmartPtrFoo) SmartPtr<Foo>;
>     ...

Now, in Ruby, everything should just "work":

>     irb(main):001:0> p = Example::CreateFoo()          # Create a smart-pointer somehow
>     #<Example::SmartPtrFoo:0x4016f4df>
>     irb(main):002:0> p.x = 3                          # Foo::x
>     3
>     irb(main):003:0> p.bar()                          # Foo::bar

If you ever need to access the underlying pointer returned by `operator->()` itself, simply use the `__deref__()` method. For example:

>     irb(main):004:0> f = p.__deref__()     # Returns underlying Foo *

## 20.4 Input and output parameters

A common problem in some C programs is handling parameters passed as simple pointers. For example:

>     void add(int x, int y, int *result) {
>        *result = x + y;
>     }
>     or
>     int sub(int *x, int *y) {
>        return *x-*y;
>     }

The easiest way to handle these situations is to use the `typemaps.i` file. For example:

>     %module Example
>     %include "typemaps.i"

>     void add(int, int, int *OUTPUT);
>     int  sub(int *INPUT, int *INPUT);

In Ruby, this allows you to pass simple values. For example:

>     a = Example.add(3,4)
>     puts a
>     7
>     b = Example.sub(7,4)
>     puts b
>     3

Notice how the `INPUT` parameters allow integer values to be passed instead of pointers and how the `OUTPUT` parameter creates a return result.

If you don't want to use the names `INPUT` or `OUTPUT`, use the `%apply` directive. For example:

>     %module Example
>     %include "typemaps.i"

>     %apply int *OUTPUT { int *result };
>     %apply int *INPUT  { int *x, int *y};

>     void add(int x, int y, int *result);
>     int  sub(int *x, int *y);

If a function mutates one of its parameters like this,

>     void negate(int *x) {
>        *x = -(*x);
>     }

you can use `INOUT` like this:

>     %include "typemaps.i"
>     ...
>     void negate(int *INOUT);

In Ruby, a mutated parameter shows up as a return value. For example:

>     a = Example.negate(3)
>     print a
>     -3

The most common use of these special typemap rules is to handle functions that return more than one value. For example, sometimes a function returns a result as well as a special error code:

>     /* send message, return number of bytes sent, success code, and error_code */
>     int send_message(char *text, int *success, int *error_code);

To wrap such a function, simply use the `OUTPUT` rule above. For example:

>     %module example
>     %include "typemaps.i"
>     ...
>     int send_message(char *, int *OUTPUT, int *OUTPUT);

When used in Ruby, the function will return an array of multiple values.

>     bytes, success, error_code = send_message("Hello World")
>     if not success
>       print "error #{error_code} : in send_message"
>     else
>       print "Sent", bytes
>     end

Another way to access multiple return values is to use the `%apply` rule. In the following example, the parameters rows and columns are related to SWIG as `OUTPUT` values through the use of `%apply`

>     %module Example
>     %include "typemaps.i"
>     %apply int *OUTPUT { int *rows, int *columns };
>     ...
>     void get_dimensions(Matrix *m, int *rows, int*columns);

In Ruby:

>     r, c = Example.get_dimensions(m)

## 20.5 Simple exception handling

The SWIG `%exception` directive can be used to define a user-definable exception handler that can convert C/C++ errors into Ruby exceptions. The chapter on Customization Features contains more details, but suppose you have a C++ class like the following :

>     class DoubleArray {
>       private:
>         int n;
>         double *ptr;
>       public:
>         // Create a new array of fixed size
>         DoubleArray(int size) {
>           ptr = new double[size];
>           n = size;
>         }
>         // Destroy an array
>         ~DoubleArray() {
>            delete ptr;
>         }
>         // Return the length of the array
>         int   length() {
>           return n;
>         }

>         // Get an array item and perform bounds checking.
>         double getitem(int i) {
>           if ((i >= 0) && (i < n))
>             return ptr[i];
>           else
>             throw RangeError();
>         }
>         // Set an array item and perform bounds checking.
>         void setitem(int i, double val) {
>           if ((i >= 0) && (i < n))
>             ptr[i] = val;
>           else {
>             throw RangeError();
>           }
>         }
>       };

Since several methods in this class can throw an exception for an out-of-bounds access, you might want to catch this in the Ruby extension by writing the following in an interface file:

>     %exception {
>       try {
>         $action
>       }
>       catch (RangeError) {
>         static VALUE cpperror = rb_define_class("CPPError", rb_eStandardError);
>         rb_raise(cpperror, "Range error.");
>       }
>     }

>     class DoubleArray {
>        ...
>     };

The exception handling code is inserted directly into generated wrapper functions. When an exception handler is defined, errors can be caught and used to gracefully raise a Ruby exception instead of forcing the entire program to terminate with an uncaught error.

As shown, the exception handling code will be added to every wrapper function. Because this is somewhat inefficient, you might consider refining the exception handler to only apply to specific methods like this:

>     %exception getitem {
>       try {
>         $action
>       }
>       catch (RangeError) {
>         static VALUE cpperror = rb_define_class("CPPError", rb_eStandardError);
>         rb_raise(cpperror, "Range error in getitem.");
>       }
>     }

>     %exception setitem {
>       try {
>         $action
>       }
>       catch (RangeError) {
>         static VALUE cpperror = rb_define_class("CPPError", rb_eStandardError);
>         rb_raise(cpperror, "Range error in setitem.");
>       }
>     }

In this case, the exception handler is only attached to methods and functions named `getitem` and `setitem`.

Since SWIG's exception handling is user-definable, you are not limited to C++ exception handling. See the chapter on Customization Features for more examples.

When raising a Ruby exception from C, use the `rb_raise()` function as shown above. The first argument passed to `rb_raise()` is the exception type. You can raise a custom exception type (like the `cpperror` example shown above) or one of the built-in Ruby exception types, listed here:

>     rb_eException
>     rb_eStandardError
>     rb_eSystemExit
>     rb_eInterrupt
>     rb_eSignal
>     rb_eFatal
>     rb_eArgError
>     rb_eEOFError
>     rb_eIndexError
>     rb_eRangeError
>     rb_eIOError
>     rb_eRuntimeError
>     rb_eSecurityError
>     rb_eSystemCallError
>     rb_eTypeError
>     rb_eZeroDivError
>     rb_eNotImpError
>     rb_eNoMemError
>     rb_eNoMethodError
>     rb_eFloatDomainError

>     rb_eScriptError
>     rb_eNameError
>     rb_eSyntaxError
>     rb_eLoadError

These exceptions are actually organized into an hierarchy as shown below.

>     Exception          - rb_eException
>     Fatal              - rb_eFatal
>     Interrupt          - rb_eInterrupt
>     NoMemoryError      - rb_eNoMemError
>     Signal             - rb_eSignal
>     ScriptError        - rb_eScriptError
>       LoadError        - rb_eLoadError
>       NameError        - rb_eNameError
>       NotImpError      - rb_eNotImpError
>       SyntaxError      - rb_eSyntaxError
>       NoMethodError    - rb_eNoMethodError
>     StandardError      - rb_eStandardError
>       ArgError         - rb_eArgError
>       FloatDomainError - rb_eFloatDomainError
>       IndexError       - rb_eIndexError
>       IOError          - rb_eIOError
>         EOFError       - rb_eEOFError
>       SecurityError    - rb_eSecurityError
>       RuntimeError     - rb_eRuntimeError
>       SystemCallError  - rb_eSystemCallError
>       TypeError        - rb_eTypeError
>       ZeroDivError     - rb_eZeroDivError
>       RangeError       - rb_eRangeError
>     SystemExit

## 20.6 Typemaps

This section describes how you can modify SWIG's default wrapping behavior for various C/C++ datatypes using the `%typemap` directive. This is an advanced topic that assumes familiarity with the Ruby C API as well as the material in the "Typemaps" chapter.

Before proceeding, it should be stressed that typemaps are not a required part of using SWIG---the default wrapping behavior is enough in most cases. Typemaps are only used if you want to change some aspect of the primitive C-Ruby interface.

### 20.6.1 What is a typemap?

A typemap is nothing more than a code generation rule that is attached to a specific C datatype. For example, to convert integers from Ruby to C, you might define a typemap like this:

>     %module Example
>     %typemap(in) int {
>       $1 = (int) NUM2INT($input);
>       printf("Received an integer : %d\n",$1);
>     }
>     extern int fact(int n);

Typemaps are always associated with some specific aspect of code generation. In this case, the "in" method refers to the conversion of input arguments to C/C++. The datatype `int` is the datatype to which the typemap will be applied. The supplied C code is used to convert values. In this code a number of special variables prefaced by a `$` are used. The `$1` variable is placeholder for a local variable of type `int`. The `$input` variable is the input Ruby object.

When this example is compiled into a Ruby module, it operates as follows:

>     require 'example'
>     puts Example.fact(6)
>     ---- Prints
>     Received an integer : 6
>     720

In this example, the typemap is applied to all occurrences of the `int` datatype. You can refine this by supplying an optional parameter name. For example:

>     %module example

>     %typemap(in) int n {
>       $1 = (int) NUM2INT($input);
>       printf("n = %d\n",$1);
>     }

>     extern int fact(int n);

In this case, the typemap code is only attached to arguments that exactly match "`int n`".

The application of a typemap to specific datatypes and argument names involves more than simple text-matching--typemaps are fully integrated into the SWIG type-system. When you define a typemap for `int`, that typemap applies to `int` and qualified variations such as `const int`. In addition, the typemap system follows `typedef` declarations. For example:

>     %typemap(in) int n {
>         $1 = (int) NUM2INT($input);
>         printf("n = %d\n",$1);
>     }

>     typedef int Integer;
>     extern int fact(Integer n);    // Above typemap is applied

However, the matching of `typedef` only occurs in one direction. If you defined a typemap for `Integer`, it is not applied to arguments of type `int`.

Typemaps can also be defined for groups of consecutive arguments. For example:

>     %typemap(in) (char *str, int len) {
>         $1 = STR2CSTR($input);
>         $2 = (int) RSTRING($input)->len;
>     };

>     int count(char c, char *str, int len);

When a multi-argument typemap is defined, the arguments are always handled as a single Ruby object. This allows the function `count` to be used as follows (notice how the length parameter is omitted):

>     puts Example.count('o','Hello World')
>     2

### 20.6.2 Ruby typemaps

The previous section illustrated an "in" typemap for converting Ruby objects to C. A variety of different typemap methods are defined by the Ruby module. For example, to convert a C integer back into a Ruby object, you might define an "out" typemap like this:

>     %typemap(out) int {
>         $result = INT2NUM($1);
>     }

The following list details all of the typemap methods that can be used by the Ruby module:

`%typemap(in)`

> Converts Ruby objects to input function arguments

`%typemap(out)`

> Converts return value of a C function to a Ruby object

`%typemap(varin)`

> Assigns a C global variable from a Ruby object

`%typemap(varout)`

> Returns a C global variable as a Ruby object

`%typemap(freearg)`

> Cleans up a function argument (if necessary)

`%typemap(argout)`

> Output argument processing

`%typemap(ret)`

> Cleanup of function return values

`%typemap(memberin)`

> Setting of structure/class member data

`%typemap(globalin)`

> Setting of C global variables

`%typemap(check)`

> Checks function input values.

`%typemap(default)`

> Set a default value for an argument (making it optional).

`%typemap(arginit)`

> Initialize an argument to a value before any conversions occur.

Examples of these typemaps appears in the section on typemap examples

### 20.6.3 Typemap variables

Within a typemap, a number of special variables prefaced with a `$` may appear. A full list of variables can be found in the "Typemaps" chapter. This is a list of the most common variables:

`$1`

> A C local variable corresponding to the actual type specified in the `%typemap` directive. For input values, this is a C local variable that is supposed to hold an argument value. For output values, this is the raw result that is supposed to be returned to Ruby.

`$input`

> A `VALUE` holding a raw Ruby object with an argument or variable value.

`$result`

> A `VALUE` that holds the result to be returned to Ruby.

`$1_name`

> The parameter name that was matched.

`$1_type`

> The actual C datatype matched by the typemap.

`$1_ltype`

> An assignable version of the datatype matched by the typemap (a type that can appear on the left-hand-side of a C assignment operation). This type is stripped of qualifiers and may be an altered version of `$1_type`. All arguments and local variables in wrapper functions are declared using this type so that their values can be properly assigned.

`$symname`

> The Ruby name of the wrapper function being created.

### 20.6.4 Useful Functions

When you write a typemap, you usually have to work directly with Ruby objects. The following functions may prove to be useful. (These functions plus many more can be found in the "Programming Ruby" book written by David Thomas and Andrew Hunt.)

#### 20.6.4.1 C Datatypes to Ruby Objects

>     INT2NUM(long or int)  - int to Fixnum or Bignum
>     INT2FIX(long or int)  - int to Fixnum (faster than INT2NUM)
>     CHR2FIX(char)         - char to Fixnum
>     rb_str_new2(char*)    - char* to String
>     rb_float_new(double)  - double to Float

#### 20.6.4.2 Ruby Objects to C Datatypes

>               int NUM2INT(Numeric)
>               int FIX2INT(Numeric)
>      unsigned int NUM2UINT(Numeric)
>      unsigned int FIX2UINT(Numeric)
>              long NUM2LONG(Numeric)
>              long FIX2LONG(Numeric)
>     unsigned long FIX2ULONG(Numeric)
>              char NUM2CHR(Numeric or String)
>            char * STR2CSTR(String)
>            char * rb_str2cstr(String, int*length)
>            double NUM2DBL(Numeric)

#### 20.6.4.3 Macros for VALUE

`RSTRING(str)->len`

> length of the Ruby string

`RSTRING(str)->ptr`

> pointer to string storage

`RARRAY(arr)->len`

> length of the Ruby array

`RARRAY(arr)->capa`

> capacity of the Ruby array

`RARRAY(arr)->ptr`

> pointer to array storage

#### 20.6.4.4 Exceptions

`void rb_raise(VALUE exception, const char *fmt, ...)`

> Raises an exception. The given format string fmt and remaining arguments are interpreted as with `printf()`.

`void rb_fatal(const char *fmt, ...)`

> Raises a fatal exception, terminating the process. No rescue blocks are called, but ensure blocks will be called. The given format string fmt and remaining arguments are interpreted as with `printf()`.

`void rb_bug(const char *fmt, ...)`

> Terminates the process immediately -- no handlers of any sort will be called. The given format string fmt and remaining arguments are interpreted as with `printf()`. You should call this function only if a fatal bug has been exposed.

`void rb_sys_fail(const char *msg)`

> Raises a platform-specific exception corresponding to the last known system error, with the given string msg.

`VALUE rb_rescue(VALUE (*body)(VALUE), VALUE args, VALUE(*rescue)(VALUE, VALUE), VALUE rargs)`

> Executes body with the given args. If a `StandardError` exception is raised, then execute rescue with the given rargs.

`VALUE rb_ensure(VALUE(*body)(VALUE), VALUE args, VALUE(*ensure)(VALUE), VALUE eargs)`

> Executes body with the given args. Whether or not an exception is raised, execute ensure with the given rargs after body has completed.

`VALUE rb_protect(VALUE (*body)(VALUE), VALUE args, int *result)`

> Executes body with the given args and returns nonzero in result if any exception was raised.

`void rb_notimplement()`

> Raises a `NotImpError` exception to indicate that the enclosed function is not implemented yet, or not available on this platform.

`void rb_exit(int status)`

> Exits Ruby with the given status. Raises a `SystemExit` exception and calls registered exit functions and finalizers.

`void rb_warn(const char *fmt, ...)`

> Unconditionally issues a warning message to standard error. The given format string fmt and remaining arguments are interpreted as with `printf()`.

`void rb_warning(const char *fmt, ...)`

> Conditionally issues a warning message to standard error if Ruby was invoked with the `-w` flag. The given format string fmt and remaining arguments are interpreted as with `printf()`.

#### 20.6.4.5 Iterators

`void rb_iter_break()`

> Breaks out of the enclosing iterator block.

`VALUE rb_each(VALUE obj)`

> Invokes the `each` method of the given obj.

`VALUE rb_yield(VALUE arg)`

> Transfers execution to the iterator block in the current context, passing arg as an argument. Multiple values may be passed in an array.

`int rb_block_given_p()`

> Returns `true` if `yield` would execute a block in the current context; that is, if a code block was passed to the current method and is available to be called.

`VALUE rb_iterate(VALUE (*method)(VALUE), VALUE args, VALUE (*block)(VALUE, VALUE), VALUE arg2)`

> Invokes method with argument args and block block. A `yield` from that method will invoke block with the argument given to `yield`, and a second argument arg2.

`VALUE rb_catch(const char *tag, VALUE (*proc)(VALUE, VALUE), VALUE value)`

> Equivalent to Ruby's `catch`.

`void rb_throw(const char *tag, VALUE value)`

> Equivalent to Ruby's `throw`.

### 20.6.5 Typemap Examples

This section includes a few examples of typemaps. For more examples, you might look at the examples in the `Example/ruby` directory.

### 20.6.6 Converting a Ruby array to a char **

A common problem in many C programs is the processing of command line arguments, which are usually passed in an array of `NULL` terminated strings. The following SWIG interface file allows a Ruby Array instance to be used as a `char **` object.

>     %module argv

>     // This tells SWIG to treat char ** as a special case
>     %typemap(in) char ** {
>       /* Get the length of the array */
>       int size = RARRAY($input)->len;
>       int i;
>       $1 = (char **) malloc((size+1)*sizeof(char *));
>       /* Get the first element in memory */
>       VALUE *ptr = RARRAY($input)->ptr;
>       for (i=0; i < size; i++, ptr++)
>         /* Convert Ruby Object String to char* */
>         $1[i]= STR2CSTR(*ptr);
>       $1[i]=NULL;  /* End of list */
>     }

>     // This cleans up the char ** array created before
>     // the function call

>     %typemap(freearg) char ** {
>       free((char *) $1);
>     }

>     // Now a test function
>     %inline %{
>     int print_args(char **argv) {
>         int i = 0;
>         while (argv[i]) {
>              printf("argv[%d] = %s\n", i,argv[i]);
>              i++;
>         }
>         return i;
>     }
>     %}

When this module is compiled, the wrapped C function now operates as follows :

>     require 'Argv'
>     Argv.print_args(["Dave","Mike","Mary","Jane","John"])
>     argv[0] = Dave
>     argv[1] = Mike
>     argv[2] = Mary
>     argv[3] = Jane
>     argv[4] = John

In the example, two different typemaps are used. The "in" typemap is used to receive an input argument and convert it to a C array. Since dynamic memory allocation is used to allocate memory for the array, the "freearg" typemap is used to later release this memory after the execution of the C function.

### 20.6.7 Collecting arguments in a hash

Ruby's solution to the "keyword arguments" capability of some other languages is to allow the programmer to pass in one or more key-value pairs as arguments to a function. All of those key-value pairs are collected in a single `Hash` argument that's presented to the function. If it makes sense, you might want to provide similar functionality for your Ruby interface. For example, suppose you'd like to wrap this C function that collects information about people's vital statistics:

>     void setVitalStats(const char *person, int nattributes, const char **names, int *values);

and you'd like to be able to call it from Ruby by passing in an arbitrary number of key-value pairs as inputs, e.g.

>     setVitalStats("Fred",
>                   'weight' => 270,
>     	      'age' => 42
>     	     )

To make this work, you need to write a typemap that expects a Ruby `Hash` as its input and somehow extracts the last three arguments (nattributes, names and values) needed by your C function. Let's start with the basics:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>     }

This `%typemap` directive tells SWIG that we want to match any function declaration that has the specified types and names of arguments somewhere in the argument list. The fact that we specified the argument names (nattributes, names and values) in our typemap is significant; this ensures that SWIG won't try to apply this typemap to other functions it sees that happen to have a similar declaration with different argument names. The arguments that appear in the second set of parentheses (keys_arr, i, key and val) define local variables that our typemap will need.

Since we expect the input argument to be a `Hash`, let's next add a check for that:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>     }

`Check_Type()` is just a macro (defined in the Ruby header files) that confirms that the input argument is of the correct type; if it isn't, an exception will be raised.

The next task is to determine how many key-value pairs are present in the hash; we'll assign this number to the first typemap argument (`$1`). This is a little tricky since the Ruby/C API doesn't provide a public function for querying the size of a hash, but we can get around that by calling the hash's size method directly and converting its result to a C `int` value:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>     }

So now we know the number of attributes. Next we need to initialize the second and third typemap arguments (i.e. the two C arrays) to `NULL` and set the stage for extracting the keys and values from the hash:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>       $2 = NULL;
>       $3 = NULL;
>       if ($1 > 0) {
>         $2 = (char **) malloc($1*sizeof(char *));
>         $3 = (int *) malloc($1*sizeof(int));
>       }
>     }

There are a number of ways we could extract the keys and values from the input hash, but the simplest approach is to first call the hash's keys method (which returns a Ruby array of the keys) and then start looping over the elements in that array:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>       $2 = NULL;
>       $3 = NULL;
>       if ($1 > 0) {
>         $2 = (char **) malloc($1*sizeof(char *));
>         $3 = (int *) malloc($1*sizeof(int));
>         keys_arr = rb_funcall($input, rb_intern("keys"), 0, NULL);
>         for (i = 0; i < $1; i++) {
>         }
>       }
>     }

Recall that keys_arr and i are local variables for this typemap. For each element in the keys_arr array, we want to get the key itself, as well as the value corresponding to that key in the hash:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>       $2 = NULL;
>       $3 = NULL;
>       if ($1 > 0) {
>         $2 = (char **) malloc($1*sizeof(char *));
>         $3 = (int *) malloc($1*sizeof(int));
>         keys_arr = rb_funcall($input, rb_intern("keys"), 0, NULL);
>         for (i = 0; i < $1; i++) {
>           key = rb_ary_entry(keys_arr, i);
>           val = rb_hash_aref($input, key);
>         }
>       }
>     }

To be safe, we should again use the `Check_Type()` macro to confirm that the key is a `String` and the value is a `Fixnum`:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>       $2 = NULL;
>       $3 = NULL;
>       if ($1 > 0) {
>         $2 = (char **) malloc($1*sizeof(char *));
>         $3 = (int *) malloc($1*sizeof(int));
>         keys_arr = rb_funcall($input, rb_intern("keys"), 0, NULL);
>         for (i = 0; i < $1; i++) {
>           key = rb_ary_entry(keys_arr, i);
>           val = rb_hash_aref($input, key);
>           Check_Type(key, T_STRING);
>           Check_Type(val, T_FIXNUM);
>         }
>       }
>     }

Finally, we can convert these Ruby objects into their C equivalents and store them in our local C arrays:

>     %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_arr, int i, VALUE key, VALUE val) {
>       Check_Type($input, T_HASH);
>       $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
>       $2 = NULL;
>       $3 = NULL;
>       if ($1 > 0) {
>         $2 = (char **) malloc($1*sizeof(char *));
>         $3 = (int *) malloc($1*sizeof(int));
>         keys_arr = rb_funcall($input, rb_intern("keys"), 0, NULL);
>         for (i = 0; i < $1; i++) {
>           key = rb_ary_entry(keys_arr, i);
>           val = rb_hash_aref($input, key);
>           Check_Type(key, T_STRING);
>           Check_Type(val, T_FIXNUM);
>           $2[i] = STR2CSTR(key);
>           $3[i] = NUM2INT(val);
>         }
>       }
>     }

We're not done yet. Since we used `malloc()` to dynamically allocate the memory used for the names and values arguments, we need to provide a corresponding "freearg" typemap to free that memory so that there is no memory leak. Fortunately, this typemap is a lot easier to write:

>     %typemap(freearg) (int nattributes, const char **names, const int *values) {
>       free((void *) $2);
>       free((void *) $3);
>     }

All of the code for this example, as well as a sample Ruby program that uses the extension, can be found in the `Examples/ruby/hashargs` directory of the SWIG distribution.

### 20.6.8 Pointer handling

Occasionally, it might be necessary to convert pointer values that have been stored using the SWIG typed-pointer representation. Since there are several ways in which pointers can be represented, the following two functions are used to safely perform this conversion:

`int SWIG_ConvertPtr(VALUE obj, void **ptr, swig_type_info *ty, int flags)`

> Converts a Ruby object obj to a C pointer whose address is ptr (i.e. ptr is a pointer to a pointer). The third argument, ty, is a pointer to a SWIG type descriptor structure. If ty is not `NULL`, that type information is used to validate type compatibility and other aspects of the type conversion. If flags is non-zero, any type errors encountered during this validation result in a Ruby `TypeError` exception being raised; if flags is zero, such type errors will cause `SWIG_ConvertPtr()` to return -1 but not raise an exception. If ty is `NULL`, no type-checking is performed.

`VALUE SWIG_NewPointerObj(void *ptr, swig_type_info *ty, int own)`

> Creates a new Ruby pointer object. Here, ptr is the pointer to convert, ty is the SWIG type descriptor structure that describes the type, and own is a flag that indicates whether or not Ruby should take ownership of the pointer (i.e. whether Ruby should free this data when the corresponding Ruby instance is garbage-collected).

Both of these functions require the use of a special SWIG type-descriptor structure. This structure contains information about the mangled name of the datatype, type-equivalence information, as well as information about converting pointer values under C++ inheritance. For a type of `Foo *`, the type descriptor structure is usually accessed as follows:

>     Foo *foo;
>     SWIG_ConvertPtr($input, (void **) &foo, SWIGTYPE_p_Foo, 1);

>     VALUE obj;
>     obj = SWIG_NewPointerObj(f, SWIGTYPE_p_Foo, 0);

In a typemap, the type descriptor should always be accessed using the special typemap variable `$1_descriptor`. For example:

>     %typemap(in) Foo * {
>        SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
>     }

#### 20.6.8.1 Ruby Datatype Wrapping

`VALUE Data_Wrap_Struct(VALUE class, void (*mark)(void *), void (*free)(void *), void *ptr)`

> Given a pointer ptr to some C data, and the two garbage collection routines for this data (mark and free), return a `VALUE` for the Ruby object.

`VALUE Data_Make_Struct(VALUE class, c-type, void (*mark)(void *), void (*free)(void *), c-type *ptr)`

> Allocates a new instance of a C data type c-type, assigns it to the pointer ptr, then wraps that pointer with `Data_Wrap_Struct()` as above.

`Data_Get_Struct(VALUE obj, c-type, c-type *ptr)`

> Retrieves the original C pointer of type c-type from the data object obj and assigns that pointer to ptr.

## 20.7 Operator overloading

SWIG allows operator overloading with, by using the `%extend` or `%rename` commands in SWIG and the following operator names (derived from Python):

>      General
>     __repr__      - inspect
>     __str__       - to_s
>     __cmp__       - <=>
>     __hash__      - hash
>     __nonzero__   - nonzero?

>      Callable
>     __call__      - call

>      Collection
>     __len__       - length
>     __getitem__   - []
>     __setitem__   - []=

>      Numeric
>     __add__       - +
>     __sub__       - -
>     __mul__       - *
>     __div__       - /
>     __mod__       - %
>     __divmod__    - divmod
>     __pow__       - **
>     __lshift__    - <<
>     __rshift__    - >>
>     __and__       - &
>     __xor__       - ^
>     __or__        - |
>     __neg__       - -@
>     __pos__       - +@
>     __abs__       - abs
>     __invert__    - ~
>     __int__       - to_i
>     __float__     - to_f
>     __coerce__    - coerce

>     Additions in 1.3.13
>     __lt__        - <
>     __le__        - <=
>     __eq__        - ==
>     __gt__        - >
>     __ge__        - >=

Note that although SWIG supports the `__eq__` magic method name for defining an equivalence operator, there is no separate method for handling inequality since Ruby parses the expression a != b as !(a == b).

### 20.7.1 An example (putting everything together)

One way to illustrate the use of operator overloading, as well as the wrapping of templates (see the "SWIG and C++" section on templates) is shown in the following `.i` example is used, given an STL vector class:

>     %module Example

>     %{
>     #include <vector>
>     #include <string>

>     using namespace std;
>     %}

>     %typemap (in) string {
>       Check_Type($input, T_STRING);
>       $1 = string(STR2CSTR($input));
>     }

>     %typemap (out) string {
>       if ($1.c_str() == NULL)
>         $result = Qnil;
>       else
>         $result = rb_str_new2($1.c_str());
>     }

>     template<class T> class vector {
>     public:
>       %rename(__getitem__) operator[](int);
>       void push_back(T);
>       int size();
>       T operator[](int);

>       %extend {
>         void __setitem__(int index, T value) {
>           (*self)[index]=value;
>         }
>       }
>     };

>     %template(StringVector) vector <string>;
>     %template(IntVector) vector <int>;

>     %extend IntVector {
>       void each () {
>         IntVector::iterator i1 = self->begin();
>         IntVector::iterator i1end = self->end();
>         for ( ; i1!=i1end; i1++)
>           rb_yield(INT2NUM(*i1));
>       }
>     }
>     %extend StringVector {
>       void each () {
>         StringVector::iterator i1 = self->begin();
>         StringVector::iterator i1end = self->end();
>         for ( ; i1!=i1end; i1++)
>           rb_yield(rb_str_new2((*i1).c_str()));
>       }
>     }

An IRB session using this extension might go like this:

>     $ irb
>     irb(main):001:0> require 'Example'
>     true
>     irb(main):002:0> Example::IntVector.new
>     #
>     irb(main):003:0> i_list.push_back(1)
>     nil
>     irb(main):004:0> i_list.push_back(2)
>     nil
>     irb(main):005:0> i_list.each { |i| puts i }
>     1
>     2
>     nil
>     irb(main):006:0> "size = #{i_list.size}"
>     "size = 2"
>     irb(main):007:0> s_list = Example::StringVector.new
>     #
>     irb(main):008:0> s_list.push_back("Hello")
>     nil
>     irb(main):009:0> s_list.push_back("There")
>     nil
>     irb(main):010:0> s_list.each { |s| puts s }
>     Hello
>     There
>     nil
>     irb(main):011:0> "size = #{s_list.size}"
>     "size = 2"
>     irb(main):012:0> "s_list[0] = #{s_list[0]}"
>     "s_list[0] = Hello"
>     irb(main):013:0> s_list[0] = "New entry"
>     nil
>     irb(main):014:0> puts "s_list[0] = #{s_list[0]}"
>     "s_list[0] = New Entry"

### 20.7.2 Expanding the example

To expand on the previous example, the following shows how to create a vector of a C++ class object:

>     class CPP_Object;
>     %template(CPP_ObjectPtrVector)  vector<CPP_Object*>;

>     %extend CPP_ObjectPtrVector {
>       void each () {
>         CPP_ObjectPtrVector::iterator i1 = self->begin();
>         CPP_ObjectPtrVector::iterator i1end = self->end();
>         for ( ; i1 != i1end; i1++)
>           rb_yield(Data_Wrap_Struct(cCPP_Object.klass, NULL, NULL, *i1));
>       }
>     }

Notice that the SWIG interface has created a `cCPP_Object` that is of type `swig_class`. This is an implementation detail that only works for SWIG 1.3.11; for previous versions of SWIG the data type of `cCPP_Object` was `VALUE`.

>     typedef struct {
>       VALUE  klass;
>       void (*mark)(void *);
>       void (*destroy)(void *);
>     } swig_class;

Thus, to access the pointer to the `CPP_Object`, the variable `klass` is used.

Also note that the call to `Data_Wrap_Struct()` sets the `free` parameter for `CPP_Object` to `NULL`. That is done because C++, owns the object and thus should free it, not Ruby.

### 20.7.3 STL Vector to Ruby Array

Another use for macros and type maps is to create a Ruby array from a STL vector of pointers. In essence, copy of all the pointers in the vector into a Ruby array. The use of the macro is to make the typemap so generic that any vector with pointers can use the type map. The following is an example of how to construct this type of macro/typemap and should give insight into constructing similar typemaps for other STL structures:

>     %define PTR_VECTOR_TO_RUBY_ARRAY(vectorclassname, classname)
>     %typemap(ruby, out) vectorclassname &, const vectorclassname & {
>       VALUE arr = rb_ary_new2($1->size());
>       vectorclassname::iterator i = $1->begin(), iend = $1->end();
>       for ( ; i!=iend; i++ )
>         rb_ary_push(arr, Data_Wrap_Struct(c ## classname.klass, 0, 0, *i));
>       $result = arr;
>     }
>     %typemap(ruby, out) vectorclassname, const vectorclassname {
>       VALUE arr = rb_ary_new2($1.size());
>       vectorclassname::iterator i = $1.begin(), iend = $1.end();
>       for ( ; i!=iend; i++ )
>         rb_ary_push(arr, Data_Wrap_Struct(c ## classname.klass, 0, 0, *i));
>       $result = arr;
>     }
>     %enddef

Note, that the "`c ## classname.klass"` is used in the preprocessor step to determine the actual object from the class name.

To use the macro with a class Foo, the following is used:

>     PTR_VECTOR_TO_RUBY_ARRAY(vector, Foo)

It is also possible to create a STL vector of Ruby objects:

>     %define RUBY_ARRAY_TO_PTR_VECTOR(vectorclassname, classname)
>     %typemap(ruby, in) vectorclassname &, const vectorclassname & {
>       Check_Type($input, T_ARRAY);
>       vectorclassname *vec = new vectorclassname;
>       int len = RARRAY($input)->len;
>       for (int i=0; i!=len; i++) {
>         VALUE inst = rb_ary_entry($input, i);
>         //The following _should_ work but doesn't on HPUX
>         //    Check_Type(inst, T_DATA);
>         classname *element = NULL;
>         Data_Get_Struct(inst, classname, element);
>         vec->push_back(element);
>       }
>       $1 = vec;
>     }

>     %typemap(ruby, freearg) vectorclassname &, const vectorclassname & {
>       delete $1;
>     }
>     %enddef

It is also possible to create a Ruby array from a vector of static data types:

    %define VECTOR_TO_RUBY_ARRAY(vectorclassname, classname)
    %typemap(ruby, out) vectorclassname &, const vectorclassname & {
      VALUE arr = rb_ary_new2($1->size());
      vectorclassname::iterator i = $1->begin(), iend = $1->end();
      for ( ; i!=iend; i++ )
        rb_ary_push(arr, Data_Wrap_Struct(c ## classname.klass, 0, 0, &(*i)));
      $result = arr;
    }
    %typemap(ruby, out) vectorclassname, const vectorclassname {
      VALUE arr = rb_ary_new2($1.size());
      vectorclassname::iterator i = $1.begin(), iend = $1.end();
      for ( ; i!=iend; i++ )
        rb_ary_push(arr, Data_Wrap_Struct(c ## classname.klass, 0, 0, &(*i)));
      $result = arr;
    }
    %enddef

## 20.8 Advanced Topics

### 20.8.1 Creating Multi-Module Packages

The chapter on Advanced Topics discusses the basics of creating multi-module extensions with SWIG, and in particular the considerations for sharing runtime type information among the different modules.

As an example, consider one module's interface file (`shape.i`) that defines our base class:

>     %module shape

>     %{
>     #include "Shape.h"
>     %}

>     class Shape {
>     protected:
>         double xpos;
>         double ypos;
>     protected:
>         Shape(double x, double y);
>     public:
>         double getX() const;
>         double getY() const;
>     };

We also have a separate interface file (`circle.i`) that defines a derived class:

>     %module circle

>     %{
>     #include "Shape.h"
>     #include "Circle.h"
>     %}

>     // Import the base class definition from Shape module
>     %import shape.i

>     class Circle : public Shape {
>     protected:
>         double radius;
>     public:
>         Circle(double x, double y, double r);
>         double getRadius() const;
>     };

Both of these modules should be compiled with SWIG's `-c` option so that the runtime library code is omitted from the wrapper files. We'll start by building the Shape extension module:

>     $ swig -c++ -ruby -c shape.i

SWIG generates a wrapper file named `shape_wrap.cxx`. To compile this into a dynamically loadable extension for Ruby, prepare an `extconf.rb` script using this template:

>     require 'mkmf'

>     # Since the SWIG runtime support library for Ruby (libswigrb.so)
>     # depends on the Ruby library, make sure it's in the list
>     # of libraries.
>     $libs = append_library($libs, Config::CONFIG['RUBY_INSTALL_NAME'])

>     # Now add the SWIG runtime support library
>     have_library('swigrb', 'SWIG_InitRuntime')

>     # Create the makefile
>     create_makefile('shape')

Run this script to create a `Makefile` and then type `make` to build the shared library:

>     $ ruby extconf.rb
>     checking for SWIG_InitRuntime() in -lswigrb... yes
>     creating Makefile

>     $ make
>     g++ -fPIC -g -O2  -I. -I/usr/local/lib/ruby/1.7/i686-linux \
>             -I. -c shape_wrap.cxx
>     gcc -shared -L/usr/local/lib -o shape.so shape_wrap.o -L. \
>     	-lruby -lswigrb -lruby -lc

Note that depending on your installation, the outputs may be slightly different; these outputs are those for a Linux-based development environment. The end result should be a shared library (here, `shape.so`) containing the extension module code. Now repeat this process in a separate directory for the Circle module:

1. Run SWIG to generate the wrapper code (`circle_wrap.cxx`);
2. Write an `extconf.rb` script that your end-users can use to create a platform-specific `Makefile` for the extension;
3. Build the shared library for this extension by typing `make`.

Once you've built both of these extension modules, you can test them interactively in IRB to confirm that the `Shape` and `Circle` modules are properly loaded and initialized:

>     $ irb
>     irb(main):001:0> require 'shape'
>     true
>     irb(main):002:0> require 'circle'
>     true
>     irb(main):003:0> c = Circle::Circle.new(5, 5, 20)
>     #<Circle::Circle:0xa097208>
>     irb(main):004:0> c.kind_of? Shape::Shape
>     true
>     irb(main):005:0> c.getX()
>     5.0

### 20.8.2 Defining Aliases

It's a fairly common practice in the Ruby built-ins and standard library to provide aliases for method names. For example, Array#size is an alias for Array#length. If you'd like to provide an alias for one of your class' instance methods, one approach is to use SWIG's `%extend` directive to add a new method of the aliased name that calls the original function. For example:

>     class MyArray {
>     public:
>       // Construct an empty array
>       MyArray();

>       // Return the size of this array
>       size_t length() const;
>     };

>     %extend MyArray {
>       // MyArray#size is an alias for MyArray#length
>       size_t size() const {
>         return self->length();
>       }
>     }

A better solution is to instead use the `%alias` directive (unique to SWIG's Ruby module). The previous example could then be rewritten as:

>     // MyArray#size is an alias for MyArray#length
>     %alias MyArray::length "size";

>     class MyArray {
>     public:
>       // Construct an empty array
>       MyArray();

>       // Return the size of this array
>       size_t length() const;
>     };

Multiple aliases can be associated with a method by providing a comma-separated list of aliases to the `%alias` directive, e.g.

>     %alias MyArray::length "amount,quantity,size";

From an end-user's standpoint, there's no functional difference between these two approaches; i.e. they should get the same result from calling either MyArray#size or MyArray#length. However, when the `%alias` directive is used, SWIG doesn't need to generate all of the wrapper code that's usually associated with added methods like our MyArray::size() example.

Note that the `%alias` directive is implemented using SWIG's "features" mechanism and so the same name matching rules used for other kinds of features apply (see the chapter on "Customization Features") for more details).

### 20.8.3 Predicate Methods

Predicate methods in Ruby are those which return either `true` or `false`. By convention, these methods' names end in a question mark; some examples from built-in Ruby classes include Array#empty? (which returns `true` for an array containing no elements) and Object#instance_of? (which returns `true` if the object is an instance of the specified class). For consistency with Ruby conventions you would also want your interface's predicate methods' names to end in a question mark and return `true` or `false`.

One cumbersome solution to this problem is to rename the method (using SWIG's `%rename` directive) and provide a custom typemap that converts the function's actual return type to Ruby's `true` or `false`. For example:

>     %rename("is_it_safe?") is_it_safe();

>     %typemap(out) int is_it_safe
>        "$result = ($1 != 0) ? Qtrue : Qfalse;";

>     int is_it_safe();

A better solution is to instead use the `%predicate` directive (unique to SWIG's Ruby module) to designate certain methods as predicate methods. For the previous example, this would look like:

>     %predicate is_it_safe();

>     int is_it_safe();

and to use this method from your Ruby code:

>     irb(main):001:0> Example::is_it_safe?
>     true

Note that the `%predicate` directive is implemented using SWIG's "features" mechanism and so the same name matching rules used for other kinds of features apply (see the chapter on "Customization Features") for more details).

### 20.8.4 Specifying Mixin Modules

The Ruby language doesn't support multiple inheritance, but it does allow you to mix one or more modules into a class using Ruby's `include` method. For example, if you have a Ruby class that defines an each instance method, e.g.

>     class Set
>       def initialize
>         @members = []
>       end

>       def each
>         @members.each { |m| yield m }
>       end
>     end

then you can mix-in Ruby's `Enumerable` module to easily add a lot of functionality to your class:

>     class Set
>       include Enumerable

>       def initialize
>         @members = []
>       end

>       def each
>         @members.each { |m| yield m }
>       end
>     end

To get the same benefit for your SWIG-wrapped classes, you can use the `%mixin` directive to specify the names of one or more modules that should be mixed-in to a class. For the above example, the SWIG interface specification might look like this:

>     %mixin Set "Enumerable";

>     class Set {
>     public:
>       // Constructor
>       Set();

>       // Iterates through set members
>       void each();
>     };

Multiple modules can be mixed into a class by providing a comma-separated list of module names to the `%mixin` directive, e.g.

>     %mixin Set "Fee,Fi,Fo,Fum";

Note that the `%mixin` directive is implemented using SWIG's "features" mechanism and so the same name matching rules used for other kinds of features apply (see the chapter on "Customization Features") for more details).

### 20.8.5 Interacting with Ruby's Garbage Collector

This section is still unfinished!

By default, SWIG ensures that any C++ objects it creates are destroyed when the corresponding Ruby instance is garbage-collected. For example, if you have an interface like this:

>     class Foo
>     {
>     public:
>         // Construct a new Foo object
>         Foo();
>     };

When a user of this extension creates a new `Foo` instance from Ruby, it will construct a new C++ `Foo` object behind the scenes, and when that Ruby instance is garbage-collected, the same C++ object will be destroyed.

But in the real world, things aren't always that simple.

It is often the case, especially for C++ libraries, that objects contain references to other objects. For example, consider a class library that models a zoo and the animals in the zoo:

>     %module zoo

>     %{
>     #include
>     #include

>     #include "zoo.h"
>     %}

>     class Animal
>     {
>     protected:
>         std::string name;

>     public:
>         // Construct an animal with this name
>         Animal(const char* nm) : name(nm) {}

>         // Return the animal's name
>         const char* getName() const { return name.c_str(); }
>     };

>     class Zoo
>     {
>     protected:
>         std::vector animals;

>     public:
>         // Construct an empty zoo
>         Zoo() {}

>         // Add a new animal to the zoo
>         void addAnimal(Animal* animal) {
>           animals.push_back(animal);
>         }

>         // Return the number of animals in the zoo
>         size_t getNumAnimals() const {
>           return animals.size();
>         }

>         // Return a pointer to the ith animal
>         Animal* getAnimal(size_t i) const {
>           return animals[i];
>         }
>     };

Basically, a `Zoo` is modeled as a "container" for animals. And we can SWIG this set of classes, and running `irb` gives the following:

>     $ irb
>     irb(main):001:0> require 'zoo'
>     true
>     irb(main):002:0> zoo = Zoo::Zoo.new
>     #<Zoo::Zoo:0xa090458>
>     irb(main):003:0> zoo.addAnimal(Zoo::Animal.new("Lassie"))
>     nil
>     irb(main):004:0> zoo.addAnimal(Zoo::Animal.new("Felix"))
>     nil
>     irb(main):005:0> zoo.getNumAnimals()
>     2
>     irb(main):006:0> zoo.getAnimal(0).getName()
>     "Lassie"
>     irb(main):007:0> GC.start
>     nil
>     irb(main):008:0> zoo.getAnimal(0).getName()
>     (irb):8: [BUG] Segmentation fault
>     ruby 1.7.2 (2002-03-25) [i386-cygwin]
>     Aborted (core dumped)

Observe that after the garbage collector runs (as a result of our call to `GC.start`) the call to `Animal#getName` causes a segmentation fault. To understand what went wrong requires a basic understanding of Ruby's "mark and sweep" garbage collection scheme.

Add brief discussion of mark and sweep here?

So the problem with our previous example is that during the GC "mark" phase, Ruby has no way of knowing that our two `Animal` instances ("Lassie" and "Felix") are still in use. As far as Ruby can tell, both of these objects are unreachable and should be garbage-collected. We'd like to fix things so that when the `Zoo` instance is visited during the "mark" phase, that it in turn marks the two animals as in use.

The Ruby/C API provides for this need by allowing extension developers to specify customized "mark" functions for data objects like our `Zoo` and `Animal` classes. This mark function takes a single argument, which is a pointer to the C++ object being marked; it should, in turn, call `rb_gc_mark()` for any instances that are reachable from the current object. The mark function for our `Zoo` class should therefore loop over all of the animals in the zoo and call `rb_gc_mark()` for each of the Ruby instances associated with those C++ `Animal` objects:

>     void Zoo_markfunc(void *ptr)
>     {
>         Animal *cppAnimal;
>         VALUE   rubyAnimal;
>         Zoo *zoo;

>         zoo = static_cast(ptr);
>         for (size_t i = 0; i < zoo->getNumAnimals(); i++) {
>             cppAnimal = zoo->getAnimal(i);
>             rubyAnimal = SWIG_RubyInstanceFor(cppAnimal);
>             rb_gc_mark(rubyAnimal);
>         }
>     }

SWIG_RubyInstanceFor() is an imaginary function that takes a pointer to a C/C++ object as its input and returns a `VALUE` corresponding to the Ruby instance that wraps this object. Currently, SWIG doesn't keep track of this kind of mapping at all.

You can use the `%markfunc` directive to associate the name of this function with a SWIGed class:

>     %markfunc Zoo "Zoo_markfunc";

Note that the `%markfunc` and `%freefunc` directives are implemented using SWIG's' "features" mechanism and so the same name matching rules used for other kinds of features apply (see the chapter on "Customization Features") for more details).