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.

100 lines
3.0 KiB

  1. /*
  2. tests/test_inheritance.cpp -- inheritance, automatic upcasting for polymorphic types
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. class Pet {
  9. public:
  10. Pet(const std::string &name, const std::string &species)
  11. : m_name(name), m_species(species) {}
  12. std::string name() const { return m_name; }
  13. std::string species() const { return m_species; }
  14. private:
  15. std::string m_name;
  16. std::string m_species;
  17. };
  18. class Dog : public Pet {
  19. public:
  20. Dog(const std::string &name) : Pet(name, "dog") {}
  21. std::string bark() const { return "Woof!"; }
  22. };
  23. class Rabbit : public Pet {
  24. public:
  25. Rabbit(const std::string &name) : Pet(name, "parrot") {}
  26. };
  27. class Hamster : public Pet {
  28. public:
  29. Hamster(const std::string &name) : Pet(name, "rodent") {}
  30. };
  31. std::string pet_name_species(const Pet &pet) {
  32. return pet.name() + " is a " + pet.species();
  33. }
  34. std::string dog_bark(const Dog &dog) {
  35. return dog.bark();
  36. }
  37. struct BaseClass { virtual ~BaseClass() {} };
  38. struct DerivedClass1 : BaseClass { };
  39. struct DerivedClass2 : BaseClass { };
  40. test_initializer inheritance([](py::module &m) {
  41. py::class_<Pet> pet_class(m, "Pet");
  42. pet_class
  43. .def(py::init<std::string, std::string>())
  44. .def("name", &Pet::name)
  45. .def("species", &Pet::species);
  46. /* One way of declaring a subclass relationship: reference parent's class_ object */
  47. py::class_<Dog>(m, "Dog", pet_class)
  48. .def(py::init<std::string>());
  49. /* Another way of declaring a subclass relationship: reference parent's C++ type */
  50. py::class_<Rabbit, Pet>(m, "Rabbit")
  51. .def(py::init<std::string>());
  52. /* And another: list parent in class template arguments */
  53. py::class_<Hamster, Pet>(m, "Hamster")
  54. .def(py::init<std::string>());
  55. m.def("pet_name_species", pet_name_species);
  56. m.def("dog_bark", dog_bark);
  57. py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
  58. py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
  59. py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
  60. m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
  61. m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
  62. m.def("return_class_n", [](int n) -> BaseClass* {
  63. if (n == 1) return new DerivedClass1();
  64. if (n == 2) return new DerivedClass2();
  65. return new BaseClass();
  66. });
  67. m.def("return_none", []() -> BaseClass* { return nullptr; });
  68. m.def("test_isinstance", [](py::list l) {
  69. struct Unregistered { }; // checks missing type_info code path
  70. return py::make_tuple(
  71. py::isinstance<py::tuple>(l[0]),
  72. py::isinstance<py::dict>(l[1]),
  73. py::isinstance<Pet>(l[2]),
  74. py::isinstance<Pet>(l[3]),
  75. py::isinstance<Dog>(l[4]),
  76. py::isinstance<Rabbit>(l[5]),
  77. py::isinstance<Unregistered>(l[6])
  78. );
  79. });
  80. });