2022-10-17 18:22:49 +00:00
|
|
|
/*
|
|
|
|
tests/test_custom_base.cpp -- test custom type hierarchy support
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Base {
|
|
|
|
int i = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived {
|
|
|
|
int j = 6;
|
|
|
|
|
|
|
|
// just to prove the base can be anywhere
|
|
|
|
Base base;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_SUBMODULE(custom_base, m) {
|
|
|
|
|
|
|
|
py::class_<Base>(m, "Base").def_readwrite("i", &Base::i);
|
|
|
|
|
|
|
|
py::class_<Derived>(m, "Derived", py::custom_base<Base>([](void *o) -> void * {
|
|
|
|
return &reinterpret_cast<Derived *>(o)->base;
|
2022-10-17 18:31:11 +00:00
|
|
|
}))
|
|
|
|
.def_readwrite("j", &Derived::j);
|
2022-10-17 18:22:49 +00:00
|
|
|
|
|
|
|
m.def("create_derived", []() { return new Derived; });
|
|
|
|
m.def("create_base", []() { return new Base; });
|
|
|
|
|
|
|
|
m.def("base_i", [](Base *b) { return b->i; });
|
|
|
|
|
|
|
|
m.def("derived_j", [](Derived *d) { return d->j; });
|
2022-10-17 18:31:11 +00:00
|
|
|
};
|