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 | |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 13 | struct Base1 { |
| 14 | Base1(int i) : i(i) { } |
| 15 | int foo() { return i; } |
| 16 | int i; |
| 17 | }; |
| 18 | |
| 19 | struct Base2 { |
| 20 | Base2(int i) : i(i) { } |
| 21 | int bar() { return i; } |
| 22 | int i; |
| 23 | }; |
| 24 | |
| 25 | struct Base12 : Base1, Base2 { |
| 26 | Base12(int i, int j) : Base1(i), Base2(j) { } |
| 27 | }; |
| 28 | |
| 29 | struct MIType : Base12 { |
| 30 | MIType(int i, int j) : Base12(i, j) { } |
| 31 | }; |
| 32 | |
| 33 | test_initializer multiple_inheritance([](py::module &m) { |
| 34 | py::class_<Base1>(m, "Base1") |
| 35 | .def(py::init<int>()) |
| 36 | .def("foo", &Base1::foo); |
| 37 | |
| 38 | py::class_<Base2>(m, "Base2") |
| 39 | .def(py::init<int>()) |
| 40 | .def("bar", &Base2::bar); |
| 41 | |
| 42 | py::class_<Base12, Base1, Base2>(m, "Base12"); |
| 43 | |
| 44 | py::class_<MIType, Base12>(m, "MIType") |
| 45 | .def(py::init<int, int>()); |
| 46 | }); |
| 47 | |
| 48 | /* Test the case where not all base classes are specified, |
| 49 | and where pybind11 requires the py::multiple_inheritance |
| 50 | flag to perform proper casting between types */ |
| 51 | |
| 52 | struct Base1a { |
| 53 | Base1a(int i) : i(i) { } |
| 54 | int foo() { return i; } |
| 55 | int i; |
| 56 | }; |
| 57 | |
| 58 | struct Base2a { |
| 59 | Base2a(int i) : i(i) { } |
| 60 | int bar() { return i; } |
| 61 | int i; |
| 62 | }; |
| 63 | |
| 64 | struct Base12a : Base1a, Base2a { |
| 65 | Base12a(int i, int j) : Base1a(i), Base2a(j) { } |
| 66 | }; |
| 67 | |
| 68 | test_initializer multiple_inheritance_nonexplicit([](py::module &m) { |
| 69 | py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a") |
| 70 | .def(py::init<int>()) |
| 71 | .def("foo", &Base1a::foo); |
| 72 | |
| 73 | py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a") |
| 74 | .def(py::init<int>()) |
| 75 | .def("bar", &Base2a::bar); |
| 76 | |
| 77 | py::class_<Base12a, /* Base1 missing */ Base2a, |
| 78 | std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance()) |
| 79 | .def(py::init<int, int>()); |
| 80 | |
| 81 | m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); |
| 82 | m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); }); |
| 83 | }); |