I ended up providing the following interface:
==
my $ob = StringToIntMap->new();
$ob->reset_iterator();
while ( my ($key, $value) = $ob->iterate_next()) {
}
===meaning the XS class keeps the STL iterator as a private member variable, and there can only be one iterator active (which is OK for me). Thanks for all your comments. -gordon Gabor Szabo wrote, On 07/11/2010 01:40 AM: > Personally I prefer the original iterator version Assaf provided. > The tied array will give the feeling that you can easily access > arbitrary element. > > The only thing I'd consider doing differently is uniting > the 'new' call and the 'begin' call. > > regards > Gabor > > > On Sat, Jul 10, 2010 at 10:39 PM, Shmuel Fomberg <[email protected]> wrote: >> Hi Assaf. >> >> Maybe you should try sending a tied array, so that >> for my $rec (@array) {...} >> will work. (hopefully Perl won't pre-fatch all the items...) >> >> See the documentation for the 'tie' command to see which methods you >> need to implement, and Tie::Array for some shortcuts. >> >> Shmuel. >> >> >> Assaf Gordon wrote: >>> Hello, >>> >>> I've have an XS module that exports a C++ class. >>> Following example EX7 in Dean Roehrich's CookBookA [1], my class/package >>> compiles and works fine. >>> >>> Now, >>> In the XS/CPP class, I use an STL container to store some information. >>> What would be the most perl-like way to allow the Perl programmer to >>> iterate over all the elements in my CPP class ? >>> >>> For the sake of this argument, >>> Let's assume the container is a simple "std::list<int>" - how do expose >>> this to the Perl programmer ? >>> I couldn't find a perl typemap that would go with >>> std::list<int>::const_itearator, so exposing begin()/end() doesn't work >>> (perhaps I missed something, online examples are scarce with XS+CPP+STL). >>> >>> Another complication: >>> I can't return an perl list/array containing all the elements (there are >>> too many elements) - I need to iterate them in a perl loop. >>> >>> I'd imagine something like: >>> === >>> my $obj = MyClass->new(); >>> my $iter = $obj->begin(); >>> while ( my $value = $obj->get_next($iter) ) { >>> # Do something with $value >>> } >>> === >>> >>> I realize it's not a very perlish style, so any ideas and suggestion would >>> be appropriated. >>> >>> >>> Thanks, >>> -gordon >>> >>> >>> >>> [1] >>> http://search.cpan.org/~dmr/CookBookA-19960430/ > _______________________________________________ > Perl mailing list > [email protected] > http://mail.perl.org.il/mailman/listinfo/perl _______________________________________________ Perl mailing list [email protected] http://mail.perl.org.il/mailman/listinfo/perl
