Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:55

0001 /*
0002     tests/test_multiple_inheritance.cpp -- multiple inheritance,
0003     implicit MI casts
0004 
0005     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
0006 
0007     All rights reserved. Use of this source code is governed by a
0008     BSD-style license that can be found in the LICENSE file.
0009 */
0010 
0011 #include "constructor_stats.h"
0012 #include "pybind11_tests.h"
0013 
0014 namespace {
0015 
0016 // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
0017 // space for holder constructed flags) works.
0018 template <int N>
0019 struct BaseN {
0020     explicit BaseN(int i) : i(i) {}
0021     int i;
0022 };
0023 
0024 // test_mi_static_properties
0025 struct Vanilla {
0026     std::string vanilla() { return "Vanilla"; };
0027 };
0028 struct WithStatic1 {
0029     static std::string static_func1() { return "WithStatic1"; };
0030     static int static_value1;
0031 };
0032 struct WithStatic2 {
0033     static std::string static_func2() { return "WithStatic2"; };
0034     static int static_value2;
0035 };
0036 struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
0037     static std::string static_func() { return "VanillaStaticMix1"; }
0038     static int static_value;
0039 };
0040 struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
0041     static std::string static_func() { return "VanillaStaticMix2"; }
0042     static int static_value;
0043 };
0044 int WithStatic1::static_value1 = 1;
0045 int WithStatic2::static_value2 = 2;
0046 int VanillaStaticMix1::static_value = 12;
0047 int VanillaStaticMix2::static_value = 12;
0048 
0049 // test_multiple_inheritance_virtbase
0050 struct Base1a {
0051     explicit Base1a(int i) : i(i) {}
0052     int foo() const { return i; }
0053     int i;
0054 };
0055 struct Base2a {
0056     explicit Base2a(int i) : i(i) {}
0057     int bar() const { return i; }
0058     int i;
0059 };
0060 struct Base12a : Base1a, Base2a {
0061     Base12a(int i, int j) : Base1a(i), Base2a(j) {}
0062 };
0063 
0064 // test_mi_unaligned_base
0065 // test_mi_base_return
0066 struct I801B1 {
0067     int a = 1;
0068     I801B1() = default;
0069     I801B1(const I801B1 &) = default;
0070     virtual ~I801B1() = default;
0071 };
0072 struct I801B2 {
0073     int b = 2;
0074     I801B2() = default;
0075     I801B2(const I801B2 &) = default;
0076     virtual ~I801B2() = default;
0077 };
0078 struct I801C : I801B1, I801B2 {};
0079 struct I801D : I801C {}; // Indirect MI
0080 
0081 } // namespace
0082 
0083 TEST_SUBMODULE(multiple_inheritance, m) {
0084     // Please do not interleave `struct` and `class` definitions with bindings code,
0085     // but implement `struct`s and `class`es in the anonymous namespace above.
0086     // This helps keeping the smart_holder branch in sync with master.
0087 
0088     // test_multiple_inheritance_mix1
0089     // test_multiple_inheritance_mix2
0090     struct Base1 {
0091         explicit Base1(int i) : i(i) {}
0092         int foo() const { return i; }
0093         int i;
0094     };
0095     py::class_<Base1> b1(m, "Base1");
0096     b1.def(py::init<int>()).def("foo", &Base1::foo);
0097 
0098     struct Base2 {
0099         explicit Base2(int i) : i(i) {}
0100         int bar() const { return i; }
0101         int i;
0102     };
0103     py::class_<Base2> b2(m, "Base2");
0104     b2.def(py::init<int>()).def("bar", &Base2::bar);
0105 
0106     // test_multiple_inheritance_cpp
0107     struct Base12 : Base1, Base2 {
0108         Base12(int i, int j) : Base1(i), Base2(j) {}
0109     };
0110     struct MIType : Base12 {
0111         MIType(int i, int j) : Base12(i, j) {}
0112     };
0113     py::class_<Base12, Base1, Base2>(m, "Base12");
0114     py::class_<MIType, Base12>(m, "MIType").def(py::init<int, int>());
0115 
0116     // test_multiple_inheritance_python_many_bases
0117 #define PYBIND11_BASEN(N)                                                                         \
0118     py::class_<BaseN<(N)>>(m, "BaseN" #N).def(py::init<int>()).def("f" #N, [](BaseN<N> &b) {      \
0119         return b.i + (N);                                                                         \
0120     })
0121     PYBIND11_BASEN(1);
0122     PYBIND11_BASEN(2);
0123     PYBIND11_BASEN(3);
0124     PYBIND11_BASEN(4);
0125     PYBIND11_BASEN(5);
0126     PYBIND11_BASEN(6);
0127     PYBIND11_BASEN(7);
0128     PYBIND11_BASEN(8);
0129     PYBIND11_BASEN(9);
0130     PYBIND11_BASEN(10);
0131     PYBIND11_BASEN(11);
0132     PYBIND11_BASEN(12);
0133     PYBIND11_BASEN(13);
0134     PYBIND11_BASEN(14);
0135     PYBIND11_BASEN(15);
0136     PYBIND11_BASEN(16);
0137     PYBIND11_BASEN(17);
0138 
0139     // Uncommenting this should result in a compile time failure (MI can only be specified via
0140     // template parameters because pybind has to know the types involved; see discussion in #742
0141     // for details).
0142     //    struct Base12v2 : Base1, Base2 {
0143     //        Base12v2(int i, int j) : Base1(i), Base2(j) { }
0144     //    };
0145     //    py::class_<Base12v2>(m, "Base12v2", b1, b2)
0146     //        .def(py::init<int, int>());
0147 
0148     // test_multiple_inheritance_virtbase
0149     // Test the case where not all base classes are specified, and where pybind11 requires the
0150     // py::multiple_inheritance flag to perform proper casting between types.
0151     py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
0152         .def(py::init<int>())
0153         .def("foo", &Base1a::foo);
0154 
0155     py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
0156         .def(py::init<int>())
0157         .def("bar", &Base2a::bar);
0158 
0159     py::class_<Base12a, /* Base1 missing */ Base2a, std::shared_ptr<Base12a>>(
0160         m, "Base12a", py::multiple_inheritance())
0161         .def(py::init<int, int>());
0162 
0163     m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
0164     m.def("bar_base2a_sharedptr", [](const std::shared_ptr<Base2a> &b) { return b->bar(); });
0165 
0166     // test_mi_unaligned_base
0167     // test_mi_base_return
0168     // Issue #801: invalid casting to derived type with MI bases
0169     // Unregistered classes:
0170     struct I801B3 {
0171         int c = 3;
0172         virtual ~I801B3() = default;
0173     };
0174     struct I801E : I801B3, I801D {};
0175 
0176     py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1")
0177         .def(py::init<>())
0178         .def_readonly("a", &I801B1::a);
0179     py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2")
0180         .def(py::init<>())
0181         .def_readonly("b", &I801B2::b);
0182     py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>());
0183     py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>());
0184 
0185     // Two separate issues here: first, we want to recognize a pointer to a base type as being a
0186     // known instance even when the pointer value is unequal (i.e. due to a non-first
0187     // multiple-inheritance base class):
0188     m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); });
0189     m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); });
0190     m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); });
0191     m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); });
0192 
0193     // Second, when returned a base class pointer to a derived instance, we cannot assume that the
0194     // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class
0195     // pointer could be offset.
0196     m.def("i801c_b1", []() -> I801B1 * { return new I801C(); });
0197     m.def("i801c_b2", []() -> I801B2 * { return new I801C(); });
0198     m.def("i801d_b1", []() -> I801B1 * { return new I801D(); });
0199     m.def("i801d_b2", []() -> I801B2 * { return new I801D(); });
0200 
0201     // Return a base class pointer to a pybind-registered type when the actual derived type
0202     // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base)
0203     m.def("i801e_c", []() -> I801C * { return new I801E(); });
0204     m.def("i801e_b2", []() -> I801B2 * { return new I801E(); });
0205 
0206     // test_mi_static_properties
0207     py::class_<Vanilla>(m, "Vanilla").def(py::init<>()).def("vanilla", &Vanilla::vanilla);
0208 
0209     py::class_<WithStatic1>(m, "WithStatic1")
0210         .def(py::init<>())
0211         .def_static("static_func1", &WithStatic1::static_func1)
0212         .def_readwrite_static("static_value1", &WithStatic1::static_value1);
0213 
0214     py::class_<WithStatic2>(m, "WithStatic2")
0215         .def(py::init<>())
0216         .def_static("static_func2", &WithStatic2::static_func2)
0217         .def_readwrite_static("static_value2", &WithStatic2::static_value2);
0218 
0219     py::class_<VanillaStaticMix1, Vanilla, WithStatic1, WithStatic2>(m, "VanillaStaticMix1")
0220         .def(py::init<>())
0221         .def_static("static_func", &VanillaStaticMix1::static_func)
0222         .def_readwrite_static("static_value", &VanillaStaticMix1::static_value);
0223 
0224     py::class_<VanillaStaticMix2, WithStatic1, Vanilla, WithStatic2>(m, "VanillaStaticMix2")
0225         .def(py::init<>())
0226         .def_static("static_func", &VanillaStaticMix2::static_func)
0227         .def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
0228 
0229     struct WithDict {};
0230     struct VanillaDictMix1 : Vanilla, WithDict {};
0231     struct VanillaDictMix2 : WithDict, Vanilla {};
0232     py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
0233     py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
0234     py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
0235 
0236     // test_diamond_inheritance
0237     // Issue #959: segfault when constructing diamond inheritance instance
0238     // All of these have int members so that there will be various unequal pointers involved.
0239     struct B {
0240         int b;
0241         B() = default;
0242         B(const B &) = default;
0243         virtual ~B() = default;
0244     };
0245     struct C0 : public virtual B {
0246         int c0;
0247     };
0248     struct C1 : public virtual B {
0249         int c1;
0250     };
0251     struct D : public C0, public C1 {
0252         int d;
0253     };
0254     py::class_<B>(m, "B").def("b", [](B *self) { return self; });
0255     py::class_<C0, B>(m, "C0").def("c0", [](C0 *self) { return self; });
0256     py::class_<C1, B>(m, "C1").def("c1", [](C1 *self) { return self; });
0257     py::class_<D, C0, C1>(m, "D").def(py::init<>());
0258 
0259     // test_pr3635_diamond_*
0260     // - functions are get_{base}_{var}, return {var}
0261     struct MVB {
0262         MVB() = default;
0263         MVB(const MVB &) = default;
0264         virtual ~MVB() = default;
0265 
0266         int b = 1;
0267         int get_b_b() const { return b; }
0268     };
0269     struct MVC : virtual MVB {
0270         int c = 2;
0271         int get_c_b() const { return b; }
0272         int get_c_c() const { return c; }
0273     };
0274     struct MVD0 : virtual MVC {
0275         int d0 = 3;
0276         int get_d0_b() const { return b; }
0277         int get_d0_c() const { return c; }
0278         int get_d0_d0() const { return d0; }
0279     };
0280     struct MVD1 : virtual MVC {
0281         int d1 = 4;
0282         int get_d1_b() const { return b; }
0283         int get_d1_c() const { return c; }
0284         int get_d1_d1() const { return d1; }
0285     };
0286     struct MVE : virtual MVD0, virtual MVD1 {
0287         int e = 5;
0288         int get_e_b() const { return b; }
0289         int get_e_c() const { return c; }
0290         int get_e_d0() const { return d0; }
0291         int get_e_d1() const { return d1; }
0292         int get_e_e() const { return e; }
0293     };
0294     struct MVF : virtual MVE {
0295         int f = 6;
0296         int get_f_b() const { return b; }
0297         int get_f_c() const { return c; }
0298         int get_f_d0() const { return d0; }
0299         int get_f_d1() const { return d1; }
0300         int get_f_e() const { return e; }
0301         int get_f_f() const { return f; }
0302     };
0303     py::class_<MVB>(m, "MVB")
0304         .def(py::init<>())
0305         .def("get_b_b", &MVB::get_b_b)
0306         .def_readwrite("b", &MVB::b);
0307     py::class_<MVC, MVB>(m, "MVC")
0308         .def(py::init<>())
0309         .def("get_c_b", &MVC::get_c_b)
0310         .def("get_c_c", &MVC::get_c_c)
0311         .def_readwrite("c", &MVC::c);
0312     py::class_<MVD0, MVC>(m, "MVD0")
0313         .def(py::init<>())
0314         .def("get_d0_b", &MVD0::get_d0_b)
0315         .def("get_d0_c", &MVD0::get_d0_c)
0316         .def("get_d0_d0", &MVD0::get_d0_d0)
0317         .def_readwrite("d0", &MVD0::d0);
0318     py::class_<MVD1, MVC>(m, "MVD1")
0319         .def(py::init<>())
0320         .def("get_d1_b", &MVD1::get_d1_b)
0321         .def("get_d1_c", &MVD1::get_d1_c)
0322         .def("get_d1_d1", &MVD1::get_d1_d1)
0323         .def_readwrite("d1", &MVD1::d1);
0324     py::class_<MVE, MVD0, MVD1>(m, "MVE")
0325         .def(py::init<>())
0326         .def("get_e_b", &MVE::get_e_b)
0327         .def("get_e_c", &MVE::get_e_c)
0328         .def("get_e_d0", &MVE::get_e_d0)
0329         .def("get_e_d1", &MVE::get_e_d1)
0330         .def("get_e_e", &MVE::get_e_e)
0331         .def_readwrite("e", &MVE::e);
0332     py::class_<MVF, MVE>(m, "MVF")
0333         .def(py::init<>())
0334         .def("get_f_b", &MVF::get_f_b)
0335         .def("get_f_c", &MVF::get_f_c)
0336         .def("get_f_d0", &MVF::get_f_d0)
0337         .def("get_f_d1", &MVF::get_f_d1)
0338         .def("get_f_e", &MVF::get_f_e)
0339         .def("get_f_f", &MVF::get_f_f)
0340         .def_readwrite("f", &MVF::f);
0341 }