Ruby/C++ integration with SWIG: problem with namespaces

C

Comfortably Numb

Hi everyone,

I'm trying to import a C++ library in Ruby using SWIG, but I have a
problem with namespaces and enumerators: I have a namespace "mymodule"
with a function "myfunction" which takes an element of type "myenum" as
an argument.

If I define "myfunction" and "myenum" in the same source file
"mymodule.h" everything works, but if I separate them, placing "myenum"
in "myenum.h" something odd happens: I can print the value of an element
of "myenum" via Ruby but cannot pass it as an argument to "myfunction".

Here are the source files and the error I get:

##################
### mymodule.h ###
##################

#ifndef MYMODULE_H
#define MYMODULE_H

#include "myenum.h"

namespace mymodule {

void myfunction(myenum);

}

#endif

################
### myenum.h ###
################

#ifndef MYENUM_H
#define MYENUM_H

namespace mymodule {

enum myenum { A, B, C };

}

#endif

####################
### mymodule.cxx ###
####################

#include "mymodule.h"

namespace mymodule {

void myfunction(myenum c)
{
// do nothing
}

}

##################
### mymodule.i ###
##################

%module mymodule

%{
#include "mymodule.h"
#include "myenum.h"

using namespace mymodule;
%}

%include mymodule.h
%include myenum.h

################
### myapp.rb ###
################

require 'mymodule'

print "I can print Mymodule::B = ", Mymodule::B, "\n"

print "but I cannot pass it as an argument:\n"

Mymodule::myfunction(Mymodule::B)

####################
### command-line ###
####################

$ swig -c++ -ruby mymodule.i

$ g++ -fPIC -c mymodule_wrap.cxx -I/usr/lib/ruby/1.8/x86_64-linux

$ g++ -fPIC -c mymodule.cxx

$ g++ -shared -o mymodule.so *.o

$ ruby myapp.rb

I can print Mymodule::B = 1

but I cannot pass it as an argument:

myapp.rb:7:in `myfunction': Expected argument 0 of type myenum, but got
Fixnum 1 (TypeError)
in SWIG method 'mymodule::myfunction' from myapp.rb:7
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top