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" |
Jason Rhinelander | 14e7065 | 2017-04-21 17:14:22 -0400 | [diff] [blame] | 12 | #include "constructor_stats.h" |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 13 | |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 14 | struct Base1 { |
| 15 | Base1(int i) : i(i) { } |
| 16 | int foo() { return i; } |
| 17 | int i; |
| 18 | }; |
| 19 | |
| 20 | struct Base2 { |
| 21 | Base2(int i) : i(i) { } |
| 22 | int bar() { return i; } |
| 23 | int i; |
| 24 | }; |
| 25 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 26 | template <int N> struct BaseN { |
| 27 | BaseN(int i) : i(i) { } |
| 28 | int i; |
| 29 | }; |
| 30 | |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 31 | struct Base12 : Base1, Base2 { |
| 32 | Base12(int i, int j) : Base1(i), Base2(j) { } |
| 33 | }; |
| 34 | |
| 35 | struct MIType : Base12 { |
| 36 | MIType(int i, int j) : Base12(i, j) { } |
| 37 | }; |
| 38 | |
| 39 | test_initializer multiple_inheritance([](py::module &m) { |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 40 | py::class_<Base1> b1(m, "Base1"); |
| 41 | b1.def(py::init<int>()) |
| 42 | .def("foo", &Base1::foo); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 43 | |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 44 | py::class_<Base2> b2(m, "Base2"); |
| 45 | b2.def(py::init<int>()) |
| 46 | .def("bar", &Base2::bar); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 47 | |
| 48 | py::class_<Base12, Base1, Base2>(m, "Base12"); |
| 49 | |
| 50 | py::class_<MIType, Base12>(m, "MIType") |
| 51 | .def(py::init<int, int>()); |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 52 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 53 | // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra |
| 54 | // space for holder constructed flags) works. |
| 55 | #define PYBIND11_BASEN(N) py::class_<BaseN<N>>(m, "BaseN" #N).def(py::init<int>()).def("f" #N, [](BaseN<N> &b) { return b.i + N; }) |
| 56 | PYBIND11_BASEN( 1); PYBIND11_BASEN( 2); PYBIND11_BASEN( 3); PYBIND11_BASEN( 4); |
| 57 | PYBIND11_BASEN( 5); PYBIND11_BASEN( 6); PYBIND11_BASEN( 7); PYBIND11_BASEN( 8); |
| 58 | PYBIND11_BASEN( 9); PYBIND11_BASEN(10); PYBIND11_BASEN(11); PYBIND11_BASEN(12); |
| 59 | PYBIND11_BASEN(13); PYBIND11_BASEN(14); PYBIND11_BASEN(15); PYBIND11_BASEN(16); |
| 60 | PYBIND11_BASEN(17); |
| 61 | |
Jason Rhinelander | b961626 | 2017-03-16 20:10:48 -0300 | [diff] [blame] | 62 | // Uncommenting this should result in a compile time failure (MI can only be specified via |
| 63 | // template parameters because pybind has to know the types involved; see discussion in #742 for |
| 64 | // details). |
| 65 | // struct Base12v2 : Base1, Base2 { |
| 66 | // Base12v2(int i, int j) : Base1(i), Base2(j) { } |
| 67 | // }; |
| 68 | // py::class_<Base12v2>(m, "Base12v2", b1, b2) |
| 69 | // .def(py::init<int, int>()); |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 70 | }); |
| 71 | |
| 72 | /* Test the case where not all base classes are specified, |
| 73 | and where pybind11 requires the py::multiple_inheritance |
| 74 | flag to perform proper casting between types */ |
| 75 | |
| 76 | struct Base1a { |
| 77 | Base1a(int i) : i(i) { } |
| 78 | int foo() { return i; } |
| 79 | int i; |
| 80 | }; |
| 81 | |
| 82 | struct Base2a { |
| 83 | Base2a(int i) : i(i) { } |
| 84 | int bar() { return i; } |
| 85 | int i; |
| 86 | }; |
| 87 | |
| 88 | struct Base12a : Base1a, Base2a { |
| 89 | Base12a(int i, int j) : Base1a(i), Base2a(j) { } |
| 90 | }; |
| 91 | |
| 92 | test_initializer multiple_inheritance_nonexplicit([](py::module &m) { |
| 93 | py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a") |
| 94 | .def(py::init<int>()) |
| 95 | .def("foo", &Base1a::foo); |
| 96 | |
| 97 | py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a") |
| 98 | .def(py::init<int>()) |
| 99 | .def("bar", &Base2a::bar); |
| 100 | |
| 101 | py::class_<Base12a, /* Base1 missing */ Base2a, |
| 102 | std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance()) |
| 103 | .def(py::init<int, int>()); |
| 104 | |
| 105 | m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); |
| 106 | m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); }); |
| 107 | }); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 108 | |
Jason Rhinelander | 14e7065 | 2017-04-21 17:14:22 -0400 | [diff] [blame] | 109 | // Issue #801: invalid casting to derived type with MI bases |
| 110 | struct I801B1 { int a = 1; virtual ~I801B1() = default; }; |
| 111 | struct I801B2 { int b = 2; virtual ~I801B2() = default; }; |
| 112 | struct I801C : I801B1, I801B2 {}; |
| 113 | struct I801D : I801C {}; // Indirect MI |
| 114 | // Unregistered classes: |
| 115 | struct I801B3 { int c = 3; virtual ~I801B3() = default; }; |
| 116 | struct I801E : I801B3, I801D {}; |
| 117 | |
| 118 | test_initializer multiple_inheritance_casting([](py::module &m) { |
| 119 | py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1").def(py::init<>()).def_readonly("a", &I801B1::a); |
| 120 | py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2").def(py::init<>()).def_readonly("b", &I801B2::b); |
| 121 | py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>()); |
| 122 | py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>()); |
| 123 | |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 124 | // Two separate issues here: first, we want to recognize a pointer to a base type as being a |
| 125 | // known instance even when the pointer value is unequal (i.e. due to a non-first |
| 126 | // multiple-inheritance base class): |
| 127 | m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); }); |
| 128 | m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); }); |
| 129 | m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); }); |
| 130 | m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); }); |
| 131 | |
| 132 | // Second, when returned a base class pointer to a derived instance, we cannot assume that the |
| 133 | // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class |
Jason Rhinelander | 14e7065 | 2017-04-21 17:14:22 -0400 | [diff] [blame] | 134 | // pointer could be offset. |
| 135 | m.def("i801c_b1", []() -> I801B1 * { return new I801C(); }); |
| 136 | m.def("i801c_b2", []() -> I801B2 * { return new I801C(); }); |
| 137 | m.def("i801d_b1", []() -> I801B1 * { return new I801D(); }); |
| 138 | m.def("i801d_b2", []() -> I801B2 * { return new I801D(); }); |
| 139 | |
| 140 | // Return a base class pointer to a pybind-registered type when the actual derived type |
| 141 | // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base) |
| 142 | m.def("i801e_c", []() -> I801C * { return new I801E(); }); |
| 143 | m.def("i801e_b2", []() -> I801B2 * { return new I801E(); }); |
| 144 | }); |
| 145 | |
| 146 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 147 | struct Vanilla { |
| 148 | std::string vanilla() { return "Vanilla"; }; |
| 149 | }; |
| 150 | |
| 151 | struct WithStatic1 { |
| 152 | static std::string static_func1() { return "WithStatic1"; }; |
| 153 | static int static_value1; |
| 154 | }; |
| 155 | |
| 156 | struct WithStatic2 { |
| 157 | static std::string static_func2() { return "WithStatic2"; }; |
| 158 | static int static_value2; |
| 159 | }; |
| 160 | |
| 161 | struct WithDict { }; |
| 162 | |
| 163 | struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 { |
| 164 | static std::string static_func() { return "VanillaStaticMix1"; } |
| 165 | static int static_value; |
| 166 | }; |
| 167 | |
| 168 | struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 { |
| 169 | static std::string static_func() { return "VanillaStaticMix2"; } |
| 170 | static int static_value; |
| 171 | }; |
| 172 | |
| 173 | struct VanillaDictMix1 : Vanilla, WithDict { }; |
| 174 | struct VanillaDictMix2 : WithDict, Vanilla { }; |
| 175 | |
| 176 | int WithStatic1::static_value1 = 1; |
| 177 | int WithStatic2::static_value2 = 2; |
| 178 | int VanillaStaticMix1::static_value = 12; |
| 179 | int VanillaStaticMix2::static_value = 12; |
| 180 | |
| 181 | test_initializer mi_static_properties([](py::module &pm) { |
| 182 | auto m = pm.def_submodule("mi"); |
| 183 | |
| 184 | py::class_<Vanilla>(m, "Vanilla") |
| 185 | .def(py::init<>()) |
| 186 | .def("vanilla", &Vanilla::vanilla); |
| 187 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 188 | py::class_<WithStatic1>(m, "WithStatic1") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 189 | .def(py::init<>()) |
| 190 | .def_static("static_func1", &WithStatic1::static_func1) |
| 191 | .def_readwrite_static("static_value1", &WithStatic1::static_value1); |
| 192 | |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 193 | py::class_<WithStatic2>(m, "WithStatic2") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 194 | .def(py::init<>()) |
| 195 | .def_static("static_func2", &WithStatic2::static_func2) |
| 196 | .def_readwrite_static("static_value2", &WithStatic2::static_value2); |
| 197 | |
| 198 | py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>( |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 199 | m, "VanillaStaticMix1") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 200 | .def(py::init<>()) |
| 201 | .def_static("static_func", &VanillaStaticMix1::static_func) |
| 202 | .def_readwrite_static("static_value", &VanillaStaticMix1::static_value); |
| 203 | |
| 204 | py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>( |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 205 | m, "VanillaStaticMix2") |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 206 | .def(py::init<>()) |
| 207 | .def_static("static_func", &VanillaStaticMix2::static_func) |
| 208 | .def_readwrite_static("static_value", &VanillaStaticMix2::static_value); |
| 209 | |
| 210 | #if !defined(PYPY_VERSION) |
| 211 | py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>()); |
| 212 | py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>()); |
| 213 | py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>()); |
| 214 | #endif |
| 215 | }); |