basewrapper_p.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 BASEWRAPPER_P_H
  4. #define BASEWRAPPER_P_H
  5. #include "sbkpython.h"
  6. #include "basewrapper.h"
  7. #include <unordered_map>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include <iosfwd>
  12. struct SbkObject;
  13. struct SbkConverter;
  14. namespace Shiboken
  15. {
  16. /**
  17. * This mapping associates a method and argument of an wrapper object with the wrapper of
  18. * said argument when it needs the binding to help manage its reference count.
  19. */
  20. using RefCountMap = std::unordered_multimap<std::string, PyObject *> ;
  21. /// Linked list of SbkBaseWrapper pointers
  22. using ChildrenList = std::set<SbkObject *>;
  23. /// Structure used to store information about object parent and children.
  24. struct ParentInfo
  25. {
  26. /// Pointer to parent object.
  27. SbkObject *parent = nullptr;
  28. /// List of object children.
  29. ChildrenList children;
  30. /// has internal ref
  31. bool hasWrapperRef = false;
  32. };
  33. } // namespace Shiboken
  34. extern "C"
  35. {
  36. /**
  37. * \internal
  38. * Private data for SbkBaseWrapper
  39. */
  40. struct SbkObjectPrivate
  41. {
  42. SbkObjectPrivate() noexcept = default;
  43. SbkObjectPrivate(const SbkObjectPrivate &) = delete;
  44. SbkObjectPrivate(SbkObjectPrivate &&o) = delete;
  45. SbkObjectPrivate &operator=(const SbkObjectPrivate &) = delete;
  46. SbkObjectPrivate &operator=(SbkObjectPrivate &&o) = delete;
  47. /// Pointer to the C++ class.
  48. void ** cptr;
  49. /// True when Python is responsible for freeing the used memory.
  50. unsigned int hasOwnership : 1;
  51. /// This is true when the C++ class of the wrapped object has a virtual destructor AND was created by Python.
  52. unsigned int containsCppWrapper : 1;
  53. /// Marked as false when the object is lost to C++ and the binding can not know if it was deleted or not.
  54. unsigned int validCppObject : 1;
  55. /// Marked as true when the object constructor was called
  56. unsigned int cppObjectCreated : 1;
  57. /// PYSIDE-1470: Marked as true if this is the Q*Application singleton.
  58. /// This bit allows app deletion from shiboken?.delete() .
  59. unsigned int isQAppSingleton : 1;
  60. /// Information about the object parents and children, may be null.
  61. Shiboken::ParentInfo *parentInfo;
  62. /// Manage reference count of objects that are referred to but not owned from.
  63. Shiboken::RefCountMap *referredObjects;
  64. ~SbkObjectPrivate()
  65. {
  66. delete parentInfo;
  67. parentInfo = nullptr;
  68. delete referredObjects;
  69. referredObjects = nullptr;
  70. }
  71. };
  72. // TODO-CONVERTERS: to be deprecated/removed
  73. /// The type behaviour was not defined yet
  74. #define BEHAVIOUR_UNDEFINED 0
  75. /// The type is a value type
  76. #define BEHAVIOUR_VALUETYPE 1
  77. /// The type is an object type
  78. #define BEHAVIOUR_OBJECTTYPE 2
  79. struct SbkObjectTypePrivate
  80. {
  81. SbkConverter *converter;
  82. int *mi_offsets;
  83. MultipleInheritanceInitFunction mi_init;
  84. /// Special cast function, null if this class doesn't have multiple inheritance.
  85. SpecialCastFunction mi_specialcast;
  86. TypeDiscoveryFuncV2 type_discovery;
  87. /// Pointer to a function responsible for deletion of the C++ instance calling the proper destructor.
  88. ObjectDestructor cpp_dtor;
  89. /// C++ name
  90. char *original_name;
  91. /// Type user data
  92. void *user_data;
  93. DeleteUserDataFunc d_func;
  94. void (*subtype_init)(PyTypeObject *, PyObject *, PyObject *);
  95. const char **propertyStrings;
  96. const char **enumFlagInfo;
  97. PyObject *enumFlagsDict;
  98. PyObject *enumTypeDict;
  99. /// True if this type holds two or more C++ instances, e.g.: a Python class which inherits from two C++ classes.
  100. unsigned int is_multicpp : 1;
  101. /// True if this type was defined by the user (a class written in Python inheriting
  102. /// a class provided by a Shiboken binding).
  103. unsigned int is_user_type : 1;
  104. /// Tells is the type is a value type or an object-type, see BEHAVIOUR_ *constants.
  105. unsigned int type_behaviour : 2;
  106. unsigned int delete_in_main_thread : 1;
  107. };
  108. } // extern "C"
  109. namespace Shiboken
  110. {
  111. /**
  112. * \internal
  113. * Data required to invoke a C++ destructor
  114. */
  115. struct DestructorEntry
  116. {
  117. ObjectDestructor destructor;
  118. void *cppInstance;
  119. };
  120. /**
  121. * Utility function used to transform a PyObject that implements sequence protocol into a std::list.
  122. **/
  123. std::vector<SbkObject *> splitPyObject(PyObject *pyObj);
  124. int getNumberOfCppBaseClasses(PyTypeObject *baseType);
  125. namespace Object
  126. {
  127. /**
  128. * Decrements the reference counters of every object referred by self.
  129. * \param self the wrapper instance that keeps references to other objects.
  130. */
  131. void clearReferences(SbkObject *self);
  132. /**
  133. * Destroy internal data
  134. **/
  135. void deallocData(SbkObject *self, bool doCleanup);
  136. void _debugFormat(std::ostream &str, SbkObject *self);
  137. } // namespace Object
  138. inline PyTypeObject *pyType(SbkObject *sbo)
  139. {
  140. return Py_TYPE(reinterpret_cast<PyObject *>(sbo));
  141. }
  142. } // namespace Shiboken
  143. #endif