The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

215 lines
7.7 KiB

4 weeks ago
  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. struct Base1 {
  11. Base1(int i) : i(i) { }
  12. int foo() { return i; }
  13. int i;
  14. };
  15. struct Base2 {
  16. Base2(int i) : i(i) { }
  17. int bar() { return i; }
  18. int i;
  19. };
  20. template <int N> struct BaseN {
  21. BaseN(int i) : i(i) { }
  22. int i;
  23. };
  24. struct Base12 : Base1, Base2 {
  25. Base12(int i, int j) : Base1(i), Base2(j) { }
  26. };
  27. struct MIType : Base12 {
  28. MIType(int i, int j) : Base12(i, j) { }
  29. };
  30. test_initializer multiple_inheritance([](py::module &m) {
  31. py::class_<Base1> b1(m, "Base1");
  32. b1.def(py::init<int>())
  33. .def("foo", &Base1::foo);
  34. py::class_<Base2> b2(m, "Base2");
  35. b2.def(py::init<int>())
  36. .def("bar", &Base2::bar);
  37. py::class_<Base12, Base1, Base2>(m, "Base12");
  38. py::class_<MIType, Base12>(m, "MIType")
  39. .def(py::init<int, int>());
  40. // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
  41. // space for holder constructed flags) works.
  42. #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; })
  43. PYBIND11_BASEN( 1); PYBIND11_BASEN( 2); PYBIND11_BASEN( 3); PYBIND11_BASEN( 4);
  44. PYBIND11_BASEN( 5); PYBIND11_BASEN( 6); PYBIND11_BASEN( 7); PYBIND11_BASEN( 8);
  45. PYBIND11_BASEN( 9); PYBIND11_BASEN(10); PYBIND11_BASEN(11); PYBIND11_BASEN(12);
  46. PYBIND11_BASEN(13); PYBIND11_BASEN(14); PYBIND11_BASEN(15); PYBIND11_BASEN(16);
  47. PYBIND11_BASEN(17);
  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>());
  56. });
  57. /* Test the case where not all base classes are specified,
  58. and where pybind11 requires the py::multiple_inheritance
  59. flag to perform proper casting between types */
  60. struct Base1a {
  61. Base1a(int i) : i(i) { }
  62. int foo() { return i; }
  63. int i;
  64. };
  65. struct Base2a {
  66. Base2a(int i) : i(i) { }
  67. int bar() { return i; }
  68. int i;
  69. };
  70. struct Base12a : Base1a, Base2a {
  71. Base12a(int i, int j) : Base1a(i), Base2a(j) { }
  72. };
  73. test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
  74. py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
  75. .def(py::init<int>())
  76. .def("foo", &Base1a::foo);
  77. py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
  78. .def(py::init<int>())
  79. .def("bar", &Base2a::bar);
  80. py::class_<Base12a, /* Base1 missing */ Base2a,
  81. std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
  82. .def(py::init<int, int>());
  83. m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
  84. m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
  85. });
  86. // Issue #801: invalid casting to derived type with MI bases
  87. struct I801B1 { int a = 1; virtual ~I801B1() = default; };
  88. struct I801B2 { int b = 2; virtual ~I801B2() = default; };
  89. struct I801C : I801B1, I801B2 {};
  90. struct I801D : I801C {}; // Indirect MI
  91. // Unregistered classes:
  92. struct I801B3 { int c = 3; virtual ~I801B3() = default; };
  93. struct I801E : I801B3, I801D {};
  94. test_initializer multiple_inheritance_casting([](py::module &m) {
  95. py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1").def(py::init<>()).def_readonly("a", &I801B1::a);
  96. py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2").def(py::init<>()).def_readonly("b", &I801B2::b);
  97. py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>());
  98. py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>());
  99. // Two separate issues here: first, we want to recognize a pointer to a base type as being a
  100. // known instance even when the pointer value is unequal (i.e. due to a non-first
  101. // multiple-inheritance base class):
  102. m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); });
  103. m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); });
  104. m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); });
  105. m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); });
  106. // Second, when returned a base class pointer to a derived instance, we cannot assume that the
  107. // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class
  108. // pointer could be offset.
  109. m.def("i801c_b1", []() -> I801B1 * { return new I801C(); });
  110. m.def("i801c_b2", []() -> I801B2 * { return new I801C(); });
  111. m.def("i801d_b1", []() -> I801B1 * { return new I801D(); });
  112. m.def("i801d_b2", []() -> I801B2 * { return new I801D(); });
  113. // Return a base class pointer to a pybind-registered type when the actual derived type
  114. // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base)
  115. m.def("i801e_c", []() -> I801C * { return new I801E(); });
  116. m.def("i801e_b2", []() -> I801B2 * { return new I801E(); });
  117. });
  118. struct Vanilla {
  119. std::string vanilla() { return "Vanilla"; };
  120. };
  121. struct WithStatic1 {
  122. static std::string static_func1() { return "WithStatic1"; };
  123. static int static_value1;
  124. };
  125. struct WithStatic2 {
  126. static std::string static_func2() { return "WithStatic2"; };
  127. static int static_value2;
  128. };
  129. struct WithDict { };
  130. struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
  131. static std::string static_func() { return "VanillaStaticMix1"; }
  132. static int static_value;
  133. };
  134. struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
  135. static std::string static_func() { return "VanillaStaticMix2"; }
  136. static int static_value;
  137. };
  138. struct VanillaDictMix1 : Vanilla, WithDict { };
  139. struct VanillaDictMix2 : WithDict, Vanilla { };
  140. int WithStatic1::static_value1 = 1;
  141. int WithStatic2::static_value2 = 2;
  142. int VanillaStaticMix1::static_value = 12;
  143. int VanillaStaticMix2::static_value = 12;
  144. test_initializer mi_static_properties([](py::module &pm) {
  145. auto m = pm.def_submodule("mi");
  146. py::class_<Vanilla>(m, "Vanilla")
  147. .def(py::init<>())
  148. .def("vanilla", &Vanilla::vanilla);
  149. py::class_<WithStatic1>(m, "WithStatic1")
  150. .def(py::init<>())
  151. .def_static("static_func1", &WithStatic1::static_func1)
  152. .def_readwrite_static("static_value1", &WithStatic1::static_value1);
  153. py::class_<WithStatic2>(m, "WithStatic2")
  154. .def(py::init<>())
  155. .def_static("static_func2", &WithStatic2::static_func2)
  156. .def_readwrite_static("static_value2", &WithStatic2::static_value2);
  157. py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(
  158. m, "VanillaStaticMix1")
  159. .def(py::init<>())
  160. .def_static("static_func", &VanillaStaticMix1::static_func)
  161. .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
  162. py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(
  163. m, "VanillaStaticMix2")
  164. .def(py::init<>())
  165. .def_static("static_func", &VanillaStaticMix2::static_func)
  166. .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
  167. #if !defined(PYPY_VERSION)
  168. py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
  169. py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
  170. py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
  171. #endif
  172. });