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