issues.cpp 2.11 KB
Newer Older
1
2
3
/*
    example/issues.cpp -- collection of testcases for miscellaneous issues

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6
7
8
9
10

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

#include "example.h"
11
#include <pybind11/stl.h>
12
13
14
15
16
17

void init_issues(py::module &m) {
    py::module m2 = m.def_submodule("issues");

    // #137: const char* isn't handled properly
    m2.def("print_cchar", [](const char *string) { std::cout << string << std::endl; });
18
19
20

    // #150: char bindings broken
    m2.def("print_char", [](char c) { std::cout << c << std::endl; });
21
22

    // #159: virtual function dispatch has problems with similar-named functions
Wenzel Jakob's avatar
Wenzel Jakob committed
23
24
25
26
27
28
29
30
    struct Base { virtual void dispatch(void) const = 0; };

    struct DispatchIssue : Base {
        virtual void dispatch(void) const {
            PYBIND11_OVERLOAD_PURE(void, Base, dispatch, /* no arguments */);
        }
    };

31
    py::class_<DispatchIssue> base(m2, "DispatchIssue");
32
    base.alias<Base>()
33
        .def(py::init<>())
34
35
        .def("dispatch", &Base::dispatch);

Wenzel Jakob's avatar
Wenzel Jakob committed
36
37
38
    m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });

    struct Placeholder { int i; Placeholder(int i) : i(i) { } };
39
40

    py::class_<Placeholder>(m2, "Placeholder")
41
        .def(py::init<int>())
42
43
44
        .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });

    // #171: Can't return reference wrappers (or STL datastructures containing them)
45
    m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
46
47
        Placeholder *p1 = new Placeholder{1};
        Placeholder *p2 = new Placeholder{2};
48
        Placeholder *p3 = new Placeholder{3};
49
50
51
52
        std::vector<std::reference_wrapper<Placeholder>> v;
        v.push_back(std::ref(*p1));
        v.push_back(std::ref(*p2));
        v.push_back(std::ref(*p3));
53
        v.push_back(p4);
54
55
        return v;
    });
56
57
58
59
60

    // #181: iterator passthrough did not compile
    m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
        return py::make_iterator(std::begin(s), std::end(s));
    });
61
}