1/* 2 tests/test_stl_binders.cpp -- Usage of stl_binders functions 3 4 Copyright (c) 2016 Sergey Lyskov 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 10#include "pybind11_tests.h" 11 12#include <pybind11/stl_bind.h>
| 1/* 2 tests/test_stl_binders.cpp -- Usage of stl_binders functions 3 4 Copyright (c) 2016 Sergey Lyskov 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 10#include "pybind11_tests.h" 11 12#include <pybind11/stl_bind.h>
|
| 13#include <pybind11/numpy.h>
|
13#include <map> 14#include <deque> 15#include <unordered_map> 16
| 14#include <map> 15#include <deque> 16#include <unordered_map> 17
|
| 18#ifdef _MSC_VER 19// We get some really long type names here which causes MSVC to emit warnings 20# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated 21#endif 22
|
17class El { 18public: 19 El() = delete; 20 El(int v) : a(v) { } 21 22 int a; 23}; 24 25std::ostream & operator<<(std::ostream &s, El const&v) { 26 s << "El{" << v.a << '}'; 27 return s; 28} 29 30/// Issue #487: binding std::vector<E> with E non-copyable 31class E_nc { 32public: 33 explicit E_nc(int i) : value{i} {} 34 E_nc(const E_nc &) = delete; 35 E_nc &operator=(const E_nc &) = delete; 36 E_nc(E_nc &&) = default; 37 E_nc &operator=(E_nc &&) = default; 38 39 int value; 40}; 41 42template <class Container> Container *one_to_n(int n) { 43 auto v = new Container(); 44 for (int i = 1; i <= n; i++) 45 v->emplace_back(i); 46 return v; 47} 48 49template <class Map> Map *times_ten(int n) { 50 auto m = new Map(); 51 for (int i = 1; i <= n; i++) 52 m->emplace(int(i), E_nc(10*i)); 53 return m; 54} 55
| 23class El { 24public: 25 El() = delete; 26 El(int v) : a(v) { } 27 28 int a; 29}; 30 31std::ostream & operator<<(std::ostream &s, El const&v) { 32 s << "El{" << v.a << '}'; 33 return s; 34} 35 36/// Issue #487: binding std::vector<E> with E non-copyable 37class E_nc { 38public: 39 explicit E_nc(int i) : value{i} {} 40 E_nc(const E_nc &) = delete; 41 E_nc &operator=(const E_nc &) = delete; 42 E_nc(E_nc &&) = default; 43 E_nc &operator=(E_nc &&) = default; 44 45 int value; 46}; 47 48template <class Container> Container *one_to_n(int n) { 49 auto v = new Container(); 50 for (int i = 1; i <= n; i++) 51 v->emplace_back(i); 52 return v; 53} 54 55template <class Map> Map *times_ten(int n) { 56 auto m = new Map(); 57 for (int i = 1; i <= n; i++) 58 m->emplace(int(i), E_nc(10*i)); 59 return m; 60} 61
|
| 62struct VStruct { 63 bool w; 64 uint32_t x; 65 double y; 66 bool z; 67}; 68 69struct VUndeclStruct { //dtype not declared for this version 70 bool w; 71 uint32_t x; 72 double y; 73 bool z; 74}; 75
|
56test_initializer stl_binder_vector([](py::module &m) { 57 py::class_<El>(m, "El") 58 .def(py::init<int>()); 59
| 76test_initializer stl_binder_vector([](py::module &m) { 77 py::class_<El>(m, "El") 78 .def(py::init<int>()); 79
|
60 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
| 80 py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol()); 81 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
|
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
| 82 py::bind_vector<std::vector<bool>>(m, "VectorBool"); 83 84 py::bind_vector<std::vector<El>>(m, "VectorEl"); 85 86 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl"); 87
|
| 88 m.def("create_undeclstruct", [m] () mutable { 89 py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol()); 90 }); 91 92 try { 93 py::module::import("numpy"); 94 } catch (...) { 95 return; 96 } 97 PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z); 98 py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x); 99 py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol()); 100 m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
|
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", ×_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", ×_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference); 94});
| 101}); 102 103test_initializer stl_binder_map([](py::module &m) { 104 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble"); 105 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble"); 106 107 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst"); 108 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst"); 109 110}); 111 112test_initializer stl_binder_noncopyable([](py::module &m) { 113 py::class_<E_nc>(m, "ENC") 114 .def(py::init<int>()) 115 .def_readwrite("value", &E_nc::value); 116 117 py::bind_vector<std::vector<E_nc>>(m, "VectorENC"); 118 m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference); 119 120 py::bind_vector<std::deque<E_nc>>(m, "DequeENC"); 121 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference); 122 123 py::bind_map<std::map<int, E_nc>>(m, "MapENC"); 124 m.def("get_mnc", ×_ten<std::map<int, E_nc>>, py::return_value_policy::reference); 125 126 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC"); 127 m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference); 128});
|
95
| |
| |