From 67b58c28c05077facb898ea3e03cc74766889534 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sat, 19 Nov 2022 17:08:05 +0100 Subject: [PATCH] ENH: IOobjectList simpler construction of unregistered - accept IOobjectOption::registerOption with (MUST_READ, NO_WRITE) being implicit. Direct handling of IOobjectOption itself, for consistency with IOobject. The disabling of object registration is currently the only case where IOobjectList doesn't use default construction parameters, but it was previously a bit awkward to specify. --- .../decomposePar/decomposePar.C | 8 +- src/OpenFOAM/db/IOobjectList/IOobjectList.C | 94 +----------- src/OpenFOAM/db/IOobjectList/IOobjectList.H | 110 ++++++++++---- src/OpenFOAM/db/IOobjectList/IOobjectListI.H | 142 ++++++++++++++++++ src/lagrangian/basic/Cloud/CloudIO.C | 11 +- 5 files changed, 232 insertions(+), 133 deletions(-) create mode 100644 src/OpenFOAM/db/IOobjectList/IOobjectListI.H diff --git a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C index b993118321..c0a02132a2 100644 --- a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C +++ b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C @@ -887,9 +887,7 @@ int main(int argc, char *argv[]) mesh, runTime.timeName(), cloud::prefix/cloudDir, - IOobject::MUST_READ, - IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ); // Note: look up "positions" for backwards compatibility @@ -972,9 +970,7 @@ int main(int argc, char *argv[]) mesh, runTime.timeName(), cloud::prefix/cloudDirs[cloudI], - IOobject::MUST_READ, - IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ); lagrangianFieldCache.readAllFields diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index ae82fd6246..3a035b9f47 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -99,38 +99,12 @@ void Foam::IOobjectList::syncNames(wordList& objNames) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::IOobjectList::IOobjectList() -: - HashPtrTable() -{} - - -Foam::IOobjectList::IOobjectList(const label nObjects) -: - HashPtrTable(nObjects) // Could also use 2*nObjects instead -{} - - -Foam::IOobjectList::IOobjectList(const IOobjectList& list) -: - HashPtrTable(list) -{} - - -Foam::IOobjectList::IOobjectList(IOobjectList&& list) -: - HashPtrTable(std::move(list)) -{} - - Foam::IOobjectList::IOobjectList ( const objectRegistry& db, const fileName& instance, const fileName& local, - IOobjectOption::readOption rOpt, - IOobjectOption::writeOption wOpt, - bool registerObject + IOobjectOption ioOpt ) : HashPtrTable() @@ -152,9 +126,7 @@ Foam::IOobjectList::IOobjectList newInstance, local, db, - rOpt, - wOpt, - registerObject + ioOpt ); bool ok = false; @@ -182,51 +154,7 @@ Foam::IOobjectList::IOobjectList // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -bool Foam::IOobjectList::add(autoPtr& objectPtr) -{ - if (objectPtr) - { - return insert(objectPtr->name(), objectPtr); - } - - return false; -} - - -bool Foam::IOobjectList::add(autoPtr&& objectPtr) -{ - if (objectPtr) - { - return insert(objectPtr->name(), objectPtr); - } - - return false; -} - - -Foam::label Foam::IOobjectList::append(const IOobjectList& other) -{ - label count = 0; - - forAllConstIters(other, iter) - { - if (!found(iter.key())) - { - if (IOobject::debug) - { - InfoInFunction << "Copy append " << iter.key() << nl; - } - - set(iter.key(), new IOobject(*iter.val())); - ++count; - } - } - - return count; -} - - -Foam::label Foam::IOobjectList::append(IOobjectList&& other) +Foam::label Foam::IOobjectList::merge(IOobjectList&& other) { // Remove by name to avoid uncertainties about invalid iterators @@ -240,7 +168,7 @@ Foam::label Foam::IOobjectList::append(IOobjectList&& other) { if (IOobject::debug) { - InfoInFunction << "Move append " << key << nl; + InfoInFunction << "Merge " << key << nl; } if (add(other.remove(key))) @@ -254,12 +182,6 @@ Foam::label Foam::IOobjectList::append(IOobjectList&& other) } -bool Foam::IOobjectList::remove(const IOobject& io) -{ - return erase(io.name()); -} - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // const Foam::IOobject* Foam::IOobjectList::cfindObject @@ -444,14 +366,6 @@ void Foam::IOobjectList::checkNames(const bool syncPar) const } -// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // - -void Foam::IOobjectList::operator=(IOobjectList&& list) -{ - transfer(list); -} - - // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // Foam::Ostream& Foam::operator<<(Ostream& os, const IOobjectList& list) diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index b6d1fad9bd..91d02f54a4 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -29,9 +29,12 @@ Class Description List of IOobjects with searching and retrieving facilities. + Implemented as a HashTable, so the various sorted methods should + be used if traversing in parallel. SourceFiles IOobjectList.C + IOobjectListI.H IOobjectListTemplates.C \*---------------------------------------------------------------------------*/ @@ -41,8 +44,8 @@ SourceFiles #include "HashPtrTable.H" #include "HashSet.H" -#include "UPtrList.H" #include "IOobject.H" +#include "UPtrList.H" #include "wordRes.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -164,27 +167,52 @@ public: // Constructors - //- Construct null with default (128) table capacity - IOobjectList(); + //- Default construct (empty) with default (128) table capacity + inline IOobjectList(); //- Construct given initial table capacity - explicit IOobjectList(const label nObjects); + inline explicit IOobjectList(const label nObjects); //- Copy construct - IOobjectList(const IOobjectList& list); + inline IOobjectList(const IOobjectList& list); //- Move construct - IOobjectList(IOobjectList&& list); + inline IOobjectList(IOobjectList&& list); - //- Construct from objectRegistry and instance path + //- Construct from registry, instance, io options + inline IOobjectList + ( + const objectRegistry& db, + const fileName& instance, + IOobjectOption ioOpt + ); + + //- Construct from registry, instance, local, io options IOobjectList ( const objectRegistry& db, const fileName& instance, - const fileName& local = "", - IOobjectOption::readOption rOpt = IOobjectOption::MUST_READ, - IOobjectOption::writeOption wOpt = IOobjectOption::NO_WRITE, - bool registerObject = true + const fileName& local, + IOobjectOption ioOpt + ); + + //- Construct from registry, instance, registration option + //- with MUST_READ, NO_WRITE + inline IOobjectList + ( + const objectRegistry& db, + const fileName& instance, + IOobjectOption::registerOption = IOobjectOption::REGISTER + ); + + //- Construct from registry, instance, local, registration option + //- with MUST_READ, NO_WRITE + inline IOobjectList + ( + const objectRegistry& db, + const fileName& instance, + const fileName& local, + IOobjectOption::registerOption = IOobjectOption::REGISTER ); @@ -196,35 +224,32 @@ public: // Basic methods - //- Add IOobject to the list - bool add(autoPtr& objectPtr); + //- Move insert IOobject into the list + inline bool add(autoPtr&& objectPtr); - //- Add IOobject to the list - bool add(autoPtr&& objectPtr); + //- Move insert IOobject into the list + inline bool add(autoPtr& objectPtr); - //- Copy append objects from other to this list, but do not overwrite - //- existing keys. - // - // \return number of items added - label append(const IOobjectList& other); - - //- Move append objects from other to this list, but do not overwrite + //- Add objects from other to this list without overwriting //- existing keys. // After calling this, the other parameter will contains any items - // that could not be moved. + // that could not be merged. // - // \return number of items added - label append(IOobjectList&& other); + // \return number of items merged + label merge(IOobjectList&& other); - //- Remove IOobject from the list, by name or by iterator. + //- Remove object from the list, by name or by iterator. // // \return autoPtr using HashPtrTable::remove; - //- Remove IOobject from the list. + //- Remove object from the list by its IOobject::name(). // - // \return True if object was removed - bool remove(const IOobject& io); + // \return autoPtr + autoPtr remove(const IOobject& io) + { + return remove(io.name()); + } // Lookup single item @@ -708,11 +733,32 @@ public: void operator=(const IOobjectList&) = delete; //- Move assignment - void operator=(IOobjectList&& list); + inline void operator=(IOobjectList&& list); // Housekeeping + //- Construct from registry, instance, local, io options + // \deprecated(2022-11) prefer IOobjectOption or registerOption + IOobjectList + ( + const objectRegistry& db, + const fileName& instance, + const fileName& local, + IOobjectOption::readOption rOpt, + IOobjectOption::writeOption wOpt = IOobjectOption::NO_WRITE, + bool registerObject = true + ) + : + IOobjectList + ( + db, + instance, + local, + IOobjectOption(rOpt, wOpt, registerObject) + ) + {} + //- Deprecated(2018-11) Locate an object by name (c-string). //- Disambiguated from multiple-lookup version by calling parameter. // \deprecated(2018-11) use findObject() for non-ambiguous resolution @@ -741,6 +787,10 @@ Ostream& operator<<(Ostream& os, const IOobjectList& list); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#include "IOobjectListI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #ifdef NoRepository #include "IOobjectListTemplates.C" #endif diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListI.H b/src/OpenFOAM/db/IOobjectList/IOobjectListI.H new file mode 100644 index 0000000000..31f3fe6f1d --- /dev/null +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListI.H @@ -0,0 +1,142 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2022 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::IOobjectList::IOobjectList() +: + HashPtrTable() +{} + + +inline Foam::IOobjectList::IOobjectList(const label nObjects) +: + HashPtrTable(nObjects) // Could also use 2*nObjects instead +{} + + +inline Foam::IOobjectList::IOobjectList(const IOobjectList& list) +: + HashPtrTable(list) +{} + + +inline Foam::IOobjectList::IOobjectList(IOobjectList&& list) +: + HashPtrTable(std::move(list)) +{} + + +inline Foam::IOobjectList::IOobjectList +( + const objectRegistry& db, + const fileName& instance, + IOobjectOption ioOpt +) +: + IOobjectList(db, instance, fileName::null, ioOpt) +{} + + +inline Foam::IOobjectList::IOobjectList +( + const objectRegistry& db, + const fileName& instance, + IOobjectOption::registerOption registerObject +) +: + IOobjectList + ( + db, + instance, + fileName::null, + IOobjectOption + ( + IOobjectOption::MUST_READ, + IOobjectOption::NO_WRITE, + registerObject + ) + ) +{} + + +inline Foam::IOobjectList::IOobjectList +( + const objectRegistry& db, + const fileName& instance, + const fileName& local, + IOobjectOption::registerOption registerObject +) +: + IOobjectList + ( + db, + instance, + local, + IOobjectOption + ( + IOobjectOption::MUST_READ, + IOobjectOption::NO_WRITE, + registerObject + ) + ) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline bool Foam::IOobjectList::add(autoPtr&& objectPtr) +{ + if (objectPtr) + { + return insert(objectPtr->name(), std::move(objectPtr)); + } + + return false; +} + + +inline bool Foam::IOobjectList::add(autoPtr& objectPtr) +{ + if (objectPtr) + { + return insert(objectPtr->name(), std::move(objectPtr)); + } + + return false; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline void Foam::IOobjectList::operator=(IOobjectList&& list) +{ + transfer(list); +} + + +// ************************************************************************* // diff --git a/src/lagrangian/basic/Cloud/CloudIO.C b/src/lagrangian/basic/Cloud/CloudIO.C index d9eed02792..cbe38d4aed 100644 --- a/src/lagrangian/basic/Cloud/CloudIO.C +++ b/src/lagrangian/basic/Cloud/CloudIO.C @@ -51,7 +51,7 @@ void Foam::Cloud::readCloudUniformProperties() db(), IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ); if (dictObj.typeHeaderOk(true)) @@ -97,7 +97,7 @@ void Foam::Cloud::writeCloudUniformProperties() const db(), IOobject::NO_READ, IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ) ); @@ -200,7 +200,7 @@ Foam::IOobject Foam::Cloud::fieldIOobject *this, rOpt, IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ); } @@ -274,10 +274,7 @@ void Foam::Cloud::readFromFiles ( *this, time().timeName(), - "", - IOobject::MUST_READ, - IOobject::NO_WRITE, - false + IOobject::NO_REGISTER ); const wordRes::filter pred(selectFields, excludeFields);