ArafatK
ArafatK

Reputation: 760

How to Use SWIG for STL Maps?

I am using SWIG with ruby. We can use SWIG to generate wrappers for Map using

%include <std_map.i>
%template(IntMap)  std::map<int,int>; 

This in the interface file. Example of usage

2.2.1 :001 > a = Example::IntMap.new
=> std::map,std::allocator< std::pair< int const,int > > > {}
2.2.1 :002 > a[1] = 2
=> 2
2.2.1 :003 > a[6] = -12
=> -12
2.2.1 :004 > a[4] = 92
=> 92
2.2.1 :005 > a
=> std::map,std::allocator< std::pair< int const,int > > > {1=>2,4=>92,6=>-12}

Is there a way to define data type directly from ruby so that the type of the key and the value associated are defined from ruby ? For example

2.2.1 :002 > b["asr"] = 2
=> 2
2.2.1 :003 > b["tee"] = -12
=> -12
2.2.1 :004 > b["wetwe"] = 92

Upvotes: 2

Views: 354

Answers (1)

ArafatK
ArafatK

Reputation: 760

Use this in interface file

%include <std_map.i>
namespace std {
   %template(Imap) map<swig::GC_VALUE, swig::GC_VALUE>;
}

Upvotes: 2

Related Questions