Skip to content

Commit 44d22b2

Browse files
committed
Use polymorphism for meshes/particlesPath in Python
1 parent 65da901 commit 44d22b2

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
@@ -23,13 +23,15 @@
2323
#include "openPMD/Iteration.hpp"
2424
#include "openPMD/IterationEncoding.hpp"
2525
#include "openPMD/auxiliary/JSON.hpp"
26+
#include "openPMD/auxiliary/Variant.hpp"
2627
#include "openPMD/binding/python/Pickle.hpp"
2728
#include "openPMD/config.hpp"
2829
#include "openPMD/snapshots/Snapshots.hpp"
2930
#include "openPMD/snapshots/StatefulIterator.hpp"
3031

3132
#include "openPMD/binding/python/Common.hpp"
3233
#include <optional>
34+
#include <variant>
3335

3436
#if openPMD_HAVE_MPI
3537
// re-implemented signatures:
@@ -297,26 +299,60 @@ this method.
297299
&Series::openPMDextension,
298300
&Series::setOpenPMDextension)
299301
.def_property("base_path", &Series::basePath, &Series::setBasePath)
300-
.def_property(
301-
"meshes_path",
302-
&Series::meshesPath,
303-
py::overload_cast<std::string const &>(&Series::setMeshesPath))
304302
.def("get_rank_table", &Series::rankTable, py::arg("collective"))
305303
.def("set_rank_table", &Series::setRankTable, py::arg("my_rank_info"))
306304
.def_property(
307-
"particles_path",
308-
&Series::particlesPath,
309-
py::overload_cast<std::string const &>(&Series::setParticlesPath))
310-
.def_property(
311-
"meshes_paths",
312-
&Series::meshesPath,
313-
py::overload_cast<std::vector<std::string> const &>(
314-
&Series::setMeshesPath))
305+
"meshes_path",
306+
[](Series &self)
307+
-> std::variant<std::string, std::vector<std::string>> {
308+
using res_t =
309+
std::variant<std::string, std::vector<std::string>>;
310+
auto res = self.meshesPaths();
311+
if (res.size() == 1)
312+
{
313+
return res_t{std::move(res[0])};
314+
}
315+
else
316+
{
317+
return res_t{std::move(res)};
318+
}
319+
},
320+
[](Series &self,
321+
std::variant<std::string, std::vector<std::string>> const &arg)
322+
-> Series & {
323+
std::visit(
324+
[&](auto const &arg_resolved) {
325+
self.setMeshesPath(arg_resolved);
326+
},
327+
arg);
328+
return self;
329+
})
315330
.def_property(
316-
"particles_paths",
317-
&Series::particlesPath,
318-
py::overload_cast<std::vector<std::string> const &>(
319-
&Series::setParticlesPath))
331+
"particles_path",
332+
[](Series &self)
333+
-> std::variant<std::string, std::vector<std::string>> {
334+
using res_t =
335+
std::variant<std::string, std::vector<std::string>>;
336+
auto res = self.particlesPaths();
337+
if (res.size() == 1)
338+
{
339+
return res_t{std::move(res[0])};
340+
}
341+
else
342+
{
343+
return res_t{std::move(res)};
344+
}
345+
},
346+
[](Series &self,
347+
std::variant<std::string, std::vector<std::string>> const &arg)
348+
-> Series & {
349+
std::visit(
350+
[&](auto const &arg_resolved) {
351+
self.setParticlesPath(arg_resolved);
352+
},
353+
arg);
354+
return self;
355+
})
320356
.def_property("author", &Series::author, &Series::setAuthor)
321357
.def_property(
322358
"machine",

0 commit comments

Comments
 (0)