Temporary version hack
[libvirt-python/ericb.git] / typewrappers.h
blob486017a74121f30b3bdcd2bae0be38225e92c0f0
1 /*
2 * libvirt_wrap.h: type wrappers for libvir python bindings
4 * Copyright (C) 2005, 2011-2012 Red Hat, Inc.
6 * Daniel Veillard <veillard@redhat.com>
7 */
9 #include <Python.h>
10 #include <stdbool.h>
11 #include <libvirt/libvirt.h>
12 #include <libvirt/virterror.h>
14 #ifdef __GNUC__
15 # ifdef ATTRIBUTE_UNUSED
16 # undef ATTRIBUTE_UNUSED
17 # endif
18 # ifndef ATTRIBUTE_UNUSED
19 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
20 # endif /* ATTRIBUTE_UNUSED */
21 #else
22 # define ATTRIBUTE_UNUSED
23 #endif
25 /* Work around really old python. */
26 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
27 typedef ssize_t Py_ssize_t;
28 #endif
30 #if !LIBVIR_CHECK_VERSION(4, 5, 0)
31 typedef struct _virNWFilterBinding *virNWFilterBindingPtr;
32 #endif
33 #if !LIBVIR_CHECK_VERSION(5, 5, 0)
34 typedef struct _virNetworkPort *virNetworkPortPtr;
35 #endif
36 #if !LIBVIR_CHECK_VERSION(5, 6, 0)
37 typedef struct _virDomainCheckpoint *virDomainCheckpointPtr;
38 #endif
40 #define PyvirConnect_Get(v) (((v) == Py_None) ? NULL : \
41 (((PyvirConnect_Object *)(v))->obj))
43 typedef struct {
44 PyObject_HEAD
45 virConnectPtr obj;
46 } PyvirConnect_Object;
49 #define PyvirDomain_Get(v) (((v) == Py_None) ? NULL : \
50 (((PyvirDomain_Object *)(v))->obj))
52 typedef struct {
53 PyObject_HEAD
54 virDomainPtr obj;
55 } PyvirDomain_Object;
58 #define PyvirNetwork_Get(v) (((v) == Py_None) ? NULL : \
59 (((PyvirNetwork_Object *)(v))->obj))
61 typedef struct {
62 PyObject_HEAD
63 virNetworkPtr obj;
64 } PyvirNetwork_Object;
67 #define PyvirNetworkPort_Get(v) (((v) == Py_None) ? NULL : \
68 (((PyvirNetworkPort_Object *)(v))->obj))
70 typedef struct {
71 PyObject_HEAD
72 virNetworkPortPtr obj;
73 } PyvirNetworkPort_Object;
76 #define PyvirInterface_Get(v) (((v) == Py_None) ? NULL : \
77 (((PyvirInterface_Object *)(v))->obj))
79 typedef struct {
80 PyObject_HEAD
81 virInterfacePtr obj;
82 } PyvirInterface_Object;
85 #define PyvirStoragePool_Get(v) (((v) == Py_None) ? NULL : \
86 (((PyvirStoragePool_Object *)(v))->obj))
88 typedef struct {
89 PyObject_HEAD
90 virStoragePoolPtr obj;
91 } PyvirStoragePool_Object;
94 #define PyvirStorageVol_Get(v) (((v) == Py_None) ? NULL : \
95 (((PyvirStorageVol_Object *)(v))->obj))
97 typedef struct {
98 PyObject_HEAD
99 virStorageVolPtr obj;
100 } PyvirStorageVol_Object;
103 #define PyvirNodeDevice_Get(v) (((v) == Py_None) ? NULL : \
104 (((PyvirNodeDevice_Object *)(v))->obj))
106 typedef struct {
107 PyObject_HEAD
108 virNodeDevicePtr obj;
109 } PyvirNodeDevice_Object;
111 #define PyvirSecret_Get(v) (((v) == Py_None) ? NULL : \
112 (((PyvirSecret_Object *)(v))->obj))
114 typedef struct {
115 PyObject_HEAD
116 virSecretPtr obj;
117 } PyvirSecret_Object;
119 #define PyvirNWFilter_Get(v) (((v) == Py_None) ? NULL : \
120 (((PyvirNWFilter_Object *)(v))->obj))
122 typedef struct {
123 PyObject_HEAD
124 virNWFilterPtr obj;
125 } PyvirNWFilter_Object;
128 #define PyvirNWFilterBinding_Get(v) (((v) == Py_None) ? NULL : \
129 (((PyvirNWFilterBinding_Object *)(v))->obj))
131 typedef struct {
132 PyObject_HEAD
133 virNWFilterBindingPtr obj;
134 } PyvirNWFilterBinding_Object;
137 #define PyvirStream_Get(v) (((v) == Py_None) ? NULL : \
138 (((PyvirStream_Object *)(v))->obj))
140 typedef struct {
141 PyObject_HEAD
142 virStreamPtr obj;
143 } PyvirStream_Object;
146 #define PyvirDomainCheckpoint_Get(v) (((v) == Py_None) ? NULL : \
147 (((PyvirDomainCheckpoint_Object *)(v))->obj))
149 typedef struct {
150 PyObject_HEAD
151 virDomainCheckpointPtr obj;
152 } PyvirDomainCheckpoint_Object;
155 #define PyvirDomainSnapshot_Get(v) (((v) == Py_None) ? NULL : \
156 (((PyvirDomainSnapshot_Object *)(v))->obj))
158 typedef struct {
159 PyObject_HEAD
160 virDomainSnapshotPtr obj;
161 } PyvirDomainSnapshot_Object;
164 #define PyvirEventHandleCallback_Get(v) (((v) == Py_None) ? NULL : \
165 (((PyvirEventHandleCallback_Object *)(v))->obj))
167 typedef struct {
168 PyObject_HEAD
169 virEventHandleCallback obj;
170 } PyvirEventHandleCallback_Object;
172 #define PyvirEventTimeoutCallback_Get(v) (((v) == Py_None) ? NULL : \
173 (((PyvirEventTimeoutCallback_Object *)(v))->obj))
175 typedef struct {
176 PyObject_HEAD
177 virEventTimeoutCallback obj;
178 } PyvirEventTimeoutCallback_Object;
180 #define PyvirFreeCallback_Get(v) (((v) == Py_None) ? NULL : \
181 (((PyvirFreeCallback_Object *)(v))->obj))
183 typedef struct {
184 PyObject_HEAD
185 virFreeCallback obj;
186 } PyvirFreeCallback_Object;
188 #define PyvirVoidPtr_Get(v) (((v) == Py_None) ? NULL : \
189 (((PyvirVoidPtr_Object *)(v))->obj))
191 typedef struct {
192 PyObject_HEAD
193 void* obj;
194 } PyvirVoidPtr_Object;
196 PyObject * libvirt_intWrap(int val);
197 PyObject * libvirt_uintWrap(uint val);
198 PyObject * libvirt_longWrap(long val);
199 PyObject * libvirt_ulongWrap(unsigned long val);
200 PyObject * libvirt_longlongWrap(long long val);
201 PyObject * libvirt_ulonglongWrap(unsigned long long val);
202 PyObject * libvirt_charPtrWrap(char *str);
203 PyObject * libvirt_charPtrSizeWrap(char *str, Py_ssize_t size);
204 PyObject * libvirt_constcharPtrWrap(const char *str);
205 PyObject * libvirt_boolWrap(int val);
206 int libvirt_intUnwrap(PyObject *obj, int *val);
207 int libvirt_uintUnwrap(PyObject *obj, unsigned int *val);
208 int libvirt_longUnwrap(PyObject *obj, long *val);
209 int libvirt_ulongUnwrap(PyObject *obj, unsigned long *val);
210 int libvirt_longlongUnwrap(PyObject *obj, long long *val);
211 int libvirt_ulonglongUnwrap(PyObject *obj, unsigned long long *val);
212 int libvirt_doubleUnwrap(PyObject *obj, double *val);
213 int libvirt_boolUnwrap(PyObject *obj, bool *val);
214 int libvirt_charPtrUnwrap(PyObject *obj, char **str);
215 int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size);
216 PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
217 PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
218 PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
219 PyObject * libvirt_virNetworkPortPtrWrap(virNetworkPortPtr node);
220 PyObject * libvirt_virInterfacePtrWrap(virInterfacePtr node);
221 PyObject * libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node);
222 PyObject * libvirt_virStorageVolPtrWrap(virStorageVolPtr node);
223 PyObject * libvirt_virEventHandleCallbackWrap(virEventHandleCallback node);
224 PyObject * libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node);
225 PyObject * libvirt_virFreeCallbackWrap(virFreeCallback node);
226 PyObject * libvirt_virVoidPtrWrap(void* node);
227 PyObject * libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node);
228 PyObject * libvirt_virSecretPtrWrap(virSecretPtr node);
229 PyObject * libvirt_virNWFilterPtrWrap(virNWFilterPtr node);
230 PyObject * libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node);
231 PyObject * libvirt_virStreamPtrWrap(virStreamPtr node);
232 PyObject * libvirt_virDomainCheckpointPtrWrap(virDomainCheckpointPtr node);
233 PyObject * libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node);
236 /* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
237 * LIBVIRT_STMT_START { statements; } LIBVIRT_STMT_END;
238 * can be used as a single statement, as in
239 * if (x) LIBVIRT_STMT_START { ... } LIBVIRT_STMT_END; else ...
241 * When GCC is compiling C code in non-ANSI mode, it will use the
242 * compiler __extension__ to wrap the statements within `({' and '})' braces.
243 * When compiling on platforms where configure has defined
244 * HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
245 * For any other platforms (SunOS4 is known to have this issue), wrap the
246 * statements with `if (1)' and `else (void) 0'.
248 #if !(defined (LIBVIRT_STMT_START) && defined (LIBVIRT_STMT_END))
249 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
250 # define LIBVIRT_STMT_START (void) __extension__ (
251 # define LIBVIRT_STMT_END )
252 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
253 # if defined (HAVE_DOWHILE_MACROS)
254 # define LIBVIRT_STMT_START do
255 # define LIBVIRT_STMT_END while (0)
256 # else /* !HAVE_DOWHILE_MACROS */
257 # define LIBVIRT_STMT_START if (1)
258 # define LIBVIRT_STMT_END else (void) 0
259 # endif /* !HAVE_DOWHILE_MACROS */
260 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
261 #endif
263 #define LIBVIRT_BEGIN_ALLOW_THREADS \
264 LIBVIRT_STMT_START { \
265 PyThreadState *_save = NULL; \
266 if (PyEval_ThreadsInitialized()) \
267 _save = PyEval_SaveThread();
269 #define LIBVIRT_END_ALLOW_THREADS \
270 if (PyEval_ThreadsInitialized()) \
271 PyEval_RestoreThread(_save); \
272 } LIBVIRT_STMT_END
274 #define LIBVIRT_ENSURE_THREAD_STATE \
275 LIBVIRT_STMT_START { \
276 PyGILState_STATE _save = PyGILState_UNLOCKED; \
277 if (PyEval_ThreadsInitialized()) \
278 _save = PyGILState_Ensure();
280 #define LIBVIRT_RELEASE_THREAD_STATE \
281 if (PyEval_ThreadsInitialized()) \
282 PyGILState_Release(_save); \
283 } LIBVIRT_STMT_END