Skip to content

Commit 2603556

Browse files
committed
Use cpp_function for defining properties
1 parent ab29b2a commit 2603556

3 files changed

Lines changed: 26 additions & 20 deletions

File tree

src/binding/python/Iteration.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,22 @@ void init_Iteration(py::module &m)
9494
.def("set_dt", &Iteration::setDt<double>)
9595
.def("set_time_unit_SI", &Iteration::setTimeUnitSI)
9696

97-
.def_readwrite(
97+
.def_property_readonly(
9898
"meshes",
99-
&Iteration::meshes,
100-
py::return_value_policy::copy,
101-
// garbage collection: return value must be freed before Iteration
102-
py::keep_alive<1, 0>())
103-
.def_readwrite(
99+
py::cpp_function(
100+
[](Iteration &i) { return i.meshes; },
101+
py::return_value_policy::copy,
102+
// garbage collection: return value must be freed before
103+
// Iteration
104+
py::keep_alive<1, 0>()))
105+
.def_property_readonly(
104106
"particles",
105-
&Iteration::particles,
106-
py::return_value_policy::copy,
107-
// garbage collection: return value must be freed before Iteration
108-
py::keep_alive<1, 0>());
107+
py::cpp_function(
108+
[](Iteration &i) { return i.particles; },
109+
py::return_value_policy::copy,
110+
// garbage collection: return value must be freed before
111+
// Iteration
112+
py::keep_alive<1, 0>()));
109113

110114
add_pickle(
111115
cl, [](openPMD::Series series, std::vector<std::string> const &group) {

src/binding/python/ParticleSpecies.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ void init_ParticleSpecies(py::module &m)
4848
return stream.str();
4949
})
5050

51-
.def_readwrite(
51+
.def_property_readonly(
5252
"particle_patches",
53-
&ParticleSpecies::particlePatches,
54-
py::return_value_policy::copy,
55-
// garbage collection: return value must be freed before Series
56-
py::keep_alive<1, 0>());
53+
py::cpp_function(
54+
[](ParticleSpecies &ps) { return ps.particlePatches; },
55+
py::return_value_policy::copy,
56+
// garbage collection: return value must be freed before Series
57+
py::keep_alive<1, 0>()));
5758
add_pickle(
5859
cl, [](openPMD::Series series, std::vector<std::string> const &group) {
5960
uint64_t const n_it = std::stoull(group.at(1));

src/binding/python/Series.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,13 @@ this method.
497497
.def("set_iteration_format", &Series::setIterationFormat)
498498
.def("set_name", &Series::setName)
499499

500-
.def_readwrite(
500+
.def_property_readonly(
501501
"iterations",
502-
&Series::iterations,
503-
py::return_value_policy::copy,
504-
// garbage collection: return value must be freed before Series
505-
py::keep_alive<1, 0>())
502+
py::cpp_function(
503+
[](Series &s) { return s.iterations; },
504+
py::return_value_policy::copy,
505+
// garbage collection: return value must be freed before Series
506+
py::keep_alive<1, 0>()))
506507
.def(
507508
"read_iterations",
508509
[](Series &s) {

0 commit comments

Comments
 (0)