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