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.

162 lines
4.8 KiB

8 years 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. struct Base1 {
  10. Base1(int i) : i(i) { }
  11. int foo() { return i; }
  12. int i;
  13. };
  14. struct Base2 {
  15. Base2(int i) : i(i) { }
  16. int bar() { return i; }
  17. int i;
  18. };
  19. struct Base12 : Base1, Base2 {
  20. Base12(int i, int j) : Base1(i), Base2(j) { }
  21. };
  22. struct MIType : Base12 {
  23. MIType(int i, int j) : Base12(i, j) { }
  24. };
  25. test_initializer multiple_inheritance([](py::module &m) {
  26. py::class_<Base1> b1(m, "Base1");
  27. b1.def(py::init<int>())
  28. .def("foo", &Base1::foo);
  29. py::class_<Base2> b2(m, "Base2");
  30. b2.def(py::init<int>())
  31. .def("bar", &Base2::bar);
  32. py::class_<Base12, Base1, Base2>(m, "Base12");
  33. py::class_<MIType, Base12>(m, "MIType")
  34. .def(py::init<int, int>());
  35. // Uncommenting this should result in a compile time failure (MI can only be specified via
  36. // template parameters because pybind has to know the types involved; see discussion in #742 for
  37. // details).
  38. // struct Base12v2 : Base1, Base2 {
  39. // Base12v2(int i, int j) : Base1(i), Base2(j) { }
  40. // };
  41. // py::class_<Base12v2>(m, "Base12v2", b1, b2)
  42. // .def(py::init<int, int>());
  43. });
  44. /* Test the case where not all base classes are specified,
  45. and where pybind11 requires the py::multiple_inheritance
  46. flag to perform proper casting between types */
  47. struct Base1a {
  48. Base1a(int i) : i(i) { }
  49. int foo() { return i; }
  50. int i;
  51. };
  52. struct Base2a {
  53. Base2a(int i) : i(i) { }
  54. int bar() { return i; }
  55. int i;
  56. };
  57. struct Base12a : Base1a, Base2a {
  58. Base12a(int i, int j) : Base1a(i), Base2a(j) { }
  59. };
  60. test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
  61. py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
  62. .def(py::init<int>())
  63. .def("foo", &Base1a::foo);
  64. py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
  65. .def(py::init<int>())
  66. .def("bar", &Base2a::bar);
  67. py::class_<Base12a, /* Base1 missing */ Base2a,
  68. std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
  69. .def(py::init<int, int>());
  70. m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
  71. m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
  72. });
  73. struct Vanilla {
  74. std::string vanilla() { return "Vanilla"; };
  75. };
  76. struct WithStatic1 {
  77. static std::string static_func1() { return "WithStatic1"; };
  78. static int static_value1;
  79. };
  80. struct WithStatic2 {
  81. static std::string static_func2() { return "WithStatic2"; };
  82. static int static_value2;
  83. };
  84. struct WithDict { };
  85. struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
  86. static std::string static_func() { return "VanillaStaticMix1"; }
  87. static int static_value;
  88. };
  89. struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
  90. static std::string static_func() { return "VanillaStaticMix2"; }
  91. static int static_value;
  92. };
  93. struct VanillaDictMix1 : Vanilla, WithDict { };
  94. struct VanillaDictMix2 : WithDict, Vanilla { };
  95. int WithStatic1::static_value1 = 1;
  96. int WithStatic2::static_value2 = 2;
  97. int VanillaStaticMix1::static_value = 12;
  98. int VanillaStaticMix2::static_value = 12;
  99. test_initializer mi_static_properties([](py::module &pm) {
  100. auto m = pm.def_submodule("mi");
  101. py::class_<Vanilla>(m, "Vanilla")
  102. .def(py::init<>())
  103. .def("vanilla", &Vanilla::vanilla);
  104. py::class_<WithStatic1>(m, "WithStatic1")
  105. .def(py::init<>())
  106. .def_static("static_func1", &WithStatic1::static_func1)
  107. .def_readwrite_static("static_value1", &WithStatic1::static_value1);
  108. py::class_<WithStatic2>(m, "WithStatic2")
  109. .def(py::init<>())
  110. .def_static("static_func2", &WithStatic2::static_func2)
  111. .def_readwrite_static("static_value2", &WithStatic2::static_value2);
  112. py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(
  113. m, "VanillaStaticMix1")
  114. .def(py::init<>())
  115. .def_static("static_func", &VanillaStaticMix1::static_func)
  116. .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
  117. py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(
  118. m, "VanillaStaticMix2")
  119. .def(py::init<>())
  120. .def_static("static_func", &VanillaStaticMix2::static_func)
  121. .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
  122. #if !defined(PYPY_VERSION)
  123. py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
  124. py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
  125. py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
  126. #endif
  127. });