1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * Accept Perl references as pointers
8 * ----------------------------------------------------------------------------- */
11 The following methods make Perl references work like simple C
12 pointers. References can only be used for simple input/output
13 values, not C arrays however. It should also be noted that
14 REFERENCES are specific to Perl and not supported in other
15 scripting languages at this time.
20 unsigned int *REFERENCE
21 unsigned short *REFERENCE
22 unsigned long *REFERENCE
23 unsigned char *REFERENCE
27 For example, suppose you were trying to wrap the following function :
33 You could wrap it with SWIG as follows :
36 void neg(double *REFERENCE);
38 or you can use the %apply directive :
41 %apply double *REFERENCE { double *x };
44 Unlike the INOUT mapping described in typemaps.i, this approach directly
45 modifies the value of a Perl reference. Thus, you could use it
50 print "$x\n"; # Should print out -3.
54 %typemap(in) double *REFERENCE (double dvalue), double &REFERENCE(double dvalue)
58 SWIG_croak("expected a reference");
60 tempsv = SvRV($input);
61 if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
62 printf("Received %d\n", SvTYPE(tempsv));
63 SWIG_croak("Expected a double reference.");
65 dvalue = SvNV(tempsv);
69 %typemap(in) float *REFERENCE (float dvalue), float &REFERENCE(float dvalue)
73 SWIG_croak("expected a reference");
75 tempsv = SvRV($input);
76 if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
77 SWIG_croak("expected a double reference");
79 dvalue = (float) SvNV(tempsv);
83 %typemap(in) int *REFERENCE (int dvalue), int &REFERENCE (int dvalue)
87 SWIG_croak("expected a reference");
89 tempsv = SvRV($input);
91 SWIG_croak("expected a integer reference");
93 dvalue = SvIV(tempsv);
97 %typemap(in) short *REFERENCE (short dvalue), short &REFERENCE(short dvalue)
100 if (!SvROK($input)) {
101 SWIG_croak("expected a reference");
103 tempsv = SvRV($input);
104 if (!SvIOK(tempsv)) {
105 SWIG_croak("expected a integer reference");
107 dvalue = (short) SvIV(tempsv);
110 %typemap(in) long *REFERENCE (long dvalue), long &REFERENCE(long dvalue)
113 if (!SvROK($input)) {
114 SWIG_croak("expected a reference");
116 tempsv = SvRV($input);
117 if (!SvIOK(tempsv)) {
118 SWIG_croak("expected a integer reference");
120 dvalue = (long) SvIV(tempsv);
123 %typemap(in) unsigned int *REFERENCE (unsigned int dvalue), unsigned int &REFERENCE(unsigned int dvalue)
126 if (!SvROK($input)) {
127 SWIG_croak("expected a reference");
129 tempsv = SvRV($input);
130 if (!SvIOK(tempsv)) {
131 SWIG_croak("expected a integer reference");
133 dvalue = (unsigned int) SvUV(tempsv);
136 %typemap(in) unsigned short *REFERENCE (unsigned short dvalue), unsigned short &REFERENCE(unsigned short dvalue)
139 if (!SvROK($input)) {
140 SWIG_croak("expected a reference");
142 tempsv = SvRV($input);
143 if (!SvIOK(tempsv)) {
144 SWIG_croak("expected a integer reference");
146 dvalue = (unsigned short) SvUV(tempsv);
149 %typemap(in) unsigned long *REFERENCE (unsigned long dvalue), unsigned long &REFERENCE(unsigned long dvalue)
152 if (!SvROK($input)) {
153 SWIG_croak("expected a reference");
155 tempsv = SvRV($input);
156 if (!SvIOK(tempsv)) {
157 SWIG_croak("expected a integer reference");
159 dvalue = (unsigned long) SvUV(tempsv);
163 %typemap(in) unsigned char *REFERENCE (unsigned char dvalue), unsigned char &REFERENCE(unsigned char dvalue)
166 if (!SvROK($input)) {
167 SWIG_croak("expected a reference");
169 tempsv = SvRV($input);
170 if (!SvIOK(tempsv)) {
171 SWIG_croak("expected a integer reference");
173 dvalue = (unsigned char) SvUV(tempsv);
177 %typemap(in) signed char *REFERENCE (signed char dvalue), signed char &REFERENCE(signed char dvalue)
180 if (!SvROK($input)) {
181 SWIG_croak("expected a reference");
183 tempsv = SvRV($input);
184 if (!SvIOK(tempsv)) {
185 SWIG_croak("expected a integer reference");
187 dvalue = (signed char) SvIV(tempsv);
191 %typemap(in) bool *REFERENCE (bool dvalue), bool &REFERENCE(bool dvalue)
194 if (!SvROK($input)) {
195 SWIG_croak("expected a reference");
197 tempsv = SvRV($input);
198 if (!SvIOK(tempsv)) {
199 SWIG_croak("expected a integer reference");
201 dvalue = (bool) SvIV(tempsv);
205 %typemap(typecheck) int *REFERENCE, int &REFERENCE,
206 short *REFERENCE, short &REFERENCE,
207 long *REFERENCE, long &REFERENCE,
208 signed char *REFERENCE, signed char &REFERENCE,
209 bool *REFERENCE, bool &REFERENCE
211 $1 = SvROK($input) && SvIOK(SvRV($input));
213 %typemap(typecheck) double *REFERENCE, double &REFERENCE,
214 float *REFERENCE, float &REFERENCE
218 SV *tmpsv = SvRV($input);
219 $1 = SvNOK(tmpsv) || SvIOK(tmpsv);
222 %typemap(typecheck) unsigned int *REFERENCE, unsigned int &REFERENCE,
223 unsigned short *REFERENCE, unsigned short &REFERENCE,
224 unsigned long *REFERENCE, unsigned long &REFERENCE,
225 unsigned char *REFERENCE, unsigned char &REFERENCE
229 SV *tmpsv = SvRV($input);
230 $1 = SvUOK(tmpsv) || SvIOK(tmpsv);
234 %typemap(argout) double *REFERENCE, double &REFERENCE,
235 float *REFERENCE, float &REFERENCE
239 if (!$1) SWIG_croak("expected a reference");
240 sv_setnv(tempsv, (double) *$1);
243 %typemap(argout) int *REFERENCE, int &REFERENCE,
244 short *REFERENCE, short &REFERENCE,
245 long *REFERENCE, long &REFERENCE,
246 signed char *REFERENCE, signed char &REFERENCE,
247 bool *REFERENCE, bool &REFERENCE
250 tempsv = SvRV($input);
251 if (!$1) SWIG_croak("expected a reference");
252 sv_setiv(tempsv, (IV) *$1);
255 %typemap(argout) unsigned int *REFERENCE, unsigned int &REFERENCE,
256 unsigned short *REFERENCE, unsigned short &REFERENCE,
257 unsigned long *REFERENCE, unsigned long &REFERENCE,
258 unsigned char *REFERENCE, unsigned char &REFERENCE
261 tempsv = SvRV($input);
262 if (!$1) SWIG_croak("expected a reference");
263 sv_setuv(tempsv, (UV) *$1);