2021-03-08 14:54:22 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
#include <pybind11/numpy.h>
|
|
|
|
#include <pybind11/stl_bind.h>
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
#include <deque>
|
2022-06-20 22:59:56 +00:00
|
|
|
#include <map>
|
2021-03-08 14:54:22 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
class El {
|
|
|
|
public:
|
|
|
|
El() = delete;
|
2022-06-20 22:59:56 +00:00
|
|
|
explicit El(int v) : a(v) {}
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
std::ostream &operator<<(std::ostream &s, El const &v) {
|
2021-03-08 14:54:22 +00:00
|
|
|
s << "El{" << v.a << '}';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Issue #487: binding std::vector<E> 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;
|
|
|
|
};
|
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
template <class Container>
|
|
|
|
Container *one_to_n(int n) {
|
|
|
|
auto *v = new Container();
|
|
|
|
for (int i = 1; i <= n; i++) {
|
2021-03-08 14:54:22 +00:00
|
|
|
v->emplace_back(i);
|
2022-06-20 22:59:56 +00:00
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
template <class Map>
|
|
|
|
Map *times_ten(int n) {
|
|
|
|
auto *m = new Map();
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
m->emplace(int(i), E_nc(10 * i));
|
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
template <class NestMap>
|
|
|
|
NestMap *times_hundred(int n) {
|
|
|
|
auto *m = new NestMap();
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
(*m)[i].emplace(int(j * 10), E_nc(100 * j));
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_SUBMODULE(stl_binders, m) {
|
|
|
|
// test_vector_int
|
|
|
|
py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
|
|
|
|
|
|
|
|
// test_vector_custom
|
2022-06-20 22:59:56 +00:00
|
|
|
py::class_<El>(m, "El").def(py::init<int>());
|
2021-03-08 14:54:22 +00:00
|
|
|
py::bind_vector<std::vector<El>>(m, "VectorEl");
|
|
|
|
py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
|
|
|
|
|
|
|
|
// test_map_string_double
|
|
|
|
py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
|
|
|
|
py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
|
|
|
|
|
|
|
|
// test_map_string_double_const
|
|
|
|
py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
|
2022-06-20 22:59:56 +00:00
|
|
|
py::bind_map<std::unordered_map<std::string, double const>>(m,
|
|
|
|
"UnorderedMapStringDoubleConst");
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
// test_noncopyable_containers
|
|
|
|
py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
|
|
|
|
m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
|
|
|
|
py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
|
|
|
|
m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
|
|
|
|
py::bind_map<std::map<int, E_nc>>(m, "MapENC");
|
|
|
|
m.def("get_mnc", ×_ten<std::map<int, E_nc>>);
|
|
|
|
py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
|
|
|
|
m.def("get_umnc", ×_ten<std::unordered_map<int, E_nc>>);
|
|
|
|
// Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
|
|
|
|
py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
|
2022-06-20 22:59:56 +00:00
|
|
|
m.def("get_nvnc", [](int n) {
|
|
|
|
auto *m = new std::map<int, std::vector<E_nc>>();
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
(*m)[i].emplace_back(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
});
|
2021-03-08 14:54:22 +00:00
|
|
|
py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
|
|
|
|
m.def("get_nmnc", ×_hundred<std::map<int, std::map<int, E_nc>>>);
|
|
|
|
py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
|
|
|
|
m.def("get_numnc", ×_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
|
|
|
|
|
|
|
|
// test_vector_buffer
|
|
|
|
py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
|
|
|
|
// no dtype declared for this version:
|
2022-06-20 22:59:56 +00:00
|
|
|
struct VUndeclStruct {
|
|
|
|
bool w;
|
|
|
|
uint32_t x;
|
|
|
|
double y;
|
|
|
|
bool z;
|
|
|
|
};
|
|
|
|
m.def("create_undeclstruct", [m]() mutable {
|
|
|
|
py::bind_vector<std::vector<VUndeclStruct>>(
|
|
|
|
m, "VectorUndeclStruct", py::buffer_protocol());
|
2021-03-08 14:54:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// The rest depends on numpy:
|
2022-06-20 22:59:56 +00:00
|
|
|
try {
|
|
|
|
py::module_::import("numpy");
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
// test_vector_buffer_numpy
|
2022-06-20 22:59:56 +00:00
|
|
|
struct VStruct {
|
|
|
|
bool w;
|
|
|
|
uint32_t x;
|
|
|
|
double y;
|
|
|
|
bool z;
|
|
|
|
};
|
2021-03-08 14:54:22 +00:00
|
|
|
PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
|
|
|
|
py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
|
|
|
|
py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
|
2021-08-30 22:58:05 +00:00
|
|
|
m.def("get_vectorstruct", [] {
|
|
|
|
return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
|
|
|
|
});
|
2021-03-08 14:54:22 +00:00
|
|
|
}
|