3

I have been using PerlXS to write a perl wrapper around a C++ Object. Usually my fcn takes in a string/int etc and I can just make them with no problem. I just write code like this in the .xs file

MyClass::func_a(std::string a, int b);

This time; I have a need to have a function that takes in a stl vector

MyClass::func_a(std::vector<std::string> vector)

I get this error:

conversion from `SV*' to non-scalar type 
  `std::vector<std::string, std::allocator<std::string> >'
osgx
  • 90,338
  • 53
  • 357
  • 513
shergill
  • 3,738
  • 10
  • 41
  • 50

2 Answers2

4

You can't directly call the Native function which takes a STL container. You should write a wrapper and convert SV* manually to STL container.

If You don't know how to do this (like I was), try to use SWIG http://www.swig.org/

It can generate wrappers for a native funciton to use it from scripting languages (including PERL and XS-generator). The code from SWIG is not a very beautiful, also it has some limitations, but it is the easy way to write a wrapper.

SWIG has a limited support of STL builtin: http://www.swig.org/Doc1.3/Library.html#Library_stl_cpp_library

Also, for using PerlXS and vectors, check this thread http://www.mail-archive.com/perl-xs@perl.org/msg00623.html

osgx
  • 90,338
  • 53
  • 357
  • 513
2

Check out XS++.

Some links:

Slides for my talk at YAPC::EU on XS++

The reference docs

Instructions on how to use in a CPAN distribution

tsee
  • 5,034
  • 1
  • 19
  • 27
  • tsee, does XS++ automatically work with complex STL structures, like vector of strings – osgx Dec 19 '10 at 16:51
  • No. But there's a few reusable typemaps available on CPAN. See ExtUtils::Typemap and ExtUtils::Typemap::STL and its siblings. Specifically, vectors of strings are supported. What it doesn't do is automatic, recursive mapping. I.e. "I know how vectors work and how strings work. That means I also know how vectors of vectors of vectors of strings work." – tsee Dec 19 '10 at 22:20