Skip to content

Commit 87f564e

Browse files
committed
Use polymorphism for meshes/particlesPath in Python
1 parent fb57025 commit 87f564e

1 file changed

Lines changed: 52 additions & 16 deletions

File tree

src/binding/python/Series.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "openPMD/IterationEncoding.hpp"
2626
#include "openPMD/auxiliary/JSON.hpp"
2727
#include "openPMD/binding/python/Common.hpp"
28+
#include "openPMD/auxiliary/Variant.hpp"
2829
#include "openPMD/binding/python/Pickle.hpp"
2930
#include "openPMD/binding/python/auxiliary.hpp"
3031
#include "openPMD/config.hpp"
@@ -33,6 +34,7 @@
3334

3435
#include <optional>
3536
#include <tuple>
37+
#include <variant>
3638

3739
#if openPMD_USE_FILESYSTEM_HEADER
3840
#include <filesystem>
@@ -419,26 +421,60 @@ this method.
419421
&Series::openPMDextension,
420422
&Series::setOpenPMDextension)
421423
.def_property("base_path", &Series::basePath, &Series::setBasePath)
422-
.def_property(
423-
"meshes_path",
424-
&Series::meshesPath,
425-
py::overload_cast<std::string const &>(&Series::setMeshesPath))
426424
.def("get_rank_table", &Series::rankTable, py::arg("collective"))
427425
.def("set_rank_table", &Series::setRankTable, py::arg("my_rank_info"))
428426
.def_property(
429-
"particles_path",
430-
&Series::particlesPath,
431-
py::overload_cast<std::string const &>(&Series::setParticlesPath))
432-
.def_property(
433-
"meshes_paths",
434-
&Series::meshesPath,
435-
py::overload_cast<std::vector<std::string> const &>(
436-
&Series::setMeshesPath))
427+
"meshes_path",
428+
[](Series &self)
429+
-> std::variant<std::string, std::vector<std::string>> {
430+
using res_t =
431+
std::variant<std::string, std::vector<std::string>>;
432+
auto res = self.meshesPaths();
433+
if (res.size() == 1)
434+
{
435+
return res_t{std::move(res[0])};
436+
}
437+
else
438+
{
439+
return res_t{std::move(res)};
440+
}
441+
},
442+
[](Series &self,
443+
std::variant<std::string, std::vector<std::string>> const &arg)
444+
-> Series & {
445+
std::visit(
446+
[&](auto const &arg_resolved) {
447+
self.setMeshesPath(arg_resolved);
448+
},
449+
arg);
450+
return self;
451+
})
437452
.def_property(
438-
"particles_paths",
439-
&Series::particlesPath,
440-
py::overload_cast<std::vector<std::string> const &>(
441-
&Series::setParticlesPath))
453+
"particles_path",
454+
[](Series &self)
455+
-> std::variant<std::string, std::vector<std::string>> {
456+
using res_t =
457+
std::variant<std::string, std::vector<std::string>>;
458+
auto res = self.particlesPaths();
459+
if (res.size() == 1)
460+
{
461+
return res_t{std::move(res[0])};
462+
}
463+
else
464+
{
465+
return res_t{std::move(res)};
466+
}
467+
},
468+
[](Series &self,
469+
std::variant<std::string, std::vector<std::string>> const &arg)
470+
-> Series & {
471+
std::visit(
472+
[&](auto const &arg_resolved) {
473+
self.setParticlesPath(arg_resolved);
474+
},
475+
arg);
476+
return self;
477+
})
442478
.def_property("author", &Series::author, &Series::setAuthor)
443479
.def_property(
444480
"machine",

0 commit comments

Comments
 (0)