ArafatK ArafatK - 6 months ago 61
Ruby Question

Using SWIG with C++ template functions

I have a few template functions for map in c++ and I want to use them in ruby.

template <class Collection>
const typename Collection::value_type::second_type& FindWithDefault(
const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}


In c++ it works like this

int main(int argc, char const *argv[])
{
typedef std::map<std::string, std::string> Map;
Map m;
std::string d= FindWithDefault(m, "foo", "");
std::cout << d << "\n";
//
m["foo"] = "bar";
d= FindWithDefault(m, "foo", "");
std::cout << d << "\n";
// bar
}


Its easy to make use of map and set in ruby by looking at the answer of the question I have answered before(take a look here).

As for this question My interface file has this

%include <std_map.i>
namespace std {
%template(IntMap) map<int,int>;
}
template <typename Collection>
const typename Collection::value_type::second_type& FindWithDefault(
const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) ;
%template(d) FindWithDefault<std::map<int,int> >;


My c++ header file has the complete function mentioned above. Now when I try to check this in Irb, the map works fine, but I get an error on using the template function

2.2.1 :001 > a = Example::IntMap.new
=> std::map<int,int,std::less< int >,std::allocator< std::pair< int const,int > > > {}
2.2.1 :002 > a[1]=2
=> 2
2.2.1 :003 > a[5]=3
=> 3
2.2.1 :004 > a[4]=11
=> 11
2.2.1 :005 > a
=> std::map<int,int,std::less< int >,std::allocator< std::pair< int const,int > > > {1=>2,4=>11,5=>3}
2.2.1 :006 > b=Example::d(a,2,2)
ArgumentError: Wrong arguments for overloaded method 'd'.
Possible C/C++ prototypes are:
std::map< int,int,std::less< int >,std::allocator< std::pair< int const,int > > >::value_type::second_type const & d(int a, int b)
std::map< int,int,std::less< int >,std::allocator< std::pair< int const,int > > >::value_type::second_type const & d(std::map< int,int,std::less< int >,std::allocator< std::pair< int const,int > > > const &collection, std::map< int,int,std::less< int >,std::allocator< std::pair< int const,int > > >::value_type::first_type const &key, std::map< int,int,std::less< int >,std::allocator< std::pair< int const,int > > >::value_type::second_type const &value)

from (irb):6:in `d'
from (irb):6
from /usr/share/rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'


How can I make this work ?

Answer

It seems SWIG has trouble determining the type of Collection::value_type::first_type and Collection::value_type::second_type. I am not exactly sure why.

A possible solution / workaround that happens to work in this case is to replace Collection::value_type::first_type with Collection::key_type and Collection::value_type::second_type with Collection::mapped_type. It is sufficient to do this in the SWIG interface file, i.e., leave your C++ code unchanged but put this in the interface file instead:

%include <std_map.i>
namespace std {
   %template(IntMap) map<int,int>;
}
template <typename Collection>
const typename Collection::mapped_type& FindWithDefault(
    const Collection& collection,
    const typename Collection::key_type& key,
    const typename Collection::mapped_type& value) ;
%template(d) FindWithDefault<std::map<int,int> >;