Skip to content

Commit dc23e0c

Browse files
committed
Fallback implementation for old compilers
1 parent 2b6106f commit dc23e0c

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ option(openPMD_USE_INTERNAL_TOML11 "Use internally shipped toml11" ${op
151151

152152
option(openPMD_USE_INVASIVE_TESTS "Enable unit tests that modify source code" OFF)
153153
option(openPMD_USE_VERIFY "Enable internal VERIFY (assert) macro independent of build type" ON)
154+
option(openPMD_USE_FILESYSTEM_HEADER "Enable filesystem header. May be disabled for old compiler versions." ON)
155+
mark_as_advanced(openPMD_USE_FILESYSTEM_HEADER)
154156

155157
set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")
156158
if(NOT CMAKE_BUILD_TYPE)
@@ -731,6 +733,14 @@ if(openPMD_USE_INVASIVE_TESTS)
731733
target_compile_definitions(openPMD PRIVATE openPMD_USE_INVASIVE_TESTS=1)
732734
endif()
733735

736+
foreach(target openPMD openPMD.py)
737+
if(openPMD_USE_FILESYSTEM_HEADER)
738+
target_compile_definitions(${target} PRIVATE openPMD_USE_FILESYSTEM_HEADER=1)
739+
else()
740+
target_compile_definitions(${target} PRIVATE openPMD_USE_FILESYSTEM_HEADER=0)
741+
endif()
742+
endforeach()
743+
734744
if(openPMD_BUILD_TESTING)
735745
# compile Catch2 implementation part separately
736746
add_library(CatchRunner ${_openpmd_lib_type}

include/openPMD/binding/python/Common.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#include <pybind11/numpy.h>
2626
#include <pybind11/pybind11.h>
2727
#include <pybind11/stl.h>
28-
#include <pybind11/stl/filesystem.h>
2928
#include <pybind11/stl_bind.h>
29+
30+
#if openPMD_USE_FILESYSTEM_HEADER
31+
#include <pybind11/stl/filesystem.h>
32+
#endif
3033
// not yet used:
3134
// pybind11/functional.h // for std::function
3235

src/binding/python/Series.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
#include "openPMD/snapshots/StatefulIterator.hpp"
3030

3131
#include "openPMD/binding/python/Common.hpp"
32-
#include <filesystem>
3332
#include <optional>
3433
#include <tuple>
3534

35+
#if openPMD_USE_FILESYSTEM_HEADER
36+
#include <filesystem>
37+
#endif
38+
3639
#if openPMD_HAVE_MPI
3740
// re-implemented signatures:
3841
// include <mpi4py/mpi4py.h>
@@ -85,6 +88,26 @@ struct DefineSeriesConstructorPerPathType
8588
return ::auxiliary::json_dumps(obj);
8689
}
8790

91+
static constexpr auto filepath_as_string(std::string const &str)
92+
-> std::string const &
93+
{
94+
return str;
95+
}
96+
97+
#if openPMD_USE_FILESYSTEM_HEADER
98+
static auto filepath_as_string(std::filesystem::path const &path)
99+
-> std::string
100+
{
101+
return path;
102+
}
103+
#else
104+
static auto filepath_as_string(py::object const &path) -> std::string
105+
{
106+
auto casted = path.attr("__str__")();
107+
return py::cast<std::string>(casted);
108+
}
109+
#endif
110+
88111
template <typename TupleType>
89112
static void call(py::class_<Series, Attributable> &py_class)
90113
{
@@ -96,9 +119,10 @@ struct DefineSeriesConstructorPerPathType
96119
py::init([](PathType const &filepath,
97120
Access at,
98121
JsonCfgType const &options) {
99-
auto options_ = json_cfg_as_string(options);
122+
decltype(auto) filepath_ = filepath_as_string(filepath);
123+
decltype(auto) options_ = json_cfg_as_string(options);
100124
py::gil_scoped_release release;
101-
return new Series(filepath, at, options_);
125+
return new Series(filepath_, at, options_);
102126
}),
103127
py::arg("filepath"),
104128
py::arg("access"),
@@ -155,7 +179,8 @@ It will be replaced with an automatically determined file name extension:
155179
Access at,
156180
py::object &comm,
157181
JsonCfgType const &options) {
158-
auto options_ = json_cfg_as_string(options);
182+
decltype(auto) filepath_ = filepath_as_string(filepath);
183+
decltype(auto) options_ = json_cfg_as_string(options);
159184
auto variant = pythonObjectAsMpiComm(comm);
160185
if (auto errorMsg =
161186
std::get_if<py_object_to_mpi_comm_error>(&variant))
@@ -185,7 +210,7 @@ It will be replaced with an automatically determined file name extension:
185210
{
186211
py::gil_scoped_release release;
187212
return new Series(
188-
filepath,
213+
filepath_,
189214
at,
190215
std::get<MPI_Comm>(variant),
191216
options_);
@@ -343,8 +368,14 @@ not possible once it has been closed.
343368
::internal::DefineSeriesConstructorPerPathType,
344369
std::tuple<std::string, std::string>,
345370
std::tuple<std::string, py::object>,
371+
#if openPMD_USE_FILESYSTEM_HEADER
346372
std::tuple<std::filesystem::path, std::string>,
347-
std::tuple<std::filesystem::path, py::object> >::template call(cl);
373+
std::tuple<std::filesystem::path, py::object>
374+
#else
375+
std::tuple<py::object, std::string>,
376+
std::tuple<py::object, py::object>
377+
#endif
378+
>::template call(cl);
348379

349380
cl.def("__bool__", &Series::operator bool)
350381
.def("__len__", [](Series const &s) { return s.iterations.size(); })

0 commit comments

Comments
 (0)