Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_stl_binders.cpp -- Usage of stl_binders functions |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 3 | |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Sergey Lyskov |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 11 | |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 12 | #include <pybind11/stl_bind.h> |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 13 | #include <map> |
Jason Rhinelander | 617fbcf | 2016-11-15 06:30:38 -0500 | [diff] [blame] | 14 | #include <deque> |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 15 | #include <unordered_map> |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 16 | |
Jason Rhinelander | fa5d05e | 2016-12-12 18:11:49 -0500 | [diff] [blame] | 17 | #ifdef _MSC_VER |
| 18 | // We get some really long type names here which causes MSVC to emit warnings |
| 19 | # pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated |
| 20 | #endif |
| 21 | |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 22 | class El { |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 23 | public: |
Jason Rhinelander | 540ae61 | 2016-08-28 13:46:25 -0400 | [diff] [blame] | 24 | El() = delete; |
| 25 | El(int v) : a(v) { } |
Sergey Lyskov | a95bde1 | 2016-05-08 19:31:55 -0400 | [diff] [blame] | 26 | |
Jason Rhinelander | 540ae61 | 2016-08-28 13:46:25 -0400 | [diff] [blame] | 27 | int a; |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 30 | std::ostream & operator<<(std::ostream &s, El const&v) { |
Jason Rhinelander | 540ae61 | 2016-08-28 13:46:25 -0400 | [diff] [blame] | 31 | s << "El{" << v.a << '}'; |
| 32 | return s; |
Sergey Lyskov | a95bde1 | 2016-05-08 19:31:55 -0400 | [diff] [blame] | 33 | } |
Sergey Lyskov | eae7744 | 2016-05-07 00:26:19 -0400 | [diff] [blame] | 34 | |
Jason Rhinelander | 617fbcf | 2016-11-15 06:30:38 -0500 | [diff] [blame] | 35 | /// Issue #487: binding std::vector<E> with E non-copyable |
| 36 | class E_nc { |
| 37 | public: |
| 38 | explicit E_nc(int i) : value{i} {} |
| 39 | E_nc(const E_nc &) = delete; |
| 40 | E_nc &operator=(const E_nc &) = delete; |
| 41 | E_nc(E_nc &&) = default; |
| 42 | E_nc &operator=(E_nc &&) = default; |
| 43 | |
| 44 | int value; |
| 45 | }; |
| 46 | |
| 47 | template <class Container> Container *one_to_n(int n) { |
| 48 | auto v = new Container(); |
| 49 | for (int i = 1; i <= n; i++) |
| 50 | v->emplace_back(i); |
| 51 | return v; |
| 52 | } |
| 53 | |
| 54 | template <class Map> Map *times_ten(int n) { |
| 55 | auto m = new Map(); |
| 56 | for (int i = 1; i <= n; i++) |
| 57 | m->emplace(int(i), E_nc(10*i)); |
| 58 | return m; |
| 59 | } |
| 60 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 61 | test_initializer stl_binder_vector([](py::module &m) { |
Jason Rhinelander | 540ae61 | 2016-08-28 13:46:25 -0400 | [diff] [blame] | 62 | py::class_<El>(m, "El") |
| 63 | .def(py::init<int>()); |
Sergey Lyskov | a95bde1 | 2016-05-08 19:31:55 -0400 | [diff] [blame] | 64 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 65 | py::bind_vector<std::vector<unsigned int>>(m, "VectorInt"); |
| 66 | py::bind_vector<std::vector<bool>>(m, "VectorBool"); |
Sergey Lyskov | a95bde1 | 2016-05-08 19:31:55 -0400 | [diff] [blame] | 67 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 68 | py::bind_vector<std::vector<El>>(m, "VectorEl"); |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 69 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 70 | py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl"); |
Jason Rhinelander | 617fbcf | 2016-11-15 06:30:38 -0500 | [diff] [blame] | 71 | |
Sergey Lyskov | 7520418 | 2016-08-29 22:50:38 -0400 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | test_initializer stl_binder_map([](py::module &m) { |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 75 | py::bind_map<std::map<std::string, double>>(m, "MapStringDouble"); |
| 76 | py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble"); |
Sergey Lyskov | 7520418 | 2016-08-29 22:50:38 -0400 | [diff] [blame] | 77 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 78 | py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst"); |
| 79 | py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst"); |
Jason Rhinelander | 617fbcf | 2016-11-15 06:30:38 -0500 | [diff] [blame] | 80 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 81 | }); |
Jason Rhinelander | 617fbcf | 2016-11-15 06:30:38 -0500 | [diff] [blame] | 82 | |
| 83 | test_initializer stl_binder_noncopyable([](py::module &m) { |
| 84 | py::class_<E_nc>(m, "ENC") |
| 85 | .def(py::init<int>()) |
| 86 | .def_readwrite("value", &E_nc::value); |
| 87 | |
| 88 | py::bind_vector<std::vector<E_nc>>(m, "VectorENC"); |
| 89 | m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference); |
| 90 | |
| 91 | py::bind_vector<std::deque<E_nc>>(m, "DequeENC"); |
| 92 | m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference); |
| 93 | |
| 94 | py::bind_map<std::map<int, E_nc>>(m, "MapENC"); |
| 95 | m.def("get_mnc", ×_ten<std::map<int, E_nc>>, py::return_value_policy::reference); |
| 96 | |
| 97 | py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC"); |
| 98 | m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference); |
| 99 | }); |
| 100 | |