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