aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/swig/Lib/perl5/reference.i
blob: b424c533b1bd4815f349a6340fe6c96562ba5d15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* -----------------------------------------------------------------------------
 * reference.i
 *
 * Accept Perl references as pointers
 * ----------------------------------------------------------------------------- */

/*
The following methods make Perl references work like simple C
pointers.  References can only be used for simple input/output
values, not C arrays however.  It should also be noted that 
REFERENCES are specific to Perl and not supported in other
scripting languages at this time.

         int            *REFERENCE
         short          *REFERENCE
         long           *REFERENCE
         unsigned int   *REFERENCE
         unsigned short *REFERENCE
         unsigned long  *REFERENCE
         unsigned char  *REFERENCE
         float          *REFERENCE
         double         *REFERENCE
         
For example, suppose you were trying to wrap the following function :

        void neg(double *x) {
             *x = -(*x);
        }

You could wrap it with SWIG as follows :

        %include reference.i
        void neg(double *REFERENCE);

or you can use the %apply directive :

        %include reference.i
        %apply double *REFERENCE { double *x };
        void neg(double *x);

Unlike the INOUT mapping described in typemaps.i, this approach directly
modifies the value of a Perl reference.  Thus, you could use it
as follows :

       $x = 3;
       neg(\$x);
       print "$x\n";         # Should print out -3.

*/

%typemap(in) double *REFERENCE (double dvalue), double &REFERENCE(double dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
	printf("Received %d\n", SvTYPE(tempsv));
	SWIG_croak("Expected a double reference.");
  }
  dvalue = SvNV(tempsv);
  $1 = &dvalue;
}

%typemap(in) float *REFERENCE (float dvalue), float &REFERENCE(float dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
    SWIG_croak("expected a double reference");
  }
  dvalue = (float) SvNV(tempsv);
  $1 = &dvalue;
}

%typemap(in) int *REFERENCE (int dvalue), int &REFERENCE (int dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = SvIV(tempsv);
  $1 = &dvalue;
}

%typemap(in) short *REFERENCE (short dvalue), short &REFERENCE(short dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (short) SvIV(tempsv);
  $1 = &dvalue;
}
%typemap(in) long *REFERENCE (long dvalue), long &REFERENCE(long dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (long) SvIV(tempsv);
  $1 = &dvalue;
}
%typemap(in) unsigned int *REFERENCE (unsigned int dvalue), unsigned int &REFERENCE(unsigned int dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (unsigned int) SvUV(tempsv);
  $1 = &dvalue;
}
%typemap(in) unsigned short *REFERENCE (unsigned short dvalue), unsigned short &REFERENCE(unsigned short dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (unsigned short) SvUV(tempsv);
  $1 = &dvalue;
}
%typemap(in) unsigned long *REFERENCE (unsigned long dvalue), unsigned long &REFERENCE(unsigned long dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (unsigned long) SvUV(tempsv);
  $1 = &dvalue;
}

%typemap(in) unsigned char *REFERENCE (unsigned char dvalue), unsigned char &REFERENCE(unsigned char dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (unsigned char) SvUV(tempsv);
  $1 = &dvalue;
}

%typemap(in) signed char *REFERENCE (signed char dvalue), signed char &REFERENCE(signed char dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = (signed char) SvIV(tempsv);
  $1 = &dvalue;
}

%typemap(in) bool *REFERENCE (bool dvalue), bool &REFERENCE(bool dvalue)
{
  SV *tempsv;
  if (!SvROK($input)) {
    SWIG_croak("expected a reference");
  }
  tempsv = SvRV($input);
  if (!SvIOK(tempsv)) {
    SWIG_croak("expected an integer reference");
  }
  dvalue = SvIV(tempsv) ? true : false;
  $1 = &dvalue;
}

%typemap(typecheck) int *REFERENCE, int &REFERENCE,
                    short *REFERENCE, short &REFERENCE,
                    long *REFERENCE, long  &REFERENCE,
                    signed char *REFERENCE, signed char &REFERENCE,
                    bool *REFERENCE, bool &REFERENCE
{
  $1 = SvROK($input) && SvIOK(SvRV($input));
}
%typemap(typecheck) double *REFERENCE, double &REFERENCE,
                    float *REFERENCE, float &REFERENCE
{
  $1 = SvROK($input);
  if($1) {
    SV *tmpsv = SvRV($input);
    $1 = SvNOK(tmpsv) || SvIOK(tmpsv);
  }
}
%typemap(typecheck) unsigned int   *REFERENCE, unsigned int &REFERENCE,
                    unsigned short *REFERENCE, unsigned short &REFERENCE,
                    unsigned long  *REFERENCE, unsigned long &REFERENCE,
                    unsigned char  *REFERENCE, unsigned char &REFERENCE
{
  $1 = SvROK($input);
  if($1) {
    SV *tmpsv = SvRV($input);
    $1 = SvUOK(tmpsv) || SvIOK(tmpsv);
  }
}

%typemap(argout) double *REFERENCE, double &REFERENCE,
                 float  *REFERENCE, float &REFERENCE
{
  SV *tempsv;
  tempsv = SvRV($arg);
  if (!$1) SWIG_croak("expected a reference");
  sv_setnv(tempsv, (double) *$1);
}

%typemap(argout)       int            *REFERENCE, int &REFERENCE,
                       short          *REFERENCE, short &REFERENCE,
                       long           *REFERENCE, long  &REFERENCE,
                       signed char    *REFERENCE, signed char &REFERENCE,
                       bool           *REFERENCE, bool &REFERENCE
{
  SV *tempsv;
  tempsv = SvRV($input);
  if (!$1) SWIG_croak("expected a reference");
  sv_setiv(tempsv, (IV) *$1);
}

%typemap(argout)       unsigned int   *REFERENCE, unsigned int &REFERENCE,
                       unsigned short *REFERENCE, unsigned short &REFERENCE,
                       unsigned long  *REFERENCE, unsigned long &REFERENCE,
                       unsigned char  *REFERENCE, unsigned char &REFERENCE
{
  SV *tempsv;
  tempsv = SvRV($input);
  if (!$1) SWIG_croak("expected a reference");
  sv_setuv(tempsv, (UV) *$1);
}