PageRenderTime 66ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/Wrapping/PythonCore/vtkPythonUtil.h

https://github.com/demarle/VTK
C Header | 218 lines | 88 code | 35 blank | 95 comment | 0 complexity | 9f05c169643356c6dffef90b8797be60 MD5 | raw file
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: vtkPythonUtil.h
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. // .NAME vtkPythonUtil
  12. #ifndef vtkPythonUtil_h
  13. #define vtkPythonUtil_h
  14. #include "vtkPython.h"
  15. #include "PyVTKClass.h"
  16. #include "PyVTKMutableObject.h"
  17. #include "PyVTKNamespace.h"
  18. #include "PyVTKObject.h"
  19. #include "PyVTKSpecialObject.h"
  20. class vtkPythonClassMap;
  21. class vtkPythonCommand;
  22. class vtkPythonCommandList;
  23. class vtkPythonGhostMap;
  24. class vtkPythonObjectMap;
  25. class vtkPythonSpecialTypeMap;
  26. class vtkPythonNamespaceMap;
  27. class vtkStdString;
  28. class vtkUnicodeString;
  29. class vtkVariant;
  30. extern "C" void vtkPythonUtilDelete();
  31. class VTKWRAPPINGPYTHONCORE_EXPORT vtkPythonUtil
  32. {
  33. public:
  34. // Description:
  35. // If the name is templated or mangled, converts it into
  36. // a python-printable name.
  37. static const char *PythonicClassName(const char *classname);
  38. // Description:
  39. // Add a PyVTKClass to the type lookup table, this allows us to later
  40. // create object given only the class name.
  41. static void AddClassToMap(PyObject *obj, const char *classname);
  42. // Description:
  43. // Get information about a special VTK type, given the type name.
  44. static PyObject *FindClass(const char *classname);
  45. // Description:
  46. // For an VTK object whose class is not in the ClassMap, search
  47. // the whole ClassMap to find out which class is the closest base
  48. // class of the object. Returns a PyVTKClass.
  49. static PyObject *FindNearestBaseClass(vtkObjectBase *ptr);
  50. // Description:
  51. // Extract the vtkObjectBase from a PyVTKObject. If the PyObject is
  52. // not a PyVTKObject, or is not a PyVTKObject of the specified type,
  53. // the python error indicator will be set.
  54. // Special behavior: Py_None is converted to NULL without no error.
  55. static vtkObjectBase *GetPointerFromObject(PyObject *obj,
  56. const char *classname);
  57. // Description:
  58. // Convert a vtkObjectBase to a PyVTKObject. This will first check to
  59. // see if the PyVTKObject already exists, and create a new PyVTKObject
  60. // if necessary. This function also passes ownership of the reference
  61. // to the PyObject.
  62. // Special behaviour: NULL is converted to Py_None.
  63. static PyObject *GetObjectFromPointer(vtkObjectBase *ptr);
  64. // Description:
  65. // Extract the SIP wrapped object from a PyObject. If the conversion cannot
  66. // be done, an error indicator is set.
  67. // Special behavior: Py_None is converted to NULL without no error.
  68. static void *SIPGetPointerFromObject(PyObject *obj, const char *classname);
  69. // Description:
  70. // Convert a SIP wrapped object to a PyObject.
  71. // Special behaviour: NULL is converted to Py_None.
  72. static PyObject *SIPGetObjectFromPointer(
  73. const void *ptr, const char* classname, bool is_new);
  74. // Description:
  75. // Try to convert some PyObject into a PyVTKObject, currently conversion
  76. // is supported for SWIG-style mangled pointer strings.
  77. static PyObject *GetObjectFromObject(PyObject *arg, const char *type);
  78. // Description:
  79. // Add PyVTKObject/vtkObjectBase pairs to the internal mapping.
  80. // This methods do not change the reference counts of either the
  81. // vtkObjectBase or the PyVTKObject.
  82. static void AddObjectToMap(PyObject *obj, vtkObjectBase *anInstance);
  83. // Description:
  84. // Remove a PyVTKObject from the internal mapping. No reference
  85. // counts are changed.
  86. static void RemoveObjectFromMap(PyObject *obj);
  87. // Description:
  88. // Add a special VTK type to the type lookup table, this allows us to
  89. // later create object given only the class name.
  90. static PyVTKSpecialType *AddSpecialTypeToMap(
  91. PyTypeObject *pytype, PyMethodDef *methods, PyMethodDef *constructors,
  92. const char *docstring[], PyVTKSpecialCopyFunc copyfunc);
  93. // Description:
  94. // Get information about a special VTK type, given the type name.
  95. static PyVTKSpecialType *FindSpecialType(const char *classname);
  96. // Description:
  97. // Given a PyObject, convert it into a "result_type" object, where
  98. // "result_type" must be a wrapped type. The C object is returned
  99. // as a void *, which must be cast to a pointer of the desired type.
  100. // If conversion was necessary, then the created python object is
  101. // returned in "newobj", but if the original python object was
  102. // already of the correct type, then "newobj" will be set to NULL.
  103. // If a python exception was raised, NULL will be returned.
  104. static void *GetPointerFromSpecialObject(
  105. PyObject *obj, const char *result_type, PyObject **newobj);
  106. // Description:
  107. // Add a wrapped C++ namespace as a python module object. This allows
  108. // the namespace to be retrieved and added to as necessary.
  109. static void AddNamespaceToMap(PyObject *o);
  110. // Description:
  111. // Remove a wrapped C++ namespace from consideration. This is called
  112. // from the namespace destructor.
  113. static void RemoveNamespaceFromMap(PyObject *o);
  114. // Description:
  115. // Return an existing namespace, or NULL if it doesn't exist.
  116. static PyObject *FindNamespace(const char *name);
  117. // Description:
  118. // Utility function to build a docstring by concatenating a series
  119. // of strings until a null string is found.
  120. static PyObject *BuildDocString(const char *docstring[]);
  121. // Description:
  122. // Utility function for creating SWIG-style mangled pointer string.
  123. static char *ManglePointer(const void *ptr, const char *type);
  124. // Description:
  125. // Utility function decoding a SWIG-style mangled pointer string.
  126. static void *UnmanglePointer(char *ptrText, int *len, const char *type);
  127. // Description:
  128. // Compute a hash for a vtkVariant.
  129. static long VariantHash(const vtkVariant *variant);
  130. // Description:
  131. // Register a vtkPythonCommand. Registering vtkPythonCommand instances ensures
  132. // that when the interpreter is destroyed (and Py_AtExit() gets called), the
  133. // vtkPythonCommand state is updated to avoid referring to dangling Python
  134. // objects pointers. Note, this will not work with Py_NewInterpreter.
  135. static void RegisterPythonCommand(vtkPythonCommand*);
  136. static void UnRegisterPythonCommand(vtkPythonCommand*);
  137. private:
  138. vtkPythonUtil();
  139. ~vtkPythonUtil();
  140. vtkPythonUtil(const vtkPythonUtil&); // Not implemented.
  141. void operator=(const vtkPythonUtil&); // Not implemented.
  142. vtkPythonObjectMap *ObjectMap;
  143. vtkPythonGhostMap *GhostMap;
  144. vtkPythonClassMap *ClassMap;
  145. vtkPythonSpecialTypeMap *SpecialTypeMap;
  146. vtkPythonNamespaceMap *NamespaceMap;
  147. vtkPythonCommandList *PythonCommandList;
  148. friend void vtkPythonUtilDelete();
  149. friend void vtkPythonUtilCreateIfNeeded();
  150. };
  151. // For use by SetXXMethod() , SetXXMethodArgDelete()
  152. extern VTKWRAPPINGPYTHONCORE_EXPORT void vtkPythonVoidFunc(void *);
  153. extern VTKWRAPPINGPYTHONCORE_EXPORT void vtkPythonVoidFuncArgDelete(void *);
  154. // The following macro is used to suppress missing initializer
  155. // warnings. Python documentation says these should not be necessary.
  156. // We define it as a macro in case the length needs to change across
  157. // python versions.
  158. #if PY_VERSION_HEX >= 0x02060000 // for tp_version_tag
  159. #define VTK_PYTHON_UTIL_SUPRESS_UNINITIALIZED \
  160. 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0, 0,
  161. #define VTK_WRAP_PYTHON_SUPRESS_UNINITIALIZED \
  162. 0, 0,
  163. #elif PY_VERSION_HEX >= 0x02030000
  164. #define VTK_PYTHON_UTIL_SUPRESS_UNINITIALIZED \
  165. 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,
  166. #define VTK_WRAP_PYTHON_SUPRESS_UNINITIALIZED \
  167. 0,
  168. #elif PY_VERSION_HEX >= 0x02020000
  169. #define VTK_PYTHON_UTIL_SUPRESS_UNINITIALIZED \
  170. 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
  171. #define VTK_WRAP_PYTHON_SUPRESS_UNINITIALIZED
  172. #else
  173. #define VTK_PYTHON_UTIL_SUPRESS_UNINITIALIZED
  174. #define VTK_WRAP_PYTHON_SUPRESS_UNINITIALIZED
  175. #endif
  176. #if PY_VERSION_HEX < 0x02050000
  177. typedef int Py_ssize_t;
  178. #endif
  179. #endif