test_multiple_inheritance.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. tests/test_multiple_inheritance.cpp -- multiple inheritance,
  3. implicit MI casts
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include "constructor_stats.h"
  10. namespace {
  11. // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
  12. // space for holder constructed flags) works.
  13. template <int N> struct BaseN {
  14. BaseN(int i) : i(i) { }
  15. int i;
  16. };
  17. // test_mi_static_properties
  18. struct Vanilla {
  19. std::string vanilla() { return "Vanilla"; };
  20. };
  21. struct WithStatic1 {
  22. static std::string static_func1() { return "WithStatic1"; };
  23. static int static_value1;
  24. };
  25. struct WithStatic2 {
  26. static std::string static_func2() { return "WithStatic2"; };
  27. static int static_value2;
  28. };
  29. struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
  30. static std::string static_func() { return "VanillaStaticMix1"; }
  31. static int static_value;
  32. };
  33. struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
  34. static std::string static_func() { return "VanillaStaticMix2"; }
  35. static int static_value;
  36. };
  37. int WithStatic1::static_value1 = 1;
  38. int WithStatic2::static_value2 = 2;
  39. int VanillaStaticMix1::static_value = 12;
  40. int VanillaStaticMix2::static_value = 12;
  41. // test_multiple_inheritance_virtbase
  42. struct Base1a {
  43. Base1a(int i) : i(i) { }
  44. int foo() { return i; }
  45. int i;
  46. };
  47. struct Base2a {
  48. Base2a(int i) : i(i) { }
  49. int bar() { return i; }
  50. int i;
  51. };
  52. struct Base12a : Base1a, Base2a {
  53. Base12a(int i, int j) : Base1a(i), Base2a(j) { }
  54. };
  55. // test_mi_unaligned_base
  56. // test_mi_base_return
  57. struct I801B1 { int a = 1; I801B1() = default; I801B1(const I801B1 &) = default; virtual ~I801B1() = default; };
  58. struct I801B2 { int b = 2; I801B2() = default; I801B2(const I801B2 &) = default; virtual ~I801B2() = default; };
  59. struct I801C : I801B1, I801B2 {};
  60. struct I801D : I801C {}; // Indirect MI
  61. } // namespace
  62. TEST_SUBMODULE(multiple_inheritance, m) {
  63. // Please do not interleave `struct` and `class` definitions with bindings code,
  64. // but implement `struct`s and `class`es in the anonymous namespace above.
  65. // This helps keeping the smart_holder branch in sync with master.
  66. // test_multiple_inheritance_mix1
  67. // test_multiple_inheritance_mix2
  68. struct Base1 {
  69. Base1(int i) : i(i) { }
  70. int foo() { return i; }
  71. int i;
  72. };
  73. py::class_<Base1> b1(m, "Base1");
  74. b1.def(py::init<int>())
  75. .def("foo", &Base1::foo);
  76. struct Base2 {
  77. Base2(int i) : i(i) { }
  78. int bar() { return i; }
  79. int i;
  80. };
  81. py::class_<Base2> b2(m, "Base2");
  82. b2.def(py::init<int>())
  83. .def("bar", &Base2::bar);
  84. // test_multiple_inheritance_cpp
  85. struct Base12 : Base1, Base2 {
  86. Base12(int i, int j) : Base1(i), Base2(j) { }
  87. };
  88. struct MIType : Base12 {
  89. MIType(int i, int j) : Base12(i, j) { }
  90. };
  91. py::class_<Base12, Base1, Base2>(m, "Base12");
  92. py::class_<MIType, Base12>(m, "MIType")
  93. .def(py::init<int, int>());
  94. // test_multiple_inheritance_python_many_bases
  95. #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; })
  96. PYBIND11_BASEN( 1); PYBIND11_BASEN( 2); PYBIND11_BASEN( 3); PYBIND11_BASEN( 4);
  97. PYBIND11_BASEN( 5); PYBIND11_BASEN( 6); PYBIND11_BASEN( 7); PYBIND11_BASEN( 8);
  98. PYBIND11_BASEN( 9); PYBIND11_BASEN(10); PYBIND11_BASEN(11); PYBIND11_BASEN(12);
  99. PYBIND11_BASEN(13); PYBIND11_BASEN(14); PYBIND11_BASEN(15); PYBIND11_BASEN(16);
  100. PYBIND11_BASEN(17);
  101. // Uncommenting this should result in a compile time failure (MI can only be specified via
  102. // template parameters because pybind has to know the types involved; see discussion in #742 for
  103. // details).
  104. // struct Base12v2 : Base1, Base2 {
  105. // Base12v2(int i, int j) : Base1(i), Base2(j) { }
  106. // };
  107. // py::class_<Base12v2>(m, "Base12v2", b1, b2)
  108. // .def(py::init<int, int>());
  109. // test_multiple_inheritance_virtbase
  110. // Test the case where not all base classes are specified, and where pybind11 requires the
  111. // py::multiple_inheritance flag to perform proper casting between types.
  112. py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
  113. .def(py::init<int>())
  114. .def("foo", &Base1a::foo);
  115. py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
  116. .def(py::init<int>())
  117. .def("bar", &Base2a::bar);
  118. py::class_<Base12a, /* Base1 missing */ Base2a,
  119. std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
  120. .def(py::init<int, int>());
  121. m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
  122. m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
  123. // test_mi_unaligned_base
  124. // test_mi_base_return
  125. // Issue #801: invalid casting to derived type with MI bases
  126. // Unregistered classes:
  127. struct I801B3 { int c = 3; virtual ~I801B3() = default; };
  128. struct I801E : I801B3, I801D {};
  129. py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1").def(py::init<>()).def_readonly("a", &I801B1::a);
  130. py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2").def(py::init<>()).def_readonly("b", &I801B2::b);
  131. py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>());
  132. py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>());
  133. // Two separate issues here: first, we want to recognize a pointer to a base type as being a
  134. // known instance even when the pointer value is unequal (i.e. due to a non-first
  135. // multiple-inheritance base class):
  136. m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); });
  137. m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); });
  138. m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); });
  139. m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); });
  140. // Second, when returned a base class pointer to a derived instance, we cannot assume that the
  141. // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class
  142. // pointer could be offset.
  143. m.def("i801c_b1", []() -> I801B1 * { return new I801C(); });
  144. m.def("i801c_b2", []() -> I801B2 * { return new I801C(); });
  145. m.def("i801d_b1", []() -> I801B1 * { return new I801D(); });
  146. m.def("i801d_b2", []() -> I801B2 * { return new I801D(); });
  147. // Return a base class pointer to a pybind-registered type when the actual derived type
  148. // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base)
  149. m.def("i801e_c", []() -> I801C * { return new I801E(); });
  150. m.def("i801e_b2", []() -> I801B2 * { return new I801E(); });
  151. // test_mi_static_properties
  152. py::class_<Vanilla>(m, "Vanilla")
  153. .def(py::init<>())
  154. .def("vanilla", &Vanilla::vanilla);
  155. py::class_<WithStatic1>(m, "WithStatic1")
  156. .def(py::init<>())
  157. .def_static("static_func1", &WithStatic1::static_func1)
  158. .def_readwrite_static("static_value1", &WithStatic1::static_value1);
  159. py::class_<WithStatic2>(m, "WithStatic2")
  160. .def(py::init<>())
  161. .def_static("static_func2", &WithStatic2::static_func2)
  162. .def_readwrite_static("static_value2", &WithStatic2::static_value2);
  163. py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(
  164. m, "VanillaStaticMix1")
  165. .def(py::init<>())
  166. .def_static("static_func", &VanillaStaticMix1::static_func)
  167. .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
  168. py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(
  169. m, "VanillaStaticMix2")
  170. .def(py::init<>())
  171. .def_static("static_func", &VanillaStaticMix2::static_func)
  172. .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
  173. struct WithDict { };
  174. struct VanillaDictMix1 : Vanilla, WithDict { };
  175. struct VanillaDictMix2 : WithDict, Vanilla { };
  176. py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
  177. py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
  178. py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
  179. // test_diamond_inheritance
  180. // Issue #959: segfault when constructing diamond inheritance instance
  181. // All of these have int members so that there will be various unequal pointers involved.
  182. struct B { int b; B() = default; B(const B&) = default; virtual ~B() = default; };
  183. struct C0 : public virtual B { int c0; };
  184. struct C1 : public virtual B { int c1; };
  185. struct D : public C0, public C1 { int d; };
  186. py::class_<B>(m, "B")
  187. .def("b", [](B *self) { return self; });
  188. py::class_<C0, B>(m, "C0")
  189. .def("c0", [](C0 *self) { return self; });
  190. py::class_<C1, B>(m, "C1")
  191. .def("c1", [](C1 *self) { return self; });
  192. py::class_<D, C0, C1>(m, "D")
  193. .def(py::init<>());
  194. }