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.

84 lines
2.1 KiB

  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>(m, "Base1")
  27. .def(py::init<int>())
  28. .def("foo", &Base1::foo);
  29. py::class_<Base2>(m, "Base2")
  30. .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. });
  36. /* Test the case where not all base classes are specified,
  37. and where pybind11 requires the py::multiple_inheritance
  38. flag to perform proper casting between types */
  39. struct Base1a {
  40. Base1a(int i) : i(i) { }
  41. int foo() { return i; }
  42. int i;
  43. };
  44. struct Base2a {
  45. Base2a(int i) : i(i) { }
  46. int bar() { return i; }
  47. int i;
  48. };
  49. struct Base12a : Base1a, Base2a {
  50. Base12a(int i, int j) : Base1a(i), Base2a(j) { }
  51. };
  52. test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
  53. py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
  54. .def(py::init<int>())
  55. .def("foo", &Base1a::foo);
  56. py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
  57. .def(py::init<int>())
  58. .def("bar", &Base2a::bar);
  59. py::class_<Base12a, /* Base1 missing */ Base2a,
  60. std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
  61. .def(py::init<int, int>());
  62. m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
  63. m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
  64. });