updated to use LBV::XS, not LBV
[language-befunge-vector-xs.git] / XS.xs
blob68e390b665950ceb466bd7703039147e64348a05
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
5 #include "ppport.h"
7 #include "const-c.inc"
9 typedef int intArray;
10 void* intArrayPtr(int num) {
11     SV* mortal;
12     mortal = sv_2mortal( NEWSV(0, num * sizeof(intArray)) );
13     return SvPVX(mortal);
17 MODULE = Language::Befunge::Vector::XS          PACKAGE = Language::Befunge::Vector::XS
19 INCLUDE: const-xs.inc
21 #-- CONSTRUCTORS
24 # my $vec = LB::Vector->new( $x [, $y, ...] );
26 # Create a new vector. The arguments are the actual vector data; one
27 # integer per dimension.
29 SV *
30 new( class, array, ... )
31         char*       class;
32         intArray*   array
34         INIT:
35             AV*  self;
36             I32  i;
37             SV*  val;
38             HV*  stash;
40         CODE:
41             /* create the object and populate it */
42             self = newAV();
43             for ( i=0; i<ix_array; i++ ) {
44                 val = newSViv( array[i] );
45                 av_push(self, val);
46             }
48             /* Return a blessed reference to the AV */
49             RETVAL = newRV_noinc( (SV *)self );
50             stash  = gv_stashpv( class, TRUE );
51             sv_bless( (SV *)RETVAL, stash );
53         OUTPUT:
54             RETVAL
58 # my $vec = Language::Befunge::Vector::XS->new_zeroes( $dims );
60 # Create a new vector, set to the origin. The only argument is the dimension of
61 # the vector to be created.
63 # ->new_zeroes(2) is exactly equivalent to ->new([0,0])
65 SV *
66 new_zeroes( class, dimension )
67         char* class;
68         I32   dimension;
70         INIT:
71             AV*  self;
72             I32  i;
73             SV*  zero;
74             HV*  stash;
76         CODE:
77             /* create the object and populate it */
78             self = newAV();
79             for ( i=0; i<dimension; i++ ) {
80                 zero = newSViv(0);
81                 av_push(self, zero);
82             }
84             /* return a blessed reference to the AV */
85             RETVAL = newRV_noinc( (SV *)self );
86             stash  = gv_stashpv( class, TRUE );
87             sv_bless( (SV *)RETVAL, stash );
89         OUTPUT:
90             RETVAL
93 #-- PUBLIC METHODS
97 # my $dims = $vec->get_dims;
99 # Return the number of dimensions, an integer.
102 get_dims( self )
103         AV* self
105     CODE:
106         RETVAL = av_len(self) + 1;
108     OUTPUT:
109         RETVAL
112 # my $val = $vec->get_component( $index );
114 # Return the value for dimension $index.
117 get_component( self, index )
118         AV* self
119         I32 index
121     INIT:
122         SV** val;
124     CODE:
125         val = av_fetch(self, index, 0);
126         RETVAL = SvIV(*val);
128     OUTPUT:
129         RETVAL