pybind11/tests/test_class_sh_property_non_owning.cpp
Ralf W. Grosse-Kunstleve 37d0bf4289
[smart_holder] Enable properties for non-owning holders (#4586)
* Add test_class_sh_property_non_owning.cpp,py

Failing:

```
__________________________________________________________ test_persistent_holder __________________________________________________________

    def test_persistent_holder():
        h = m.DataFieldsHolder(2)
>       c = h.vec_at(0).core_fld
E       RuntimeError: Non-owning holder (loaded_as_shared_ptr).

h          = <pybind11_tests.class_sh_property_non_owning.DataFieldsHolder object at 0x7fabab516470>

test_class_sh_property_non_owning.py:6: RuntimeError
__________________________________________________________ test_temporary_holder ___________________________________________________________

    def test_temporary_holder():
        d = m.DataFieldsHolder(2).vec_at(1)
>       c = d.core_fld
E       RuntimeError: Non-owning holder (loaded_as_shared_ptr).

d          = <pybind11_tests.class_sh_property_non_owning.DataField object at 0x7fabab548770>

test_class_sh_property_non_owning.py:13: RuntimeError
```

* Introduce `shared_ptr_from_python(responsible_parent)` and use in all `property_cpp_function`s with `const shared_ptr<T> &` arguments.

Tests are incomplete.

* Complete tests.

* Add comment for `smart_holder_type_caster_load<T>::shared_ptr_from_python`
2023-03-23 17:21:41 -07:00

69 lines
2.3 KiB
C++

#include "pybind11/smart_holder.h"
#include "pybind11_tests.h"
#include <cstddef>
#include <vector>
namespace test_class_sh_property_non_owning {
struct CoreField {
explicit CoreField(int int_value = -99) : int_value{int_value} {}
int int_value;
};
struct DataField {
DataField(int i_value, int i_shared, int i_unique)
: core_fld_value{i_value}, core_fld_shared_ptr{new CoreField{i_shared}},
core_fld_raw_ptr{core_fld_shared_ptr.get()}, core_fld_unique_ptr{
new CoreField{i_unique}} {}
CoreField core_fld_value;
std::shared_ptr<CoreField> core_fld_shared_ptr;
CoreField *core_fld_raw_ptr;
std::unique_ptr<CoreField> core_fld_unique_ptr;
};
struct DataFieldsHolder {
private:
std::vector<DataField> vec;
public:
DataFieldsHolder(std::size_t vec_size) {
for (std::size_t i = 0; i < vec_size; i++) {
int i11 = static_cast<int>(i) * 11;
vec.push_back(DataField(13 + i11, 14 + i11, 15 + i11));
}
}
DataField *vec_at(std::size_t index) {
if (index >= vec.size()) {
return nullptr;
}
return &vec[index];
}
};
} // namespace test_class_sh_property_non_owning
using namespace test_class_sh_property_non_owning;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CoreField)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataField)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder)
TEST_SUBMODULE(class_sh_property_non_owning, m) {
py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
py::classh<DataField>(m, "DataField")
.def_readonly("core_fld_value_ro", &DataField::core_fld_value)
.def_readwrite("core_fld_value_rw", &DataField::core_fld_value)
.def_readonly("core_fld_shared_ptr_ro", &DataField::core_fld_shared_ptr)
.def_readwrite("core_fld_shared_ptr_rw", &DataField::core_fld_shared_ptr)
.def_readonly("core_fld_raw_ptr_ro", &DataField::core_fld_raw_ptr)
.def_readwrite("core_fld_raw_ptr_rw", &DataField::core_fld_raw_ptr)
.def_readwrite("core_fld_unique_ptr_rw", &DataField::core_fld_unique_ptr);
py::classh<DataFieldsHolder>(m, "DataFieldsHolder")
.def(py::init<std::size_t>())
.def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal);
}