Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
PythonInterpreter.cxx
1 /**
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip 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.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 TLP_PYTHON_SCOPE void decrefPyObject(PyObject *obj);
21 
22 template<typename T>
23 bool PythonInterpreter::evalSingleStatementAndGetValue(const QString &pythonStatement, T &value) {
24  holdGIL();
25 
26  PyObject *ret = evalPythonStatement(pythonStatement);
27 
28  bool ok = false;
29 
30  if (ret) {
31  PyObjectToCppObjectConvertor<T> convertor;
32  ok = convertor.convert(ret, value);
33  decrefPyObject(ret);
34  }
35 
36  releaseGIL();
37 
38  return ok;
39 }
40 
41 template<typename PARAM_TYPE>
42 bool PythonInterpreter::callFunctionOneParam(const QString &module, const QString &function, const PARAM_TYPE &parameter) {
43  tlp::DataSet parameters;
44  parameters.set("param1", parameter);
45  return callFunction(module, function, parameters);
46 }
47 
48 template<typename PARAM1_TYPE, typename PARAM2_TYPE>
49 bool PythonInterpreter::callFunctionTwoParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2) {
50  tlp::DataSet parameters;
51  parameters.set("param1", parameter1);
52  parameters.set("param2", parameter2);
53  return callFunction(module, function, parameters);
54 }
55 
56 template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE>
57 bool PythonInterpreter::callFunctionThreeParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
58  const PARAM3_TYPE &parameter3) {
59  tlp::DataSet parameters;
60  parameters.set("param1", parameter1);
61  parameters.set("param2", parameter2);
62  parameters.set("param3", parameter3);
63  return callFunction(module, function, parameters);
64 }
65 
66 template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE>
67 bool PythonInterpreter::callFunctionFourParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
68  const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4) {
69  tlp::DataSet parameters;
70  parameters.set("param1", parameter1);
71  parameters.set("param2", parameter2);
72  parameters.set("param3", parameter3);
73  parameters.set("param4", parameter4);
74  return callFunction(module, function, parameters);
75 }
76 
77 template<typename PARAM_TYPE, typename RETURN_TYPE>
78 bool PythonInterpreter::callFunctionOneParamAndGetReturnValue(const QString &module, const QString &function, const PARAM_TYPE &parameter, RETURN_TYPE &returnValue) {
79  tlp::DataSet parameters;
80  parameters.set("param1", parameter);
81  return callFunctionAndGetReturnValue(module, function, parameters, returnValue);
82 }
83 
84 template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename RETURN_TYPE>
85 bool PythonInterpreter::callFunctionTwoParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1,
86  const PARAM2_TYPE &parameter2, RETURN_TYPE &returnValue) {
87 
88  tlp::DataSet parameters;
89  parameters.set("param1", parameter1);
90  parameters.set("param2", parameter2);
91  return callFunctionAndGetReturnValue(module, function, parameters, returnValue);
92 }
93 
94 template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename RETURN_TYPE>
95 bool PythonInterpreter::callFunctionThreeParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1,
96  const PARAM2_TYPE &parameter2, const PARAM3_TYPE &parameter3, RETURN_TYPE &returnValue) {
97 
98  tlp::DataSet parameters;
99  parameters.set("param1", parameter1);
100  parameters.set("param2", parameter2);
101  parameters.set("param3", parameter3);
102  return callFunctionAndGetReturnValue(module, function, parameters, returnValue);
103 }
104 
105 template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE, typename RETURN_TYPE>
106 bool PythonInterpreter::callFunctionFourParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1,
107  const PARAM2_TYPE &parameter2, const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4,
108  RETURN_TYPE &returnValue) {
109 
110  tlp::DataSet parameters;
111  parameters.set("param1", parameter1);
112  parameters.set("param2", parameter2);
113  parameters.set("param3", parameter3);
114  parameters.set("param4", parameter4);
115  return callFunctionAndGetReturnValue(module, function, parameters, returnValue);
116 }
117 
118 template<typename RETURN_TYPE>
119 bool PythonInterpreter::callFunctionAndGetReturnValue(const QString &module, const QString &function, const tlp::DataSet &parameters, RETURN_TYPE &returnValue) {
120  holdGIL();
121  bool ok = false;
122  PyObject *ret = callPythonFunction(module, function, parameters);
123  PyObjectToCppObjectConvertor<RETURN_TYPE> retConvertor;
124 
125  if (ret && retConvertor.convert(ret, returnValue)) {
126  ok = true;
127  }
128 
129  decrefPyObject(ret);
130  releaseGIL();
131  return ok;
132 }
133