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) { |
| 23 | std::cout << "Original implementation of ExampleVirt::run(state=" << state |
| 24 | << ", value=" << value << ")" << std::endl; |
| 25 | return state + value; |
| 26 | } |
| 27 | |
| 28 | virtual bool run_bool() = 0; |
| 29 | virtual void pure_virtual() = 0; |
| 30 | private: |
| 31 | int state; |
| 32 | }; |
| 33 | |
| 34 | /* This is a wrapper class that must be generated */ |
| 35 | class PyExampleVirt : public ExampleVirt { |
| 36 | public: |
| 37 | using ExampleVirt::ExampleVirt; /* Inherit constructors */ |
| 38 | |
| 39 | virtual int run(int value) { |
| 40 | /* Generate wrapping code that enables native function overloading */ |
| 41 | PYBIND11_OVERLOAD( |
| 42 | int, /* Return type */ |
| 43 | ExampleVirt, /* Parent class */ |
| 44 | run, /* Name of function */ |
| 45 | value /* Argument(s) */ |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | virtual bool run_bool() { |
| 50 | PYBIND11_OVERLOAD_PURE( |
| 51 | bool, /* Return type */ |
| 52 | ExampleVirt, /* Parent class */ |
| 53 | run_bool, /* Name of function */ |
| 54 | /* This function has no arguments. The trailing comma |
| 55 | in the previous line is needed for some compilers */ |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | virtual void pure_virtual() { |
| 60 | PYBIND11_OVERLOAD_PURE( |
| 61 | void, /* Return type */ |
| 62 | ExampleVirt, /* Parent class */ |
| 63 | pure_virtual, /* Name of function */ |
| 64 | /* This function has no arguments. The trailing comma |
| 65 | in the previous line is needed for some compilers */ |
| 66 | ); |
| 67 | } |
| 68 | }; |
| 69 | |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 70 | class NonCopyable { |
| 71 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 72 | NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); } |
| 73 | NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 74 | NonCopyable(const NonCopyable &) = delete; |
| 75 | NonCopyable() = delete; |
| 76 | void operator=(const NonCopyable &) = delete; |
| 77 | void operator=(NonCopyable &&) = delete; |
| 78 | std::string get_value() const { |
| 79 | if (value) return std::to_string(*value); else return "(null)"; |
| 80 | } |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 81 | ~NonCopyable() { print_destroyed(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | std::unique_ptr<int> value; |
| 85 | }; |
| 86 | |
| 87 | // This is like the above, but is both copy and movable. In effect this means it should get moved |
| 88 | // when it is not referenced elsewhere, but copied if it is still referenced. |
| 89 | class Movable { |
| 90 | public: |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 91 | Movable(int a, int b) : value{a+b} { print_created(this, a, b); } |
| 92 | Movable(const Movable &m) { value = m.value; print_copy_created(this); } |
| 93 | Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 94 | int get_value() const { return value; } |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 95 | ~Movable() { print_destroyed(this); } |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 96 | private: |
| 97 | int value; |
| 98 | }; |
| 99 | |
| 100 | class NCVirt { |
| 101 | public: |
| 102 | virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); } |
| 103 | virtual Movable get_movable(int a, int b) = 0; |
| 104 | |
| 105 | void print_nc(int a, int b) { std::cout << get_noncopyable(a, b).get_value() << std::endl; } |
| 106 | void print_movable(int a, int b) { std::cout << get_movable(a, b).get_value() << std::endl; } |
| 107 | }; |
| 108 | class NCVirtTrampoline : public NCVirt { |
| 109 | virtual NonCopyable get_noncopyable(int a, int b) { |
| 110 | PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b); |
| 111 | } |
| 112 | virtual Movable get_movable(int a, int b) { |
| 113 | PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b); |
| 114 | } |
| 115 | }; |
| 116 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 117 | int runExampleVirt(ExampleVirt *ex, int value) { |
| 118 | return ex->run(value); |
| 119 | } |
| 120 | |
| 121 | bool runExampleVirtBool(ExampleVirt* ex) { |
| 122 | return ex->run_bool(); |
| 123 | } |
| 124 | |
| 125 | void runExampleVirtVirtual(ExampleVirt *ex) { |
| 126 | ex->pure_virtual(); |
| 127 | } |
| 128 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 129 | |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 130 | // Inheriting virtual methods. We do two versions here: the repeat-everything version and the |
| 131 | // templated trampoline versions mentioned in docs/advanced.rst. |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 132 | // |
Jason Rhinelander | d6c365b | 2016-08-05 17:44:28 -0400 | [diff] [blame] | 133 | // These base classes are exactly the same, but we technically need distinct |
| 134 | // classes for this example code because we need to be able to bind them |
| 135 | // 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] | 136 | // multiple python classes). |
| 137 | class A_Repeat { |
| 138 | #define A_METHODS \ |
| 139 | public: \ |
| 140 | virtual int unlucky_number() = 0; \ |
| 141 | virtual void say_something(unsigned times) { \ |
| 142 | for (unsigned i = 0; i < times; i++) std::cout << "hi"; \ |
| 143 | std::cout << std::endl; \ |
| 144 | } |
| 145 | A_METHODS |
| 146 | }; |
| 147 | class B_Repeat : public A_Repeat { |
| 148 | #define B_METHODS \ |
| 149 | public: \ |
| 150 | int unlucky_number() override { return 13; } \ |
| 151 | void say_something(unsigned times) override { \ |
| 152 | std::cout << "B says hi " << times << " times" << std::endl; \ |
| 153 | } \ |
| 154 | virtual double lucky_number() { return 7.0; } |
| 155 | B_METHODS |
| 156 | }; |
| 157 | class C_Repeat : public B_Repeat { |
| 158 | #define C_METHODS \ |
| 159 | public: \ |
| 160 | int unlucky_number() override { return 4444; } \ |
| 161 | double lucky_number() override { return 888; } |
| 162 | C_METHODS |
| 163 | }; |
| 164 | class D_Repeat : public C_Repeat { |
| 165 | #define D_METHODS // Nothing overridden. |
| 166 | D_METHODS |
| 167 | }; |
| 168 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 169 | // Base classes for templated inheritance trampolines. Identical to the repeat-everything version: |
| 170 | class A_Tpl { A_METHODS }; |
| 171 | class B_Tpl : public A_Tpl { B_METHODS }; |
| 172 | class C_Tpl : public B_Tpl { C_METHODS }; |
| 173 | class D_Tpl : public C_Tpl { D_METHODS }; |
| 174 | |
| 175 | |
| 176 | // Inheritance approach 1: each trampoline gets every virtual method (11 in total) |
| 177 | class PyA_Repeat : public A_Repeat { |
| 178 | public: |
| 179 | using A_Repeat::A_Repeat; |
| 180 | int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); } |
| 181 | void say_something(unsigned times) override { PYBIND11_OVERLOAD(void, A_Repeat, say_something, times); } |
| 182 | }; |
| 183 | class PyB_Repeat : public B_Repeat { |
| 184 | public: |
| 185 | using B_Repeat::B_Repeat; |
| 186 | int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); } |
| 187 | void say_something(unsigned times) override { PYBIND11_OVERLOAD(void, B_Repeat, say_something, times); } |
| 188 | double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); } |
| 189 | }; |
| 190 | class PyC_Repeat : public C_Repeat { |
| 191 | public: |
| 192 | using C_Repeat::C_Repeat; |
| 193 | int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); } |
| 194 | void say_something(unsigned times) override { PYBIND11_OVERLOAD(void, C_Repeat, say_something, times); } |
| 195 | double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); } |
| 196 | }; |
| 197 | class PyD_Repeat : public D_Repeat { |
| 198 | public: |
| 199 | using D_Repeat::D_Repeat; |
| 200 | int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); } |
| 201 | void say_something(unsigned times) override { PYBIND11_OVERLOAD(void, D_Repeat, say_something, times); } |
| 202 | double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); } |
| 203 | }; |
| 204 | |
| 205 | // Inheritance approach 2: templated trampoline classes. |
| 206 | // |
| 207 | // Advantages: |
| 208 | // - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for |
| 209 | // any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11 |
| 210 | // methods (repeat). |
| 211 | // - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can |
| 212 | // properly inherit constructors. |
| 213 | // |
| 214 | // Disadvantage: |
| 215 | // - the compiler must still generate and compile 14 different methods (more, even, than the 11 |
| 216 | // required for the repeat approach) instead of the 6 required for MI. (If there was no pure |
| 217 | // method (or no pure method override), the number would drop down to the same 11 as the repeat |
| 218 | // approach). |
| 219 | template <class Base = A_Tpl> |
| 220 | class PyA_Tpl : public Base { |
| 221 | public: |
| 222 | using Base::Base; // Inherit constructors |
| 223 | int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); } |
| 224 | void say_something(unsigned times) override { PYBIND11_OVERLOAD(void, Base, say_something, times); } |
| 225 | }; |
| 226 | template <class Base = B_Tpl> |
| 227 | class PyB_Tpl : public PyA_Tpl<Base> { |
| 228 | public: |
| 229 | using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors) |
| 230 | int unlucky_number() override { PYBIND11_OVERLOAD(int, Base, unlucky_number, ); } |
Wenzel Jakob | 216df0d | 2016-08-12 00:59:57 +0200 | [diff] [blame] | 231 | double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); } |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 232 | }; |
| 233 | // Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can |
| 234 | // use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead): |
| 235 | /* |
| 236 | template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> { |
| 237 | public: |
| 238 | using PyB_Tpl<Base>::PyB_Tpl; |
| 239 | }; |
| 240 | template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> { |
| 241 | public: |
| 242 | using PyC_Tpl<Base>::PyC_Tpl; |
| 243 | }; |
| 244 | */ |
| 245 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 246 | |
| 247 | void initialize_inherited_virtuals(py::module &m) { |
| 248 | // Method 1: repeat |
| 249 | py::class_<A_Repeat, std::unique_ptr<A_Repeat>, PyA_Repeat>(m, "A_Repeat") |
| 250 | .def(py::init<>()) |
| 251 | .def("unlucky_number", &A_Repeat::unlucky_number) |
| 252 | .def("say_something", &A_Repeat::say_something); |
| 253 | py::class_<B_Repeat, std::unique_ptr<B_Repeat>, PyB_Repeat>(m, "B_Repeat", py::base<A_Repeat>()) |
| 254 | .def(py::init<>()) |
| 255 | .def("lucky_number", &B_Repeat::lucky_number); |
| 256 | py::class_<C_Repeat, std::unique_ptr<C_Repeat>, PyC_Repeat>(m, "C_Repeat", py::base<B_Repeat>()) |
| 257 | .def(py::init<>()); |
| 258 | py::class_<D_Repeat, std::unique_ptr<D_Repeat>, PyD_Repeat>(m, "D_Repeat", py::base<C_Repeat>()) |
| 259 | .def(py::init<>()); |
| 260 | |
| 261 | // Method 2: Templated trampolines |
| 262 | py::class_<A_Tpl, std::unique_ptr<A_Tpl>, PyA_Tpl<>>(m, "A_Tpl") |
| 263 | .def(py::init<>()) |
| 264 | .def("unlucky_number", &A_Tpl::unlucky_number) |
| 265 | .def("say_something", &A_Tpl::say_something); |
| 266 | py::class_<B_Tpl, std::unique_ptr<B_Tpl>, PyB_Tpl<>>(m, "B_Tpl", py::base<A_Tpl>()) |
| 267 | .def(py::init<>()) |
| 268 | .def("lucky_number", &B_Tpl::lucky_number); |
| 269 | py::class_<C_Tpl, std::unique_ptr<C_Tpl>, PyB_Tpl<C_Tpl>>(m, "C_Tpl", py::base<B_Tpl>()) |
| 270 | .def(py::init<>()); |
| 271 | py::class_<D_Tpl, std::unique_ptr<D_Tpl>, PyB_Tpl<D_Tpl>>(m, "D_Tpl", py::base<C_Tpl>()) |
| 272 | .def(py::init<>()); |
| 273 | |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 277 | void init_ex_virtual_functions(py::module &m) { |
| 278 | /* Important: indicate the trampoline class PyExampleVirt using the third |
| 279 | argument to py::class_. The second argument with the unique pointer |
| 280 | is simply the default holder type used by pybind11. */ |
| 281 | py::class_<ExampleVirt, std::unique_ptr<ExampleVirt>, PyExampleVirt>(m, "ExampleVirt") |
| 282 | .def(py::init<int>()) |
| 283 | /* Reference original class in function definitions */ |
| 284 | .def("run", &ExampleVirt::run) |
| 285 | .def("run_bool", &ExampleVirt::run_bool) |
| 286 | .def("pure_virtual", &ExampleVirt::pure_virtual); |
| 287 | |
Jason Rhinelander | ed14879 | 2016-07-21 21:31:05 -0400 | [diff] [blame] | 288 | py::class_<NonCopyable>(m, "NonCopyable") |
| 289 | .def(py::init<int, int>()) |
| 290 | ; |
| 291 | py::class_<Movable>(m, "Movable") |
| 292 | .def(py::init<int, int>()) |
| 293 | ; |
| 294 | py::class_<NCVirt, std::unique_ptr<NCVirt>, NCVirtTrampoline>(m, "NCVirt") |
| 295 | .def(py::init<>()) |
| 296 | .def("get_noncopyable", &NCVirt::get_noncopyable) |
| 297 | .def("get_movable", &NCVirt::get_movable) |
| 298 | .def("print_nc", &NCVirt::print_nc) |
| 299 | .def("print_movable", &NCVirt::print_movable) |
| 300 | ; |
| 301 | |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 302 | m.def("runExampleVirt", &runExampleVirt); |
| 303 | m.def("runExampleVirtBool", &runExampleVirtBool); |
| 304 | m.def("runExampleVirtVirtual", &runExampleVirtVirtual); |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 305 | |
Jason Rhinelander | 3f58937 | 2016-08-07 13:05:26 -0400 | [diff] [blame] | 306 | m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>); |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame] | 307 | initialize_inherited_virtuals(m); |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 308 | } |