Blame external/pybind11/tests/test_stl_binders.cpp

Packit 534379
/*
Packit 534379
    tests/test_stl_binders.cpp -- Usage of stl_binders functions
Packit 534379
Packit 534379
    Copyright (c) 2016 Sergey Lyskov
Packit 534379
Packit 534379
    All rights reserved. Use of this source code is governed by a
Packit 534379
    BSD-style license that can be found in the LICENSE file.
Packit 534379
*/
Packit 534379
Packit 534379
#include "pybind11_tests.h"
Packit 534379
Packit 534379
#include <pybind11/stl_bind.h>
Packit 534379
#include <pybind11/numpy.h>
Packit 534379
#include <map>
Packit 534379
#include <deque>
Packit 534379
#include <unordered_map>
Packit 534379
Packit 534379
class El {
Packit 534379
public:
Packit 534379
    El() = delete;
Packit 534379
    El(int v) : a(v) { }
Packit 534379
Packit 534379
    int a;
Packit 534379
};
Packit 534379
Packit 534379
std::ostream & operator<<(std::ostream &s, El const&v) {
Packit 534379
    s << "El{" << v.a << '}';
Packit 534379
    return s;
Packit 534379
}
Packit 534379
Packit 534379
/// Issue #487: binding std::vector<E> with E non-copyable
Packit 534379
class E_nc {
Packit 534379
public:
Packit 534379
    explicit E_nc(int i) : value{i} {}
Packit 534379
    E_nc(const E_nc &) = delete;
Packit 534379
    E_nc &operator=(const E_nc &) = delete;
Packit 534379
    E_nc(E_nc &&) = default;
Packit 534379
    E_nc &operator=(E_nc &&) = default;
Packit 534379
Packit 534379
    int value;
Packit 534379
};
Packit 534379
Packit 534379
template <class Container> Container *one_to_n(int n) {
Packit 534379
    auto v = new Container();
Packit 534379
    for (int i = 1; i <= n; i++)
Packit 534379
        v->emplace_back(i);
Packit 534379
    return v;
Packit 534379
}
Packit 534379
Packit 534379
template <class Map> Map *times_ten(int n) {
Packit 534379
    auto m = new Map();
Packit 534379
    for (int i = 1; i <= n; i++)
Packit 534379
        m->emplace(int(i), E_nc(10*i));
Packit 534379
    return m;
Packit 534379
}
Packit 534379
Packit 534379
TEST_SUBMODULE(stl_binders, m) {
Packit 534379
    // test_vector_int
Packit 534379
    py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
Packit 534379
Packit 534379
    // test_vector_custom
Packit 534379
    py::class_<El>(m, "El")
Packit 534379
        .def(py::init<int>());
Packit 534379
    py::bind_vector<std::vector<El>>(m, "VectorEl");
Packit 534379
    py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
Packit 534379
Packit 534379
    // test_map_string_double
Packit 534379
    py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
Packit 534379
    py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Packit 534379
Packit 534379
    // test_map_string_double_const
Packit 534379
    py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
Packit 534379
    py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
Packit 534379
Packit 534379
    py::class_<E_nc>(m, "ENC")
Packit 534379
        .def(py::init<int>())
Packit 534379
        .def_readwrite("value", &E_nc::value);
Packit 534379
Packit 534379
    // test_noncopyable_containers
Packit 534379
    py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
Packit 534379
    m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
Packit 534379
    py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
Packit 534379
    m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
Packit 534379
    py::bind_map<std::map<int, E_nc>>(m, "MapENC");
Packit 534379
    m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
Packit 534379
    py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
Packit 534379
    m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
Packit 534379
Packit 534379
    // test_vector_buffer
Packit 534379
    py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
Packit 534379
    // no dtype declared for this version:
Packit 534379
    struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
Packit 534379
    m.def("create_undeclstruct", [m] () mutable {
Packit 534379
        py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
Packit 534379
    });
Packit 534379
Packit 534379
    // The rest depends on numpy:
Packit 534379
    try { py::module::import("numpy"); }
Packit 534379
    catch (...) { return; }
Packit 534379
Packit 534379
    // test_vector_buffer_numpy
Packit 534379
    struct VStruct { bool w; uint32_t x; double y; bool z; };
Packit 534379
    PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
Packit 534379
    py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
Packit 534379
    py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
Packit 534379
    m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
Packit 534379
}