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.

123 lines
3.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  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. class Chimera : public Pet {
  32. Chimera() : Pet("Kimmy", "chimera") {}
  33. };
  34. std::string pet_name_species(const Pet &pet) {
  35. return pet.name() + " is a " + pet.species();
  36. }
  37. std::string dog_bark(const Dog &dog) {
  38. return dog.bark();
  39. }
  40. struct BaseClass { virtual ~BaseClass() {} };
  41. struct DerivedClass1 : BaseClass { };
  42. struct DerivedClass2 : BaseClass { };
  43. struct MismatchBase1 { };
  44. struct MismatchDerived1 : MismatchBase1 { };
  45. struct MismatchBase2 { };
  46. struct MismatchDerived2 : MismatchBase2 { };
  47. test_initializer inheritance([](py::module &m) {
  48. py::class_<Pet> pet_class(m, "Pet");
  49. pet_class
  50. .def(py::init<std::string, std::string>())
  51. .def("name", &Pet::name)
  52. .def("species", &Pet::species);
  53. /* One way of declaring a subclass relationship: reference parent's class_ object */
  54. py::class_<Dog>(m, "Dog", pet_class)
  55. .def(py::init<std::string>());
  56. /* Another way of declaring a subclass relationship: reference parent's C++ type */
  57. py::class_<Rabbit, Pet>(m, "Rabbit")
  58. .def(py::init<std::string>());
  59. /* And another: list parent in class template arguments */
  60. py::class_<Hamster, Pet>(m, "Hamster")
  61. .def(py::init<std::string>());
  62. py::class_<Chimera, Pet>(m, "Chimera");
  63. m.def("pet_name_species", pet_name_species);
  64. m.def("dog_bark", dog_bark);
  65. py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
  66. py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
  67. py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
  68. m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
  69. m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
  70. m.def("return_class_n", [](int n) -> BaseClass* {
  71. if (n == 1) return new DerivedClass1();
  72. if (n == 2) return new DerivedClass2();
  73. return new BaseClass();
  74. });
  75. m.def("return_none", []() -> BaseClass* { return nullptr; });
  76. m.def("test_isinstance", [](py::list l) {
  77. struct Unregistered { }; // checks missing type_info code path
  78. return py::make_tuple(
  79. py::isinstance<py::tuple>(l[0]),
  80. py::isinstance<py::dict>(l[1]),
  81. py::isinstance<Pet>(l[2]),
  82. py::isinstance<Pet>(l[3]),
  83. py::isinstance<Dog>(l[4]),
  84. py::isinstance<Rabbit>(l[5]),
  85. py::isinstance<Unregistered>(l[6])
  86. );
  87. });
  88. m.def("test_mismatched_holder_type_1", []() {
  89. auto m = py::module::import("__main__");
  90. py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(m, "MismatchBase1");
  91. py::class_<MismatchDerived1, MismatchBase1>(m, "MismatchDerived1");
  92. });
  93. m.def("test_mismatched_holder_type_2", []() {
  94. auto m = py::module::import("__main__");
  95. py::class_<MismatchBase2>(m, "MismatchBase2");
  96. py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(m, "MismatchDerived2");
  97. });
  98. });