bindingmanager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. #ifndef BINDINGMANAGER_H
  4. #define BINDINGMANAGER_H
  5. #include "sbkpython.h"
  6. #include "shibokenmacros.h"
  7. #include <set>
  8. #include <utility>
  9. struct SbkObject;
  10. namespace Shiboken
  11. {
  12. namespace Module {
  13. struct TypeInitStruct;
  14. }
  15. struct DestructorEntry;
  16. using ObjectVisitor = void (*)(SbkObject *, void *);
  17. class LIBSHIBOKEN_API BindingManager
  18. {
  19. public:
  20. BindingManager(const BindingManager &) = delete;
  21. BindingManager(BindingManager &&) = delete;
  22. BindingManager &operator=(const BindingManager &) = delete;
  23. BindingManager &operator=(BindingManager &&) = delete;
  24. static BindingManager &instance();
  25. bool hasWrapper(const void *cptr, PyTypeObject *typeObject) const;
  26. bool hasWrapper(const void *cptr) const;
  27. void registerWrapper(SbkObject *pyObj, void *cptr);
  28. void releaseWrapper(SbkObject *wrapper);
  29. void runDeletionInMainThread();
  30. void addToDeletionInMainThread(const DestructorEntry &);
  31. SbkObject *retrieveWrapper(const void *cptr, PyTypeObject *typeObject) const;
  32. SbkObject *retrieveWrapper(const void *cptr) const;
  33. static PyObject *getOverride(SbkObject *wrapper, PyObject *pyMethodName);
  34. void addClassInheritance(Module::TypeInitStruct *parent, Module::TypeInitStruct *child);
  35. /// Try to find the correct type of cptr via type discovery knowing that it's at least
  36. /// of type \p type. If a derived class is found, it returns a cptr cast to the type
  37. /// (which may be different in case of multiple inheritance.
  38. /// \param cptr a pointer to the instance of type \p type
  39. /// \param type type of cptr
  40. using TypeCptrPair = std::pair<PyTypeObject *, void *>;
  41. TypeCptrPair findDerivedType(void *cptr, PyTypeObject *type) const;
  42. /**
  43. * Try to find the correct type of *cptr knowing that it's at least of type \p type.
  44. * In case of multiple inheritance this function may change the contents of cptr.
  45. * \param cptr a pointer to a pointer to the instance of type \p type
  46. * \param type type of *cptr
  47. * \warning This function is slow, use it only as last resort.
  48. */
  49. [[deprecated]] PyTypeObject *resolveType(void **cptr, PyTypeObject *type);
  50. std::set<PyObject *> getAllPyObjects();
  51. /**
  52. * Calls the function \p visitor for each object registered on binding manager.
  53. * \note As various C++ pointers can point to the same PyObject due to multiple inheritance
  54. * a PyObject can be called more than one time for each PyObject.
  55. * \param visitor function called for each object.
  56. * \param data user data passed as second argument to the visitor function.
  57. */
  58. void visitAllPyObjects(ObjectVisitor visitor, void *data);
  59. bool dumpTypeGraph(const char *fileName) const;
  60. void dumpWrapperMap();
  61. private:
  62. ~BindingManager();
  63. BindingManager();
  64. struct BindingManagerPrivate;
  65. BindingManagerPrivate *m_d;
  66. };
  67. LIBSHIBOKEN_API bool callInheritedInit(PyObject *self, PyObject *args, PyObject *kwds,
  68. const char *fullName);
  69. LIBSHIBOKEN_API bool callInheritedInit(PyObject *self, PyObject *args, PyObject *kwds,
  70. Module::TypeInitStruct typeStruct);
  71. } // namespace Shiboken
  72. #endif // BINDINGMANAGER_H