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.
This commit is contained in:
Mark Olesen 2022-11-19 17:08:05 +01:00
parent db57c456f6
commit 67b58c28c0
5 changed files with 232 additions and 133 deletions

View File

@ -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

View File

@ -99,38 +99,12 @@ void Foam::IOobjectList::syncNames(wordList& objNames)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IOobjectList::IOobjectList()
:
HashPtrTable<IOobject>()
{}
Foam::IOobjectList::IOobjectList(const label nObjects)
:
HashPtrTable<IOobject>(nObjects) // Could also use 2*nObjects instead
{}
Foam::IOobjectList::IOobjectList(const IOobjectList& list)
:
HashPtrTable<IOobject>(list)
{}
Foam::IOobjectList::IOobjectList(IOobjectList&& list)
:
HashPtrTable<IOobject>(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<IOobject>()
@ -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<IOobject>& objectPtr)
{
if (objectPtr)
{
return insert(objectPtr->name(), objectPtr);
}
return false;
}
bool Foam::IOobjectList::add(autoPtr<IOobject>&& 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)

View File

@ -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<IOobject>& objectPtr);
//- Move insert IOobject into the list
inline bool add(autoPtr<IOobject>&& objectPtr);
//- Add IOobject to the list
bool add(autoPtr<IOobject>&& objectPtr);
//- Move insert IOobject into the list
inline bool add(autoPtr<IOobject>& 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<IOobject>
using HashPtrTable<IOobject>::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<IOobject>
autoPtr<IOobject> 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

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::IOobjectList::IOobjectList()
:
HashPtrTable<IOobject>()
{}
inline Foam::IOobjectList::IOobjectList(const label nObjects)
:
HashPtrTable<IOobject>(nObjects) // Could also use 2*nObjects instead
{}
inline Foam::IOobjectList::IOobjectList(const IOobjectList& list)
:
HashPtrTable<IOobject>(list)
{}
inline Foam::IOobjectList::IOobjectList(IOobjectList&& list)
:
HashPtrTable<IOobject>(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<IOobject>&& objectPtr)
{
if (objectPtr)
{
return insert(objectPtr->name(), std::move(objectPtr));
}
return false;
}
inline bool Foam::IOobjectList::add(autoPtr<IOobject>& objectPtr)
{
if (objectPtr)
{
return insert(objectPtr->name(), std::move(objectPtr));
}
return false;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline void Foam::IOobjectList::operator=(IOobjectList&& list)
{
transfer(list);
}
// ************************************************************************* //

View File

@ -51,7 +51,7 @@ void Foam::Cloud<ParticleType>::readCloudUniformProperties()
db(),
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE,
false
IOobject::NO_REGISTER
);
if (dictObj.typeHeaderOk<IOdictionary>(true))
@ -97,7 +97,7 @@ void Foam::Cloud<ParticleType>::writeCloudUniformProperties() const
db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobject::NO_REGISTER
)
);
@ -200,7 +200,7 @@ Foam::IOobject Foam::Cloud<ParticleType>::fieldIOobject
*this,
rOpt,
IOobject::NO_WRITE,
false
IOobject::NO_REGISTER
);
}
@ -274,10 +274,7 @@ void Foam::Cloud<ParticleType>::readFromFiles
(
*this,
time().timeName(),
"",
IOobject::MUST_READ,
IOobject::NO_WRITE,
false
IOobject::NO_REGISTER
);
const wordRes::filter pred(selectFields, excludeFields);