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