"example/example-arg-keywords-and-defaults.cpp" did not exist on "6c03beb86731ad0c9797b04a29dfa36a38204e18"
test_stl_binders.cpp 3.69 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.
*/

Dean Moldovan's avatar
Dean Moldovan committed
10
#include "pybind11_tests.h"
Sergey Lyskov's avatar
Sergey Lyskov committed
11

12
#include <pybind11/stl_bind.h>
13
#include <pybind11/numpy.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
14
#include <map>
15
#include <deque>
Wenzel Jakob's avatar
Wenzel Jakob committed
16
#include <unordered_map>
Sergey Lyskov's avatar
Sergey Lyskov committed
17

18
19
20
21
22
#ifdef _MSC_VER
// We get some really long type names here which causes MSVC to emit warnings
#  pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
#endif

23
class El {
Sergey Lyskov's avatar
Sergey Lyskov committed
24
public:
25
26
    El() = delete;
    El(int v) : a(v) { }
27

28
    int a;
Sergey Lyskov's avatar
Sergey Lyskov committed
29
30
};

31
std::ostream & operator<<(std::ostream &s, El const&v) {
32
33
    s << "El{" << v.a << '}';
    return s;
34
35
}

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// 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;
};

template <class Container> Container *one_to_n(int n) {
    auto v = new Container();
    for (int i = 1; i <= n; i++)
        v->emplace_back(i);
    return v;
}

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));
    return m;
}

62
63
64
65
66
67
68
69
70
71
72
73
74
75
struct VStruct {
    bool w;
    uint32_t x;
    double y;
    bool z;
};

struct VUndeclStruct { //dtype not declared for this version
    bool w;
    uint32_t x;
    double y;
    bool z;
};

76
test_initializer stl_binder_vector([](py::module &m) {
77
78
    py::class_<El>(m, "El")
        .def(py::init<int>());
79

80
81
    py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
    py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
Wenzel Jakob's avatar
Wenzel Jakob committed
82
    py::bind_vector<std::vector<bool>>(m, "VectorBool");
83

Wenzel Jakob's avatar
Wenzel Jakob committed
84
    py::bind_vector<std::vector<El>>(m, "VectorEl");
Sergey Lyskov's avatar
Sergey Lyskov committed
85

Wenzel Jakob's avatar
Wenzel Jakob committed
86
    py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
87

88
89
90
91
92
93
94
95
96
97
98
99
100
    m.def("create_undeclstruct", [m] () mutable {
        py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
    });

    try {
        py::module::import("numpy");
    } catch (...) {
        return;
    }
    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());
    m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
Sergey Lyskov's avatar
Sergey Lyskov committed
101
102
103
});

test_initializer stl_binder_map([](py::module &m) {
Wenzel Jakob's avatar
Wenzel Jakob committed
104
105
    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
106

Wenzel Jakob's avatar
Wenzel Jakob committed
107
108
    py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
    py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

});

test_initializer stl_binder_noncopyable([](py::module &m) {
    py::class_<E_nc>(m, "ENC")
        .def(py::init<int>())
        .def_readwrite("value", &E_nc::value);

    py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
    m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);

    py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
    m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);

    py::bind_map<std::map<int, E_nc>>(m, "MapENC");
    m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);

    py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
    m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
128
});