/* tests/test_stl_binders.cpp -- Usage of stl_binders functions Copyright (c) 2016 Sergey Lyskov All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #include "pybind11_tests.h" #include #include #include #include class El { public: El() = delete; El(int v) : a(v) { } int a; }; std::ostream & operator<<(std::ostream &s, El const&v) { s << "El{" << v.a << '}'; return s; } /// Issue #487: binding std::vector with E non-copyable class E_nc { public: explicit E_nc(int i) : value{i} {} E_nc(const E_nc &) = delete; E_nc &operator=(const E_nc &) = delete; E_nc(E_nc &&) = default; E_nc &operator=(E_nc &&) = default; int value; }; template Container *one_to_n(int n) { auto v = new Container(); for (int i = 1; i <= n; i++) v->emplace_back(i); return v; } template Map *times_ten(int n) { auto m = new Map(); for (int i = 1; i <= n; i++) m->emplace(int(i), E_nc(10*i)); return m; } test_initializer stl_binder_vector([](py::module &m) { py::class_(m, "El") .def(py::init()); py::bind_vector>(m, "VectorInt"); py::bind_vector>(m, "VectorBool"); py::bind_vector>(m, "VectorEl"); py::bind_vector>>(m, "VectorVectorEl"); }); test_initializer stl_binder_map([](py::module &m) { py::bind_map>(m, "MapStringDouble"); py::bind_map>(m, "UnorderedMapStringDouble"); py::bind_map>(m, "MapStringDoubleConst"); py::bind_map>(m, "UnorderedMapStringDoubleConst"); }); test_initializer stl_binder_noncopyable([](py::module &m) { py::class_(m, "ENC") .def(py::init()) .def_readwrite("value", &E_nc::value); py::bind_vector>(m, "VectorENC"); m.def("get_vnc", &one_to_n>, py::return_value_policy::reference); py::bind_vector>(m, "DequeENC"); m.def("get_dnc", &one_to_n>, py::return_value_policy::reference); py::bind_map>(m, "MapENC"); m.def("get_mnc", ×_ten>, py::return_value_policy::reference); py::bind_map>(m, "UmapENC"); m.def("get_umnc", ×_ten>, py::return_value_policy::reference); });