test_stl_binders.cpp 4.51 KB
Newer Older
Sergey Lyskov's avatar
Sergey Lyskov committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/test_stl_binders.cpp -- Usage of stl_binders functions
Sergey Lyskov's avatar
Sergey Lyskov committed
3

4
    Copyright (c) 2016 Sergey Lyskov
Sergey Lyskov's avatar
Sergey Lyskov committed
5
6
7
8
9

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

10
11
12
#include <pybind11/numpy.h>
#include <pybind11/stl_bind.h>

Dean Moldovan's avatar
Dean Moldovan committed
13
#include "pybind11_tests.h"
Sergey Lyskov's avatar
Sergey Lyskov committed
14

15
#include <deque>
16
#include <map>
Wenzel Jakob's avatar
Wenzel Jakob committed
17
#include <unordered_map>
Sergey Lyskov's avatar
Sergey Lyskov committed
18

19
class El {
Sergey Lyskov's avatar
Sergey Lyskov committed
20
public:
21
    El() = delete;
22
    explicit El(int v) : a(v) {}
23

24
    int a;
Sergey Lyskov's avatar
Sergey Lyskov committed
25
26
};

27
std::ostream &operator<<(std::ostream &s, El const &v) {
28
29
    s << "El{" << v.a << '}';
    return s;
30
31
}

32
33
34
35
36
37
38
39
40
41
42
43
/// 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;
};

44
45
template <class Container>
Container *one_to_n(int n) {
46
    auto *v = new Container();
47
    for (int i = 1; i <= n; i++) {
48
        v->emplace_back(i);
49
    }
50
51
52
    return v;
}

53
54
template <class Map>
Map *times_ten(int n) {
55
    auto *m = new Map();
56
57
58
    for (int i = 1; i <= n; i++) {
        m->emplace(int(i), E_nc(10 * i));
    }
59
60
61
    return m;
}

62
63
template <class NestMap>
NestMap *times_hundred(int n) {
64
    auto *m = new NestMap();
65
66
67
68
69
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            (*m)[i].emplace(int(j * 10), E_nc(100 * j));
        }
    }
70
71
72
    return m;
}

73
74
TEST_SUBMODULE(stl_binders, m) {
    // test_vector_int
75
    py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
76
77

    // test_vector_custom
78
    py::class_<El>(m, "El").def(py::init<int>());
Wenzel Jakob's avatar
Wenzel Jakob committed
79
80
    py::bind_vector<std::vector<El>>(m, "VectorEl");
    py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
81

82
    // test_map_string_double
Wenzel Jakob's avatar
Wenzel Jakob committed
83
84
    py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
    py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Sergey Lyskov's avatar
Sergey Lyskov committed
85

86
    // test_map_string_double_const
Wenzel Jakob's avatar
Wenzel Jakob committed
87
    py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
88
89
    py::bind_map<std::unordered_map<std::string, double const>>(m,
                                                                "UnorderedMapStringDoubleConst");
90

91
    py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);
92

93
    // test_noncopyable_containers
94
    py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
95
    m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
96
    py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
97
    m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
98
    py::bind_map<std::map<int, E_nc>>(m, "MapENC");
99
    m.def("get_mnc", &times_ten<std::map<int, E_nc>>);
100
    py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
101
    m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>);
102
103
    // 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");
104
105
106
107
108
    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);
109
            }
110
111
112
        }
        return m;
    });
113
    py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
114
    m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>);
115
    py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
116
    m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
117
118
119
120

    // test_vector_buffer
    py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
    // no dtype declared for this version:
121
122
123
124
125
126
127
128
129
    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());
130
131
132
    });

    // The rest depends on numpy:
133
134
135
136
137
    try {
        py::module_::import("numpy");
    } catch (...) {
        return;
    }
138
139

    // test_vector_buffer_numpy
140
141
142
143
144
145
    struct VStruct {
        bool w;
        uint32_t x;
        double y;
        bool z;
    };
146
147
148
    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());
149
150
151
    m.def("get_vectorstruct", [] {
        return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
    });
152
}