You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.3 KiB

8 years ago
8 years ago
8 years ago
  1. Smart pointers
  2. ##############
  3. std::unique_ptr
  4. ===============
  5. Given a class ``Example`` with Python bindings, it's possible to return
  6. instances wrapped in C++11 unique pointers, like so
  7. .. code-block:: cpp
  8. std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); }
  9. .. code-block:: cpp
  10. m.def("create_example", &create_example);
  11. In other words, there is nothing special that needs to be done. While returning
  12. unique pointers in this way is allowed, it is *illegal* to use them as function
  13. arguments. For instance, the following function signature cannot be processed
  14. by pybind11.
  15. .. code-block:: cpp
  16. void do_something_with_example(std::unique_ptr<Example> ex) { ... }
  17. The above signature would imply that Python needs to give up ownership of an
  18. object that is passed to this function, which is generally not possible (for
  19. instance, the object might be referenced elsewhere).
  20. std::shared_ptr
  21. ===============
  22. The binding generator for classes, :class:`class_`, can be passed a template
  23. type that denotes a special *holder* type that is used to manage references to
  24. the object. If no such holder type template argument is given, the default for
  25. a type named ``Type`` is ``std::unique_ptr<Type>``, which means that the object
  26. is deallocated when Python's reference count goes to zero.
  27. It is possible to switch to other types of reference counting wrappers or smart
  28. pointers, which is useful in codebases that rely on them. For instance, the
  29. following snippet causes ``std::shared_ptr`` to be used instead.
  30. .. code-block:: cpp
  31. py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example");
  32. Note that any particular class can only be associated with a single holder type.
  33. One potential stumbling block when using holder types is that they need to be
  34. applied consistently. Can you guess what's broken about the following binding
  35. code?
  36. .. code-block:: cpp
  37. class Child { };
  38. class Parent {
  39. public:
  40. Parent() : child(std::make_shared<Child>()) { }
  41. Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */
  42. private:
  43. std::shared_ptr<Child> child;
  44. };
  45. PYBIND11_PLUGIN(example) {
  46. py::module m("example");
  47. py::class_<Child, std::shared_ptr<Child>>(m, "Child");
  48. py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
  49. .def(py::init<>())
  50. .def("get_child", &Parent::get_child);
  51. return m.ptr();
  52. }
  53. The following Python code will cause undefined behavior (and likely a
  54. segmentation fault).
  55. .. code-block:: python
  56. from example import Parent
  57. print(Parent().get_child())
  58. The problem is that ``Parent::get_child()`` returns a pointer to an instance of
  59. ``Child``, but the fact that this instance is already managed by
  60. ``std::shared_ptr<...>`` is lost when passing raw pointers. In this case,
  61. pybind11 will create a second independent ``std::shared_ptr<...>`` that also
  62. claims ownership of the pointer. In the end, the object will be freed **twice**
  63. since these shared pointers have no way of knowing about each other.
  64. There are two ways to resolve this issue:
  65. 1. For types that are managed by a smart pointer class, never use raw pointers
  66. in function arguments or return values. In other words: always consistently
  67. wrap pointers into their designated holder types (such as
  68. ``std::shared_ptr<...>``). In this case, the signature of ``get_child()``
  69. should be modified as follows:
  70. .. code-block:: cpp
  71. std::shared_ptr<Child> get_child() { return child; }
  72. 2. Adjust the definition of ``Child`` by specifying
  73. ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a
  74. base class. This adds a small bit of information to ``Child`` that allows
  75. pybind11 to realize that there is already an existing
  76. ``std::shared_ptr<...>`` and communicate with it. In this case, the
  77. declaration of ``Child`` should look as follows:
  78. .. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
  79. .. code-block:: cpp
  80. class Child : public std::enable_shared_from_this<Child> { };
  81. .. _smart_pointers:
  82. Custom smart pointers
  83. =====================
  84. pybind11 supports ``std::unique_ptr`` and ``std::shared_ptr`` right out of the
  85. box. For any other custom smart pointer, transparent conversions can be enabled
  86. using a macro invocation similar to the following. It must be declared at the
  87. top namespace level before any binding code:
  88. .. code-block:: cpp
  89. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
  90. The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
  91. placeholder name that is used as a template parameter of the second argument.
  92. Thus, feel free to use any identifier, but use it consistently on both sides;
  93. also, don't use the name of a type that already exists in your codebase.
  94. The macro also accepts a third optional boolean parameter that is set to false
  95. by default. Specify
  96. .. code-block:: cpp
  97. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true);
  98. if ``SmartPtr<T>`` can always be initialized from a ``T*`` pointer without the
  99. risk of inconsistencies (such as multiple independent ``SmartPtr`` instances
  100. believing that they are the sole owner of the ``T*`` pointer). A common
  101. situation where ``true`` should be passed is when the ``T`` instances use
  102. *intrusive* reference counting.
  103. Please take a look at the :ref:`macro_notes` before using this feature.
  104. By default, pybind11 assumes that your custom smart pointer has a standard
  105. interface, i.e. provides a ``.get()`` member function to access the underlying
  106. raw pointer. If this is not the case, pybind11's ``holder_helper`` must be
  107. specialized:
  108. .. code-block:: cpp
  109. // Always needed for custom holder types
  110. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
  111. // Only needed if the type's `.get()` goes by another name
  112. namespace pybind11 { namespace detail {
  113. template <typename T>
  114. struct holder_helper<SmartPtr<T>> { // <-- specialization
  115. static const T *get(const SmartPtr<T> &p) { return p.getPointer(); }
  116. };
  117. }}
  118. The above specialization informs pybind11 that the custom ``SmartPtr`` class
  119. provides ``.get()`` functionality via ``.getPointer()``.
  120. .. seealso::
  121. The file :file:`tests/test_smart_ptr.cpp` contains a complete example
  122. that demonstrates how to work with custom reference-counting holder types
  123. in more detail.