Post-release version bump to 11.1.0
[libvirt-python.git] / typewrappers.h
blob427323a5fa8fb53a4a60424f6a0d0c1006e9ccf6
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 #if !LIBVIR_CHECK_VERSION(4, 5, 0)
26 typedef struct _virNWFilterBinding *virNWFilterBindingPtr;
27 #endif
28 #if !LIBVIR_CHECK_VERSION(5, 5, 0)
29 typedef struct _virNetworkPort *virNetworkPortPtr;
30 #endif
31 #if !LIBVIR_CHECK_VERSION(5, 6, 0)
32 typedef struct _virDomainCheckpoint *virDomainCheckpointPtr;
33 #endif
35 #define PyvirConnect_Get(v) (((v) == Py_None) ? NULL : \
36 (((PyvirConnect_Object *)(v))->obj))
38 typedef struct {
39 PyObject_HEAD
40 virConnectPtr obj;
41 } PyvirConnect_Object;
44 #define PyvirDomain_Get(v) (((v) == Py_None) ? NULL : \
45 (((PyvirDomain_Object *)(v))->obj))
47 typedef struct {
48 PyObject_HEAD
49 virDomainPtr obj;
50 } PyvirDomain_Object;
53 #define PyvirNetwork_Get(v) (((v) == Py_None) ? NULL : \
54 (((PyvirNetwork_Object *)(v))->obj))
56 typedef struct {
57 PyObject_HEAD
58 virNetworkPtr obj;
59 } PyvirNetwork_Object;
62 #define PyvirNetworkPort_Get(v) (((v) == Py_None) ? NULL : \
63 (((PyvirNetworkPort_Object *)(v))->obj))
65 typedef struct {
66 PyObject_HEAD
67 virNetworkPortPtr obj;
68 } PyvirNetworkPort_Object;
71 #define PyvirInterface_Get(v) (((v) == Py_None) ? NULL : \
72 (((PyvirInterface_Object *)(v))->obj))
74 typedef struct {
75 PyObject_HEAD
76 virInterfacePtr obj;
77 } PyvirInterface_Object;
80 #define PyvirStoragePool_Get(v) (((v) == Py_None) ? NULL : \
81 (((PyvirStoragePool_Object *)(v))->obj))
83 typedef struct {
84 PyObject_HEAD
85 virStoragePoolPtr obj;
86 } PyvirStoragePool_Object;
89 #define PyvirStorageVol_Get(v) (((v) == Py_None) ? NULL : \
90 (((PyvirStorageVol_Object *)(v))->obj))
92 typedef struct {
93 PyObject_HEAD
94 virStorageVolPtr obj;
95 } PyvirStorageVol_Object;
98 #define PyvirNodeDevice_Get(v) (((v) == Py_None) ? NULL : \
99 (((PyvirNodeDevice_Object *)(v))->obj))
101 typedef struct {
102 PyObject_HEAD
103 virNodeDevicePtr obj;
104 } PyvirNodeDevice_Object;
106 #define PyvirSecret_Get(v) (((v) == Py_None) ? NULL : \
107 (((PyvirSecret_Object *)(v))->obj))
109 typedef struct {
110 PyObject_HEAD
111 virSecretPtr obj;
112 } PyvirSecret_Object;
114 #define PyvirNWFilter_Get(v) (((v) == Py_None) ? NULL : \
115 (((PyvirNWFilter_Object *)(v))->obj))
117 typedef struct {
118 PyObject_HEAD
119 virNWFilterPtr obj;
120 } PyvirNWFilter_Object;
123 #define PyvirNWFilterBinding_Get(v) (((v) == Py_None) ? NULL : \
124 (((PyvirNWFilterBinding_Object *)(v))->obj))
126 typedef struct {
127 PyObject_HEAD
128 virNWFilterBindingPtr obj;
129 } PyvirNWFilterBinding_Object;
132 #define PyvirStream_Get(v) (((v) == Py_None) ? NULL : \
133 (((PyvirStream_Object *)(v))->obj))
135 typedef struct {
136 PyObject_HEAD
137 virStreamPtr obj;
138 } PyvirStream_Object;
141 #define PyvirDomainCheckpoint_Get(v) (((v) == Py_None) ? NULL : \
142 (((PyvirDomainCheckpoint_Object *)(v))->obj))
144 typedef struct {
145 PyObject_HEAD
146 virDomainCheckpointPtr obj;
147 } PyvirDomainCheckpoint_Object;
150 #define PyvirDomainSnapshot_Get(v) (((v) == Py_None) ? NULL : \
151 (((PyvirDomainSnapshot_Object *)(v))->obj))
153 typedef struct {
154 PyObject_HEAD
155 virDomainSnapshotPtr obj;
156 } PyvirDomainSnapshot_Object;
159 #define PyvirEventHandleCallback_Get(v) (((v) == Py_None) ? NULL : \
160 (((PyvirEventHandleCallback_Object *)(v))->obj))
162 typedef struct {
163 PyObject_HEAD
164 virEventHandleCallback obj;
165 } PyvirEventHandleCallback_Object;
167 #define PyvirEventTimeoutCallback_Get(v) (((v) == Py_None) ? NULL : \
168 (((PyvirEventTimeoutCallback_Object *)(v))->obj))
170 typedef struct {
171 PyObject_HEAD
172 virEventTimeoutCallback obj;
173 } PyvirEventTimeoutCallback_Object;
175 #define PyvirFreeCallback_Get(v) (((v) == Py_None) ? NULL : \
176 (((PyvirFreeCallback_Object *)(v))->obj))
178 typedef struct {
179 PyObject_HEAD
180 virFreeCallback obj;
181 } PyvirFreeCallback_Object;
183 #define PyvirVoidPtr_Get(v) (((v) == Py_None) ? NULL : \
184 (((PyvirVoidPtr_Object *)(v))->obj))
186 typedef struct {
187 PyObject_HEAD
188 void* obj;
189 } PyvirVoidPtr_Object;
191 PyObject * libvirt_intWrap(int val);
192 PyObject * libvirt_uintWrap(unsigned int val);
193 PyObject * libvirt_longWrap(long val);
194 PyObject * libvirt_ulongWrap(unsigned long val);
195 PyObject * libvirt_longlongWrap(long long val);
196 PyObject * libvirt_ulonglongWrap(unsigned long long val);
197 PyObject * libvirt_charPtrWrap(char *str);
198 PyObject * libvirt_charPtrSizeWrap(char *str, Py_ssize_t size);
199 PyObject * libvirt_constcharPtrWrap(const char *str);
200 PyObject * libvirt_boolWrap(int val);
201 int libvirt_intUnwrap(PyObject *obj, int *val);
202 int libvirt_uintUnwrap(PyObject *obj, unsigned int *val);
203 int libvirt_longUnwrap(PyObject *obj, long *val);
204 int libvirt_ulongUnwrap(PyObject *obj, unsigned long *val);
205 int libvirt_longlongUnwrap(PyObject *obj, long long *val);
206 int libvirt_ulonglongUnwrap(PyObject *obj, unsigned long long *val);
207 int libvirt_doubleUnwrap(PyObject *obj, double *val);
208 int libvirt_boolUnwrap(PyObject *obj, bool *val);
209 int libvirt_charPtrUnwrap(PyObject *obj, char **str);
210 int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size);
211 PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
212 PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
213 PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
214 PyObject * libvirt_virNetworkPortPtrWrap(virNetworkPortPtr node);
215 PyObject * libvirt_virInterfacePtrWrap(virInterfacePtr node);
216 PyObject * libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node);
217 PyObject * libvirt_virStorageVolPtrWrap(virStorageVolPtr node);
218 PyObject * libvirt_virEventHandleCallbackWrap(virEventHandleCallback node);
219 PyObject * libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node);
220 PyObject * libvirt_virFreeCallbackWrap(virFreeCallback node);
221 PyObject * libvirt_virVoidPtrWrap(void* node);
222 PyObject * libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node);
223 PyObject * libvirt_virSecretPtrWrap(virSecretPtr node);
224 PyObject * libvirt_virNWFilterPtrWrap(virNWFilterPtr node);
225 PyObject * libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node);
226 PyObject * libvirt_virStreamPtrWrap(virStreamPtr node);
227 PyObject * libvirt_virDomainCheckpointPtrWrap(virDomainCheckpointPtr node);
228 PyObject * libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node);
231 /* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
232 * LIBVIRT_STMT_START { statements; } LIBVIRT_STMT_END;
233 * can be used as a single statement, as in
234 * if (x) LIBVIRT_STMT_START { ... } LIBVIRT_STMT_END; else ...
236 * When GCC is compiling C code in non-ANSI mode, it will use the
237 * compiler __extension__ to wrap the statements within `({' and '})' braces.
238 * When compiling on platforms where configure has defined
239 * HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
240 * For any other platforms (SunOS4 is known to have this issue), wrap the
241 * statements with `if (1)' and `else (void) 0'.
243 #if !(defined (LIBVIRT_STMT_START) && defined (LIBVIRT_STMT_END))
244 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
245 # define LIBVIRT_STMT_START (void) __extension__ (
246 # define LIBVIRT_STMT_END )
247 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
248 # if defined (HAVE_DOWHILE_MACROS)
249 # define LIBVIRT_STMT_START do
250 # define LIBVIRT_STMT_END while (0)
251 # else /* !HAVE_DOWHILE_MACROS */
252 # define LIBVIRT_STMT_START if (1)
253 # define LIBVIRT_STMT_END else (void) 0
254 # endif /* !HAVE_DOWHILE_MACROS */
255 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
256 #endif
258 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
259 # define LIBVIRT_BEGIN_ALLOW_THREADS \
260 LIBVIRT_STMT_START { \
261 PyThreadState *_save = NULL; \
262 if (PyEval_ThreadsInitialized()) \
263 _save = PyEval_SaveThread();
265 # define LIBVIRT_END_ALLOW_THREADS \
266 if (PyEval_ThreadsInitialized()) \
267 PyEval_RestoreThread(_save); \
268 } LIBVIRT_STMT_END
270 # define LIBVIRT_ENSURE_THREAD_STATE \
271 LIBVIRT_STMT_START { \
272 PyGILState_STATE _save = PyGILState_UNLOCKED; \
273 if (PyEval_ThreadsInitialized()) \
274 _save = PyGILState_Ensure();
276 # define LIBVIRT_RELEASE_THREAD_STATE \
277 if (PyEval_ThreadsInitialized()) \
278 PyGILState_Release(_save); \
279 } LIBVIRT_STMT_END
281 #else
283 # define LIBVIRT_BEGIN_ALLOW_THREADS \
284 LIBVIRT_STMT_START { \
285 PyThreadState *_save = PyEval_SaveThread();
287 # define LIBVIRT_END_ALLOW_THREADS \
288 PyEval_RestoreThread(_save); \
289 } LIBVIRT_STMT_END
291 # define LIBVIRT_ENSURE_THREAD_STATE \
292 LIBVIRT_STMT_START { \
293 PyGILState_STATE _save = PyGILState_Ensure();
295 # define LIBVIRT_RELEASE_THREAD_STATE \
296 PyGILState_Release(_save); \
297 } LIBVIRT_STMT_END
298 #endif
300 #ifndef NULLSTR
301 #define NULLSTR(s) ((s) ? (s) : "<null>")
302 #endif