authoring tests
[language-befunge-vector-xs.git] / XS.xs
blobf98361f01f508e14bd1d2df7b5c4623518ec6420
1 /*
3 # This file is part of Language::Befunge::Vector::XS.
4 # Copyright (c) 2008 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
17 #include "ppport.h"
19 typedef int intArray;
20 void* intArrayPtr(int num) {
21     SV* mortal;
22     mortal = sv_2mortal( NEWSV(0, num * sizeof(intArray)) );
23     return SvPVX(mortal);
27 MODULE = Language::Befunge::Vector::XS          PACKAGE = Language::Befunge::Vector::XS
30 #-- CONSTRUCTORS
33 # my $vec = LB::Vector->new( $x [, $y, ...] );
35 # Create a new vector. The arguments are the actual vector data; one
36 # integer per dimension.
38 SV *
39 new( class, array, ... )
40         char*       class;
41         intArray*   array
43         INIT:
44             AV*  self;
45             I32  i;
46             SV*  val;
47             HV*  stash;
49         CODE:
50             /* create the object and populate it */
51             self = newAV();
52             for ( i=0; i<ix_array; i++ ) {
53                 val = newSViv( array[i] );
54                 av_push(self, val);
55             }
57             /* Return a blessed reference to the AV */
58             RETVAL = newRV_noinc( (SV *)self );
59             stash  = gv_stashpv( class, TRUE );
60             sv_bless( (SV *)RETVAL, stash );
62         OUTPUT:
63             RETVAL
67 # my $vec = Language::Befunge::Vector::XS->new_zeroes( $dims );
69 # Create a new vector, set to the origin. The only argument is the dimension of
70 # the vector to be created.
72 # ->new_zeroes(2) is exactly equivalent to ->new([0,0])
74 SV *
75 new_zeroes( class, dimension )
76         char* class;
77         I32   dimension;
79         INIT:
80             AV*  self;
81             I32  i;
82             SV*  zero;
83             HV*  stash;
85         CODE:
86             /* create the object and populate it */
87             self = newAV();
88             for ( i=0; i<dimension; i++ ) {
89                 zero = newSViv(0);
90                 av_push(self, zero);
91             }
93             /* return a blessed reference to the AV */
94             RETVAL = newRV_noinc( (SV *)self );
95             stash  = gv_stashpv( class, TRUE );
96             sv_bless( (SV *)RETVAL, stash );
98         OUTPUT:
99             RETVAL
102 #-- PUBLIC METHODS
106 # my $dims = $vec->get_dims;
108 # Return the number of dimensions, an integer.
111 get_dims( self )
112         AV* self
114     CODE:
115         RETVAL = av_len(self) + 1;
117     OUTPUT:
118         RETVAL
121 # my $val = $vec->get_component( $index );
123 # Return the value for dimension $index.
126 get_component( self, index )
127         AV* self
128         I32 index
130     INIT:
131         SV** val;
133     CODE:
134         val = av_fetch(self, index, 0);
135         RETVAL = SvIV(*val);
137     OUTPUT:
138         RETVAL