c0642f966eb639eb2ec850a24c561e740804d4d7
[debian/amanda] / perl / amglue / amglue.h
1 /*
2  * Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as
6  * published by the Free Software Foundation.
7  *
8  * This library 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 Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 #ifndef AMANDA_AMGLUE_H
22 #define AMANDA_AMGLUE_H
23
24 #include "EXTERN.h"
25 #include "perl.h"
26 #include "XSUB.h"
27 #include <glib.h>
28 #include <glib-object.h>
29
30 /* These defines are missing from older glibs, so we add them here */
31 #ifndef G_MAXINT8
32 #define G_MAXINT8 (127)
33 #endif
34
35 #ifndef G_MININT8
36 #define G_MININT8 (-127-1)
37 #endif
38
39 #ifndef G_MAXUINT8
40 #define G_MAXUINT8 (255)
41 #endif
42
43 #ifndef G_MAXINT16
44 #define G_MAXINT16 (32767)
45 #endif
46
47 #ifndef G_MININT16
48 #define G_MININT16 (-32767-1)
49 #endif
50
51 #ifndef G_MAXUINT16
52 #define G_MAXUINT16 (65535)
53 #endif
54
55 #ifndef G_MAXINT32
56 #define G_MAXINT32 (2147483647)
57 #endif
58
59 #ifndef G_MININT32
60 #define G_MININT32 (-2147483647-1)
61 #endif
62
63 #ifndef G_MAXUINT32
64 #define G_MAXUINT32 (4294967295U)
65 #endif
66
67 /*
68  * prototypes for ghashtable.c
69  */
70
71 /* Turn a GLib hash table (mapping strings to strings) into a reference
72  * to a Perl hash table.
73  *
74  * @param hash: GLib hash table
75  * @returns: Perl hashref
76  */
77 SV *g_hash_table_to_hashref(GHashTable *hash);
78
79 /* Turn a GLib hash table (mapping strings to GSList of strings) into a reference
80  * to a Perl hash table.
81  *
82  * @param hash: GLib hash table
83  * @returns: Perl hashref
84  */
85 SV *g_hash_table_to_hashref_gslist(GHashTable *hash);
86
87 /* Turn a GLib hash table (mapping strings to property_t) into a reference
88  * to a Perl hash table.
89  *
90  * @param hash: GLib hash table
91  * @returns: Perl hashref
92  */
93 SV *g_hash_table_to_hashref_property(GHashTable *hash);
94
95 /*
96  * prototypes for bigint.c
97  */
98
99 /*
100  * These functions handle conversion of integers to and from Perl-compatible
101  * values.  Most perls do not natively support 64-bit integers, so these functions
102  * interface with the Math::BigInt module to support those integers.  The functions
103  * also handle conversions from floating-point to integer values, with silent fraction
104  * truncation, as perl automatically promotes integers to doubles on overflow.
105  */
106
107 /* Convert an (unsigned) integer to a Perl SV.  These will always produce a 
108  * Math::BigInt object.  Any failure is fatal.  *All* C-to-Perl integer conversions
109  * must use these functions.
110  *
111  * @param v: value to convert
112  * @returns: pointer to a new SV (refcount=1)
113  */
114 SV *amglue_newSVi64(gint64 v);
115 SV *amglue_newSVu64(guint64 v);
116
117 /* Convert a Perl SV to an integer of the specified size.  These functions should
118  * be used for *all* Perl-to-C integer conversions, since the Perl value may be a
119  * Math::BigInt object.  All of these functions will call croak() on an overflow
120  * condition, rather than silently truncate.
121  *
122  * @param sv: perl value to convert
123  * @returns: value of the given type
124  */
125 gint64 amglue_SvI64(SV *sv);
126 guint64 amglue_SvU64(SV *sv);
127 gint32 amglue_SvI32(SV *sv);
128 guint32 amglue_SvU32(SV *sv);
129 gint16 amglue_SvI16(SV *sv);
130 guint16 amglue_SvU16(SV *sv);
131 gint8 amglue_SvI8(SV *sv);
132 guint8 amglue_SvU8(SV *sv);
133
134 /*
135  * prototypes for source.c
136  */
137
138 typedef enum amglue_Source_state {
139     AMGLUE_SOURCE_NEW,
140     AMGLUE_SOURCE_ATTACHED,
141     AMGLUE_SOURCE_DESTROYED
142 } amglue_Source_state;
143
144 /* There is *one* amglue_Source object for each GSource; this
145  * allows us to attach amglue-related information to the 
146  * GSource.  See amglue/source.c for more detail. */
147
148 typedef struct amglue_Source {
149     GSource *src;
150     GSourceFunc callback;
151     gint refcount;
152     amglue_Source_state state;
153     SV *callback_sv;
154 } amglue_Source;
155
156 /* Get the amglue_Source object associated with this GSource, creating a
157  * new one if necessary, and increment its refcount.
158  *
159  * The 'callback' parameter should be a C function with the
160  * appropriate signature for this GSource.  The callback will
161  * be given the amglue_Source as its 'data' argument, and should
162  * invoke its callback_sv as a Perl sub with the appropriate
163  * parameters.  Simple GSources can use amglue_source_callback_simple,
164  * below.
165  *
166  * This amglue_Source object can be returned directly to perl via a
167  * SWIG binding; it will be bound as an Amanda::MainLoop::Source
168  * object, and its memory management will be handled correctly.
169  *
170  * @param gsrc: the GSource object to wrap
171  * @param callback: function to trigger a perl callback
172  * @returns: an amglue_Source with appropriate refcount
173  */
174 amglue_Source *amglue_source_get(GSource *gsrc, GSourceFunc callback);
175
176 /* Create a new amglue_Source object for this GSource.  Use this when
177  * the GSource was just created and does not yet have a corresponding
178  * amglue_Source.
179  *
180  * @param gsrc: the GSource object to wrap
181  * @param callback: function to trigger a perl callback
182  * @returns: an amglue_Source with appropriate refcount
183  */
184 amglue_Source *amglue_source_new(GSource *gsrc, GSourceFunc callback);
185
186 /* Increment the refcount on an amglue_Source */
187 #define amglue_source_ref(aS) aS->refcount++
188
189 /* Unref an amglue_Source object, freeing it if its refcount reaches
190  * zero.  */
191 #define amglue_source_unref(aS) if (!--(aS)->refcount) amglue_source_free((aS))
192 void amglue_source_free(amglue_Source *);
193
194 #endif /* AMANDA_AMGLUE_H */