blob: 6ea7c2cfd96596abf454d3b31f7bed7f8f459312 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_virtual_functions.cpp -- overriding virtual functions from Python
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04003
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 Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040012#include <pybind11/functional.h>
13
14/* This is an example class that we'll want to be able to extend from Python */
15class ExampleVirt {
16public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040017 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 Rhinelanderb3f3d792016-07-18 16:43:18 -040021
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;
30private:
31 int state;
32};
33
34/* This is a wrapper class that must be generated */
35class PyExampleVirt : public ExampleVirt {
36public:
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 Rhinelandered148792016-07-21 21:31:05 -040070class NonCopyable {
71public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040072 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 Rhinelandered148792016-07-21 21:31:05 -040074 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 Rhinelander3f589372016-08-07 13:05:26 -040081 ~NonCopyable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -040082
83private:
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.
89class Movable {
90public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040091 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 Rhinelandered148792016-07-21 21:31:05 -040094 int get_value() const { return value; }
Jason Rhinelander3f589372016-08-07 13:05:26 -040095 ~Movable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -040096private:
97 int value;
98};
99
100class NCVirt {
101public:
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};
108class 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 Rhinelanderb3f3d792016-07-18 16:43:18 -0400117int runExampleVirt(ExampleVirt *ex, int value) {
118 return ex->run(value);
119}
120
121bool runExampleVirtBool(ExampleVirt* ex) {
122 return ex->run_bool();
123}
124
125void runExampleVirtVirtual(ExampleVirt *ex) {
126 ex->pure_virtual();
127}
128
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400129
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400130// Inheriting virtual methods. We do two versions here: the repeat-everything version and the
131// templated trampoline versions mentioned in docs/advanced.rst.
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400132//
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400133// 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 Rhinelander0ca96e22016-08-05 17:02:33 -0400136// multiple python classes).
137class A_Repeat {
138#define A_METHODS \
139public: \
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 }
145A_METHODS
146};
147class B_Repeat : public A_Repeat {
148#define B_METHODS \
149public: \
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; }
155B_METHODS
156};
157class C_Repeat : public B_Repeat {
158#define C_METHODS \
159public: \
160 int unlucky_number() override { return 4444; } \
161 double lucky_number() override { return 888; }
162C_METHODS
163};
164class D_Repeat : public C_Repeat {
165#define D_METHODS // Nothing overridden.
166D_METHODS
167};
168
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400169// Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
170class A_Tpl { A_METHODS };
171class B_Tpl : public A_Tpl { B_METHODS };
172class C_Tpl : public B_Tpl { C_METHODS };
173class D_Tpl : public C_Tpl { D_METHODS };
174
175
176// Inheritance approach 1: each trampoline gets every virtual method (11 in total)
177class PyA_Repeat : public A_Repeat {
178public:
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};
183class PyB_Repeat : public B_Repeat {
184public:
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};
190class PyC_Repeat : public C_Repeat {
191public:
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};
197class PyD_Repeat : public D_Repeat {
198public:
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).
219template <class Base = A_Tpl>
220class PyA_Tpl : public Base {
221public:
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};
226template <class Base = B_Tpl>
227class PyB_Tpl : public PyA_Tpl<Base> {
228public:
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 Jakob216df0d2016-08-12 00:59:57 +0200231 double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400232};
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/*
236template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
237public:
238 using PyB_Tpl<Base>::PyB_Tpl;
239};
240template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
241public:
242 using PyC_Tpl<Base>::PyC_Tpl;
243};
244*/
245
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400246
247void 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 Rhinelander0ca96e22016-08-05 17:02:33 -0400274};
275
276
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400277void 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 Rhinelandered148792016-07-21 21:31:05 -0400288 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 Rhinelanderb3f3d792016-07-18 16:43:18 -0400302 m.def("runExampleVirt", &runExampleVirt);
303 m.def("runExampleVirtBool", &runExampleVirtBool);
304 m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400305
Jason Rhinelander3f589372016-08-07 13:05:26 -0400306 m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400307 initialize_inherited_virtuals(m);
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400308}