Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
PythonCppTypesConverter.h
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 #ifndef PYTHONCPPTYPESCONVERTERS_H
21 #define PYTHONCPPTYPESCONVERTERS_H
22 
23 #include <tulip/Coord.h>
24 #include <tulip/Color.h>
25 #include <tulip/ColorScale.h>
26 #include <tulip/StringCollection.h>
27 #include <tulip/Size.h>
28 #include <tulip/Node.h>
29 #include <tulip/Edge.h>
30 #include <tulip/BooleanProperty.h>
31 #include <tulip/ColorProperty.h>
32 #include <tulip/DoubleProperty.h>
33 #include <tulip/IntegerProperty.h>
34 #include <tulip/LayoutProperty.h>
35 #include <tulip/SizeProperty.h>
36 #include <tulip/StringProperty.h>
37 #include <tulip/TlpTools.h>
38 
39 TLP_PYTHON_SCOPE void *convertSipWrapperToCppType(PyObject *sipWrapper, const std::string &cppTypename, const bool transferTo=false);
40 TLP_PYTHON_SCOPE PyObject *convertCppTypeToSipWrapper(void *cppObj, const std::string &cppTypename, bool fromNew=false);
41 
42 TLP_PYTHON_SCOPE bool convertPyObjectToBool(PyObject *pyObject, bool &cppObject);
43 TLP_PYTHON_SCOPE PyObject *convertBoolToPyObject(bool cppObject);
44 
45 TLP_PYTHON_SCOPE bool convertPyObjectToDouble(PyObject *pyObject, double &cppObject);
46 TLP_PYTHON_SCOPE PyObject *convertDoubleToPyObject(double cppObject);
47 
48 TLP_PYTHON_SCOPE bool convertPyObjectToLong(PyObject *pyObject, long &cppObject);
49 TLP_PYTHON_SCOPE PyObject *convertLongToPyObject(long cppObject);
50 
51 TLP_PYTHON_SCOPE bool convertPyObjectToUnsignedLong(PyObject *pyObject, unsigned long &cppObject);
52 TLP_PYTHON_SCOPE PyObject *convertUnsignedLongToPyObject(unsigned long cppObject);
53 
54 class TLP_PYTHON_SCOPE ValueSetter {
55 
56 public:
57 
58  ValueSetter(tlp::DataSet *dataSet, std::string key) :
59  dataSet(dataSet), graph(NULL), key(key) {}
60 
61  ValueSetter(tlp::Graph *graph, std::string key) :
62  dataSet(NULL), graph(graph), key(key) {}
63 
64  template <typename T>
65  void setValue(const T &value) {
66  if (dataSet) {
67  dataSet->set(key, value);
68  }
69  else if (graph) {
70  graph->setAttribute(key, value);
71  }
72  }
73 
74 private :
75 
76  tlp::DataSet *dataSet;
77  tlp::Graph *graph;
78  std::string key;
79 };
80 
81 TLP_PYTHON_SCOPE PyObject *getPyObjectFromDataType(const tlp::DataType *dataType, bool noCopy=false);
82 
83 TLP_PYTHON_SCOPE bool setCppValueFromPyObject(PyObject *pyObj, ValueSetter &valSetter, tlp::DataType *dataType=NULL);
84 
85 template <typename T>
86 class PyObjectToCppObjectConvertor {
87 
88 public:
89 
90  bool convert(PyObject *pyObject, T &cppObject) {
91  std::string className = tlp::demangleClassName(typeid(T).name());
92 
93  void *cppObjPointer = convertSipWrapperToCppType(pyObject, className);
94 
95  if (cppObjPointer) {
96  cppObject = *reinterpret_cast<T*>(cppObjPointer);
97  return true;
98  }
99 
100  return false;
101  }
102 
103 };
104 
105 template <typename T>
106 class PyObjectToCppObjectConvertor<T*> {
107 
108 public:
109 
110  bool convert(PyObject *pyObject, T *&cppObject) {
111  std::string className = tlp::demangleClassName(typeid(T).name());
112 
113  void *cppObjPointer = convertSipWrapperToCppType(pyObject, className, true);
114 
115  if (cppObjPointer) {
116  cppObject = reinterpret_cast<T*>(cppObjPointer);
117  return true;
118  }
119 
120  return false;
121  }
122 };
123 
124 template <>
125 class PyObjectToCppObjectConvertor<bool> {
126 public:
127  bool convert(PyObject *pyObject, bool &cppObject) {
128  return convertPyObjectToBool(pyObject, cppObject);
129  }
130 };
131 
132 template <>
133 class PyObjectToCppObjectConvertor<double> {
134 public:
135  bool convert(PyObject *pyObject, double &cppObject) {
136  return convertPyObjectToDouble(pyObject, cppObject);
137  }
138 };
139 
140 template <>
141 class PyObjectToCppObjectConvertor<float> {
142 public:
143  bool convert(PyObject *pyObject, float &cppObject) {
144  double val=0;
145  PyObjectToCppObjectConvertor<double> convertor;
146  bool ok = convertor.convert(pyObject, val);
147  cppObject = val;
148  return ok;
149  }
150 };
151 
152 template <>
153 class PyObjectToCppObjectConvertor<long> {
154 
155 public:
156  bool convert(PyObject *pyObject, long &cppObject) {
157  return convertPyObjectToLong(pyObject, cppObject);
158  }
159 };
160 
161 template <>
162 class PyObjectToCppObjectConvertor<int> {
163 public:
164  bool convert(PyObject *pyObject, int &cppObject) {
165  long val=0;
166  PyObjectToCppObjectConvertor<long> convertor;
167  bool ok = convertor.convert(pyObject, val);
168  cppObject = val;
169  return ok;
170  }
171 };
172 
173 template <>
174 class PyObjectToCppObjectConvertor<unsigned long> {
175 public:
176  bool convert(PyObject *pyObject, unsigned long &cppObject) {
177  return convertPyObjectToUnsignedLong(pyObject, cppObject);
178  }
179 };
180 
181 template <>
182 class PyObjectToCppObjectConvertor<unsigned int> {
183 public:
184  bool convert(PyObject *pyObject, unsigned int &cppObject) {
185  unsigned long val=0;
186  PyObjectToCppObjectConvertor<unsigned long> convertor;
187  bool ok = convertor.convert(pyObject, val);
188  cppObject = val;
189  return ok;
190  }
191 };
192 
193 template <typename T>
194 class CppObjectToPyObjectConvertor {
195 
196 public:
197 
198  bool convert(const T &cppObject, PyObject *&pyObject) {
199  std::string className = tlp::demangleClassName(typeid(T).name());
200 
201  T *objCopy = new T(cppObject);
202  PyObject *pyObj = convertCppTypeToSipWrapper(objCopy, className, true);
203 
204  if (pyObj) {
205  pyObject = pyObj;
206  return true;
207  }
208  else {
209  delete objCopy;
210  }
211 
212  return false;
213  }
214 };
215 
216 template <typename T>
217 class CppObjectToPyObjectConvertor<T*> {
218 
219 public:
220 
221  bool convert(T *cppObject, PyObject *&pyObject) {
222  std::string className = tlp::demangleClassName(typeid(T).name());
223 
224  PyObject *pyObj = convertCppTypeToSipWrapper(cppObject, className);
225 
226  if (pyObj) {
227  pyObject = pyObj;
228  return true;
229  }
230 
231  return false;
232  }
233 
234 };
235 
236 template <>
237 class CppObjectToPyObjectConvertor<bool> {
238 public:
239  bool convert(const bool &cppObject, PyObject *&pyObject) {
240  pyObject = convertBoolToPyObject(cppObject);
241  return true;
242  }
243 };
244 
245 template <>
246 class CppObjectToPyObjectConvertor<long> {
247 public:
248  bool convert(const long &cppObject, PyObject *&pyObject) {
249  pyObject = convertLongToPyObject(cppObject);
250  return true;
251  }
252 };
253 
254 template <>
255 class CppObjectToPyObjectConvertor<int> {
256 public:
257  bool convert(const int &cppObject, PyObject *&pyObject) {
258  pyObject = convertLongToPyObject(cppObject);
259  return true;
260  }
261 };
262 
263 template <>
264 class CppObjectToPyObjectConvertor<unsigned int> {
265 public:
266  bool convert(const unsigned int &cppObject, PyObject *&pyObject) {
267  pyObject = convertUnsignedLongToPyObject(cppObject);
268  return true;
269  }
270 };
271 
272 template <>
273 class CppObjectToPyObjectConvertor<unsigned long> {
274 public:
275  bool convert(const unsigned long &cppObject, PyObject *&pyObject) {
276  pyObject = convertUnsignedLongToPyObject(cppObject);
277  return true;
278  }
279 };
280 
281 template <>
282 class CppObjectToPyObjectConvertor<double> {
283 public:
284  bool convert(const double &cppObject, PyObject *&pyObject) {
285  pyObject = convertDoubleToPyObject(cppObject);
286  return true;
287  }
288 };
289 
290 template <>
291 class CppObjectToPyObjectConvertor<float> {
292 public:
293  bool convert(const float &cppObject, PyObject *&pyObject) {
294  pyObject = convertDoubleToPyObject(cppObject);
295  return true;
296  }
297 };
298 
299 
300 
301 #endif // PYTHONCPPTYPESCONVERTERS_H