Imported Upstream version 3.3.3
[debian/amanda] / perl / amglue / integers.swg
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %{
23 #include "amglue.h"
24 %}
25
26 /*
27  * perl -> C (input)
28  */
29
30 %typemap(in) guint64 {
31     $1 = amglue_SvU64($input);
32 }
33
34 %typemap(in) gint64 {
35     $1 = amglue_SvI64($input);
36 }
37
38 %typemap(in) guint32 {
39     $1 = amglue_SvU32($input);
40 }
41
42 %typemap(in) gint32 {
43     $1 = amglue_SvI32($input);
44 }
45
46 %typemap(in) guint16 {
47     $1 = amglue_SvU16($input);
48 }
49
50 %typemap(in) gint16 {
51     $1 = amglue_SvI16($input);
52 }
53
54 %typemap(in) guint8 {
55     $1 = amglue_SvU8($input);
56 }
57
58 %typemap(in) gint8 {
59     $1 = amglue_SvI8($input);
60 }
61
62 /* remaining types depend on the complier to optimize out constant sizeof() 
63  * expressions.  The SWIG preprocessor can't perform calculations based on 
64  * sizeof(). */
65
66 %define typemap_in_unsigned(type)
67 %typemap(in) type {
68     if (sizeof(type) == 1) {
69         $1 = amglue_SvU8($input);
70     } else if (sizeof(type) == 2) {
71         $1 = amglue_SvU16($input);
72     } else if (sizeof(type) == 4) {
73         $1 = amglue_SvU32($input);
74     } else if (sizeof(type) == 8) {
75         $1 = amglue_SvU64($input);
76     } else {
77         croak("Unexpected type >64 bits?"); /* should be optimized out unless sizeof(type) > 8 */
78     }
79 }
80 %enddef
81
82 typemap_in_unsigned(unsigned int)
83 typemap_in_unsigned(guint)
84 typemap_in_unsigned(unsigned short)
85 typemap_in_unsigned(gushort)
86 typemap_in_unsigned(size_t)
87 typemap_in_unsigned(gsize)
88 typemap_in_unsigned(unsigned long long)
89 typemap_in_unsigned(unsigned long)
90 typemap_in_unsigned(gulong)
91 typemap_in_unsigned(off_t)
92 typemap_in_unsigned(ptrdiff_t)
93 typemap_in_unsigned(uintmax_t)
94
95 %define typemap_in_signed(type)
96 %typemap(in) type {
97     if (sizeof(type) == 1) {
98         $1 = amglue_SvI8($input);
99     } else if (sizeof(type) == 2) {
100         $1 = amglue_SvI16($input);
101     } else if (sizeof(type) == 4) {
102         $1 = amglue_SvI32($input);
103     } else if (sizeof(type) == 8) {
104         $1 = amglue_SvI64($input);
105     } else {
106         g_critical("Unexpected type >64 bits?"); /* should be optimized out unless sizeof(type) > 8 */
107     }
108 }
109 %enddef
110
111 typemap_in_signed(int)
112 typemap_in_signed(gint)
113 typemap_in_signed(signed int)
114 typemap_in_signed(short)
115 typemap_in_signed(gshort)
116 typemap_in_signed(signed short)
117 typemap_in_signed(ssize_t)
118 typemap_in_signed(gssize)
119 typemap_in_signed(long long)
120 typemap_in_signed(signed long long)
121 typemap_in_signed(long)
122 typemap_in_signed(signed long)
123 typemap_in_signed(glong)
124 typemap_in_signed(intmax_t)
125
126 /*
127  * C -> perl (output)
128  */
129
130 /* All conversions from C to Perl create Math::BigInt objects, even when the
131  * C datatype is 32 bits or smaller.  This is to ensure that Perl's automatic
132  * promotion to double does not silently corrupt arithmetic on large numbers.
133  *
134  * The complex typemaps here are to ensure that the argument stack is protected
135  * against stomping by amglue_newSV*64, which may invoke a significant amount
136  * of perl code.  "SP += argvi; PUTBACK;" increments the global stack pointer
137  * to cover the arguments processed so far, while "SPAGAIN; SP -= argvi;"
138  * restores the local stack pointer.  The latter must be done before the newest
139  * argument is added to the stack.  This whole process is a hack around SWIG's
140  * habit of invoking (out) typemaps while building the stack, instead of doing
141  * so in advance.
142  */
143
144 /* (these all use newSV*64, relying on C to upcast to a 64-bit integer) */
145
146 %define typemap_out_unsigned(type)
147 %typemap(out) type {
148     SV *for_stack;
149     SP += argvi; PUTBACK;
150     for_stack = sv_2mortal(amglue_newSVu64($1));
151     SPAGAIN; SP -= argvi;
152     $result = for_stack;
153     argvi++;
154 }
155 %enddef
156
157 typemap_out_unsigned(guint64)
158 typemap_out_unsigned(guint32)
159 typemap_out_unsigned(guint16)
160 typemap_out_unsigned(guint8)
161 typemap_out_unsigned(unsigned int)
162 typemap_out_unsigned(guint)
163 typemap_out_unsigned(unsigned short)
164 typemap_out_unsigned(gushort)
165 typemap_out_unsigned(size_t)
166 typemap_out_unsigned(gsize)
167 typemap_out_unsigned(unsigned long long)
168 typemap_out_unsigned(unsigned long)
169 typemap_out_unsigned(gulong)
170 typemap_out_unsigned(off_t)
171 typemap_out_unsigned(ptrdiff_t)
172 typemap_out_unsigned(uintmax_t)
173
174 %define typemap_out_signed(type)
175 %typemap(out) type {
176     SV *for_stack;
177     SP += argvi; PUTBACK;
178     for_stack = sv_2mortal(amglue_newSVi64($1));
179     SPAGAIN; SP -= argvi;
180     $result = for_stack;
181     argvi++;
182 }
183 %enddef
184
185 typemap_out_signed(gint64)
186 typemap_out_signed(gint32)
187 typemap_out_signed(gint16)
188 typemap_out_signed(gint8)
189 typemap_out_signed(int)
190 typemap_out_signed(gint)
191 typemap_out_signed(signed int)
192 typemap_out_signed(short)
193 typemap_out_signed(gshort)
194 typemap_out_signed(signed short)
195 typemap_out_signed(ssize_t)
196 typemap_out_signed(gssize)
197 typemap_out_signed(long long)
198 typemap_out_signed(signed long long)
199 typemap_out_signed(long)
200 typemap_out_signed(signed long)
201 typemap_out_signed(glong)
202 typemap_out_signed(intmax_t)