Debian Codegraph @

src/App/ExtensionPython.h

raw
1 /***************************************************************************
2 * Copyright (c) 2016 Stefan Tröger <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef APP_EXTENSIONPYTHON_H
25 #define APP_EXTENSIONPYTHON_H
26
27 #include <Base/Interpreter.h>
28 #include "Extension.h"
29 #include "PropertyPythonObject.h"
30
31 namespace App
32 {
33
34 /**
35 * Generic Python extension class which allows every extension derived
36 * class to behave as a Python extension -- simply by subclassing.
37 */
38 template<class ExtensionT>
39 class ExtensionPythonT: public ExtensionT // NOLINT
40 {
41 EXTENSION_PROPERTY_HEADER_WITH_OVERRIDE(App::ExtensionPythonT<ExtensionT>);
42
43 public:
44 using Inherited = ExtensionT;
45
46 ExtensionPythonT()
47 {
48 ExtensionT::m_isPythonExtension = true;
49 ExtensionT::initExtensionType(ExtensionPythonT::getExtensionClassTypeId());
50 }
51 ~ExtensionPythonT() override = default;
52
53 ExtensionPythonT(const ExtensionPythonT&) = delete;
54 ExtensionPythonT(ExtensionPythonT&&) = delete;
55 ExtensionPythonT& operator=(const ExtensionPythonT&) = delete;
56 ExtensionPythonT& operator=(ExtensionPythonT&&) = delete;
57 };
58
59 using ExtensionPython = ExtensionPythonT<App::Extension>;
60
61 // Helper macros to define python extensions
62 #define EXTENSION_PROXY_FIRST(function) \
63 Base::PyGILStateLocker lock; \
64 Py::Object result; \
65 try { \
66 App::Property* proxy = this->getExtendedContainer()->getPropertyByName("Proxy"); \
67 if (proxy && proxy->is<App::PropertyPythonObject>()) { \
68 Py::Object feature = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); \
69 if (feature.hasAttr(std::string(#function))) { \
70 if (feature.hasAttr("__object__")) { \
71 Py::Callable method(feature.getAttr(std::string(#function)));
72
73
74 #define EXTENSION_PROXY_SECOND(function) \
75 result = method.apply(args); \
76 } \
77 else \
78 { \
79 Py::Callable method(feature.getAttr(std::string(#function)));
80
81 #define EXTENSION_PROXY_THIRD() \
82 result = method.apply(args); \
83 } \
84 } \
85 } \
86 } \
87 catch (Py::Exception&) \
88 { \
89 Base::PyException e; \
90 e.reportException(); \
91 }
92
93 #define EXTENSION_PROXY_NOARG(function) \
94 EXTENSION_PROXY_FIRST(function) \
95 Py::Tuple args; \
96 EXTENSION_PROXY_SECOND(function) \
97 Py::Tuple args(1); \
98 args.setItem(0, Py::Object(this->getExtensionPyObject(), true)); \
99 EXTENSION_PROXY_THIRD()
100
101 #define EXTENSION_PROXY_ONEARG(function, arg) \
102 EXTENSION_PROXY_FIRST(function) \
103 Py::Tuple args; \
104 args.setItem(0, arg); \
105 EXTENSION_PROXY_SECOND(function) \
106 Py::Tuple args(2); \
107 args.setItem(0, Py::Object(this->getExtensionPyObject(), true)); \
108 args.setItem(1, arg); \
109 EXTENSION_PROXY_THIRD()
110
111 #define EXTENSION_PYTHON_OVERRIDE_VOID_NOARGS(function) \
112 virtual void function() override { EXTENSION_PROXY_NOARG(function) };
113
114 #define EXTENSION_PYTHON_OVERRIDE_OBJECT_NOARGS(function) \
115 virtual PyObject* function() override \
116 { \
117 EXTENSION_PROXY_NOARG(function) \
118 return result.ptr(); \
119 };
120
121 } // namespace App
122
123 #endif // APP_EXTENSIONPYTHON_H