From e964232e06fa88e3e4ffa4d5e2810ca935b16b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Wed, 10 Jan 2024 19:10:56 +0100 Subject: [PATCH] Without Cleanup: todos from previous commit --- include/openPMD/IO/AbstractIOHandler.hpp | 12 ++++-- include/openPMD/IO/DummyIOHandler.hpp | 1 + include/openPMD/Series.hpp | 27 +++++++++---- src/IO/DummyIOHandler.cpp | 5 +++ src/Series.cpp | 51 ++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 13 deletions(-) diff --git a/include/openPMD/IO/AbstractIOHandler.hpp b/include/openPMD/IO/AbstractIOHandler.hpp index 71a0587b0f..dc7ad22586 100644 --- a/include/openPMD/IO/AbstractIOHandler.hpp +++ b/include/openPMD/IO/AbstractIOHandler.hpp @@ -218,6 +218,12 @@ class AbstractIOHandler {} virtual ~AbstractIOHandler() = default; + AbstractIOHandler(AbstractIOHandler const &) = default; + AbstractIOHandler(AbstractIOHandler &&) = default; + + AbstractIOHandler &operator=(AbstractIOHandler const &) = default; + AbstractIOHandler &operator=(AbstractIOHandler &&) = default; + /** Add provided task to queue according to FIFO. * * @param iotask Task to be executed after all previously enqueued @@ -245,7 +251,7 @@ class AbstractIOHandler /** The currently used backend */ virtual std::string backendName() const = 0; - std::string const directory; + std::string directory; /* * Originally, the reason for distinguishing these two was that during * parsing in reading access modes, the access type would be temporarily @@ -261,8 +267,8 @@ class AbstractIOHandler * which is entirely implemented by the frontend, which internally uses * the backend in CREATE mode. */ - Access const m_backendAccess; - Access const m_frontendAccess; + Access m_backendAccess; + Access m_frontendAccess; internal::SeriesStatus m_seriesStatus = internal::SeriesStatus::Default; std::queue m_work; /** diff --git a/include/openPMD/IO/DummyIOHandler.hpp b/include/openPMD/IO/DummyIOHandler.hpp index 7cd4123699..cecdf4706b 100644 --- a/include/openPMD/IO/DummyIOHandler.hpp +++ b/include/openPMD/IO/DummyIOHandler.hpp @@ -45,5 +45,6 @@ class DummyIOHandler : public AbstractIOHandler * without IO. */ std::future flush(internal::ParsedFlushParams &) override; + std::string backendName() const override; }; // DummyIOHandler } // namespace openPMD diff --git a/include/openPMD/Series.hpp b/include/openPMD/Series.hpp index 21f4f9039b..64d6539fba 100644 --- a/include/openPMD/Series.hpp +++ b/include/openPMD/Series.hpp @@ -602,10 +602,7 @@ OPENPMD_private { if (m_series->m_deferred_initialization.has_value()) { - decltype(m_series->m_deferred_initialization) steal_the_goods = - std::nullopt; - m_series->m_deferred_initialization.swap(steal_the_goods); - steal_the_goods->operator()(); + return *m_series; } return *m_series; } @@ -791,19 +788,35 @@ OPENPMD_private */ std::optional> currentSnapshot() const; + static std::string debug(AbstractIOHandler const *ptr) + { + std::stringstream res; + res << ptr; + if (ptr) + res << '(' << ptr->directory << ')'; + return res.str(); + } + AbstractIOHandler *IOHandler() { auto res = Attributable::IOHandler(); - if (!res) + if ((!res || res->backendName() == "Dummy") && + m_series->m_deferred_initialization.has_value()) { - get(); + decltype(m_series->m_deferred_initialization) steal_the_goods = + std::nullopt; + m_series->m_deferred_initialization.swap(steal_the_goods); + steal_the_goods->operator()(); res = Attributable::IOHandler(); } + // std::cout << "Returning " << debug(res) << std::endl; return res; } AbstractIOHandler const *IOHandler() const { - return Attributable::IOHandler(); + auto res = Attributable::IOHandler(); + // std::cout << "Returning " << debug(res) << std::endl; + return res; } }; // Series } // namespace openPMD diff --git a/src/IO/DummyIOHandler.cpp b/src/IO/DummyIOHandler.cpp index 6bc6ec4d64..7882c9d5e3 100644 --- a/src/IO/DummyIOHandler.cpp +++ b/src/IO/DummyIOHandler.cpp @@ -36,4 +36,9 @@ std::future DummyIOHandler::flush(internal::ParsedFlushParams &) { return std::future(); } + +std::string DummyIOHandler::backendName() const +{ + return "Dummy"; +} } // namespace openPMD diff --git a/src/Series.cpp b/src/Series.cpp index d334bafdf1..03d0309cb7 100644 --- a/src/Series.cpp +++ b/src/Series.cpp @@ -23,6 +23,7 @@ #include "openPMD/IO/AbstractIOHandler.hpp" #include "openPMD/IO/AbstractIOHandlerHelper.hpp" #include "openPMD/IO/Access.hpp" +#include "openPMD/IO/DummyIOHandler.hpp" #include "openPMD/IO/Format.hpp" #include "openPMD/IterationEncoding.hpp" #include "openPMD/ReadIterations.hpp" @@ -37,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -289,6 +291,10 @@ IterationEncoding Series::iterationEncoding() const Series &Series::setIterationEncoding(IterationEncoding ie) { auto &series = get(); + if (series.m_deferred_initialization) + { + IOHandler(); + } if (written()) throw std::runtime_error( "A files iterationEncoding can not (yet) be changed after it has " @@ -670,10 +676,45 @@ void Series::initSeries( std::unique_ptr input) { auto &series = get(); - writable().IOHandler = - std::make_shared>>( + auto &writable = series.m_writable; + + // auto print = [&](auto const &info) { + // std::cout << "[" << info << "] " + // << debug(writable.IOHandler ? (**writable.IOHandler).get() + // : nullptr) + // << " <- " << debug(ioHandler.get()) << std::endl; + // }; + // print("begin"); + + std::string directory = ioHandler->directory; + + if (writable.IOHandler) + { + if (writable.IOHandler->has_value()) + { + ioHandler->operator=(***writable.IOHandler); + ioHandler->directory = std::move(directory); + } + } + else + { + writable.IOHandler = std::make_shared< + std::optional>>(); + } + { + std::optional> *ptr = + writable.IOHandler.get(); + *ptr = std::nullopt; + *ptr = std::make_optional>( std::move(ioHandler)); - series.iterations.linkHierarchy(writable()); + } + // print("after"); + // *writable.IOHandler = std::nullopt; + // *writable.IOHandler = + // std::make_optional>( + // std::move(ioHandler)); + + series.iterations.linkHierarchy(writable); series.iterations.writable().ownKeyWithinParent = "iterations"; series.m_name = input->name; @@ -2475,6 +2516,10 @@ Series::Series( case Access::READ_LINEAR: case Access::APPEND: { auto &series = get(); + writable().IOHandler = + std::make_shared>>( + std::make_unique("dummy", at)); + get().iterations.linkHierarchy(writable()); series.m_deferred_initialization = [this, filepath, at, options]() { return init(filepath, at, options); };