blob: 3ebeb202be54a235fb5daaafde08e29a48b568c8 [file] [log] [blame]
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001/*
2 tests/test_multiple_inheritance.cpp -- multiple inheritance,
3 implicit MI casts
4
5 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
11#include "pybind11_tests.h"
12
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090013struct Base1 {
14 Base1(int i) : i(i) { }
15 int foo() { return i; }
16 int i;
17};
18
19struct Base2 {
20 Base2(int i) : i(i) { }
21 int bar() { return i; }
22 int i;
23};
24
25struct Base12 : Base1, Base2 {
26 Base12(int i, int j) : Base1(i), Base2(j) { }
27};
28
29struct MIType : Base12 {
30 MIType(int i, int j) : Base12(i, j) { }
31};
32
33test_initializer multiple_inheritance([](py::module &m) {
Jason Rhinelanderb9616262017-03-16 20:10:48 -030034 py::class_<Base1> b1(m, "Base1");
35 b1.def(py::init<int>())
36 .def("foo", &Base1::foo);
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090037
Jason Rhinelanderb9616262017-03-16 20:10:48 -030038 py::class_<Base2> b2(m, "Base2");
39 b2.def(py::init<int>())
40 .def("bar", &Base2::bar);
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090041
42 py::class_<Base12, Base1, Base2>(m, "Base12");
43
44 py::class_<MIType, Base12>(m, "MIType")
45 .def(py::init<int, int>());
Jason Rhinelanderb9616262017-03-16 20:10:48 -030046
47 // Uncommenting this should result in a compile time failure (MI can only be specified via
48 // template parameters because pybind has to know the types involved; see discussion in #742 for
49 // details).
50// struct Base12v2 : Base1, Base2 {
51// Base12v2(int i, int j) : Base1(i), Base2(j) { }
52// };
53// py::class_<Base12v2>(m, "Base12v2", b1, b2)
54// .def(py::init<int, int>());
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090055});
56
57/* Test the case where not all base classes are specified,
58 and where pybind11 requires the py::multiple_inheritance
59 flag to perform proper casting between types */
60
61struct Base1a {
62 Base1a(int i) : i(i) { }
63 int foo() { return i; }
64 int i;
65};
66
67struct Base2a {
68 Base2a(int i) : i(i) { }
69 int bar() { return i; }
70 int i;
71};
72
73struct Base12a : Base1a, Base2a {
74 Base12a(int i, int j) : Base1a(i), Base2a(j) { }
75};
76
77test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
78 py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
79 .def(py::init<int>())
80 .def("foo", &Base1a::foo);
81
82 py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
83 .def(py::init<int>())
84 .def("bar", &Base2a::bar);
85
86 py::class_<Base12a, /* Base1 missing */ Base2a,
87 std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
88 .def(py::init<int, int>());
89
90 m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
91 m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
92});
Dean Moldovan08cbe8d2017-02-15 21:10:25 +010093
94struct Vanilla {
95 std::string vanilla() { return "Vanilla"; };
96};
97
98struct WithStatic1 {
99 static std::string static_func1() { return "WithStatic1"; };
100 static int static_value1;
101};
102
103struct WithStatic2 {
104 static std::string static_func2() { return "WithStatic2"; };
105 static int static_value2;
106};
107
108struct WithDict { };
109
110struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
111 static std::string static_func() { return "VanillaStaticMix1"; }
112 static int static_value;
113};
114
115struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
116 static std::string static_func() { return "VanillaStaticMix2"; }
117 static int static_value;
118};
119
120struct VanillaDictMix1 : Vanilla, WithDict { };
121struct VanillaDictMix2 : WithDict, Vanilla { };
122
123int WithStatic1::static_value1 = 1;
124int WithStatic2::static_value2 = 2;
125int VanillaStaticMix1::static_value = 12;
126int VanillaStaticMix2::static_value = 12;
127
128test_initializer mi_static_properties([](py::module &pm) {
129 auto m = pm.def_submodule("mi");
130
131 py::class_<Vanilla>(m, "Vanilla")
132 .def(py::init<>())
133 .def("vanilla", &Vanilla::vanilla);
134
Dean Moldovandd016652017-02-16 23:02:56 +0100135 py::class_<WithStatic1>(m, "WithStatic1")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100136 .def(py::init<>())
137 .def_static("static_func1", &WithStatic1::static_func1)
138 .def_readwrite_static("static_value1", &WithStatic1::static_value1);
139
Dean Moldovandd016652017-02-16 23:02:56 +0100140 py::class_<WithStatic2>(m, "WithStatic2")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100141 .def(py::init<>())
142 .def_static("static_func2", &WithStatic2::static_func2)
143 .def_readwrite_static("static_value2", &WithStatic2::static_value2);
144
145 py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(
Dean Moldovandd016652017-02-16 23:02:56 +0100146 m, "VanillaStaticMix1")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100147 .def(py::init<>())
148 .def_static("static_func", &VanillaStaticMix1::static_func)
149 .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
150
151 py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(
Dean Moldovandd016652017-02-16 23:02:56 +0100152 m, "VanillaStaticMix2")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100153 .def(py::init<>())
154 .def_static("static_func", &VanillaStaticMix2::static_func)
155 .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
156
157#if !defined(PYPY_VERSION)
158 py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
159 py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
160 py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
161#endif
162});