c array setitem

View: New views
1 Messages — Rating Filter:   Alert me  

c array setitem

by Pierre Mallard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've been looking for somedays now to find a solution to set element
of a c array in perl in this way :

$array->[$indice] = $value;

Unfortunately as far as I understand SWIG wraps only HASH.
I tryed to use overloading operator "@{}" but could not find the right
solution to retrieve my allocated pointer.

Can anyone help me find what is wrong or how can I achieve my goal another way ?

Thanks a lot,
Pierre

Here is the code, note that overload usage is done in example.pm in
example::INTARRAY class :

Example.pl
==========================================================
my $anintarray = example::INTARRAY->new(4);
$anintarray->[0] = 5;
==========================================================

Example.i
==========================================================
%module example
%include <carrays.i>
%array_class(int,INTARRAY)
==========================================================

Example.pm
===
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.36
#
# Don't modify this file, modify the SWIG interface instead.

package example;
use base qw(Exporter);
package examplec;
package examplec;
boot_example();
package example;
@EXPORT = qw(new_INTARRAY delete_INTARRAY INTARRAY_getitem
INTARRAY_setitem INTARRAY_cast INTARRAY_frompointer );

# ---------- BASE METHODS -------------

package example;

sub TIEHASH {
    my ($classname,$obj) = @_;
    return bless $obj, $classname;
}

sub CLEAR { }

sub FIRSTKEY { }

sub NEXTKEY { }

sub FETCH {
    my ($self,$field) = @_;
    my $member_func = "swig_${field}_get";
    $self->$member_func();
}

sub STORE {
    my ($self,$field,$newval) = @_;
    my $member_func = "swig_${field}_set";
    $self->$member_func($newval);
}

sub this {
    my $ptr = shift;
    return tied(%$ptr);
}


# ------- FUNCTION WRAPPERS --------

package example;


############# Class : example::INTARRAY ##############

package example::INTARRAY;
use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);
@ISA = qw( example );
%OWNER = ();
%ITERATORS = ();
sub new {
    my $pkg = shift;
    my $self = examplec::new_INTARRAY(@_);
    bless $self, $pkg if defined($self);
}

sub DESTROY {
    return unless $_[0]->isa('HASH');
    my $self = tied(%{$_[0]});
    return unless defined $self;
    delete $ITERATORS{$self};
    if (exists $OWNER{$self}) {
        examplec::delete_INTARRAY($self);
        delete $OWNER{$self};
    }
}

*getitem = *examplec::INTARRAY_getitem;
*setitem = *examplec::INTARRAY_setitem;
*cast = *examplec::INTARRAY_cast;
*frompointer = *examplec::INTARRAY_frompointer;
sub DISOWN {
    my $self = shift;
    my $ptr = tied(%$self);
    delete $OWNER{$ptr};
}

sub ACQUIRE {
    my $self = shift;
    my $ptr = tied(%$self);
    $OWNER{$ptr} = 1;
}

######################################
#      ADDED PART for use overload
######################################
use overload fallback => 1, '@{}' => \&getarray;

sub getarray {
    my @a;
    my $self = shift;

    tie @a, ref $self, $self;
    \@a;
}

sub TIEARRAY { my $p = shift; bless \ shift, $p }

sub STORE {
    setitem(@_);
}

######################################
#     END OF ADDED PART for use overload
######################################


# ------- VARIABLE STUBS --------

package example;

1;
=======================================================

--
Pierre Mallard

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user