blob: e591ba922a74005db8ce4ed905208a9c4c5ce1a3 [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); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +020094 std::string get_value() const { return std::to_string(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
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200105 std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
106 std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400107};
108class NCVirtTrampoline : public NCVirt {
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200109#if !defined(__INTEL_COMPILER)
Jason Rhinelandered148792016-07-21 21:31:05 -0400110 virtual NonCopyable get_noncopyable(int a, int b) {
111 PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b);
112 }
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200113#endif
Jason Rhinelandered148792016-07-21 21:31:05 -0400114 virtual Movable get_movable(int a, int b) {
115 PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b);
116 }
117};
118
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400119int runExampleVirt(ExampleVirt *ex, int value) {
120 return ex->run(value);
121}
122
123bool runExampleVirtBool(ExampleVirt* ex) {
124 return ex->run_bool();
125}
126
127void runExampleVirtVirtual(ExampleVirt *ex) {
128 ex->pure_virtual();
129}
130
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400131
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400132// Inheriting virtual methods. We do two versions here: the repeat-everything version and the
133// templated trampoline versions mentioned in docs/advanced.rst.
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400134//
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400135// These base classes are exactly the same, but we technically need distinct
136// classes for this example code because we need to be able to bind them
137// properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400138// multiple python classes).
139class A_Repeat {
140#define A_METHODS \
141public: \
142 virtual int unlucky_number() = 0; \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200143 virtual std::string say_something(unsigned times) { \
144 std::string s = ""; \
145 for (unsigned i = 0; i < times; ++i) \
146 s += "hi"; \
147 return s; \
Jason Rhinelander20978262016-08-29 18:16:46 -0400148 } \
149 std::string say_everything() { \
150 return say_something(1) + " " + std::to_string(unlucky_number()); \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400151 }
152A_METHODS
153};
154class B_Repeat : public A_Repeat {
155#define B_METHODS \
156public: \
157 int unlucky_number() override { return 13; } \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200158 std::string say_something(unsigned times) override { \
159 return "B says hi " + std::to_string(times) + " times"; \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400160 } \
161 virtual double lucky_number() { return 7.0; }
162B_METHODS
163};
164class C_Repeat : public B_Repeat {
165#define C_METHODS \
166public: \
167 int unlucky_number() override { return 4444; } \
168 double lucky_number() override { return 888; }
169C_METHODS
170};
171class D_Repeat : public C_Repeat {
172#define D_METHODS // Nothing overridden.
173D_METHODS
174};
175
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400176// Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
177class A_Tpl { A_METHODS };
178class B_Tpl : public A_Tpl { B_METHODS };
179class C_Tpl : public B_Tpl { C_METHODS };
180class D_Tpl : public C_Tpl { D_METHODS };
181
182
183// Inheritance approach 1: each trampoline gets every virtual method (11 in total)
184class PyA_Repeat : public A_Repeat {
185public:
186 using A_Repeat::A_Repeat;
187 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200188 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, A_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400189};
190class PyB_Repeat : public B_Repeat {
191public:
192 using B_Repeat::B_Repeat;
193 int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200194 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, B_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400195 double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); }
196};
197class PyC_Repeat : public C_Repeat {
198public:
199 using C_Repeat::C_Repeat;
200 int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200201 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, C_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400202 double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); }
203};
204class PyD_Repeat : public D_Repeat {
205public:
206 using D_Repeat::D_Repeat;
207 int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200208 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, D_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400209 double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); }
210};
211
212// Inheritance approach 2: templated trampoline classes.
213//
214// Advantages:
215// - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for
216// any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11
217// methods (repeat).
218// - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can
219// properly inherit constructors.
220//
221// Disadvantage:
222// - the compiler must still generate and compile 14 different methods (more, even, than the 11
223// required for the repeat approach) instead of the 6 required for MI. (If there was no pure
224// method (or no pure method override), the number would drop down to the same 11 as the repeat
225// approach).
226template <class Base = A_Tpl>
227class PyA_Tpl : public Base {
228public:
229 using Base::Base; // Inherit constructors
230 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200231 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, Base, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400232};
233template <class Base = B_Tpl>
234class PyB_Tpl : public PyA_Tpl<Base> {
235public:
236 using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
237 int unlucky_number() override { PYBIND11_OVERLOAD(int, Base, unlucky_number, ); }
Wenzel Jakob216df0d2016-08-12 00:59:57 +0200238 double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400239};
240// Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can
241// use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):
242/*
243template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
244public:
245 using PyB_Tpl<Base>::PyB_Tpl;
246};
247template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
248public:
249 using PyC_Tpl<Base>::PyC_Tpl;
250};
251*/
252
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400253
254void initialize_inherited_virtuals(py::module &m) {
255 // Method 1: repeat
256 py::class_<A_Repeat, std::unique_ptr<A_Repeat>, PyA_Repeat>(m, "A_Repeat")
257 .def(py::init<>())
258 .def("unlucky_number", &A_Repeat::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400259 .def("say_something", &A_Repeat::say_something)
260 .def("say_everything", &A_Repeat::say_everything);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400261 py::class_<B_Repeat, std::unique_ptr<B_Repeat>, PyB_Repeat>(m, "B_Repeat", py::base<A_Repeat>())
262 .def(py::init<>())
263 .def("lucky_number", &B_Repeat::lucky_number);
264 py::class_<C_Repeat, std::unique_ptr<C_Repeat>, PyC_Repeat>(m, "C_Repeat", py::base<B_Repeat>())
265 .def(py::init<>());
266 py::class_<D_Repeat, std::unique_ptr<D_Repeat>, PyD_Repeat>(m, "D_Repeat", py::base<C_Repeat>())
267 .def(py::init<>());
268
269 // Method 2: Templated trampolines
270 py::class_<A_Tpl, std::unique_ptr<A_Tpl>, PyA_Tpl<>>(m, "A_Tpl")
271 .def(py::init<>())
272 .def("unlucky_number", &A_Tpl::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400273 .def("say_something", &A_Tpl::say_something)
274 .def("say_everything", &A_Tpl::say_everything);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400275 py::class_<B_Tpl, std::unique_ptr<B_Tpl>, PyB_Tpl<>>(m, "B_Tpl", py::base<A_Tpl>())
276 .def(py::init<>())
277 .def("lucky_number", &B_Tpl::lucky_number);
278 py::class_<C_Tpl, std::unique_ptr<C_Tpl>, PyB_Tpl<C_Tpl>>(m, "C_Tpl", py::base<B_Tpl>())
279 .def(py::init<>());
280 py::class_<D_Tpl, std::unique_ptr<D_Tpl>, PyB_Tpl<D_Tpl>>(m, "D_Tpl", py::base<C_Tpl>())
281 .def(py::init<>());
282
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400283};
284
285
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400286test_initializer virtual_functions([](py::module &m) {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400287 /* Important: indicate the trampoline class PyExampleVirt using the third
288 argument to py::class_. The second argument with the unique pointer
289 is simply the default holder type used by pybind11. */
290 py::class_<ExampleVirt, std::unique_ptr<ExampleVirt>, PyExampleVirt>(m, "ExampleVirt")
291 .def(py::init<int>())
292 /* Reference original class in function definitions */
293 .def("run", &ExampleVirt::run)
294 .def("run_bool", &ExampleVirt::run_bool)
295 .def("pure_virtual", &ExampleVirt::pure_virtual);
296
Jason Rhinelandered148792016-07-21 21:31:05 -0400297 py::class_<NonCopyable>(m, "NonCopyable")
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200298 .def(py::init<int, int>());
299
Jason Rhinelandered148792016-07-21 21:31:05 -0400300 py::class_<Movable>(m, "Movable")
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200301 .def(py::init<int, int>());
302
303#if !defined(__INTEL_COMPILER)
Jason Rhinelandered148792016-07-21 21:31:05 -0400304 py::class_<NCVirt, std::unique_ptr<NCVirt>, NCVirtTrampoline>(m, "NCVirt")
305 .def(py::init<>())
306 .def("get_noncopyable", &NCVirt::get_noncopyable)
307 .def("get_movable", &NCVirt::get_movable)
308 .def("print_nc", &NCVirt::print_nc)
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200309 .def("print_movable", &NCVirt::print_movable);
310#endif
Jason Rhinelandered148792016-07-21 21:31:05 -0400311
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400312 m.def("runExampleVirt", &runExampleVirt);
313 m.def("runExampleVirtBool", &runExampleVirtBool);
314 m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400315
Jason Rhinelander3f589372016-08-07 13:05:26 -0400316 m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400317 initialize_inherited_virtuals(m);
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400318});