blob: ef26cd849a1af3d2d84c484c546f50cf502ea394 [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) {
34 py::class_<Base1>(m, "Base1")
35 .def(py::init<int>())
36 .def("foo", &Base1::foo);
37
38 py::class_<Base2>(m, "Base2")
39 .def(py::init<int>())
40 .def("bar", &Base2::bar);
41
42 py::class_<Base12, Base1, Base2>(m, "Base12");
43
44 py::class_<MIType, Base12>(m, "MIType")
45 .def(py::init<int, int>());
46});
47
48/* Test the case where not all base classes are specified,
49 and where pybind11 requires the py::multiple_inheritance
50 flag to perform proper casting between types */
51
52struct Base1a {
53 Base1a(int i) : i(i) { }
54 int foo() { return i; }
55 int i;
56};
57
58struct Base2a {
59 Base2a(int i) : i(i) { }
60 int bar() { return i; }
61 int i;
62};
63
64struct Base12a : Base1a, Base2a {
65 Base12a(int i, int j) : Base1a(i), Base2a(j) { }
66};
67
68test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
69 py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
70 .def(py::init<int>())
71 .def("foo", &Base1a::foo);
72
73 py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
74 .def(py::init<int>())
75 .def("bar", &Base2a::bar);
76
77 py::class_<Base12a, /* Base1 missing */ Base2a,
78 std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
79 .def(py::init<int, int>());
80
81 m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
82 m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
83});
Dean Moldovan08cbe8d2017-02-15 21:10:25 +010084
85struct Vanilla {
86 std::string vanilla() { return "Vanilla"; };
87};
88
89struct WithStatic1 {
90 static std::string static_func1() { return "WithStatic1"; };
91 static int static_value1;
92};
93
94struct WithStatic2 {
95 static std::string static_func2() { return "WithStatic2"; };
96 static int static_value2;
97};
98
99struct WithDict { };
100
101struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
102 static std::string static_func() { return "VanillaStaticMix1"; }
103 static int static_value;
104};
105
106struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
107 static std::string static_func() { return "VanillaStaticMix2"; }
108 static int static_value;
109};
110
111struct VanillaDictMix1 : Vanilla, WithDict { };
112struct VanillaDictMix2 : WithDict, Vanilla { };
113
114int WithStatic1::static_value1 = 1;
115int WithStatic2::static_value2 = 2;
116int VanillaStaticMix1::static_value = 12;
117int VanillaStaticMix2::static_value = 12;
118
119test_initializer mi_static_properties([](py::module &pm) {
120 auto m = pm.def_submodule("mi");
121
122 py::class_<Vanilla>(m, "Vanilla")
123 .def(py::init<>())
124 .def("vanilla", &Vanilla::vanilla);
125
Dean Moldovandd016652017-02-16 23:02:56 +0100126 py::class_<WithStatic1>(m, "WithStatic1")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100127 .def(py::init<>())
128 .def_static("static_func1", &WithStatic1::static_func1)
129 .def_readwrite_static("static_value1", &WithStatic1::static_value1);
130
Dean Moldovandd016652017-02-16 23:02:56 +0100131 py::class_<WithStatic2>(m, "WithStatic2")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100132 .def(py::init<>())
133 .def_static("static_func2", &WithStatic2::static_func2)
134 .def_readwrite_static("static_value2", &WithStatic2::static_value2);
135
136 py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(
Dean Moldovandd016652017-02-16 23:02:56 +0100137 m, "VanillaStaticMix1")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100138 .def(py::init<>())
139 .def_static("static_func", &VanillaStaticMix1::static_func)
140 .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
141
142 py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(
Dean Moldovandd016652017-02-16 23:02:56 +0100143 m, "VanillaStaticMix2")
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100144 .def(py::init<>())
145 .def_static("static_func", &VanillaStaticMix2::static_func)
146 .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
147
148#if !defined(PYPY_VERSION)
149 py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
150 py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
151 py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
152#endif
153});