Hi Ian,

On Mon, 21 Oct 2002 19:57:34 +0000
[EMAIL PROTECTED] wrote:
> I'm trying to write some interface routines to the
> Numerical Recipes library and want to be able to pass
> Perl arrays (or references) to C++ vector<...> objects,
> and vice versa.

The xsubpp compiler doesn't support C++'s template facilities.
We need to define typemaps for each template instance as follows:

use Inline CPP => << 'EOC';
#include <string>
#include <vector>
#include <iostream>
using namespace std;

typedef vector<int> IntVector;
typedef vector<string> StringVector;

void test_int_vector(IntVector vec) {
    IntVector::iterator iter;
    for(iter = vec.begin(); iter != vec.end(); ++iter)
        cout << *iter << ' ';
    cout << endl;
}
void test_str_vector(StringVector vec) {
    StringVector::iterator iter;
    for(iter = vec.begin(); iter != vec.end(); ++iter)
        cout << *iter << ' ';
    cout << endl;
}
EOC

test_int_vector([0..9]);
test_str_vector([qw(foo bar baz)]);


# typemap file:
IntVector           T_INT_VETOR
StringVector        T_STR_VECTOR

INPUT
T_INT_VETOR
        if (SvROK($arg) && SvTYPE(SvRV($arg))==SVt_PVAV) {
            AV *avref = (AV*)SvRV($arg);
            int len = av_len(avref) + 1;
            for(int i = 0; i < len; ++i) {
                SV **elem = av_fetch(avref, i, 0);
                if( elem == NULL )
                    $var.push_back(0);
                else
                    $var.push_back(SvIV(*elem));
            }
        }
        else
            Perl_croak(aTHX_ \"$var is not an array reference\")

T_STR_VECTOR
        if (SvROK($arg) && SvTYPE(SvRV($arg))==SVt_PVAV) {
            AV *avref = (AV*)SvRV($arg);
            int len = av_len(avref) + 1;
            for(int i = 0; i < len; ++i) {
                SV **elem = av_fetch(avref, i, 0);
                if( elem == NULL )
                    $var.push_back(0);
                else
                    $var.push_back(SvPV_nolen(*elem));
            }
        }
        else
            Perl_croak(aTHX_ \"$var is not an array reference\")


> This seems like it should be an obvious mapping, but
> several days of scouring the web and newsgroups have
> turned up empty.  About two years ago there was a post to
> this mailing list about a set of typemap definitions for
> STL containers, but the code never appeared.  Has anyone
> since been able to write a good typemap definition for
> these conversions?
> 
> If what I want would be better accomplished by
> hand-coding in an XSUB or somesuch, that would be fine too...
> 
> Regards,
> Ian Barton

--
SH
[EMAIL PROTECTED]

Reply via email to