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) { |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 34 | py::class_<Base1> b1(m, "Base1"); |
| 35 | b1.def(py::init<int>()) |
| 36 | .def("foo", &Base1::foo); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 37 | |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 38 | py::class_<Base2> b2(m, "Base2"); |
| 39 | b2.def(py::init<int>()) |
| 40 | .def("bar", &Base2::bar); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 41 | |
| 42 | py::class_<Base12, Base1, Base2>(m, "Base12"); |
| 43 | |
| 44 | py::class_<MIType, Base12>(m, "MIType") |
| 45 | .def(py::init<int, int>()); |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 46 | |
| 47 | // Uncommenting this should result in a compile time failure (MI can only be specified via |
| 48 | // template parameters because pybind has to know the types involved; see discussion in #742 for |
| 49 | // details). |
| 50 | // struct Base12v2 : Base1, Base2 { |
| 51 | // Base12v2(int i, int j) : Base1(i), Base2(j) { } |
| 52 | // }; |
| 53 | // py::class_<Base12v2>(m, "Base12v2", b1, b2) |
| 54 | // .def(py::init<int, int>()); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | /* Test the case where not all base classes are specified, |
| 58 | and where pybind11 requires the py::multiple_inheritance |
| 59 | flag to perform proper casting between types */ |
| 60 | |
| 61 | struct Base1a { |
| 62 | Base1a(int i) : i(i) { } |
| 63 | int foo() { return i; } |
| 64 | int i; |
| 65 | }; |
| 66 | |
| 67 | struct Base2a { |
| 68 | Base2a(int i) : i(i) { } |
| 69 | int bar() { return i; } |
| 70 | int i; |
| 71 | }; |
| 72 | |
| 73 | struct Base12a : Base1a, Base2a { |
| 74 | Base12a(int i, int j) : Base1a(i), Base2a(j) { } |
| 75 | }; |
| 76 | |
| 77 | test_initializer multiple_inheritance_nonexplicit([](py::module &m) { |
| 78 | py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a") |
| 79 | .def(py::init<int>()) |
| 80 | .def("foo", &Base1a::foo); |
| 81 | |
| 82 | py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a") |
| 83 | .def(py::init<int>()) |
| 84 | .def("bar", &Base2a::bar); |
| 85 | |
| 86 | py::class_<Base12a, /* Base1 missing */ Base2a, |
| 87 | std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance()) |
| 88 | .def(py::init<int, int>()); |
| 89 | |
| 90 | m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); |
| 91 | m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); }); |
| 92 | }); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 93 | |
| 94 | struct Vanilla { |
| 95 | std::string vanilla() { return "Vanilla"; }; |
| 96 | }; |
| 97 | |
| 98 | struct WithStatic1 { |
| 99 | static std::string static_func1() { return "WithStatic1"; }; |
| 100 | static int static_value1; |
| 101 | }; |
| 102 | |
| 103 | struct WithStatic2 { |
| 104 | static std::string static_func2() { return "WithStatic2"; }; |
| 105 | static int static_value2; |
| 106 | }; |
| 107 | |
| 108 | struct WithDict { }; |
| 109 | |
| 110 | struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 { |
| 111 | static std::string static_func() { return "VanillaStaticMix1"; } |
| 112 | static int static_value; |
| 113 | }; |
| 114 | |
| 115 | struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 { |
| 116 | static std::string static_func() { return "VanillaStaticMix2"; } |
| 117 | static int static_value; |
| 118 | }; |
| 119 | |
| 120 | struct VanillaDictMix1 : Vanilla, WithDict { }; |
| 121 | struct VanillaDictMix2 : WithDict, Vanilla { }; |
| 122 | |
| 123 | int WithStatic1::static_value1 = 1; |
| 124 | int WithStatic2::static_value2 = 2; |
| 125 | int VanillaStaticMix1::static_value = 12; |
| 126 | int VanillaStaticMix2::static_value = 12; |
| 127 | |
| 128 | test_initializer mi_static_properties([](py::module &pm) { |
| 129 | auto m = pm.def_submodule("mi"); |
| 130 | |
| 131 | py::class_<Vanilla>(m, "Vanilla") |
| 132 | .def(py::init<>()) |
| 133 | .def("vanilla", &Vanilla::vanilla); |
| 134 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 135 | py::class_<WithStatic1>(m, "WithStatic1") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 136 | .def(py::init<>()) |
| 137 | .def_static("static_func1", &WithStatic1::static_func1) |
| 138 | .def_readwrite_static("static_value1", &WithStatic1::static_value1); |
| 139 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 140 | py::class_<WithStatic2>(m, "WithStatic2") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 141 | .def(py::init<>()) |
| 142 | .def_static("static_func2", &WithStatic2::static_func2) |
| 143 | .def_readwrite_static("static_value2", &WithStatic2::static_value2); |
| 144 | |
| 145 | py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>( |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 146 | m, "VanillaStaticMix1") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 147 | .def(py::init<>()) |
| 148 | .def_static("static_func", &VanillaStaticMix1::static_func) |
| 149 | .def_readwrite_static("static_value", &VanillaStaticMix1::static_value); |
| 150 | |
| 151 | py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>( |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 152 | m, "VanillaStaticMix2") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 153 | .def(py::init<>()) |
| 154 | .def_static("static_func", &VanillaStaticMix2::static_func) |
| 155 | .def_readwrite_static("static_value", &VanillaStaticMix2::static_value); |
| 156 | |
| 157 | #if !defined(PYPY_VERSION) |
| 158 | py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>()); |
| 159 | py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>()); |
| 160 | py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>()); |
| 161 | #endif |
| 162 | }); |