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