test_multiple_inheritance.cpp
Go to the documentation of this file.
1 /*
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 "constructor_stats.h"
12 #include "pybind11_tests.h"
13 
14 namespace {
15 
16 // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
17 // space for holder constructed flags) works.
18 template <int N>
19 struct BaseN {
20  explicit BaseN(int i) : i(i) {}
21  int i;
22 };
23 
24 // test_mi_static_properties
25 struct Vanilla {
26  std::string vanilla() { return "Vanilla"; };
27 };
28 struct WithStatic1 {
29  static std::string static_func1() { return "WithStatic1"; };
30  static int static_value1;
31 };
32 struct WithStatic2 {
33  static std::string static_func2() { return "WithStatic2"; };
34  static int static_value2;
35 };
36 struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
37  static std::string static_func() { return "VanillaStaticMix1"; }
38  static int static_value;
39 };
40 struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
41  static std::string static_func() { return "VanillaStaticMix2"; }
42  static int static_value;
43 };
44 int WithStatic1::static_value1 = 1;
45 int WithStatic2::static_value2 = 2;
46 int VanillaStaticMix1::static_value = 12;
47 int VanillaStaticMix2::static_value = 12;
48 
49 // test_multiple_inheritance_virtbase
50 struct Base1a {
51  explicit Base1a(int i) : i(i) {}
52  int foo() const { return i; }
53  int i;
54 };
55 struct Base2a {
56  explicit Base2a(int i) : i(i) {}
57  int bar() const { return i; }
58  int i;
59 };
60 struct Base12a : Base1a, Base2a {
61  Base12a(int i, int j) : Base1a(i), Base2a(j) {}
62 };
63 
64 // test_mi_unaligned_base
65 // test_mi_base_return
66 struct I801B1 {
67  int a = 1;
68  I801B1() = default;
69  I801B1(const I801B1 &) = default;
70  virtual ~I801B1() = default;
71 };
72 struct I801B2 {
73  int b = 2;
74  I801B2() = default;
75  I801B2(const I801B2 &) = default;
76  virtual ~I801B2() = default;
77 };
78 struct I801C : I801B1, I801B2 {};
79 struct I801D : I801C {}; // Indirect MI
80 
81 } // namespace
82 
83 TEST_SUBMODULE(multiple_inheritance, m) {
84  // Please do not interleave `struct` and `class` definitions with bindings code,
85  // but implement `struct`s and `class`es in the anonymous namespace above.
86  // This helps keeping the smart_holder branch in sync with master.
87 
88  // test_multiple_inheritance_mix1
89  // test_multiple_inheritance_mix2
90  struct Base1 {
91  explicit Base1(int i) : i(i) {}
92  int foo() const { return i; }
93  int i;
94  };
95  py::class_<Base1> b1(m, "Base1");
96  b1.def(py::init<int>()).def("foo", &Base1::foo);
97 
98  struct Base2 {
99  explicit Base2(int i) : i(i) {}
100  int bar() const { return i; }
101  int i;
102  };
103  py::class_<Base2> b2(m, "Base2");
104  b2.def(py::init<int>()).def("bar", &Base2::bar);
105 
106  // test_multiple_inheritance_cpp
107  struct Base12 : Base1, Base2 {
108  Base12(int i, int j) : Base1(i), Base2(j) {}
109  };
110  struct MIType : Base12 {
111  MIType(int i, int j) : Base12(i, j) {}
112  };
113  py::class_<Base12, Base1, Base2>(m, "Base12");
114  py::class_<MIType, Base12>(m, "MIType").def(py::init<int, int>());
115 
116  // test_multiple_inheritance_python_many_bases
117 #define PYBIND11_BASEN(N) \
118  py::class_<BaseN<(N)>>(m, "BaseN" #N).def(py::init<int>()).def("f" #N, [](BaseN<N> &b) { \
119  return b.i + (N); \
120  })
121  PYBIND11_BASEN(1);
122  PYBIND11_BASEN(2);
123  PYBIND11_BASEN(3);
124  PYBIND11_BASEN(4);
125  PYBIND11_BASEN(5);
126  PYBIND11_BASEN(6);
127  PYBIND11_BASEN(7);
128  PYBIND11_BASEN(8);
129  PYBIND11_BASEN(9);
130  PYBIND11_BASEN(10);
131  PYBIND11_BASEN(11);
132  PYBIND11_BASEN(12);
133  PYBIND11_BASEN(13);
134  PYBIND11_BASEN(14);
135  PYBIND11_BASEN(15);
136  PYBIND11_BASEN(16);
137  PYBIND11_BASEN(17);
138 
139  // Uncommenting this should result in a compile time failure (MI can only be specified via
140  // template parameters because pybind has to know the types involved; see discussion in #742
141  // for details).
142  // struct Base12v2 : Base1, Base2 {
143  // Base12v2(int i, int j) : Base1(i), Base2(j) { }
144  // };
145  // py::class_<Base12v2>(m, "Base12v2", b1, b2)
146  // .def(py::init<int, int>());
147 
148  // test_multiple_inheritance_virtbase
149  // Test the case where not all base classes are specified, and where pybind11 requires the
150  // py::multiple_inheritance flag to perform proper casting between types.
151  py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
152  .def(py::init<int>())
153  .def("foo", &Base1a::foo);
154 
155  py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
156  .def(py::init<int>())
157  .def("bar", &Base2a::bar);
158 
159  py::class_<Base12a, /* Base1 missing */ Base2a, std::shared_ptr<Base12a>>(
160  m, "Base12a", py::multiple_inheritance())
161  .def(py::init<int, int>());
162 
163  m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
164  m.def("bar_base2a_sharedptr", [](const std::shared_ptr<Base2a> &b) { return b->bar(); });
165 
166  // test_mi_unaligned_base
167  // test_mi_base_return
168  // Issue #801: invalid casting to derived type with MI bases
169  // Unregistered classes:
170  struct I801B3 {
171  int c = 3;
172  virtual ~I801B3() = default;
173  };
174  struct I801E : I801B3, I801D {};
175 
176  py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1")
177  .def(py::init<>())
178  .def_readonly("a", &I801B1::a);
179  py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2")
180  .def(py::init<>())
181  .def_readonly("b", &I801B2::b);
182  py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>());
183  py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>());
184 
185  // Two separate issues here: first, we want to recognize a pointer to a base type as being a
186  // known instance even when the pointer value is unequal (i.e. due to a non-first
187  // multiple-inheritance base class):
188  m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); });
189  m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); });
190  m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); });
191  m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); });
192 
193  // Second, when returned a base class pointer to a derived instance, we cannot assume that the
194  // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class
195  // pointer could be offset.
196  m.def("i801c_b1", []() -> I801B1 * { return new I801C(); });
197  m.def("i801c_b2", []() -> I801B2 * { return new I801C(); });
198  m.def("i801d_b1", []() -> I801B1 * { return new I801D(); });
199  m.def("i801d_b2", []() -> I801B2 * { return new I801D(); });
200 
201  // Return a base class pointer to a pybind-registered type when the actual derived type
202  // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base)
203  m.def("i801e_c", []() -> I801C * { return new I801E(); });
204  m.def("i801e_b2", []() -> I801B2 * { return new I801E(); });
205 
206  // test_mi_static_properties
207  py::class_<Vanilla>(m, "Vanilla").def(py::init<>()).def("vanilla", &Vanilla::vanilla);
208 
209  py::class_<WithStatic1>(m, "WithStatic1")
210  .def(py::init<>())
211  .def_static("static_func1", &WithStatic1::static_func1)
212  .def_readwrite_static("static_value1", &WithStatic1::static_value1);
213 
214  py::class_<WithStatic2>(m, "WithStatic2")
215  .def(py::init<>())
216  .def_static("static_func2", &WithStatic2::static_func2)
217  .def_readwrite_static("static_value2", &WithStatic2::static_value2);
218 
219  py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(m, "VanillaStaticMix1")
220  .def(py::init<>())
221  .def_static("static_func", &VanillaStaticMix1::static_func)
222  .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
223 
224  py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(m, "VanillaStaticMix2")
225  .def(py::init<>())
226  .def_static("static_func", &VanillaStaticMix2::static_func)
227  .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
228 
229  struct WithDict {};
230  struct VanillaDictMix1 : Vanilla, WithDict {};
231  struct VanillaDictMix2 : WithDict, Vanilla {};
232  py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
233  py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
234  py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
235 
236  // test_diamond_inheritance
237  // Issue #959: segfault when constructing diamond inheritance instance
238  // All of these have int members so that there will be various unequal pointers involved.
239  struct B {
240  int b;
241  B() = default;
242  B(const B &) = default;
243  virtual ~B() = default;
244  };
245  struct C0 : public virtual B {
246  int c0;
247  };
248  struct C1 : public virtual B {
249  int c1;
250  };
251  struct D : public C0, public C1 {
252  int d;
253  };
254  py::class_<B>(m, "B").def("b", [](B *self) { return self; });
255  py::class_<C0, B>(m, "C0").def("c0", [](C0 *self) { return self; });
256  py::class_<C1, B>(m, "C1").def("c1", [](C1 *self) { return self; });
257  py::class_<D, C0, C1>(m, "D").def(py::init<>());
258 
259  // test_pr3635_diamond_*
260  // - functions are get_{base}_{var}, return {var}
261  struct MVB {
262  MVB() = default;
263  MVB(const MVB &) = default;
264  virtual ~MVB() = default;
265 
266  int b = 1;
267  int get_b_b() const { return b; }
268  };
269  struct MVC : virtual MVB {
270  int c = 2;
271  int get_c_b() const { return b; }
272  int get_c_c() const { return c; }
273  };
274  struct MVD0 : virtual MVC {
275  int d0 = 3;
276  int get_d0_b() const { return b; }
277  int get_d0_c() const { return c; }
278  int get_d0_d0() const { return d0; }
279  };
280  struct MVD1 : virtual MVC {
281  int d1 = 4;
282  int get_d1_b() const { return b; }
283  int get_d1_c() const { return c; }
284  int get_d1_d1() const { return d1; }
285  };
286  struct MVE : virtual MVD0, virtual MVD1 {
287  int e = 5;
288  int get_e_b() const { return b; }
289  int get_e_c() const { return c; }
290  int get_e_d0() const { return d0; }
291  int get_e_d1() const { return d1; }
292  int get_e_e() const { return e; }
293  };
294  struct MVF : virtual MVE {
295  int f = 6;
296  int get_f_b() const { return b; }
297  int get_f_c() const { return c; }
298  int get_f_d0() const { return d0; }
299  int get_f_d1() const { return d1; }
300  int get_f_e() const { return e; }
301  int get_f_f() const { return f; }
302  };
303  py::class_<MVB>(m, "MVB")
304  .def(py::init<>())
305  .def("get_b_b", &MVB::get_b_b)
306  .def_readwrite("b", &MVB::b);
307  py::class_<MVC, MVB>(m, "MVC")
308  .def(py::init<>())
309  .def("get_c_b", &MVC::get_c_b)
310  .def("get_c_c", &MVC::get_c_c)
311  .def_readwrite("c", &MVC::c);
312  py::class_<MVD0, MVC>(m, "MVD0")
313  .def(py::init<>())
314  .def("get_d0_b", &MVD0::get_d0_b)
315  .def("get_d0_c", &MVD0::get_d0_c)
316  .def("get_d0_d0", &MVD0::get_d0_d0)
317  .def_readwrite("d0", &MVD0::d0);
318  py::class_<MVD1, MVC>(m, "MVD1")
319  .def(py::init<>())
320  .def("get_d1_b", &MVD1::get_d1_b)
321  .def("get_d1_c", &MVD1::get_d1_c)
322  .def("get_d1_d1", &MVD1::get_d1_d1)
323  .def_readwrite("d1", &MVD1::d1);
324  py::class_<MVE, MVD0, MVD1>(m, "MVE")
325  .def(py::init<>())
326  .def("get_e_b", &MVE::get_e_b)
327  .def("get_e_c", &MVE::get_e_c)
328  .def("get_e_d0", &MVE::get_e_d0)
329  .def("get_e_d1", &MVE::get_e_d1)
330  .def("get_e_e", &MVE::get_e_e)
331  .def_readwrite("e", &MVE::e);
332  py::class_<MVF, MVE>(m, "MVF")
333  .def(py::init<>())
334  .def("get_f_b", &MVF::get_f_b)
335  .def("get_f_c", &MVF::get_f_c)
336  .def("get_f_d0", &MVF::get_f_d0)
337  .def("get_f_d1", &MVF::get_f_d1)
338  .def("get_f_e", &MVF::get_f_e)
339  .def("get_f_f", &MVF::get_f_f)
340  .def_readwrite("f", &MVF::f);
341 }
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
simple_graph::b1
Vector2 b1(2, -1)
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
TEST_SUBMODULE
TEST_SUBMODULE(multiple_inheritance, m)
Definition: test_multiple_inheritance.cpp:83
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
d
static const double d[K][N]
Definition: igam.h:11
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
simple_graph::b2
Vector2 b2(4, -5)
E1::B
@ B
constructor_stats.h
c1
static double c1
Definition: airy.c:54
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
C1
Definition: test_operator_overloading.cpp:97
foo
void foo(CV_QUALIFIER Matrix3d &m)
Definition: block_nonconst_ctor_on_const_xpr_0.cpp:11
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
PYBIND11_BASEN
#define PYBIND11_BASEN(N)
pybind11_tests.h
vanilla
Definition: smartFactorScenarios.h:56
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28