You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
16 KiB

3 months ago
  1. /*
  2. tests/test_virtual_functions.cpp -- overriding virtual functions from Python
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. #include <pybind11/functional.h>
  10. /* This is an example class that we'll want to be able to extend from Python */
  11. class ExampleVirt {
  12. public:
  13. ExampleVirt(int state) : state(state) { print_created(this, state); }
  14. ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
  15. ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; }
  16. ~ExampleVirt() { print_destroyed(this); }
  17. virtual int run(int value) {
  18. py::print("Original implementation of "
  19. "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(state, value, get_string1(), *get_string2()));
  20. return state + value;
  21. }
  22. virtual bool run_bool() = 0;
  23. virtual void pure_virtual() = 0;
  24. // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a
  25. // bit trickier, because the actual int& or std::string& or whatever only exists temporarily, so
  26. // we have to handle it specially in the trampoline class (see below).
  27. virtual const std::string &get_string1() { return str1; }
  28. virtual const std::string *get_string2() { return &str2; }
  29. private:
  30. int state;
  31. const std::string str1{"default1"}, str2{"default2"};
  32. };
  33. /* This is a wrapper class that must be generated */
  34. class PyExampleVirt : public ExampleVirt {
  35. public:
  36. using ExampleVirt::ExampleVirt; /* Inherit constructors */
  37. int run(int value) override {
  38. /* Generate wrapping code that enables native function overloading */
  39. PYBIND11_OVERLOAD(
  40. int, /* Return type */
  41. ExampleVirt, /* Parent class */
  42. run, /* Name of function */
  43. value /* Argument(s) */
  44. );
  45. }
  46. bool run_bool() override {
  47. PYBIND11_OVERLOAD_PURE(
  48. bool, /* Return type */
  49. ExampleVirt, /* Parent class */
  50. run_bool, /* Name of function */
  51. /* This function has no arguments. The trailing comma
  52. in the previous line is needed for some compilers */
  53. );
  54. }
  55. void pure_virtual() override {
  56. PYBIND11_OVERLOAD_PURE(
  57. void, /* Return type */
  58. ExampleVirt, /* Parent class */
  59. pure_virtual, /* Name of function */
  60. /* This function has no arguments. The trailing comma
  61. in the previous line is needed for some compilers */
  62. );
  63. }
  64. // We can return reference types for compatibility with C++ virtual interfaces that do so, but
  65. // note they have some significant limitations (see the documentation).
  66. const std::string &get_string1() override {
  67. PYBIND11_OVERLOAD(
  68. const std::string &, /* Return type */
  69. ExampleVirt, /* Parent class */
  70. get_string1, /* Name of function */
  71. /* (no arguments) */
  72. );
  73. }
  74. const std::string *get_string2() override {
  75. PYBIND11_OVERLOAD(
  76. const std::string *, /* Return type */
  77. ExampleVirt, /* Parent class */
  78. get_string2, /* Name of function */
  79. /* (no arguments) */
  80. );
  81. }
  82. };
  83. class NonCopyable {
  84. public:
  85. NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
  86. NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); }
  87. NonCopyable(const NonCopyable &) = delete;
  88. NonCopyable() = delete;
  89. void operator=(const NonCopyable &) = delete;
  90. void operator=(NonCopyable &&) = delete;
  91. std::string get_value() const {
  92. if (value) return std::to_string(*value); else return "(null)";
  93. }
  94. ~NonCopyable() { print_destroyed(this); }
  95. private:
  96. std::unique_ptr<int> value;
  97. };
  98. // This is like the above, but is both copy and movable. In effect this means it should get moved
  99. // when it is not referenced elsewhere, but copied if it is still referenced.
  100. class Movable {
  101. public:
  102. Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
  103. Movable(const Movable &m) { value = m.value; print_copy_created(this); }
  104. Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); }
  105. std::string get_value() const { return std::to_string(value); }
  106. ~Movable() { print_destroyed(this); }
  107. private:
  108. int value;
  109. };
  110. class NCVirt {
  111. public:
  112. virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); }
  113. virtual Movable get_movable(int a, int b) = 0;
  114. std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
  115. std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
  116. };
  117. class NCVirtTrampoline : public NCVirt {
  118. #if !defined(__INTEL_COMPILER)
  119. NonCopyable get_noncopyable(int a, int b) override {
  120. PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b);
  121. }
  122. #endif
  123. Movable get_movable(int a, int b) override {
  124. PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b);
  125. }
  126. };
  127. int runExampleVirt(ExampleVirt *ex, int value) {
  128. return ex->run(value);
  129. }
  130. bool runExampleVirtBool(ExampleVirt* ex) {
  131. return ex->run_bool();
  132. }
  133. void runExampleVirtVirtual(ExampleVirt *ex) {
  134. ex->pure_virtual();
  135. }
  136. // Inheriting virtual methods. We do two versions here: the repeat-everything version and the
  137. // templated trampoline versions mentioned in docs/advanced.rst.
  138. //
  139. // These base classes are exactly the same, but we technically need distinct
  140. // classes for this example code because we need to be able to bind them
  141. // properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
  142. // multiple python classes).
  143. class A_Repeat {
  144. #define A_METHODS \
  145. public: \
  146. virtual int unlucky_number() = 0; \
  147. virtual std::string say_something(unsigned times) { \
  148. std::string s = ""; \
  149. for (unsigned i = 0; i < times; ++i) \
  150. s += "hi"; \
  151. return s; \
  152. } \
  153. std::string say_everything() { \
  154. return say_something(1) + " " + std::to_string(unlucky_number()); \
  155. }
  156. A_METHODS
  157. };
  158. class B_Repeat : public A_Repeat {
  159. #define B_METHODS \
  160. public: \
  161. int unlucky_number() override { return 13; } \
  162. std::string say_something(unsigned times) override { \
  163. return "B says hi " + std::to_string(times) + " times"; \
  164. } \
  165. virtual double lucky_number() { return 7.0; }
  166. B_METHODS
  167. };
  168. class C_Repeat : public B_Repeat {
  169. #define C_METHODS \
  170. public: \
  171. int unlucky_number() override { return 4444; } \
  172. double lucky_number() override { return 888; }
  173. C_METHODS
  174. };
  175. class D_Repeat : public C_Repeat {
  176. #define D_METHODS // Nothing overridden.
  177. D_METHODS
  178. };
  179. // Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
  180. class A_Tpl { A_METHODS };
  181. class B_Tpl : public A_Tpl { B_METHODS };
  182. class C_Tpl : public B_Tpl { C_METHODS };
  183. class D_Tpl : public C_Tpl { D_METHODS };
  184. // Inheritance approach 1: each trampoline gets every virtual method (11 in total)
  185. class PyA_Repeat : public A_Repeat {
  186. public:
  187. using A_Repeat::A_Repeat;
  188. int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); }
  189. std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, A_Repeat, say_something, times); }
  190. };
  191. class PyB_Repeat : public B_Repeat {
  192. public:
  193. using B_Repeat::B_Repeat;
  194. int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); }
  195. std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, B_Repeat, say_something, times); }
  196. double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); }
  197. };
  198. class PyC_Repeat : public C_Repeat {
  199. public:
  200. using C_Repeat::C_Repeat;
  201. int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); }
  202. std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, C_Repeat, say_something, times); }
  203. double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); }
  204. };
  205. class PyD_Repeat : public D_Repeat {
  206. public:
  207. using D_Repeat::D_Repeat;
  208. int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); }
  209. std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, D_Repeat, say_something, times); }
  210. double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); }
  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).
  226. template <class Base = A_Tpl>
  227. class PyA_Tpl : public Base {
  228. public:
  229. using Base::Base; // Inherit constructors
  230. int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); }
  231. std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, Base, say_something, times); }
  232. };
  233. template <class Base = B_Tpl>
  234. class PyB_Tpl : public PyA_Tpl<Base> {
  235. public:
  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, ); }
  238. double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
  239. };
  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. /*
  243. template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
  244. public:
  245. using PyB_Tpl<Base>::PyB_Tpl;
  246. };
  247. template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
  248. public:
  249. using PyC_Tpl<Base>::PyC_Tpl;
  250. };
  251. */
  252. void initialize_inherited_virtuals(py::module &m) {
  253. // Method 1: repeat
  254. py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
  255. .def(py::init<>())
  256. .def("unlucky_number", &A_Repeat::unlucky_number)
  257. .def("say_something", &A_Repeat::say_something)
  258. .def("say_everything", &A_Repeat::say_everything);
  259. py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat")
  260. .def(py::init<>())
  261. .def("lucky_number", &B_Repeat::lucky_number);
  262. py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat")
  263. .def(py::init<>());
  264. py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat")
  265. .def(py::init<>());
  266. // Method 2: Templated trampolines
  267. py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
  268. .def(py::init<>())
  269. .def("unlucky_number", &A_Tpl::unlucky_number)
  270. .def("say_something", &A_Tpl::say_something)
  271. .def("say_everything", &A_Tpl::say_everything);
  272. py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl")
  273. .def(py::init<>())
  274. .def("lucky_number", &B_Tpl::lucky_number);
  275. py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl")
  276. .def(py::init<>());
  277. py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl")
  278. .def(py::init<>());
  279. };
  280. struct Base {
  281. /* for some reason MSVC2015 can't compile this if the function is pure virtual */
  282. virtual std::string dispatch() const { return {}; };
  283. };
  284. struct DispatchIssue : Base {
  285. virtual std::string dispatch() const {
  286. PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
  287. }
  288. };
  289. TEST_SUBMODULE(virtual_functions, m) {
  290. py::class_<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. py::class_<NonCopyable>(m, "NonCopyable")
  297. .def(py::init<int, int>());
  298. py::class_<Movable>(m, "Movable")
  299. .def(py::init<int, int>());
  300. #if !defined(__INTEL_COMPILER)
  301. py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
  302. .def(py::init<>())
  303. .def("get_noncopyable", &NCVirt::get_noncopyable)
  304. .def("get_movable", &NCVirt::get_movable)
  305. .def("print_nc", &NCVirt::print_nc)
  306. .def("print_movable", &NCVirt::print_movable);
  307. #endif
  308. m.def("runExampleVirt", &runExampleVirt);
  309. m.def("runExampleVirtBool", &runExampleVirtBool);
  310. m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
  311. m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
  312. initialize_inherited_virtuals(m);
  313. // test_alias_delay_initialization1
  314. // don't invoke Python dispatch classes by default when instantiating C++ classes
  315. // that were not extended on the Python side
  316. struct A {
  317. virtual ~A() {}
  318. virtual void f() { py::print("A.f()"); }
  319. };
  320. struct PyA : A {
  321. PyA() { py::print("PyA.PyA()"); }
  322. ~PyA() { py::print("PyA.~PyA()"); }
  323. void f() override {
  324. py::print("PyA.f()");
  325. PYBIND11_OVERLOAD(void, A, f);
  326. }
  327. };
  328. py::class_<A, PyA>(m, "A")
  329. .def(py::init<>())
  330. .def("f", &A::f);
  331. m.def("call_f", [](A *a) { a->f(); });
  332. // test_alias_delay_initialization2
  333. // ... unless we explicitly request it, as in this example:
  334. struct A2 {
  335. virtual ~A2() {}
  336. virtual void f() { py::print("A2.f()"); }
  337. };
  338. struct PyA2 : A2 {
  339. PyA2() { py::print("PyA2.PyA2()"); }
  340. ~PyA2() { py::print("PyA2.~PyA2()"); }
  341. void f() override {
  342. py::print("PyA2.f()");
  343. PYBIND11_OVERLOAD(void, A2, f);
  344. }
  345. };
  346. py::class_<A2, PyA2>(m, "A2")
  347. .def(py::init_alias<>())
  348. .def("f", &A2::f);
  349. m.def("call_f", [](A2 *a2) { a2->f(); });
  350. // #159: virtual function dispatch has problems with similar-named functions
  351. py::class_<Base, DispatchIssue>(m, "DispatchIssue")
  352. .def(py::init<>())
  353. .def("dispatch", &Base::dispatch);
  354. m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
  355. // #392/397: overridding reference-returning functions
  356. class OverrideTest {
  357. public:
  358. struct A { std::string value = "hi"; };
  359. std::string v;
  360. A a;
  361. explicit OverrideTest(const std::string &v) : v{v} {}
  362. virtual std::string str_value() { return v; }
  363. virtual std::string &str_ref() { return v; }
  364. virtual A A_value() { return a; }
  365. virtual A &A_ref() { return a; }
  366. };
  367. class PyOverrideTest : public OverrideTest {
  368. public:
  369. using OverrideTest::OverrideTest;
  370. std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
  371. // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
  372. // to a python numeric value, since we only copy values in the numeric type caster:
  373. // std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
  374. // But we can work around it like this:
  375. private:
  376. std::string _tmp;
  377. std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
  378. public:
  379. std::string &str_ref() override { return _tmp = str_ref_helper(); }
  380. A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
  381. A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
  382. };
  383. py::class_<OverrideTest::A>(m, "OverrideTest_A")
  384. .def_readwrite("value", &OverrideTest::A::value);
  385. py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
  386. .def(py::init<const std::string &>())
  387. .def("str_value", &OverrideTest::str_value)
  388. // .def("str_ref", &OverrideTest::str_ref)
  389. .def("A_value", &OverrideTest::A_value)
  390. .def("A_ref", &OverrideTest::A_ref);
  391. }