test_multiple_inheritance.cpp 8.5 KB

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