Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Perl] Support for translating nested C++ std::vectors to/from multidimensional arrays #566

Open
jhkorhonen opened this issue Dec 1, 2015 · 3 comments

Comments

@jhkorhonen
Copy link

For my current project, I would need support for translating C++ std::vector<std::vector<double>> (and more generally nested vectors with various contents and nesting depths) to and from multidimensional arrays in the target languages. It seems that the current implementation of C++ standard library typemaps have this capability for Python, but not for Perl.

Looking at source files, it looks like the Python typemaps are using the new fancy %fragment stuff, but the Perl ones are not. Would it be possible to update the Perl implementations to get automatic support for multidimensional arrays also?

Here's an example demonstrating the difference in behaviour between Perl and Python. The C++ function test2_out returns a vector<vector<double>> and test2_in takes on as an input.

foo.cpp:

#include <iostream>
#include "foo.h"

std::vector<std::vector<double> > test2_out(){
   std::vector<std::vector<double> > ret (4, std::vector<double>(4,0.25));
   return ret;
}

void test2_in(const std::vector<std::vector<double> >& mat){
   for (size_t i = 0; i < mat.size(); ++i){
       for (size_t j = 0; j < mat[i].size(); ++j){
           std::cout << mat[i][j] << " ";
       }
       std::cout << "\n";    
   }
}

foo.h:

#include <vector>

std::vector<std::vector<double> > test2_out();
void test2_in(const std::vector<std::vector<double> >& mat);

foo.i:

%module foo

%{
#include "foo.h"
%}

%include "std_vector.i"
namespace std {
   %template(vector_double) vector<double>;
   %template(vector_vector_double) vector<vector<double> >;
};

%includefoo.h"

in Perl:

Calling foo::test2_out() returns an array of proxy objects:

$VAR1 = [
         bless( {}, 'foo::vector_double' ),
         bless( {}, 'foo::vector_double' ),
         bless( {}, 'foo::vector_double' ),
         bless( {}, 'foo::vector_double' )
       ];

Calling foo::test2_in([[0.25, 0.25], [0.25, 0.25]]) fails with type error from SWIG, though passing the output of test2_out works as expected.

in Python:

Calling foo.test2_in() returns a tuple of tuples:

((0.25,0.25,0.25,0.25),
 (0.25,0.25,0.25,0.25),
 (0.25,0.25,0.25,0.25),
 (0.25,0.25,0.25,0.25))

Calling foo.test2_in([[0.25, 0.25], [0.25, 0.25]]) works as expected.

@wsfulton
Copy link
Member

wsfulton commented Dec 5, 2015

Perhaps @talby- can comment, but it looks to me that Perl isn't using the full STL container support in SWIG and someone will needs to make the improvements necessary and is most likely a non-trivial task. So yes it is possible, like all SWIG development, it requires someone to volunteer.

@tlby
Copy link
Member

tlby commented Dec 7, 2015

Yes, the std::vector support in Perl is incomplete. I made a breif attempt at repairs but realized I needed to learn more about the typemap templates to align Perl's std::vector handling with the other languages. I'll try to dig up any partial results I might have from the previous effort.

@jhkorhonen
Copy link
Author

Thanks for the replies. Unfortunately I am not familiar enough with Perl/C interface or swig internals to attempt implementing this myself in the near future.

@ojwb ojwb added the Perl label Sep 9, 2016
@ojwb ojwb added the C++ label Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants