Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | tests/test_multiple_inheritance.cpp -- multiple inheritance, |
| 3 | implicit MI casts |
| 4 | |
| 5 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
| 11 | #include "pybind11_tests.h" |
| 12 | |
| 13 | PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); |
| 14 | |
| 15 | struct Base1 { |
| 16 | Base1(int i) : i(i) { } |
| 17 | int foo() { return i; } |
| 18 | int i; |
| 19 | }; |
| 20 | |
| 21 | struct Base2 { |
| 22 | Base2(int i) : i(i) { } |
| 23 | int bar() { return i; } |
| 24 | int i; |
| 25 | }; |
| 26 | |
| 27 | struct Base12 : Base1, Base2 { |
| 28 | Base12(int i, int j) : Base1(i), Base2(j) { } |
| 29 | }; |
| 30 | |
| 31 | struct MIType : Base12 { |
| 32 | MIType(int i, int j) : Base12(i, j) { } |
| 33 | }; |
| 34 | |
| 35 | test_initializer multiple_inheritance([](py::module &m) { |
| 36 | py::class_<Base1>(m, "Base1") |
| 37 | .def(py::init<int>()) |
| 38 | .def("foo", &Base1::foo); |
| 39 | |
| 40 | py::class_<Base2>(m, "Base2") |
| 41 | .def(py::init<int>()) |
| 42 | .def("bar", &Base2::bar); |
| 43 | |
| 44 | py::class_<Base12, Base1, Base2>(m, "Base12"); |
| 45 | |
| 46 | py::class_<MIType, Base12>(m, "MIType") |
| 47 | .def(py::init<int, int>()); |
| 48 | }); |
| 49 | |
| 50 | /* Test the case where not all base classes are specified, |
| 51 | and where pybind11 requires the py::multiple_inheritance |
| 52 | flag to perform proper casting between types */ |
| 53 | |
| 54 | struct Base1a { |
| 55 | Base1a(int i) : i(i) { } |
| 56 | int foo() { return i; } |
| 57 | int i; |
| 58 | }; |
| 59 | |
| 60 | struct Base2a { |
| 61 | Base2a(int i) : i(i) { } |
| 62 | int bar() { return i; } |
| 63 | int i; |
| 64 | }; |
| 65 | |
| 66 | struct Base12a : Base1a, Base2a { |
| 67 | Base12a(int i, int j) : Base1a(i), Base2a(j) { } |
| 68 | }; |
| 69 | |
| 70 | test_initializer multiple_inheritance_nonexplicit([](py::module &m) { |
| 71 | py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a") |
| 72 | .def(py::init<int>()) |
| 73 | .def("foo", &Base1a::foo); |
| 74 | |
| 75 | py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a") |
| 76 | .def(py::init<int>()) |
| 77 | .def("bar", &Base2a::bar); |
| 78 | |
| 79 | py::class_<Base12a, /* Base1 missing */ Base2a, |
| 80 | std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance()) |
| 81 | .def(py::init<int, int>()); |
| 82 | |
| 83 | m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); |
| 84 | m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); }); |
| 85 | }); |