Debian Codegraph @

src/Base/TypePyImp.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 51 Franklin Street, *
21 * Fifth Floor, Boston, MA 02110-1301, USA *
22 * *
23 ***************************************************************************/
24
25 #include "Type.h"
26 #include "BindingManager.h"
27
28 // generated out of Type.pyi
29 #include "TypePy.h"
30 #include "TypePy.cpp"
31
32 using namespace Base;
33
34 // returns a string which represent the object e.g. when printed in python
35 std::string TypePy::representation() const
36 {
37 std::stringstream str;
38 str << "<class '" << getBaseTypePtr()->getName() << "'>";
39 return str.str();
40 }
41
42 PyObject* TypePy::fromName(PyObject* args)
43 {
44 const char* name {};
45 if (!PyArg_ParseTuple(args, "s", &name)) {
46 return nullptr;
47 }
48
49 return new TypePy(new Base::Type(Type::fromName(name)));
50 }
51
52 PyObject* TypePy::fromKey(PyObject* args)
53 {
54 unsigned int index {};
55 if (!PyArg_ParseTuple(args, "I", &index)) {
56 return nullptr;
57 }
58
59 return new TypePy(new Base::Type(Type::fromKey(index)));
60 }
61
62 PyObject* TypePy::getNumTypes(PyObject* args)
63 {
64 if (!PyArg_ParseTuple(args, "")) {
65 return nullptr;
66 }
67
68 int num = Type::getNumTypes();
69 return PyLong_FromLong(num);
70 }
71
72 PyObject* TypePy::getBadType(PyObject* args)
73 {
74 if (!PyArg_ParseTuple(args, "")) {
75 return nullptr;
76 }
77
78 return new TypePy(new Base::Type(Type::BadType));
79 }
80
81 PyObject* TypePy::getParent(PyObject* args) const
82 {
83 if (!PyArg_ParseTuple(args, "")) {
84 return nullptr;
85 }
86
87 return new TypePy(new Base::Type(getBaseTypePtr()->getParent()));
88 }
89
90 PyObject* TypePy::isBad(PyObject* args) const
91 {
92 if (!PyArg_ParseTuple(args, "")) {
93 return nullptr;
94 }
95
96 bool val = getBaseTypePtr()->isBad();
97 return PyBool_FromLong(val ? 1 : 0);
98 }
99
100 PyObject* TypePy::isDerivedFrom(PyObject* args) const
101 {
102 Base::Type type;
103
104 do {
105 const char* name {};
106 if (PyArg_ParseTuple(args, "s", &name)) {
107 type = Type::fromName(name);
108 break;
109 }
110
111 PyErr_Clear();
112 PyObject* py {};
113 if (PyArg_ParseTuple(args, "O!", &TypePy::Type, &py)) {
114 type = *static_cast<TypePy*>(py)->getBaseTypePtr();
115 break;
116 }
117
118 PyErr_SetString(PyExc_TypeError, "TypeId or str expected");
119 return nullptr;
120 } while (false);
121
122 bool val = (!type.isBad() && getBaseTypePtr()->isDerivedFrom(type));
123 return PyBool_FromLong(val ? 1 : 0);
124 }
125
126 PyObject* TypePy::getAllDerivedFrom(PyObject* args)
127 {
128 Base::Type type;
129
130 do {
131 const char* name {};
132 if (PyArg_ParseTuple(args, "s", &name)) {
133 type = Type::fromName(name);
134 break;
135 }
136
137 PyErr_Clear();
138 PyObject* py {};
139 if (PyArg_ParseTuple(args, "O!", &TypePy::Type, &py)) {
140 type = *static_cast<TypePy*>(py)->getBaseTypePtr();
141 break;
142 }
143
144 PyErr_SetString(PyExc_TypeError, "TypeId or str expected");
145 return nullptr;
146 } while (false);
147
148 std::vector<Base::Type> ary;
149 Type::getAllDerivedFrom(type, ary);
150 Py::List res;
151 for (const auto& it : ary) {
152 res.append(Py::asObject(new TypePy(new Base::Type(it))));
153 }
154 return Py::new_reference_to(res);
155 }
156
157 PyObject* TypePy::getAllDerived(PyObject* args) const
158 {
159 if (!PyArg_ParseTuple(args, "")) {
160 return nullptr;
161 }
162
163 Base::Type type = Type::fromName(getBaseTypePtr()->getName());
164 std::vector<Base::Type> ary;
165 Type::getAllDerivedFrom(type, ary);
166 Py::List res;
167 for (const auto& it : ary) {
168 res.append(Py::asObject(new TypePy(new Base::Type(it))));
169 }
170 return Py::new_reference_to(res);
171 }
172
173 namespace
174 {
175 void deallocPyObject(PyObject* py)
176 {
177 PyObjectBase* pybase = static_cast<PyObjectBase*>(py);
178 BaseClass* base = static_cast<BaseClass*>(pybase->getTwinPointer());
179 if (BindingManager::instance().retrieveWrapper(base) == py) {
180 BindingManager::instance().releaseWrapper(base, py);
181 delete base;
182 }
183
184 PyObjectBase::PyDestructor(py);
185 }
186
187 PyObject* createPyObject(BaseClass* base)
188 {
189 PyObject* py = base->getPyObject();
190
191 if (PyObject_TypeCheck(py, &PyObjectBase::Type)) {
192 // if the Python wrapper is a sub-class of PyObjectBase then
193 // check if the C++ object must be added to the list of tracked objects
194 PyObjectBase* pybase = static_cast<PyObjectBase*>(py);
195 if (base == pybase->getTwinPointer()) {
196 // steal a reference because at this point the counter is at 2
197 Py_DECREF(py);
198 Py_TYPE(py)->tp_dealloc = deallocPyObject;
199 BindingManager::instance().registerWrapper(base, py);
200 }
201 else {
202 // The Python wrapper creates its own copy of the C++ object
203 delete base;
204 }
205 }
206 else {
207 // if the Python wrapper is not a sub-class of PyObjectBase then
208 // immediately destroy the C++ object
209 delete base;
210 }
211 return py;
212 }
213
214 } // namespace
215
216 PyObject* TypePy::createInstance(PyObject* args)
217 {
218 if (!PyArg_ParseTuple(args, "")) {
219 return nullptr;
220 }
221
222 Py::String name(getBaseTypePtr()->getName());
223 Py::TupleN tuple(name);
224
225 return createInstanceByName(tuple.ptr());
226 }
227
228 PyObject* TypePy::createInstanceByName(PyObject* args)
229 {
230 const char* name {};
231 PyObject* load = Py_False; // NOLINT
232 if (!PyArg_ParseTuple(args, "s|O!", &name, &PyBool_Type, &load)) {
233 return nullptr;
234 }
235
236 bool loadModule = asBoolean(load);
237 Base::Type type = Type::getTypeIfDerivedFrom(name, BaseClass::getClassTypeId(), loadModule);
238 if (type.isBad()) {
239 Py_Return;
240 }
241
242 void* typeInstance = type.createInstance();
243 if (!typeInstance) {
244 Py_Return;
245 }
246
247 BaseClass* base = static_cast<BaseClass*>(typeInstance);
248
249 return createPyObject(base);
250 }
251
252 Py::String TypePy::getName() const
253 {
254 return {std::string(getBaseTypePtr()->getName())};
255 }
256
257 Py::Long TypePy::getKey() const
258 {
259 return Py::Long(static_cast<long>(getBaseTypePtr()->getKey()));
260 }
261
262 Py::String TypePy::getModule() const
263 {
264 std::string module(getBaseTypePtr()->getName());
265 std::string::size_type pos = module.find_first_of("::");
266
267 if (pos != std::string::npos) {
268 module = std::string(module, 0, pos);
269 }
270 else {
271 module.clear();
272 }
273
274 return {module};
275 }
276
277 PyObject* TypePy::getCustomAttributes(const char* /*attr*/) const
278 {
279 return nullptr;
280 }
281
282 int TypePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
283 {
284 return 0;
285 }