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
This commit is contained in:
Mark Olesen
2021-05-11 13:17:43 +02:00
parent 2a438385a3
commit 5022591c36
4 changed files with 42 additions and 17 deletions

View File

@ -2303,7 +2303,7 @@ int main(int argc, char *argv[])
// than it writes to // than it writes to
// - reconstruct - reads parallel, write on master only and to parent // - reconstruct - reads parallel, write on master only and to parent
// directory // directory
const_cast<fileOperation&>(fileHandler()).distributed(true); fileHandler().distributed(true);
#include "foamDlOpenLibs.H" #include "foamDlOpenLibs.H"

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -116,7 +116,7 @@ int main(int argc, char *argv[])
#include "setRootCase.H" #include "setRootCase.H"
// As much as possible avoid synchronised operation // As much as possible avoid synchronised operation
const_cast<fileOperation&>(fileHandler()).distributed(true); fileHandler().distributed(true);
#include "createTime.H" #include "createTime.H"

View File

@ -729,6 +729,14 @@ Foam::fileOperation::New
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::fileOperation::distributed(bool on) const noexcept
{
bool old(distributed_);
distributed_ = on;
return old;
}
Foam::fileName Foam::fileOperation::objectPath Foam::fileName Foam::fileOperation::objectPath
( (
const IOobject& io, const IOobject& io,
@ -1461,6 +1469,17 @@ Foam::label Foam::fileOperation::detectProcessorPath(const fileName& fName)
} }
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::autoPtr<Foam::fileOperation> Foam::fileOperation::NewUncollated()
{
return autoPtr<fileOperation>
(
new fileOperations::uncollatedFileOperation(false)
);
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
const Foam::fileOperation& Foam::fileHandler() const Foam::fileOperation& Foam::fileHandler()
@ -1481,7 +1500,8 @@ const Foam::fileOperation& Foam::fileHandler()
} }
void Foam::fileHandler(autoPtr<fileOperation>&& newHandler) Foam::autoPtr<Foam::fileOperation>
Foam::fileHandler(autoPtr<fileOperation>&& newHandler)
{ {
if if
( (
@ -1490,10 +1510,14 @@ void Foam::fileHandler(autoPtr<fileOperation>&& newHandler)
&& newHandler->type() == fileOperation::fileHandlerPtr_->type() && newHandler->type() == fileOperation::fileHandlerPtr_->type()
) )
{ {
return; return nullptr; // No change
} }
autoPtr<fileOperation> old(std::move(fileOperation::fileHandlerPtr_));
fileOperation::fileHandlerPtr_ = std::move(newHandler); fileOperation::fileHandlerPtr_ = std::move(newHandler);
return old;
} }

View File

@ -112,7 +112,7 @@ protected:
const label comm_; const label comm_;
//- Distributed roots (parallel run) //- Distributed roots (parallel run)
bool distributed_; mutable bool distributed_;
//- Detected processors directories //- Detected processors directories
mutable HashTable<dirIndexList> procsDirs_; mutable HashTable<dirIndexList> procsDirs_;
@ -181,6 +181,9 @@ public:
//- Static fileOperation //- Static fileOperation
static autoPtr<fileOperation> fileHandlerPtr_; static autoPtr<fileOperation> fileHandlerPtr_;
//- Static construct the commonly used uncollatedFileOperation
static autoPtr<fileOperation> NewUncollated();
// Constructors // Constructors
@ -239,13 +242,9 @@ public:
return distributed_; return distributed_;
} }
//- Set distributed roots on/off, return old value //- Set distributed roots on/off (mutable)
bool distributed(bool on) noexcept // \return old value
{ bool distributed(bool on) const noexcept;
bool old(distributed_);
distributed_ = on;
return old;
}
// OSSpecific equivalents // OSSpecific equivalents
@ -595,14 +594,16 @@ public:
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// Global declarations: defined in fileOperation.C
// Note: defined in fileOperation.C
//- Get current file handler //- Get current file handler
const fileOperation& fileHandler(); const fileOperation& fileHandler();
//- Replace, reset file handler //- Replace, reset file handler.
void fileHandler(autoPtr<fileOperation>&& newHandler); // \return old handler on change, null otherwise
autoPtr<fileOperation> fileHandler(autoPtr<fileOperation>&& newHandler);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //