Skip to content

Commit ffb510c

Browse files
committed
Use polymorphism for meshes/particlesPath in Python
1 parent f28de4f commit ffb510c

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/binding/python/Series.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
#include "openPMD/IO/Access.hpp"
2323
#include "openPMD/IterationEncoding.hpp"
2424
#include "openPMD/auxiliary/JSON.hpp"
25+
#include "openPMD/auxiliary/Variant.hpp"
2526
#include "openPMD/binding/python/Pickle.hpp"
2627
#include "openPMD/config.hpp"
2728

2829
#include "openPMD/binding/python/Common.hpp"
30+
#include <variant>
2931

3032
#if openPMD_HAVE_MPI
3133
// re-implemented signatures:
@@ -281,26 +283,60 @@ this method.
281283
&Series::openPMDextension,
282284
&Series::setOpenPMDextension)
283285
.def_property("base_path", &Series::basePath, &Series::setBasePath)
284-
.def_property(
285-
"meshes_path",
286-
&Series::meshesPath,
287-
py::overload_cast<std::string const &>(&Series::setMeshesPath))
288286
.def("get_rank_table", &Series::rankTable, py::arg("collective"))
289287
.def("set_rank_table", &Series::setRankTable, py::arg("my_rank_info"))
290288
.def_property(
291-
"particles_path",
292-
&Series::particlesPath,
293-
py::overload_cast<std::string const &>(&Series::setParticlesPath))
294-
.def_property(
295-
"meshes_paths",
296-
&Series::meshesPath,
297-
py::overload_cast<std::vector<std::string> const &>(
298-
&Series::setMeshesPath))
289+
"meshes_path",
290+
[](Series &self)
291+
-> std::variant<std::string, std::vector<std::string>> {
292+
using res_t =
293+
std::variant<std::string, std::vector<std::string>>;
294+
auto res = self.meshesPaths();
295+
if (res.size() == 1)
296+
{
297+
return res_t{std::move(res[0])};
298+
}
299+
else
300+
{
301+
return res_t{std::move(res)};
302+
}
303+
},
304+
[](Series &self,
305+
std::variant<std::string, std::vector<std::string>> const &arg)
306+
-> Series & {
307+
std::visit(
308+
[&](auto const &arg_resolved) {
309+
self.setMeshesPath(arg_resolved);
310+
},
311+
arg);
312+
return self;
313+
})
299314
.def_property(
300-
"particles_paths",
301-
&Series::particlesPath,
302-
py::overload_cast<std::vector<std::string> const &>(
303-
&Series::setParticlesPath))
315+
"particles_path",
316+
[](Series &self)
317+
-> std::variant<std::string, std::vector<std::string>> {
318+
using res_t =
319+
std::variant<std::string, std::vector<std::string>>;
320+
auto res = self.particlesPaths();
321+
if (res.size() == 1)
322+
{
323+
return res_t{std::move(res[0])};
324+
}
325+
else
326+
{
327+
return res_t{std::move(res)};
328+
}
329+
},
330+
[](Series &self,
331+
std::variant<std::string, std::vector<std::string>> const &arg)
332+
-> Series & {
333+
std::visit(
334+
[&](auto const &arg_resolved) {
335+
self.setParticlesPath(arg_resolved);
336+
},
337+
arg);
338+
return self;
339+
})
304340
.def_property("author", &Series::author, &Series::setAuthor)
305341
.def_property(
306342
"machine",

0 commit comments

Comments
 (0)