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.

156 lines
5.2 KiB

8 years ago
8 years ago
  1. STL containers
  2. ##############
  3. Automatic conversion
  4. ====================
  5. When including the additional header file :file:`pybind11/stl.h`, conversions
  6. between ``std::vector<>``/``std::list<>``/``std::array<>``,
  7. ``std::set<>``/``std::unordered_set<>``, and
  8. ``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
  9. ``dict`` data structures are automatically enabled. The types ``std::pair<>``
  10. and ``std::tuple<>`` are already supported out of the box with just the core
  11. :file:`pybind11/pybind11.h` header.
  12. The major downside of these implicit conversions is that containers must be
  13. converted (i.e. copied) on every Python->C++ and C++->Python transition, which
  14. can have implications on the program semantics and performance. Please read the
  15. next sections for more details and alternative approaches that avoid this.
  16. .. note::
  17. Arbitrary nesting of any of these types is possible.
  18. .. seealso::
  19. The file :file:`tests/test_python_types.cpp` contains a complete
  20. example that demonstrates how to pass STL data types in more detail.
  21. .. _opaque:
  22. Making opaque types
  23. ===================
  24. pybind11 heavily relies on a template matching mechanism to convert parameters
  25. and return values that are constructed from STL data types such as vectors,
  26. linked lists, hash tables, etc. This even works in a recursive manner, for
  27. instance to deal with lists of hash maps of pairs of elementary and custom
  28. types, etc.
  29. However, a fundamental limitation of this approach is that internal conversions
  30. between Python and C++ types involve a copy operation that prevents
  31. pass-by-reference semantics. What does this mean?
  32. Suppose we bind the following function
  33. .. code-block:: cpp
  34. void append_1(std::vector<int> &v) {
  35. v.push_back(1);
  36. }
  37. and call it from Python, the following happens:
  38. .. code-block:: pycon
  39. >>> v = [5, 6]
  40. >>> append_1(v)
  41. >>> print(v)
  42. [5, 6]
  43. As you can see, when passing STL data structures by reference, modifications
  44. are not propagated back the Python side. A similar situation arises when
  45. exposing STL data structures using the ``def_readwrite`` or ``def_readonly``
  46. functions:
  47. .. code-block:: cpp
  48. /* ... definition ... */
  49. class MyClass {
  50. std::vector<int> contents;
  51. };
  52. /* ... binding code ... */
  53. py::class_<MyClass>(m, "MyClass")
  54. .def(py::init<>())
  55. .def_readwrite("contents", &MyClass::contents);
  56. In this case, properties can be read and written in their entirety. However, an
  57. ``append`` operation involving such a list type has no effect:
  58. .. code-block:: pycon
  59. >>> m = MyClass()
  60. >>> m.contents = [5, 6]
  61. >>> print(m.contents)
  62. [5, 6]
  63. >>> m.contents.append(7)
  64. >>> print(m.contents)
  65. [5, 6]
  66. Finally, the involved copy operations can be costly when dealing with very
  67. large lists. To deal with all of the above situations, pybind11 provides a
  68. macro named ``PYBIND11_MAKE_OPAQUE(T)`` that disables the template-based
  69. conversion machinery of types, thus rendering them *opaque*. The contents of
  70. opaque objects are never inspected or extracted, hence they *can* be passed by
  71. reference. For instance, to turn ``std::vector<int>`` into an opaque type, add
  72. the declaration
  73. .. code-block:: cpp
  74. PYBIND11_MAKE_OPAQUE(std::vector<int>);
  75. before any binding code (e.g. invocations to ``class_::def()``, etc.). This
  76. macro must be specified at the top level (and outside of any namespaces), since
  77. it instantiates a partial template overload. If your binding code consists of
  78. multiple compilation units, it must be present in every file preceding any
  79. usage of ``std::vector<int>``. Opaque types must also have a corresponding
  80. ``class_`` declaration to associate them with a name in Python, and to define a
  81. set of available operations, e.g.:
  82. .. code-block:: cpp
  83. py::class_<std::vector<int>>(m, "IntVector")
  84. .def(py::init<>())
  85. .def("clear", &std::vector<int>::clear)
  86. .def("pop_back", &std::vector<int>::pop_back)
  87. .def("__len__", [](const std::vector<int> &v) { return v.size(); })
  88. .def("__iter__", [](std::vector<int> &v) {
  89. return py::make_iterator(v.begin(), v.end());
  90. }, py::keep_alive<0, 1>()) /* Keep vector alive while iterator is used */
  91. // ....
  92. The ability to expose STL containers as native Python objects is a fairly
  93. common request, hence pybind11 also provides an optional header file named
  94. :file:`pybind11/stl_bind.h` that does exactly this. The mapped containers try
  95. to match the behavior of their native Python counterparts as much as possible.
  96. The following example showcases usage of :file:`pybind11/stl_bind.h`:
  97. .. code-block:: cpp
  98. // Don't forget this
  99. #include <pybind11/stl_bind.h>
  100. PYBIND11_MAKE_OPAQUE(std::vector<int>);
  101. PYBIND11_MAKE_OPAQUE(std::map<std::string, double>);
  102. // ...
  103. // later in binding code:
  104. py::bind_vector<std::vector<int>>(m, "VectorInt");
  105. py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
  106. Please take a look at the :ref:`macro_notes` before using the
  107. ``PYBIND11_MAKE_OPAQUE`` macro.
  108. .. seealso::
  109. The file :file:`tests/test_opaque_types.cpp` contains a complete
  110. example that demonstrates how to create and expose opaque types using
  111. pybind11 in more detail.
  112. The file :file:`tests/test_stl_binders.cpp` shows how to use the
  113. convenience STL container wrappers.