2016-03-10 12:24:10 +00:00
|
|
|
/*
|
|
|
|
example/issues.cpp -- collection of testcases for miscellaneous issues
|
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2016-03-10 12:24:10 +00:00
|
|
|
|
|
|
|
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"
|
2016-04-20 15:00:57 +00:00
|
|
|
#include <pybind11/stl.h>
|
2016-03-10 12:24:10 +00:00
|
|
|
|
2016-04-30 21:02:39 +00:00
|
|
|
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
|
|
|
|
|
2016-03-10 12:24:10 +00:00
|
|
|
void init_issues(py::module &m) {
|
|
|
|
py::module m2 = m.def_submodule("issues");
|
|
|
|
|
2016-05-01 08:39:45 +00:00
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
// Visual Studio 2015 currently cannot compile this test
|
|
|
|
// (see the comment in type_caster_base::make_copy_constructor)
|
|
|
|
// #70 compilation issue if operator new is not public
|
|
|
|
class NonConstructible { private: void *operator new(size_t bytes) throw(); };
|
|
|
|
py::class_<NonConstructible>(m, "Foo");
|
2016-05-01 12:42:20 +00:00
|
|
|
m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
|
2016-05-01 08:39:45 +00:00
|
|
|
py::return_value_policy::reference);
|
|
|
|
#endif
|
|
|
|
|
2016-03-10 12:24:10 +00:00
|
|
|
// #137: const char* isn't handled properly
|
|
|
|
m2.def("print_cchar", [](const char *string) { std::cout << string << std::endl; });
|
2016-03-26 22:04:10 +00:00
|
|
|
|
|
|
|
// #150: char bindings broken
|
|
|
|
m2.def("print_char", [](char c) { std::cout << c << std::endl; });
|
2016-04-11 16:13:08 +00:00
|
|
|
|
|
|
|
// #159: virtual function dispatch has problems with similar-named functions
|
2016-04-30 21:02:39 +00:00
|
|
|
struct Base { virtual void dispatch(void) const {
|
|
|
|
/* for some reason MSVC2015 can't compile this if the function is pure virtual */
|
|
|
|
}; };
|
2016-04-30 20:44:00 +00:00
|
|
|
|
|
|
|
struct DispatchIssue : Base {
|
|
|
|
virtual void dispatch(void) const {
|
|
|
|
PYBIND11_OVERLOAD_PURE(void, Base, dispatch, /* no arguments */);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 15:00:57 +00:00
|
|
|
py::class_<DispatchIssue> base(m2, "DispatchIssue");
|
2016-04-11 16:13:08 +00:00
|
|
|
base.alias<Base>()
|
2016-04-20 15:00:57 +00:00
|
|
|
.def(py::init<>())
|
2016-04-11 16:13:08 +00:00
|
|
|
.def("dispatch", &Base::dispatch);
|
|
|
|
|
2016-04-30 20:44:00 +00:00
|
|
|
m2.def("dispatch_issue_go", [](const Base * b) { b->dispatch(); });
|
|
|
|
|
|
|
|
struct Placeholder { int i; Placeholder(int i) : i(i) { } };
|
2016-04-20 15:00:57 +00:00
|
|
|
|
|
|
|
py::class_<Placeholder>(m2, "Placeholder")
|
2016-04-21 10:21:14 +00:00
|
|
|
.def(py::init<int>())
|
2016-04-20 15:00:57 +00:00
|
|
|
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
|
|
|
|
|
|
|
|
// #171: Can't return reference wrappers (or STL datastructures containing them)
|
2016-04-21 10:21:14 +00:00
|
|
|
m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
|
2016-04-20 15:00:57 +00:00
|
|
|
Placeholder *p1 = new Placeholder{1};
|
|
|
|
Placeholder *p2 = new Placeholder{2};
|
2016-04-21 10:21:14 +00:00
|
|
|
Placeholder *p3 = new Placeholder{3};
|
2016-04-20 15:00:57 +00:00
|
|
|
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));
|
2016-04-21 10:21:14 +00:00
|
|
|
v.push_back(p4);
|
2016-04-20 15:00:57 +00:00
|
|
|
return v;
|
|
|
|
});
|
2016-04-27 12:33:52 +00:00
|
|
|
|
|
|
|
// #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));
|
|
|
|
});
|
2016-04-30 21:02:39 +00:00
|
|
|
|
|
|
|
// #187: issue involving std::shared_ptr<> return value policy & garbage collection
|
|
|
|
struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
|
|
|
|
struct ElementA : ElementBase {
|
|
|
|
ElementA(int v) : v(v) { }
|
|
|
|
int value() { return v; }
|
|
|
|
int v;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ElementList {
|
|
|
|
void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
|
|
|
|
std::vector<std::shared_ptr<ElementBase>> l;
|
|
|
|
};
|
|
|
|
|
|
|
|
py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
|
|
|
|
|
|
|
|
py::class_<ElementA, std::shared_ptr<ElementA>>(m2, "ElementA", py::base<ElementBase>())
|
|
|
|
.def(py::init<int>())
|
|
|
|
.def("value", &ElementA::value);
|
|
|
|
|
|
|
|
py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("add", &ElementList::add)
|
|
|
|
.def("get", [](ElementList &el){
|
|
|
|
py::list list;
|
|
|
|
for (auto &e : el.l)
|
|
|
|
list.append(py::cast(e));
|
|
|
|
return list;
|
|
|
|
});
|
2016-05-01 12:42:20 +00:00
|
|
|
|
|
|
|
// (no id): should not be able to pass 'None' to a reference argument
|
|
|
|
m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
|
|
|
|
}
|