test_stl_binders.cpp 1.24 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>
Sergey Lyskov's avatar
Sergey Lyskov committed
13

14
class El {
Sergey Lyskov's avatar
Sergey Lyskov committed
15
public:
16
17
    El() = delete;
    El(int v) : a(v) { }
18

19
    int a;
Sergey Lyskov's avatar
Sergey Lyskov committed
20
21
};

22
std::ostream & operator<<(std::ostream &s, El const&v) {
23
24
    s << "El{" << v.a << '}';
    return s;
25
26
}

27
test_initializer stl_binder_vector([](py::module &m) {
28
29
    py::class_<El>(m, "El")
        .def(py::init<int>());
30

Sergey Lyskov's avatar
Sergey Lyskov committed
31
32
    py::bind_vector< std::vector<unsigned int> >(m, "VectorInt");
    py::bind_vector< std::vector<bool> >(m, "VectorBool");
33

Sergey Lyskov's avatar
Sergey Lyskov committed
34
    py::bind_vector< std::vector<El> >(m, "VectorEl");
Sergey Lyskov's avatar
Sergey Lyskov committed
35

Sergey Lyskov's avatar
Sergey Lyskov committed
36
37
38
39
40
41
42
43
44
    py::bind_vector< std::vector< std::vector<El> > >(m, "VectorVectorEl");
});

test_initializer stl_binder_map([](py::module &m) {
    py::bind_map< std::map<std::string, double> >(m, "MapStringDouble");
    py::bind_map< std::unordered_map<std::string, double> >(m, "UnorderedMapStringDouble");

    py::bind_map< std::map<std::string, double const> >(m, "MapStringDoubleConst");
    py::bind_map< std::unordered_map<std::string, double const> >(m, "UnorderedMapStringDoubleConst");
45
});