mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
SH, attribute and property tests
This commit is contained in:
parent
fa5ffc3d02
commit
f39efb850b
@ -31,6 +31,15 @@ struct Foo {
|
|||||||
using FooShPtr = Foo<0>;
|
using FooShPtr = Foo<0>;
|
||||||
using FooSmHld = Foo<1>;
|
using FooSmHld = Foo<1>;
|
||||||
|
|
||||||
|
struct Outer {
|
||||||
|
std::shared_ptr<FooShPtr> ShPtr;
|
||||||
|
std::shared_ptr<FooSmHld> SmHld;
|
||||||
|
Outer()
|
||||||
|
: ShPtr(std::make_shared<FooShPtr>("Outer")), SmHld(std::make_shared<FooSmHld>("Outer")) {}
|
||||||
|
std::shared_ptr<FooShPtr> getShPtr() const { return ShPtr; }
|
||||||
|
std::shared_ptr<FooSmHld> getSmHld() const { return SmHld; }
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace pybind11_tests
|
} // namespace pybind11_tests
|
||||||
|
|
||||||
@ -48,6 +57,32 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
|
|||||||
.def("get_history", &FooShPtr::get_history);
|
.def("get_history", &FooShPtr::get_history);
|
||||||
py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history);
|
py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history);
|
||||||
|
|
||||||
|
auto outer = py::class_<Outer>(m, "Outer").def(py::init());
|
||||||
|
#define MAKE_PROP(PropTyp) \
|
||||||
|
MAKE_PROP_FOO(ShPtr, PropTyp) \
|
||||||
|
MAKE_PROP_FOO(SmHld, PropTyp)
|
||||||
|
|
||||||
|
#define MAKE_PROP_FOO(FooTyp, PropTyp) \
|
||||||
|
.def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \
|
||||||
|
.def_##PropTyp( \
|
||||||
|
#FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \
|
||||||
|
.def_##PropTyp( \
|
||||||
|
#FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move)
|
||||||
|
outer MAKE_PROP(readonly) MAKE_PROP(readwrite);
|
||||||
|
#undef MAKE_PROP_FOO
|
||||||
|
|
||||||
|
#define MAKE_PROP_FOO(FooTyp, PropTyp) \
|
||||||
|
.def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \
|
||||||
|
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \
|
||||||
|
&Outer::get##FooTyp, \
|
||||||
|
py::return_value_policy::copy) \
|
||||||
|
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \
|
||||||
|
&Outer::get##FooTyp, \
|
||||||
|
py::return_value_policy::move)
|
||||||
|
outer MAKE_PROP(readonly);
|
||||||
|
#undef MAKE_PROP_FOO
|
||||||
|
#undef MAKE_PROP
|
||||||
|
|
||||||
m.def("test_ShPtr_copy", []() {
|
m.def("test_ShPtr_copy", []() {
|
||||||
auto o = std::make_shared<FooShPtr>("copy");
|
auto o = std::make_shared<FooShPtr>("copy");
|
||||||
auto l = py::list();
|
auto l = py::list();
|
||||||
|
@ -21,3 +21,21 @@ def test_shptr_move():
|
|||||||
def test_smhld_move():
|
def test_smhld_move():
|
||||||
txt = m.test_SmHld_move()[0].get_history()
|
txt = m.test_SmHld_move()[0].get_history()
|
||||||
assert txt == "FooSmHld_move"
|
assert txt == "FooSmHld_move"
|
||||||
|
|
||||||
|
|
||||||
|
def _check_property(foo_typ: str, prop_typ: str, policy: str):
|
||||||
|
o = m.Outer()
|
||||||
|
name = "{}_{}_{}".format(foo_typ, prop_typ, policy)
|
||||||
|
history = "Foo{}_Outer".format(foo_typ)
|
||||||
|
f = getattr(o, name)
|
||||||
|
assert f.get_history() == history
|
||||||
|
# and try again to check that o did not get changed
|
||||||
|
f = getattr(o, name)
|
||||||
|
assert f.get_history() == history
|
||||||
|
|
||||||
|
|
||||||
|
def test_properties():
|
||||||
|
for prop_typ in ("readonly", "readwrite", "property_readonly"):
|
||||||
|
for foo_typ in ("ShPtr", "SmHld"):
|
||||||
|
for policy in ("default", "copy", "move"):
|
||||||
|
_check_property(foo_typ, prop_typ, policy)
|
||||||
|
Loading…
Reference in New Issue
Block a user