test_stl_binders.cpp revision 11986
12SN/A/*
21762SN/A    tests/test_stl_binders.cpp -- Usage of stl_binders functions
32SN/A
42SN/A    Copyright (c) 2016 Sergey Lyskov
52SN/A
62SN/A    All rights reserved. Use of this source code is governed by a
72SN/A    BSD-style license that can be found in the LICENSE file.
82SN/A*/
92SN/A
102SN/A#include "pybind11_tests.h"
112SN/A
122SN/A#include <pybind11/stl_bind.h>
132SN/A#include <map>
142SN/A#include <deque>
152SN/A#include <unordered_map>
162SN/A
172SN/Aclass El {
182SN/Apublic:
192SN/A    El() = delete;
202SN/A    El(int v) : a(v) { }
212SN/A
222SN/A    int a;
232SN/A};
242SN/A
252SN/Astd::ostream & operator<<(std::ostream &s, El const&v) {
262SN/A    s << "El{" << v.a << '}';
272665SN/A    return s;
282665SN/A}
292665SN/A
302SN/A/// Issue #487: binding std::vector<E> with E non-copyable
312SN/Aclass E_nc {
328229SN/Apublic:
332SN/A    explicit E_nc(int i) : value{i} {}
342SN/A    E_nc(const E_nc &) = delete;
352SN/A    E_nc &operator=(const E_nc &) = delete;
365034SN/A    E_nc(E_nc &&) = default;
375034SN/A    E_nc &operator=(E_nc &&) = default;
382SN/A
392SN/A    int value;
40295SN/A};
41295SN/A
42295SN/Atemplate <class Container> Container *one_to_n(int n) {
43295SN/A    auto v = new Container();
44295SN/A    for (int i = 1; i <= n; i++)
45295SN/A        v->emplace_back(i);
46295SN/A    return v;
47295SN/A}
48295SN/A
49295SN/Atemplate <class Map> Map *times_ten(int n) {
504762SN/A    auto m = new Map();
514762SN/A    for (int i = 1; i <= n; i++)
522SN/A        m->emplace(int(i), E_nc(10*i));
535034SN/A    return m;
542SN/A}
55
56test_initializer stl_binder_vector([](py::module &m) {
57    py::class_<El>(m, "El")
58        .def(py::init<int>());
59
60    py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
61    py::bind_vector<std::vector<bool>>(m, "VectorBool");
62
63    py::bind_vector<std::vector<El>>(m, "VectorEl");
64
65    py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
66
67});
68
69test_initializer stl_binder_map([](py::module &m) {
70    py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
71    py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
72
73    py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
74    py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
75
76});
77
78test_initializer stl_binder_noncopyable([](py::module &m) {
79    py::class_<E_nc>(m, "ENC")
80        .def(py::init<int>())
81        .def_readwrite("value", &E_nc::value);
82
83    py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
84    m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
85
86    py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
87    m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
88
89    py::bind_map<std::map<int, E_nc>>(m, "MapENC");
90    m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
91
92    py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
93    m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
94});
95
96