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.

98 lines
3.0 KiB

  1. /*
  2. example/example12.cpp -- overriding virtual functions from Python
  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 "example.h"
  8. #include <pybind11/functional.h>
  9. /* This is an example class that we'll want to be able to extend from Python */
  10. class Example12 {
  11. public:
  12. Example12(int state) : state(state) {
  13. cout << "Constructing Example12.." << endl;
  14. }
  15. ~Example12() {
  16. cout << "Destructing Example12.." << endl;
  17. }
  18. virtual int run(int value) {
  19. std::cout << "Original implementation of Example12::run(state=" << state
  20. << ", value=" << value << ")" << std::endl;
  21. return state + value;
  22. }
  23. virtual bool run_bool() = 0;
  24. virtual void pure_virtual() = 0;
  25. private:
  26. int state;
  27. };
  28. /* This is a wrapper class that must be generated */
  29. class PyExample12 : public Example12 {
  30. public:
  31. using Example12::Example12; /* Inherit constructors */
  32. virtual int run(int value) {
  33. /* Generate wrapping code that enables native function overloading */
  34. PYBIND11_OVERLOAD(
  35. int, /* Return type */
  36. Example12, /* Parent class */
  37. run, /* Name of function */
  38. value /* Argument(s) */
  39. );
  40. }
  41. virtual bool run_bool() {
  42. PYBIND11_OVERLOAD_PURE(
  43. bool, /* Return type */
  44. Example12, /* Parent class */
  45. run_bool, /* Name of function */
  46. /* This function has no arguments. The trailing comma
  47. in the previous line is needed for some compilers */
  48. );
  49. }
  50. virtual void pure_virtual() {
  51. PYBIND11_OVERLOAD_PURE(
  52. void, /* Return type */
  53. Example12, /* Parent class */
  54. pure_virtual, /* Name of function */
  55. /* This function has no arguments. The trailing comma
  56. in the previous line is needed for some compilers */
  57. );
  58. }
  59. };
  60. int runExample12(Example12 *ex, int value) {
  61. return ex->run(value);
  62. }
  63. bool runExample12Bool(Example12* ex) {
  64. return ex->run_bool();
  65. }
  66. void runExample12Virtual(Example12 *ex) {
  67. ex->pure_virtual();
  68. }
  69. void init_ex12(py::module &m) {
  70. /* Important: indicate the trampoline class PyExample12 using the third
  71. argument to py::class_. The second argument with the unique pointer
  72. is simply the default holder type used by pybind11. */
  73. py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12")
  74. .def(py::init<int>())
  75. /* Reference original class in function definitions */
  76. .def("run", &Example12::run)
  77. .def("run_bool", &Example12::run_bool)
  78. .def("pure_virtual", &Example12::pure_virtual);
  79. m.def("runExample12", &runExample12);
  80. m.def("runExample12Bool", &runExample12Bool);
  81. m.def("runExample12Virtual", &runExample12Virtual);
  82. }