Convert std :: install to ruby โ€‹โ€‹using Swig

I am using Swig to use C ++ in ruby โ€‹โ€‹and currently I have made a simple example of the david.h file

#include <stdio.h> class David { public: David(int x) { this->x = x; } void announce() { printf("David %d\n", x); } int x; }; 

And another swig file like this

 %module "david" %{ #include <libdavid.h> %} class David { public: David(int x); void announce(); int x; }; 

My extconf.rb looks like this

 require 'mkmf' system('swig -c++ -ruby libdavid.i') or abort create_makefile('david') 

It helps me to execute an extremely simple example in a ruby โ€‹โ€‹like this

 2.2.1 :001 > a=David::David.new(42) => #<David::David:0x000000022c3ec0 @__swigtype__="_p_David"> 2.2.1 :002 > ax => 42 

Now I tried a lot of time, but I just can't figure out how to use set from C ++ stl, as shown in the example here . It would be very nice if someone could help me with how I could create a set of stl integers or maybe a vector and show how to insert and erase methods for this set / vector. Simple code samples would be very useful not only for me, but also for many people who may encounter this in the future.


This is just a personal request, feel free to skip this if you are busy. Honestly, I have been using stack overflows for a long time, but gradually I am starting to get discouraged in the community. I asked several questions, and I did not have satisfactory answers, No votes, no votes, nothing, I just do not get answers. It amazes me that extremely simple questions that can be answered in a simple Google search are often very popular, and important questions from new users are either voted heavily or completely ignored. I understand that the community is not doing anything. But I would be very grateful if anyone could explain the possible reasons for this.

thanks

+6
source share
1 answer

This is not complete, but may serve as a useful reference. I could not find suitable documentation for this, but it works fine. Add this to your swig file

 %include <std_set.i> namespace std { %template(IntSet) set<int>; } 

Now run extconf.rb and now the following commands for typing are supported in ruby

 2.2.1 :001 > a=David::IntSet.new => std::set<int,std::less< int >,std::allocator< int > > [] 2.2.1 :002 > a.push(4) => 4 2.2.1 :003 > a.push(1) => 1 2.2.1 :004 > a.push(123) => 123 2.2.1 :005 > a.push(612) => 612 2.2.1 :006 > a => std::set<int,std::less< int >,std::allocator< int > > [1,4,123,612] 2.2.1 :007 > a[0] => 1 2.2.1 :008 > a.erase(a.begin) => nil 2.2.1 :009 > a => std::set<int,std::less< int >,std::allocator< int > > [4,123,612] 2.2.1 :010 > a[0] => 4 2.2.1 :011 > a[1] => 123 2.2.1 :012 > a.erase(a.begin) => nil 2.2.1 :013 > a => std::set<int,std::less< int >,std::allocator< int > > [123,612] 

How push works, like insert for set, is still not clear to me, but it works. But this example clearly shows how the kit can be used. A similar approach can be used for the vector.

This answer, of course, is not complete, and responses / comments from experienced users are welcome.

+1
source

All Articles