From 5022591c368a4b3746983fde71604b03a8d3f41d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 11 May 2021 13:17:43 +0200 Subject: [PATCH] ENH: return old file handler on change - intended for the following type of use: auto oldHandler = fileHandler(fileOperation::NewUncollated()); ... do something that only works with uncollated // Restore previous (if any) if (oldHandler) { fileHandler(std::move(oldHandler)); } ENH: make fileOperation distributed(bool) mutable - use is "static-like" and akin to Pstream::parRun(bool), thus allow toggling of the switch without a const_cast --- .../redistributePar/redistributePar.C | 2 +- .../utilities/postProcessing/noise/noise.C | 4 +-- .../fileOperation/fileOperation.C | 28 +++++++++++++++++-- .../fileOperation/fileOperation.H | 25 +++++++++-------- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C index af9a933237..7180ede66b 100644 --- a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C +++ b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C @@ -2303,7 +2303,7 @@ int main(int argc, char *argv[]) // than it writes to // - reconstruct - reads parallel, write on master only and to parent // directory - const_cast(fileHandler()).distributed(true); + fileHandler().distributed(true); #include "foamDlOpenLibs.H" diff --git a/applications/utilities/postProcessing/noise/noise.C b/applications/utilities/postProcessing/noise/noise.C index 505e67b77e..19f2eb74fc 100644 --- a/applications/utilities/postProcessing/noise/noise.C +++ b/applications/utilities/postProcessing/noise/noise.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) #include "setRootCase.H" // As much as possible avoid synchronised operation - const_cast(fileHandler()).distributed(true); + fileHandler().distributed(true); #include "createTime.H" diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C index 60c396b559..3919c92f96 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C @@ -729,6 +729,14 @@ Foam::fileOperation::New // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::fileOperation::distributed(bool on) const noexcept +{ + bool old(distributed_); + distributed_ = on; + return old; +} + + Foam::fileName Foam::fileOperation::objectPath ( const IOobject& io, @@ -1461,6 +1469,17 @@ Foam::label Foam::fileOperation::detectProcessorPath(const fileName& fName) } +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +Foam::autoPtr Foam::fileOperation::NewUncollated() +{ + return autoPtr + ( + new fileOperations::uncollatedFileOperation(false) + ); +} + + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // const Foam::fileOperation& Foam::fileHandler() @@ -1481,7 +1500,8 @@ const Foam::fileOperation& Foam::fileHandler() } -void Foam::fileHandler(autoPtr&& newHandler) +Foam::autoPtr +Foam::fileHandler(autoPtr&& newHandler) { if ( @@ -1490,10 +1510,14 @@ void Foam::fileHandler(autoPtr&& newHandler) && newHandler->type() == fileOperation::fileHandlerPtr_->type() ) { - return; + return nullptr; // No change } + autoPtr old(std::move(fileOperation::fileHandlerPtr_)); + fileOperation::fileHandlerPtr_ = std::move(newHandler); + + return old; } diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H index 0b659cdc71..2cc6ad7f00 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H @@ -112,7 +112,7 @@ protected: const label comm_; //- Distributed roots (parallel run) - bool distributed_; + mutable bool distributed_; //- Detected processors directories mutable HashTable procsDirs_; @@ -181,6 +181,9 @@ public: //- Static fileOperation static autoPtr fileHandlerPtr_; + //- Static construct the commonly used uncollatedFileOperation + static autoPtr NewUncollated(); + // Constructors @@ -239,13 +242,9 @@ public: return distributed_; } - //- Set distributed roots on/off, return old value - bool distributed(bool on) noexcept - { - bool old(distributed_); - distributed_ = on; - return old; - } + //- Set distributed roots on/off (mutable) + // \return old value + bool distributed(bool on) const noexcept; // OSSpecific equivalents @@ -595,14 +594,16 @@ public: }; -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// Global declarations: defined in fileOperation.C +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +// Note: defined in fileOperation.C //- Get current file handler const fileOperation& fileHandler(); -//- Replace, reset file handler -void fileHandler(autoPtr&& newHandler); +//- Replace, reset file handler. +// \return old handler on change, null otherwise +autoPtr fileHandler(autoPtr&& newHandler); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //