Imported Upstream version 3.1.0
[debian/amanda] / perl / amglue / amglue.h
1 /*
2  * Copyright (c) 2007,2008,2009 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 #ifndef AMANDA_AMGLUE_H
22 #define AMANDA_AMGLUE_H
23
24 #include "../config/config.h"
25 #include "EXTERN.h"
26 #include "perl.h"
27 #include "XSUB.h"
28 #include <glib.h>
29 #include <glib-object.h>
30
31 /* These defines are missing from older glibs, so we add them here */
32 #ifndef G_MAXINT8
33 #define G_MAXINT8 (127)
34 #endif
35
36 #ifndef G_MININT8
37 #define G_MININT8 (-127-1)
38 #endif
39
40 #ifndef G_MAXUINT8
41 #define G_MAXUINT8 (255)
42 #endif
43
44 #ifndef G_MAXINT16
45 #define G_MAXINT16 (32767)
46 #endif
47
48 #ifndef G_MININT16
49 #define G_MININT16 (-32767-1)
50 #endif
51
52 #ifndef G_MAXUINT16
53 #define G_MAXUINT16 (65535)
54 #endif
55
56 #ifndef G_MAXINT32
57 #define G_MAXINT32 (2147483647)
58 #endif
59
60 #ifndef G_MININT32
61 #define G_MININT32 (-2147483647-1)
62 #endif
63
64 #ifndef G_MAXUINT32
65 #define G_MAXUINT32 (4294967295U)
66 #endif
67
68 /*
69  * prototypes for ghashtable.c
70  */
71
72 /* Turn a GLib hash table (mapping strings to strings) into a reference
73  * to a Perl hash table.
74  *
75  * @param hash: GLib hash table
76  * @returns: Perl hashref
77  */
78 SV *g_hash_table_to_hashref(GHashTable *hash);
79
80 /* Turn a GLib hash table (mapping strings to GSList of strings) into a reference
81  * to a Perl hash table.
82  *
83  * @param hash: GLib hash table
84  * @returns: Perl hashref
85  */
86 SV *g_hash_table_to_hashref_gslist(GHashTable *hash);
87
88 /* Turn a GLib hash table (mapping strings to property_t) into a reference
89  * to a Perl hash table.
90  *
91  * @param hash: GLib hash table
92  * @returns: Perl hashref
93  */
94 SV *g_hash_table_to_hashref_property(GHashTable *hash);
95
96 /*
97  * prototypes for gerror.c
98  */
99
100 /* Call perl's croak (die) for a GError (if there is one)
101  *
102  * @note This is not thread-safe
103  * @note This function does not return if error is non-NULL
104  *
105  * @param domain: String to prefix to error message (followed by ": ")
106  * @param error: The GError pointer
107  */
108 void croak_gerror(const char *domain, GError **error);
109
110 /*
111  * prototypes for bigint.c
112  */
113
114 /*
115  * These functions handle conversion of integers to and from Perl-compatible
116  * values.  Most perls do not natively support 64-bit integers, so these functions
117  * interface with the Math::BigInt module to support those integers.  The functions
118  * also handle conversions from floating-point to integer values, with silent fraction
119  * truncation, as perl automatically promotes integers to doubles on overflow.
120  */
121
122 /* Convert an (unsigned) integer to a Perl SV.  These will always produce a 
123  * Math::BigInt object.  Any failure is fatal.  *All* C-to-Perl integer conversions
124  * must use these functions.
125  *
126  * NOTE - NOTE - NOTE
127  *
128  * Due to the way SWIG constructs return values, *any* outgoing typemap (out or
129  * argout) must use the following syntax:
130  *   SP += argvi; PUTBACK;
131  *   $result = sv_2mortal(amglue_newSVi64(...));
132  *   SPAGAIN; SP -= argvi; argvi++;
133  * This has the effect of saving the arguments added to the perl stack so far, by
134  * setting the global perl stack to a point above them.
135  *
136  * @param v: value to convert
137  * @returns: pointer to a new SV (refcount=1)
138  */
139 SV *amglue_newSVi64(gint64 v);
140 SV *amglue_newSVu64(guint64 v);
141
142 /* Convert a Perl SV to an integer of the specified size.  These functions should
143  * be used for *all* Perl-to-C integer conversions, since the Perl value may be a
144  * Math::BigInt object.  All of these functions will call croak() on an overflow
145  * condition, rather than silently truncate.
146  *
147  * @param sv: perl value to convert
148  * @returns: value of the given type
149  */
150 gint64 amglue_SvI64(SV *sv);
151 guint64 amglue_SvU64(SV *sv);
152 gint32 amglue_SvI32(SV *sv);
153 guint32 amglue_SvU32(SV *sv);
154 gint16 amglue_SvI16(SV *sv);
155 guint16 amglue_SvU16(SV *sv);
156 gint8 amglue_SvI8(SV *sv);
157 guint8 amglue_SvU8(SV *sv);
158
159 /*
160  * prototypes for objwrap.c
161  */
162
163 /* Return a new SV with refcount 1 representing the given C object
164  * with the given class.
165  *
166  * @param c_obj: the object to represent
167  * @param perl_class: the perl with which to bless and tie the SV
168  */
169 SV * new_sv_for_c_obj(gpointer c_obj, const char *perl_class);
170
171 /* Return the C object buried in an SV, asserting that the perl SV is
172  * derived from derived_from.  Returns NULL for undefined perl values.
173  *
174  * This function is based on SWIG's SWIG_Perl_ConvertPtr.  The INT2PTR
175  * situation certainly looks strange, but is documented in perlxs.
176  *
177  * @param sv: the SV to convert
178  * @param derived_from: perl class from which the SV should be derived
179  * @return: underlying pointer
180  */
181 gpointer c_obj_from_sv(SV *sv, const char *derived_from);
182
183 /*
184  * prototypes for xferwrap.c
185  */
186
187 /* declare structs */
188 struct Xfer;
189 struct XferElement;
190
191 /* Return a new SV representing a transfer.
192  *
193  * @param xfer: the transfer to represent
194  */
195 SV *new_sv_for_xfer(struct Xfer *xfer);
196
197 /* Return a new SV representing a transfer element.
198  *
199  * @param xe: the transfer element to represent
200  */
201 SV *new_sv_for_xfer_element(struct XferElement *xe);
202
203 /* Convert an SV to an Xfer.  The Xfer's reference count is not
204  * incremented -- this is a "borrowed" reference.
205  *
206  * @param sv: the perl value
207  * @returns: pointer to the corresponding transfer, or NULL
208  */
209 struct Xfer *xfer_from_sv(SV *sv);
210
211 /* Convert an SV to an XferElement.  The element's reference count is
212  * not incremented -- this is a "borrowed" reference.
213  *
214  * @param sv: the perl value
215  * @returns: pointer to the corresponding transfer element, or NULL.
216  */
217 struct XferElement *xfer_element_from_sv(SV *sv);
218
219 /*
220  * prototypes for source.c
221  */
222
223 typedef enum amglue_Source_state {
224     AMGLUE_SOURCE_NEW,
225     AMGLUE_SOURCE_ATTACHED,
226     AMGLUE_SOURCE_DESTROYED
227 } amglue_Source_state;
228
229 /* There is *one* amglue_Source object for each GSource; this
230  * allows us to attach amglue-related information to the 
231  * GSource.  See amglue/source.c for more detail. */
232
233 typedef struct amglue_Source {
234     GSource *src;
235     GSourceFunc callback;
236     gint refcount;
237     amglue_Source_state state;
238     SV *callback_sv;
239 } amglue_Source;
240
241 /* Get the amglue_Source object associated with this GSource, creating a
242  * new one if necessary, and increment its refcount.
243  *
244  * The 'callback' parameter should be a C function with the
245  * appropriate signature for this GSource.  The callback will
246  * be given the amglue_Source as its 'data' argument, and should
247  * invoke its callback_sv as a Perl sub with the appropriate
248  * parameters.  Simple GSources can use amglue_source_callback_simple,
249  * below.
250  *
251  * This amglue_Source object can be returned directly to perl via a
252  * SWIG binding; it will be bound as an Amanda::MainLoop::Source
253  * object, and its memory management will be handled correctly.
254  *
255  * @param gsrc: the GSource object to wrap
256  * @param callback: function to trigger a perl callback
257  * @returns: an amglue_Source with appropriate refcount
258  */
259 amglue_Source *amglue_source_get(GSource *gsrc, GSourceFunc callback);
260
261 /* Create a new amglue_Source object for this GSource.  Use this when
262  * the GSource was just created and does not yet have a corresponding
263  * amglue_Source.
264  *
265  * @param gsrc: the GSource object to wrap
266  * @param callback: function to trigger a perl callback
267  * @returns: an amglue_Source with appropriate refcount
268  */
269 amglue_Source *amglue_source_new(GSource *gsrc, GSourceFunc callback);
270
271 /* Increment the refcount on an amglue_Source */
272 #define amglue_source_ref(aS) aS->refcount++
273
274 /* Unref an amglue_Source object, freeing it if its refcount reaches
275  * zero.  */
276 #define amglue_source_unref(aS) if (!--(aS)->refcount) amglue_source_free((aS))
277 void amglue_source_free(amglue_Source *);
278
279 #endif /* AMANDA_AMGLUE_H */