Debian Codegraph @

src/gphoto2/common/macros.i

raw
1 // python-gphoto2 - Python interface to libgphoto2
2 // http://github.com/jim-easterbrook/python-gphoto2
3 // Copyright (C) 2014-24 Jim Easterbrook [email protected]
4 //
5 // This file is part of python-gphoto2.
6 //
7 // python-gphoto2 is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // python-gphoto2 is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with python-gphoto2. If not, see <https://www.gnu.org/licenses/>.
19
20 %define DEFAULT_EXCEPTION
21 %exception {
22 $action
23 if (PyErr_Occurred()) SWIG_fail;
24 }
25 %enddef
26
27 %define DEPRECATED(func_name, check_result)
28 %feature("docstring") func_name
29 "This function is deprecated and will be removed in a future release."
30 %exception func_name {
31 if (PyErr_WarnEx(PyExc_DeprecationWarning, #func_name ## " is deprecated and"
32 " will be removed in a future release", 1) < 0) SWIG_fail;
33 $action
34 #if #check_result != ""
35 if (PyErr_Occurred()) SWIG_fail;
36 #endif
37 }
38 %enddef
39
40 %define GPHOTO2_ERROR(error)
41 PyErr_SetObject(PyExc_GPhoto2Error, PyInt_FromLong(error));
42 %enddef
43
44 %define PLAIN_ARGOUT(typepattern)
45 %typemap(in, numinputs=0) typepattern ($*1_type temp) {
46 temp = NULL;
47 $1 = &temp;
48 }
49 %typemap(argout) typepattern {
50 $result = SWIG_AppendOutput(
51 $result, SWIG_NewPointerObj(*$1, $*1_descriptor, SWIG_POINTER_OWN));
52 }
53 %enddef
54
55 %define CALLOC_ARGOUT(typepattern)
56 %typemap(in, numinputs=0) typepattern () {
57 $1 = ($1_type)calloc(1, sizeof($*1_type));
58 if ($1 == NULL) {
59 PyErr_SetString(PyExc_MemoryError, "Cannot allocate " "$*1_type");
60 SWIG_fail;
61 }
62 }
63 %typemap(freearg) typepattern {
64 free($1);
65 }
66 %typemap(argout) typepattern {
67 $result = SWIG_AppendOutput(
68 $result, SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN));
69 $1 = NULL;
70 }
71 %enddef
72
73 %define NEW_ARGOUT(typepattern, alloc_func, free_func)
74 %typemap(in, numinputs=0) typepattern () {
75 int error = alloc_func(&$1);
76 if (error < GP_OK) {
77 $1 = NULL;
78 GPHOTO2_ERROR(error)
79 SWIG_fail;
80 }
81 }
82 %typemap(freearg) typepattern {
83 if ($1 != NULL) {
84 free_func($1);
85 }
86 }
87 %typemap(argout) typepattern {
88 $result = SWIG_AppendOutput(
89 $result, SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN));
90 $1 = NULL;
91 }
92 %enddef
93
94 %define DEFAULT_CTOR(type, function)
95 %extend type {
96 type() {
97 struct type *result;
98 int error = function(&result);
99 if (error < GP_OK)
100 GPHOTO2_ERROR(error)
101 return result;
102 }
103 };
104 %enddef
105
106 %define DEFAULT_DTOR(name, free_func)
107 %delobject free_func;
108 %extend name {
109 ~name() {
110 int error = free_func($self);
111 if (error < GP_OK) GPHOTO2_ERROR(error)
112 }
113 };
114 %enddef
115
116 // Macros to add member functions to structs
117 %define MEMBER_FUNCTION(type, member_rtn, member, member_args,
118 function, function_args, thread_allow)
119 %extend type {
120 member_rtn member member_args {
121 #if #thread_allow != ""
122 SWIG_PYTHON_THREAD_BEGIN_ALLOW;
123 #endif
124 int result = function function_args;
125 #if #thread_allow != ""
126 SWIG_PYTHON_THREAD_END_ALLOW;
127 #endif
128 if (result < GP_OK) GPHOTO2_ERROR(result)
129 #if #member_rtn == "int" || #member_rtn == "static int"
130 return result;
131 #endif
132 }
133 };
134 %enddef
135
136 %define LEN_MEMBER_FUNCTION(type, function)
137 %feature("python:slot", "sq_length", functype="lenfunc") type::__len__;
138 MEMBER_FUNCTION(type, int, __len__, (), function, ($self), )
139 %enddef
140
141 %define VOID_MEMBER_FUNCTION(type, member, member_args, function, function_args)
142 %extend type {
143 void member member_args {
144 function function_args;
145 }
146 };
147 %enddef
148
149 %define DEFAULT_CONTEXT
150 // For functions that accept NULL context value
151 %typemap(default) (GPContext *) {
152 $1 = NULL;
153 }
154 %typemap(doc) GPContext * "$1_name: gphoto2.Context (default=None)";
155 %enddef