Back to home page

EIC code displayed by LXR

 
 

    


Warning, /jana2/src/python/externals/pybind11-2.10.3/docs/advanced/cast/stl.rst is written in an unsupported language. File is not indexed.

0001 STL containers
0002 ##############
0003 
0004 Automatic conversion
0005 ====================
0006 
0007 When including the additional header file :file:`pybind11/stl.h`, conversions
0008 between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``/``std::valarray<>``,
0009 ``std::set<>``/``std::unordered_set<>``, and
0010 ``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
0011 ``dict`` data structures are automatically enabled. The types ``std::pair<>``
0012 and ``std::tuple<>`` are already supported out of the box with just the core
0013 :file:`pybind11/pybind11.h` header.
0014 
0015 The major downside of these implicit conversions is that containers must be
0016 converted (i.e. copied) on every Python->C++ and C++->Python transition, which
0017 can have implications on the program semantics and performance. Please read the
0018 next sections for more details and alternative approaches that avoid this.
0019 
0020 .. note::
0021 
0022     Arbitrary nesting of any of these types is possible.
0023 
0024 .. seealso::
0025 
0026     The file :file:`tests/test_stl.cpp` contains a complete
0027     example that demonstrates how to pass STL data types in more detail.
0028 
0029 .. _cpp17_container_casters:
0030 
0031 C++17 library containers
0032 ========================
0033 
0034 The :file:`pybind11/stl.h` header also includes support for ``std::optional<>``
0035 and ``std::variant<>``. These require a C++17 compiler and standard library.
0036 In C++14 mode, ``std::experimental::optional<>`` is supported if available.
0037 
0038 Various versions of these containers also exist for C++11 (e.g. in Boost).
0039 pybind11 provides an easy way to specialize the ``type_caster`` for such
0040 types:
0041 
0042 .. code-block:: cpp
0043 
0044     // `boost::optional` as an example -- can be any `std::optional`-like container
0045     namespace PYBIND11_NAMESPACE { namespace detail {
0046         template <typename T>
0047         struct type_caster<boost::optional<T>> : optional_caster<boost::optional<T>> {};
0048     }}
0049 
0050 The above should be placed in a header file and included in all translation units
0051 where automatic conversion is needed. Similarly, a specialization can be provided
0052 for custom variant types:
0053 
0054 .. code-block:: cpp
0055 
0056     // `boost::variant` as an example -- can be any `std::variant`-like container
0057     namespace PYBIND11_NAMESPACE { namespace detail {
0058         template <typename... Ts>
0059         struct type_caster<boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {};
0060 
0061         // Specifies the function used to visit the variant -- `apply_visitor` instead of `visit`
0062         template <>
0063         struct visit_helper<boost::variant> {
0064             template <typename... Args>
0065             static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) {
0066                 return boost::apply_visitor(args...);
0067             }
0068         };
0069     }} // namespace PYBIND11_NAMESPACE::detail
0070 
0071 The ``visit_helper`` specialization is not required if your ``name::variant`` provides
0072 a ``name::visit()`` function. For any other function name, the specialization must be
0073 included to tell pybind11 how to visit the variant.
0074 
0075 .. warning::
0076 
0077     When converting a ``variant`` type, pybind11 follows the same rules as when
0078     determining which function overload to call (:ref:`overload_resolution`), and
0079     so the same caveats hold. In particular, the order in which the ``variant``'s
0080     alternatives are listed is important, since pybind11 will try conversions in
0081     this order. This means that, for example, when converting ``variant<int, bool>``,
0082     the ``bool`` variant will never be selected, as any Python ``bool`` is already
0083     an ``int`` and is convertible to a C++ ``int``. Changing the order of alternatives
0084     (and using ``variant<bool, int>``, in this example) provides a solution.
0085 
0086 .. note::
0087 
0088     pybind11 only supports the modern implementation of ``boost::variant``
0089     which makes use of variadic templates. This requires Boost 1.56 or newer.
0090 
0091 .. _opaque:
0092 
0093 Making opaque types
0094 ===================
0095 
0096 pybind11 heavily relies on a template matching mechanism to convert parameters
0097 and return values that are constructed from STL data types such as vectors,
0098 linked lists, hash tables, etc. This even works in a recursive manner, for
0099 instance to deal with lists of hash maps of pairs of elementary and custom
0100 types, etc.
0101 
0102 However, a fundamental limitation of this approach is that internal conversions
0103 between Python and C++ types involve a copy operation that prevents
0104 pass-by-reference semantics. What does this mean?
0105 
0106 Suppose we bind the following function
0107 
0108 .. code-block:: cpp
0109 
0110     void append_1(std::vector<int> &v) {
0111        v.push_back(1);
0112     }
0113 
0114 and call it from Python, the following happens:
0115 
0116 .. code-block:: pycon
0117 
0118    >>> v = [5, 6]
0119    >>> append_1(v)
0120    >>> print(v)
0121    [5, 6]
0122 
0123 As you can see, when passing STL data structures by reference, modifications
0124 are not propagated back the Python side. A similar situation arises when
0125 exposing STL data structures using the ``def_readwrite`` or ``def_readonly``
0126 functions:
0127 
0128 .. code-block:: cpp
0129 
0130     /* ... definition ... */
0131 
0132     class MyClass {
0133         std::vector<int> contents;
0134     };
0135 
0136     /* ... binding code ... */
0137 
0138     py::class_<MyClass>(m, "MyClass")
0139         .def(py::init<>())
0140         .def_readwrite("contents", &MyClass::contents);
0141 
0142 In this case, properties can be read and written in their entirety. However, an
0143 ``append`` operation involving such a list type has no effect:
0144 
0145 .. code-block:: pycon
0146 
0147    >>> m = MyClass()
0148    >>> m.contents = [5, 6]
0149    >>> print(m.contents)
0150    [5, 6]
0151    >>> m.contents.append(7)
0152    >>> print(m.contents)
0153    [5, 6]
0154 
0155 Finally, the involved copy operations can be costly when dealing with very
0156 large lists. To deal with all of the above situations, pybind11 provides a
0157 macro named ``PYBIND11_MAKE_OPAQUE(T)`` that disables the template-based
0158 conversion machinery of types, thus rendering them *opaque*. The contents of
0159 opaque objects are never inspected or extracted, hence they *can* be passed by
0160 reference. For instance, to turn ``std::vector<int>`` into an opaque type, add
0161 the declaration
0162 
0163 .. code-block:: cpp
0164 
0165     PYBIND11_MAKE_OPAQUE(std::vector<int>);
0166 
0167 before any binding code (e.g. invocations to ``class_::def()``, etc.). This
0168 macro must be specified at the top level (and outside of any namespaces), since
0169 it adds a template instantiation of ``type_caster``. If your binding code consists of
0170 multiple compilation units, it must be present in every file (typically via a
0171 common header) preceding any usage of ``std::vector<int>``. Opaque types must
0172 also have a corresponding ``class_`` declaration to associate them with a name
0173 in Python, and to define a set of available operations, e.g.:
0174 
0175 .. code-block:: cpp
0176 
0177     py::class_<std::vector<int>>(m, "IntVector")
0178         .def(py::init<>())
0179         .def("clear", &std::vector<int>::clear)
0180         .def("pop_back", &std::vector<int>::pop_back)
0181         .def("__len__", [](const std::vector<int> &v) { return v.size(); })
0182         .def("__iter__", [](std::vector<int> &v) {
0183            return py::make_iterator(v.begin(), v.end());
0184         }, py::keep_alive<0, 1>()) /* Keep vector alive while iterator is used */
0185         // ....
0186 
0187 .. seealso::
0188 
0189     The file :file:`tests/test_opaque_types.cpp` contains a complete
0190     example that demonstrates how to create and expose opaque types using
0191     pybind11 in more detail.
0192 
0193 .. _stl_bind:
0194 
0195 Binding STL containers
0196 ======================
0197 
0198 The ability to expose STL containers as native Python objects is a fairly
0199 common request, hence pybind11 also provides an optional header file named
0200 :file:`pybind11/stl_bind.h` that does exactly this. The mapped containers try
0201 to match the behavior of their native Python counterparts as much as possible.
0202 
0203 The following example showcases usage of :file:`pybind11/stl_bind.h`:
0204 
0205 .. code-block:: cpp
0206 
0207     // Don't forget this
0208     #include <pybind11/stl_bind.h>
0209 
0210     PYBIND11_MAKE_OPAQUE(std::vector<int>);
0211     PYBIND11_MAKE_OPAQUE(std::map<std::string, double>);
0212 
0213     // ...
0214 
0215     // later in binding code:
0216     py::bind_vector<std::vector<int>>(m, "VectorInt");
0217     py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
0218 
0219 When binding STL containers pybind11 considers the types of the container's
0220 elements to decide whether the container should be confined to the local module
0221 (via the :ref:`module_local` feature).  If the container element types are
0222 anything other than already-bound custom types bound without
0223 ``py::module_local()`` the container binding will have ``py::module_local()``
0224 applied.  This includes converting types such as numeric types, strings, Eigen
0225 types; and types that have not yet been bound at the time of the stl container
0226 binding.  This module-local binding is designed to avoid potential conflicts
0227 between module bindings (for example, from two separate modules each attempting
0228 to bind ``std::vector<int>`` as a python type).
0229 
0230 It is possible to override this behavior to force a definition to be either
0231 module-local or global.  To do so, you can pass the attributes
0232 ``py::module_local()`` (to make the binding module-local) or
0233 ``py::module_local(false)`` (to make the binding global) into the
0234 ``py::bind_vector`` or ``py::bind_map`` arguments:
0235 
0236 .. code-block:: cpp
0237 
0238     py::bind_vector<std::vector<int>>(m, "VectorInt", py::module_local(false));
0239 
0240 Note, however, that such a global binding would make it impossible to load this
0241 module at the same time as any other pybind module that also attempts to bind
0242 the same container type (``std::vector<int>`` in the above example).
0243 
0244 See :ref:`module_local` for more details on module-local bindings.
0245 
0246 .. seealso::
0247 
0248     The file :file:`tests/test_stl_binders.cpp` shows how to use the
0249     convenience STL container wrappers.