Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 1 | /* |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | tests/test_virtual_functions.cpp -- overriding virtual functions from Python |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | #include "pybind11_tests.h" |
| 11 | #include "constructor_stats.h" |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 12 | #include <pybind11/functional.h> |
Wenzel Jakob | 44e39e0 | 2018-09-11 09:32:45 +0200 | [diff] [blame] | 13 | #include <thread> |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 14 | |
| 15 | /* This is an example class that we'll want to be able to extend from Python */ |
| 16 | class ExampleVirt { |
| 17 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 18 | ExampleVirt(int state) : state(state) { print_created(this, state); } |
| 19 | ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); } |
| 20 | ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; } |
Jason Rhinelander | e763f04 | 2018-05-18 12:48:32 -0300 | [diff] [blame] | 21 | virtual ~ExampleVirt() { print_destroyed(this); } |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 22 | |
| 23 | virtual int run(int value) { |
Dean Moldovan | 81511be | 2016-09-07 00:50:10 +0200 | [diff] [blame] | 24 | py::print("Original implementation of " |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 25 | "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(state, value, get_string1(), *get_string2())); |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 26 | return state + value; |
| 27 | } |
| 28 | |
| 29 | virtual bool run_bool() = 0; |
| 30 | virtual void pure_virtual() = 0; |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 31 | |
| 32 | // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a |
| 33 | // bit trickier, because the actual int& or std::string& or whatever only exists temporarily, so |
| 34 | // we have to handle it specially in the trampoline class (see below). |
| 35 | virtual const std::string &get_string1() { return str1; } |
| 36 | virtual const std::string *get_string2() { return &str2; } |
| 37 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 38 | private: |
| 39 | int state; |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 40 | const std::string str1{"default1"}, str2{"default2"}; |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* This is a wrapper class that must be generated */ |
| 44 | class PyExampleVirt : public ExampleVirt { |
| 45 | public: |
| 46 | using ExampleVirt::ExampleVirt; /* Inherit constructors */ |
| 47 | |
Jason Rhinelander | 116d37c | 2016-09-08 14:47:05 -0400 | [diff] [blame] | 48 | int run(int value) override { |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 49 | /* Generate wrapping code that enables native function overloading */ |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 50 | PYBIND11_OVERRIDE( |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 51 | int, /* Return type */ |
| 52 | ExampleVirt, /* Parent class */ |
| 53 | run, /* Name of function */ |
| 54 | value /* Argument(s) */ |
| 55 | ); |
| 56 | } |
| 57 | |
Jason Rhinelander | 116d37c | 2016-09-08 14:47:05 -0400 | [diff] [blame] | 58 | bool run_bool() override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 59 | PYBIND11_OVERRIDE_PURE( |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 60 | bool, /* Return type */ |
| 61 | ExampleVirt, /* Parent class */ |
| 62 | run_bool, /* Name of function */ |
| 63 | /* This function has no arguments. The trailing comma |
| 64 | in the previous line is needed for some compilers */ |
| 65 | ); |
| 66 | } |
| 67 | |
Jason Rhinelander | 116d37c | 2016-09-08 14:47:05 -0400 | [diff] [blame] | 68 | void pure_virtual() override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 69 | PYBIND11_OVERRIDE_PURE( |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 70 | void, /* Return type */ |
| 71 | ExampleVirt, /* Parent class */ |
| 72 | pure_virtual, /* Name of function */ |
| 73 | /* This function has no arguments. The trailing comma |
| 74 | in the previous line is needed for some compilers */ |
| 75 | ); |
| 76 | } |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 77 | |
| 78 | // We can return reference types for compatibility with C++ virtual interfaces that do so, but |
| 79 | // note they have some significant limitations (see the documentation). |
| 80 | const std::string &get_string1() override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 81 | PYBIND11_OVERRIDE( |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 82 | const std::string &, /* Return type */ |
| 83 | ExampleVirt, /* Parent class */ |
| 84 | get_string1, /* Name of function */ |
| 85 | /* (no arguments) */ |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | const std::string *get_string2() override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 90 | PYBIND11_OVERRIDE( |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 91 | const std::string *, /* Return type */ |
| 92 | ExampleVirt, /* Parent class */ |
| 93 | get_string2, /* Name of function */ |
| 94 | /* (no arguments) */ |
| 95 | ); |
| 96 | } |
| 97 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 100 | class NonCopyable { |
| 101 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 102 | NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); } |
| 103 | NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 104 | NonCopyable(const NonCopyable &) = delete; |
| 105 | NonCopyable() = delete; |
| 106 | void operator=(const NonCopyable &) = delete; |
| 107 | void operator=(NonCopyable &&) = delete; |
| 108 | std::string get_value() const { |
| 109 | if (value) return std::to_string(*value); else return "(null)"; |
| 110 | } |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 111 | ~NonCopyable() { print_destroyed(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | std::unique_ptr<int> value; |
| 115 | }; |
| 116 | |
| 117 | // This is like the above, but is both copy and movable. In effect this means it should get moved |
| 118 | // when it is not referenced elsewhere, but copied if it is still referenced. |
| 119 | class Movable { |
| 120 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 121 | Movable(int a, int b) : value{a+b} { print_created(this, a, b); } |
| 122 | Movable(const Movable &m) { value = m.value; print_copy_created(this); } |
| 123 | Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); } |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 124 | std::string get_value() const { return std::to_string(value); } |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 125 | ~Movable() { print_destroyed(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 126 | private: |
| 127 | int value; |
| 128 | }; |
| 129 | |
| 130 | class NCVirt { |
| 131 | public: |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 132 | virtual ~NCVirt() = default; |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 133 | NCVirt() = default; |
| 134 | NCVirt(const NCVirt&) = delete; |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 135 | virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); } |
| 136 | virtual Movable get_movable(int a, int b) = 0; |
| 137 | |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 138 | std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); } |
| 139 | std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 140 | }; |
| 141 | class NCVirtTrampoline : public NCVirt { |
andriish | 38370a8 | 2020-09-12 04:06:52 +0200 | [diff] [blame] | 142 | #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__) |
Jason Rhinelander | 116d37c | 2016-09-08 14:47:05 -0400 | [diff] [blame] | 143 | NonCopyable get_noncopyable(int a, int b) override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 144 | PYBIND11_OVERRIDE(NonCopyable, NCVirt, get_noncopyable, a, b); |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 145 | } |
Wenzel Jakob | 1ffce74 | 2016-08-25 01:43:33 +0200 | [diff] [blame] | 146 | #endif |
Jason Rhinelander | 116d37c | 2016-09-08 14:47:05 -0400 | [diff] [blame] | 147 | Movable get_movable(int a, int b) override { |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 148 | PYBIND11_OVERRIDE_PURE(Movable, NCVirt, get_movable, a, b); |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 152 | struct Base { |
| 153 | /* for some reason MSVC2015 can't compile this if the function is pure virtual */ |
| 154 | virtual std::string dispatch() const { return {}; }; |
Jason Rhinelander | c4e1800 | 2017-08-17 00:01:42 -0400 | [diff] [blame] | 155 | virtual ~Base() = default; |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 156 | Base() = default; |
| 157 | Base(const Base&) = delete; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 158 | }; |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 159 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 160 | struct DispatchIssue : Base { |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 161 | std::string dispatch() const override { |
Henry Schreiner | dabbbf3 | 2020-09-15 12:10:31 -0400 | [diff] [blame] | 162 | PYBIND11_OVERRIDE_PURE(std::string, Base, dispatch, /* no arguments */); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 163 | } |
| 164 | }; |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 165 | |
Wenzel Jakob | 44e39e0 | 2018-09-11 09:32:45 +0200 | [diff] [blame] | 166 | static void test_gil() { |
| 167 | { |
| 168 | py::gil_scoped_acquire lock; |
| 169 | py::print("1st lock acquired"); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | py::gil_scoped_acquire lock; |
| 175 | py::print("2nd lock acquired"); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | static void test_gil_from_thread() { |
| 181 | py::gil_scoped_release release; |
| 182 | |
| 183 | std::thread t(test_gil); |
| 184 | t.join(); |
| 185 | } |
| 186 | |
| 187 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 188 | // Forward declaration (so that we can put the main tests here; the inherited virtual approaches are |
| 189 | // rather long). |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 190 | void initialize_inherited_virtuals(py::module_ &m); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 191 | |
| 192 | TEST_SUBMODULE(virtual_functions, m) { |
| 193 | // test_override |
| 194 | py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt") |
| 195 | .def(py::init<int>()) |
| 196 | /* Reference original class in function definitions */ |
| 197 | .def("run", &ExampleVirt::run) |
| 198 | .def("run_bool", &ExampleVirt::run_bool) |
| 199 | .def("pure_virtual", &ExampleVirt::pure_virtual); |
| 200 | |
| 201 | py::class_<NonCopyable>(m, "NonCopyable") |
| 202 | .def(py::init<int, int>()); |
| 203 | |
| 204 | py::class_<Movable>(m, "Movable") |
| 205 | .def(py::init<int, int>()); |
| 206 | |
| 207 | // test_move_support |
andriish | 38370a8 | 2020-09-12 04:06:52 +0200 | [diff] [blame] | 208 | #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__) |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 209 | py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt") |
| 210 | .def(py::init<>()) |
| 211 | .def("get_noncopyable", &NCVirt::get_noncopyable) |
| 212 | .def("get_movable", &NCVirt::get_movable) |
| 213 | .def("print_nc", &NCVirt::print_nc) |
| 214 | .def("print_movable", &NCVirt::print_movable); |
| 215 | #endif |
| 216 | |
| 217 | m.def("runExampleVirt", [](ExampleVirt *ex, int value) { return ex->run(value); }); |
| 218 | m.def("runExampleVirtBool", [](ExampleVirt* ex) { return ex->run_bool(); }); |
| 219 | m.def("runExampleVirtVirtual", [](ExampleVirt *ex) { ex->pure_virtual(); }); |
| 220 | |
| 221 | m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>); |
| 222 | initialize_inherited_virtuals(m); |
| 223 | |
| 224 | // test_alias_delay_initialization1 |
| 225 | // don't invoke Python dispatch classes by default when instantiating C++ classes |
| 226 | // that were not extended on the Python side |
| 227 | struct A { |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 228 | A() = default; |
| 229 | A(const A&) = delete; |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 230 | virtual ~A() = default; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 231 | virtual void f() { py::print("A.f()"); } |
| 232 | }; |
| 233 | |
| 234 | struct PyA : A { |
| 235 | PyA() { py::print("PyA.PyA()"); } |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 236 | PyA(const PyA&) = delete; |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 237 | ~PyA() override { py::print("PyA.~PyA()"); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 238 | |
| 239 | void f() override { |
| 240 | py::print("PyA.f()"); |
Jason Rhinelander | e88656a | 2018-02-27 22:33:41 -0400 | [diff] [blame] | 241 | // This convolution just gives a `void`, but tests that PYBIND11_TYPE() works to protect |
| 242 | // a type containing a , |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 243 | PYBIND11_OVERRIDE(PYBIND11_TYPE(typename std::enable_if<true, void>::type), A, f); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 244 | } |
| 245 | }; |
| 246 | |
| 247 | py::class_<A, PyA>(m, "A") |
| 248 | .def(py::init<>()) |
| 249 | .def("f", &A::f); |
| 250 | |
| 251 | m.def("call_f", [](A *a) { a->f(); }); |
| 252 | |
| 253 | // test_alias_delay_initialization2 |
| 254 | // ... unless we explicitly request it, as in this example: |
| 255 | struct A2 { |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 256 | A2() = default; |
| 257 | A2(const A2&) = delete; |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 258 | virtual ~A2() = default; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 259 | virtual void f() { py::print("A2.f()"); } |
| 260 | }; |
| 261 | |
| 262 | struct PyA2 : A2 { |
| 263 | PyA2() { py::print("PyA2.PyA2()"); } |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 264 | PyA2(const PyA2&) = delete; |
Henry Schreiner | 5dfbe6f | 2020-09-10 22:43:53 -0400 | [diff] [blame] | 265 | ~PyA2() override { py::print("PyA2.~PyA2()"); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 266 | void f() override { |
| 267 | py::print("PyA2.f()"); |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 268 | PYBIND11_OVERRIDE(void, A2, f); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 269 | } |
| 270 | }; |
| 271 | |
| 272 | py::class_<A2, PyA2>(m, "A2") |
| 273 | .def(py::init_alias<>()) |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame] | 274 | .def(py::init([](int) { return new PyA2(); })) |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 275 | .def("f", &A2::f); |
| 276 | |
| 277 | m.def("call_f", [](A2 *a2) { a2->f(); }); |
| 278 | |
| 279 | // test_dispatch_issue |
| 280 | // #159: virtual function dispatch has problems with similar-named functions |
| 281 | py::class_<Base, DispatchIssue>(m, "DispatchIssue") |
| 282 | .def(py::init<>()) |
| 283 | .def("dispatch", &Base::dispatch); |
| 284 | |
| 285 | m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); }); |
| 286 | |
| 287 | // test_override_ref |
Unknown | 0b3f44e | 2017-11-01 21:08:06 -0400 | [diff] [blame] | 288 | // #392/397: overriding reference-returning functions |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 289 | class OverrideTest { |
| 290 | public: |
| 291 | struct A { std::string value = "hi"; }; |
| 292 | std::string v; |
| 293 | A a; |
| 294 | explicit OverrideTest(const std::string &v) : v{v} {} |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 295 | OverrideTest() = default; |
| 296 | OverrideTest(const OverrideTest&) = delete; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 297 | virtual std::string str_value() { return v; } |
| 298 | virtual std::string &str_ref() { return v; } |
| 299 | virtual A A_value() { return a; } |
| 300 | virtual A &A_ref() { return a; } |
Jason Rhinelander | c4e1800 | 2017-08-17 00:01:42 -0400 | [diff] [blame] | 301 | virtual ~OverrideTest() = default; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | class PyOverrideTest : public OverrideTest { |
| 305 | public: |
| 306 | using OverrideTest::OverrideTest; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 307 | std::string str_value() override { PYBIND11_OVERRIDE(std::string, OverrideTest, str_value); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 308 | // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference |
| 309 | // to a python numeric value, since we only copy values in the numeric type caster: |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 310 | // std::string &str_ref() override { PYBIND11_OVERRIDE(std::string &, OverrideTest, str_ref); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 311 | // But we can work around it like this: |
| 312 | private: |
| 313 | std::string _tmp; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 314 | std::string str_ref_helper() { PYBIND11_OVERRIDE(std::string, OverrideTest, str_ref); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 315 | public: |
| 316 | std::string &str_ref() override { return _tmp = str_ref_helper(); } |
| 317 | |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 318 | A A_value() override { PYBIND11_OVERRIDE(A, OverrideTest, A_value); } |
| 319 | A &A_ref() override { PYBIND11_OVERRIDE(A &, OverrideTest, A_ref); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | py::class_<OverrideTest::A>(m, "OverrideTest_A") |
| 323 | .def_readwrite("value", &OverrideTest::A::value); |
| 324 | py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest") |
| 325 | .def(py::init<const std::string &>()) |
| 326 | .def("str_value", &OverrideTest::str_value) |
| 327 | // .def("str_ref", &OverrideTest::str_ref) |
| 328 | .def("A_value", &OverrideTest::A_value) |
| 329 | .def("A_ref", &OverrideTest::A_ref); |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 332 | |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 333 | // Inheriting virtual methods. We do two versions here: the repeat-everything version and the |
| 334 | // templated trampoline versions mentioned in docs/advanced.rst. |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 335 | // |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 336 | // These base classes are exactly the same, but we technically need distinct |
| 337 | // classes for this example code because we need to be able to bind them |
| 338 | // properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 339 | // multiple python classes). |
| 340 | class A_Repeat { |
| 341 | #define A_METHODS \ |
| 342 | public: \ |
| 343 | virtual int unlucky_number() = 0; \ |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 344 | virtual std::string say_something(unsigned times) { \ |
| 345 | std::string s = ""; \ |
| 346 | for (unsigned i = 0; i < times; ++i) \ |
| 347 | s += "hi"; \ |
| 348 | return s; \ |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 349 | } \ |
| 350 | std::string say_everything() { \ |
| 351 | return say_something(1) + " " + std::to_string(unlucky_number()); \ |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 352 | } |
| 353 | A_METHODS |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 354 | A_Repeat() = default; |
| 355 | A_Repeat(const A_Repeat&) = delete; |
Jason Rhinelander | c4e1800 | 2017-08-17 00:01:42 -0400 | [diff] [blame] | 356 | virtual ~A_Repeat() = default; |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 357 | }; |
| 358 | class B_Repeat : public A_Repeat { |
| 359 | #define B_METHODS \ |
| 360 | public: \ |
| 361 | int unlucky_number() override { return 13; } \ |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 362 | std::string say_something(unsigned times) override { \ |
| 363 | return "B says hi " + std::to_string(times) + " times"; \ |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 364 | } \ |
| 365 | virtual double lucky_number() { return 7.0; } |
| 366 | B_METHODS |
| 367 | }; |
| 368 | class C_Repeat : public B_Repeat { |
| 369 | #define C_METHODS \ |
| 370 | public: \ |
| 371 | int unlucky_number() override { return 4444; } \ |
| 372 | double lucky_number() override { return 888; } |
| 373 | C_METHODS |
| 374 | }; |
| 375 | class D_Repeat : public C_Repeat { |
| 376 | #define D_METHODS // Nothing overridden. |
| 377 | D_METHODS |
| 378 | }; |
| 379 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 380 | // Base classes for templated inheritance trampolines. Identical to the repeat-everything version: |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 381 | class A_Tpl { |
| 382 | A_METHODS; |
| 383 | A_Tpl() = default; |
| 384 | A_Tpl(const A_Tpl&) = delete; |
| 385 | virtual ~A_Tpl() = default; |
| 386 | }; |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 387 | class B_Tpl : public A_Tpl { B_METHODS }; |
| 388 | class C_Tpl : public B_Tpl { C_METHODS }; |
| 389 | class D_Tpl : public C_Tpl { D_METHODS }; |
| 390 | |
| 391 | |
| 392 | // Inheritance approach 1: each trampoline gets every virtual method (11 in total) |
| 393 | class PyA_Repeat : public A_Repeat { |
| 394 | public: |
| 395 | using A_Repeat::A_Repeat; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 396 | int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, A_Repeat, unlucky_number, ); } |
| 397 | std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, A_Repeat, say_something, times); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 398 | }; |
| 399 | class PyB_Repeat : public B_Repeat { |
| 400 | public: |
| 401 | using B_Repeat::B_Repeat; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 402 | int unlucky_number() override { PYBIND11_OVERRIDE(int, B_Repeat, unlucky_number, ); } |
| 403 | std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, B_Repeat, say_something, times); } |
| 404 | double lucky_number() override { PYBIND11_OVERRIDE(double, B_Repeat, lucky_number, ); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 405 | }; |
| 406 | class PyC_Repeat : public C_Repeat { |
| 407 | public: |
| 408 | using C_Repeat::C_Repeat; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 409 | int unlucky_number() override { PYBIND11_OVERRIDE(int, C_Repeat, unlucky_number, ); } |
| 410 | std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, C_Repeat, say_something, times); } |
| 411 | double lucky_number() override { PYBIND11_OVERRIDE(double, C_Repeat, lucky_number, ); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 412 | }; |
| 413 | class PyD_Repeat : public D_Repeat { |
| 414 | public: |
| 415 | using D_Repeat::D_Repeat; |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 416 | int unlucky_number() override { PYBIND11_OVERRIDE(int, D_Repeat, unlucky_number, ); } |
| 417 | std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, D_Repeat, say_something, times); } |
| 418 | double lucky_number() override { PYBIND11_OVERRIDE(double, D_Repeat, lucky_number, ); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | // Inheritance approach 2: templated trampoline classes. |
| 422 | // |
| 423 | // Advantages: |
| 424 | // - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for |
| 425 | // any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11 |
| 426 | // methods (repeat). |
| 427 | // - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can |
| 428 | // properly inherit constructors. |
| 429 | // |
| 430 | // Disadvantage: |
| 431 | // - the compiler must still generate and compile 14 different methods (more, even, than the 11 |
| 432 | // required for the repeat approach) instead of the 6 required for MI. (If there was no pure |
| 433 | // method (or no pure method override), the number would drop down to the same 11 as the repeat |
| 434 | // approach). |
| 435 | template <class Base = A_Tpl> |
| 436 | class PyA_Tpl : public Base { |
| 437 | public: |
| 438 | using Base::Base; // Inherit constructors |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 439 | int unlucky_number() override { PYBIND11_OVERRIDE_PURE(int, Base, unlucky_number, ); } |
| 440 | std::string say_something(unsigned times) override { PYBIND11_OVERRIDE(std::string, Base, say_something, times); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 441 | }; |
| 442 | template <class Base = B_Tpl> |
| 443 | class PyB_Tpl : public PyA_Tpl<Base> { |
| 444 | public: |
| 445 | using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors) |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 446 | int unlucky_number() override { PYBIND11_OVERRIDE(int, Base, unlucky_number, ); } |
| 447 | double lucky_number() override { PYBIND11_OVERRIDE(double, Base, lucky_number, ); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 448 | }; |
| 449 | // Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can |
| 450 | // use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead): |
| 451 | /* |
| 452 | template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> { |
| 453 | public: |
| 454 | using PyB_Tpl<Base>::PyB_Tpl; |
| 455 | }; |
| 456 | template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> { |
| 457 | public: |
| 458 | using PyC_Tpl<Base>::PyC_Tpl; |
| 459 | }; |
| 460 | */ |
| 461 | |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 462 | void initialize_inherited_virtuals(py::module_ &m) { |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 463 | // test_inherited_virtuals |
| 464 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 465 | // Method 1: repeat |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 466 | py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 467 | .def(py::init<>()) |
| 468 | .def("unlucky_number", &A_Repeat::unlucky_number) |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 469 | .def("say_something", &A_Repeat::say_something) |
| 470 | .def("say_everything", &A_Repeat::say_everything); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 471 | py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 472 | .def(py::init<>()) |
| 473 | .def("lucky_number", &B_Repeat::lucky_number); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 474 | py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 475 | .def(py::init<>()); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 476 | py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 477 | .def(py::init<>()); |
| 478 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 479 | // test_ |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 480 | // Method 2: Templated trampolines |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 481 | py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 482 | .def(py::init<>()) |
| 483 | .def("unlucky_number", &A_Tpl::unlucky_number) |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 484 | .def("say_something", &A_Tpl::say_something) |
| 485 | .def("say_everything", &A_Tpl::say_everything); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 486 | py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 487 | .def(py::init<>()) |
| 488 | .def("lucky_number", &B_Tpl::lucky_number); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 489 | py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 490 | .def(py::init<>()); |
Jason Rhinelander | 6b52c83 | 2016-09-06 12:27:00 -0400 | [diff] [blame] | 491 | py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl") |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 492 | .def(py::init<>()); |
| 493 | |
Wenzel Jakob | 44e39e0 | 2018-09-11 09:32:45 +0200 | [diff] [blame] | 494 | |
| 495 | // Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7) |
| 496 | m.def("test_gil", &test_gil); |
| 497 | m.def("test_gil_from_thread", &test_gil_from_thread); |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 498 | }; |