ENH: add sorted() to objectRegistry and IOobjectList

- returns UPtrList view (read-only or read/write) of the objects

- shorter names for IOobject checks: hasHeaderClass(), isHeaderClass()

- remove unused IOobject::isHeaderClassName(const word&) method.
  The typed versions are preferable/recommended, but can still check
  directly if needed:

     (io.headerClassName() == "foo")
This commit is contained in:
Mark Olesen
2022-05-16 14:15:13 +02:00
parent f1098673c0
commit 95e2a2e887
21 changed files with 690 additions and 208 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -39,6 +39,38 @@ Description
using namespace Foam;
void report(const UPtrList<const IOobject>& objects)
{
Info<< "name/type:" << nl
<< objects.size() << nl << '(' << nl;
for (const IOobject& io : objects)
{
Info<< " " << io.name() << " : " << io.headerClassName() << nl;
}
Info<< ')' << nl << endl;
}
template<class Type>
void report(const UPtrList<const IOobject>& objects)
{
Info<< "name/type:" << nl
<< objects.size() << nl << '(' << nl;
for (const IOobject& io : objects)
{
Info<< " " << io.name() << " : " << io.headerClassName()
<< (io.isHeaderClass<Type>() ? " is " : " is not ")
<< Type::typeName << nl;
}
Info<< ')' << nl << endl;
}
void report(const IOobjectList& objects)
{
Info<< "Names: " << flatOutput(objects.sortedNames()) << nl
@ -199,7 +231,7 @@ void registryTests(const IOobjectList& objs)
int main(int argc, char *argv[])
{
argList::noParallel();
// argList::noParallel();
argList::addOption
(
"filter",
@ -269,6 +301,15 @@ int main(int argc, char *argv[])
Info<< "Time: " << runTime.timeName() << nl;
report(objects);
report(objects.sorted());
report(objects.sorted<volScalarField>());
report(objects.sorted<volVectorField>());
// Extra checks
report<volScalarField>(objects.sorted<volScalarField>());
report<volScalarField>(objects.sorted<volVectorField>());
findObjectTest(objects);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,6 +37,7 @@ Description
#include "polyMesh.H"
#include "IOstreams.H"
#include "FlatOutput.H"
#include "PtrListOps.H"
#include "objectRegistry.H"
using namespace Foam;
@ -47,6 +48,21 @@ using namespace Foam;
bool recursive = false;
template<class Type>
void report(const UPtrList<const Type>& objects)
{
Info<< Type::typeName << " name/type:" << nl
<< objects.size() << nl << '(' << nl;
for (const Type& obj : objects)
{
Info<< " " << obj.name() << " : " << obj.type() << nl;
}
Info<< ')' << nl << endl;
}
void printRegistry
(
Foam::Ostream& os,
@ -62,8 +78,8 @@ void printRegistry
Foam::label indent
)
{
wordList names(obr.sortedNames());
wordList regs(obr.sortedNames<objectRegistry>());
UPtrList<const regIOobject> objects(obr.sorted());
wordList regNames(obr.sortedNames<objectRegistry>());
std::string prefix;
for (label i=indent; i; --i)
@ -74,15 +90,17 @@ void printRegistry
os << '#' << prefix.c_str() << obr.name()
<< " parent:" << obr.parent().name() << nl;
os << ' ' << prefix.c_str() << "objects: " << flatOutput(names) << nl;
os << ' ' << prefix.c_str() << "registries: " << flatOutput(regs) << nl;
os << ' ' << prefix.c_str() << "objects: "
<< flatOutput(PtrListOps::names(objects)) << nl;
os << ' ' << prefix.c_str() << "registries: "
<< flatOutput(regNames) << nl;
// Print, but skip expansion of sub-registries for now
for (const word& name : names)
// Print without expanding sub-registries
for (const regIOobject& obj : objects)
{
os << (regs.found(name) ? '-' : ' ')
<< prefix.c_str() << name << " => " << obr[name]->type() << nl;
os << (isA<objectRegistry>(obj) ? '-' : ' ')
<< prefix.c_str() << obj.name() << " => " << obj.type() << nl;
}
for (label i=indent; i; --i)
{
@ -91,7 +109,7 @@ void printRegistry
os << '\n';
// Now descend into the sub-registries
for (const word& name : regs)
for (const word& name : regNames)
{
const objectRegistry& next = obr.lookupObject<objectRegistry>
(
@ -117,12 +135,15 @@ void printRegistry
}
}
// Main program:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
// argList::noParallel();
argList::addBoolOption
(
"mesh",

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,10 +36,12 @@ Description
#include "fvCFD.H"
#include "fvMesh.H"
#include "volFields.H"
#include "IOobjectList.H"
#include "timeSelector.H"
#include "ReadFields.H"
#include "IOstreams.H"
#include "PtrListOps.H"
#include "IOobjectList.H"
#include "objectRegistry.H"
using namespace Foam;
@ -113,6 +115,21 @@ void loadFields(fvMesh& mesh, const IOobjectList& objects)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
void report(const UPtrList<const Type>& objects)
{
Info<< Type::typeName << " name/type:" << nl
<< objects.size() << nl << '(' << nl;
for (const Type& obj : objects)
{
Info<< " " << obj.name() << " : " << obj.type() << nl;
}
Info<< ')' << nl << endl;
}
void printRegistry
(
Foam::Ostream& os,
@ -128,8 +145,8 @@ void printRegistry
Foam::label indent
)
{
wordList names(obr.sortedNames());
wordList regs(obr.sortedNames<objectRegistry>());
UPtrList<const regIOobject> objects(obr.sorted());
wordList regNames(obr.sortedNames<objectRegistry>());
std::string prefix;
for (label i=indent; i; --i)
@ -140,15 +157,17 @@ void printRegistry
os << '#' << prefix.c_str() << obr.name()
<< " parent:" << obr.parent().name() << nl;
os << ' ' << prefix.c_str() << "objects: " << flatOutput(names) << nl;
os << ' ' << prefix.c_str() << "registries: " << flatOutput(regs) << nl;
os << ' ' << prefix.c_str() << "objects: "
<< flatOutput(PtrListOps::names(objects)) << nl;
os << ' ' << prefix.c_str() << "registries: "
<< flatOutput(regNames) << nl;
// Print, but skip expansion of sub-registries for now
for (const word& name : names)
// Print without expanding sub-registries
for (const regIOobject& obj : objects)
{
os << (regs.found(name) ? '-' : ' ')
<< prefix.c_str() << name << " => " << obr[name]->type() << nl;
os << (isA<objectRegistry>(obj) ? '-' : ' ')
<< prefix.c_str() << obj.name() << " => " << obj.type() << nl;
}
for (label i=indent; i; --i)
{
@ -157,7 +176,7 @@ void printRegistry
os << '\n';
// Now descend into the sub-registries
for (const word& name : regs)
for (const word& name : regNames)
{
const objectRegistry& next = obr.lookupObject<objectRegistry>
(
@ -251,12 +270,12 @@ void registryTests(const objectRegistry& obr)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
//argList::noParallel();
// argList::addOption
// (
// "filter",
@ -296,6 +315,9 @@ int main(int argc, char *argv[])
registryTests(mesh);
report(mesh.sorted<const volScalarField>());
report(mesh.csorted<volVectorField>());
Info<< nl;
}

View File

@ -129,7 +129,7 @@ class readFieldsHandler
const bool ok =
(
io.typeHeaderOk<regIOobject>(false) // Preload header info
&& io.hasHeaderClassName() // Extra safety
&& io.hasHeaderClass() // Extra safety
&&
(
loadField<scalar>(io)

View File

@ -129,7 +129,7 @@ class readFieldsHandler
const bool ok =
(
io.typeHeaderOk<regIOobject>(false) // Preload header info
&& io.hasHeaderClassName() // Extra safety
&& io.hasHeaderClass() // Extra safety
&&
(
loadField<scalar>(io)

View File

@ -159,20 +159,28 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::sortedToc
template<class T, class Key, class Hash>
Foam::UPtrList<const typename Foam::HashTable<T, Key, Hash>::node_type>
Foam::HashTable<T, Key, Hash>::sorted() const
Foam::HashTable<T, Key, Hash>::csorted() const
{
UPtrList<const node_type> list(size_);
UPtrList<const node_type> result(size_);
label count = 0;
for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{
list.set(count++, iter.node());
result.set(count++, iter.node());
}
Foam::sort(list);
Foam::sort(result);
return list;
return result;
}
template<class T, class Key, class Hash>
Foam::UPtrList<const typename Foam::HashTable<T, Key, Hash>::node_type>
Foam::HashTable<T, Key, Hash>::sorted() const
{
return csorted();
}
@ -180,18 +188,18 @@ template<class T, class Key, class Hash>
Foam::UPtrList<typename Foam::HashTable<T, Key, Hash>::node_type>
Foam::HashTable<T, Key, Hash>::sorted()
{
UPtrList<node_type> list(size_);
UPtrList<node_type> result(size_);
label count = 0;
for (iterator iter = begin(); iter != end(); ++iter)
{
list.set(count++, iter.node());
result.set(count++, iter.node());
}
Foam::sort(list);
Foam::sort(result);
return list;
return result;
}

View File

@ -346,10 +346,17 @@ public:
//- Const access to the hash-table contents in sorted order
//- (sorted by keys).
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const node_type> csorted() const;
//- Const access to the hash-table contents in sorted order
//- (sorted by keys).
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const node_type> sorted() const;
//- Non-const access to the hash-table contents in sorted order
//- (sorted by keys).
// The lifetime of the returned content cannot exceed the parent!
UPtrList<node_type> sorted();

View File

@ -125,13 +125,6 @@ public:
// Public Data Types
//- Enumeration defining the valid states of an IOobject
enum objectState : char
{
GOOD,
BAD
};
//- Enumeration defining the read options
enum readOption : char
{
@ -148,6 +141,13 @@ public:
AUTO_WRITE = 0x10
};
//- Enumeration defining the valid states of an IOobject
enum objectState : char
{
GOOD,
BAD
};
//- Enumeration defining the file checking options
enum fileCheckTypes : char
{
@ -474,14 +474,15 @@ public:
// Checks
//- True if headerClassName() is non-empty (after reading)
inline bool hasHeaderClassName() const noexcept;
inline bool hasHeaderClass() const noexcept;
//- Test if headerClassName() equals the given class name
inline bool isHeaderClassName(const word& clsName) const;
//- Test if headerClassName() equals Type::typeName
//- Check if headerClassName() equals Type::typeName
template<class Type>
inline bool isHeaderClassName() const;
inline bool isHeaderClass() const;
//- Same as isHeaderClass()
template<class Type>
bool isHeaderClassName() const { return isHeaderClass<Type>(); }
// Meta-data
@ -638,8 +639,7 @@ public:
// Info
//- Return info proxy.
// Used to print token information to a stream
//- Return info proxy, for printing information to a stream
InfoProxy<IOobject> info() const
{
return *this;
@ -674,7 +674,7 @@ public:
//- Specialization for \c void always returns true (no headerClassName check).
template<>
inline bool IOobject::isHeaderClassName<void>() const
inline bool IOobject::isHeaderClass<void>() const
{
return true;
}

View File

@ -146,20 +146,14 @@ inline unsigned Foam::IOobject::scalarByteSize() const noexcept
// Checks
inline bool Foam::IOobject::hasHeaderClassName() const noexcept
inline bool Foam::IOobject::hasHeaderClass() const noexcept
{
return !headerClassName_.empty();
}
inline bool Foam::IOobject::isHeaderClassName(const word& clsName) const
{
return (clsName == headerClassName_);
}
template<class Type>
inline bool Foam::IOobject::isHeaderClassName() const
inline bool Foam::IOobject::isHeaderClass() const
{
return (Type::typeName == headerClassName_);
}

View File

@ -26,7 +26,7 @@ License
\*---------------------------------------------------------------------------*/
#include "IOobject.H"
#include "token.H"
#include "Ostream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -36,14 +36,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<IOobject>& ip)
const IOobject& io = ip.t_;
os << "IOobject: "
<< io.type() << token::SPACE
<< io.name()
<< io.type() << ' ' << io.name()
<< " local: " << io.local()
<< " readOpt: " << static_cast<int>(io.readOpt())
<< " writeOpt: " << static_cast<int>(io.writeOpt())
<< " registerObject: " << io.registerObject()
<< " globalObject: " << io.globalObject()
<< token::SPACE << io.path() << endl;
<< ' ' << io.path() << endl;
return os;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,31 +34,54 @@ License
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::IOobjectList::checkNames(wordList& masterNames, const bool syncPar)
void Foam::IOobjectList::checkObjectOrder
(
const UPtrList<const IOobject>& objs,
bool syncPar
)
{
// Sort for consistent order on all processors.
// Even do this for serial runs, for consistent behaviour
Foam::sort(masterNames);
if (syncPar && Pstream::parRun())
{
const wordList localNames(masterNames);
wordList objectNames(objs.size());
auto iter = objectNames.begin();
for (const IOobject& io : objs)
{
*iter = io.name(); // nameOp<IOobject>()
++iter;
}
checkNameOrder(objectNames, syncPar);
}
}
void Foam::IOobjectList::checkNameOrder
(
const wordList& objectNames,
bool syncPar
)
{
if (syncPar && Pstream::parRun())
{
wordList masterNames;
if (Pstream::master())
{
masterNames = objectNames;
}
Pstream::broadcast(masterNames);
if (localNames != masterNames)
if (objectNames != masterNames)
{
FatalErrorInFunction
<< "Objects not synchronised across processors." << nl
<< "Master has " << flatOutput(masterNames) << nl
<< "Processor " << Pstream::myProcNo()
<< " has " << flatOutput(localNames)
<< " has " << flatOutput(objectNames) << endl
<< exit(FatalError);
return false;
}
}
return true;
}
@ -71,7 +94,7 @@ void Foam::IOobjectList::syncNames(wordList& objNames)
Pstream::broadcast(objNames);
}
// Sort for consistent order on all processors
// Consistent order on all processors
Foam::sort(objNames);
}
@ -196,7 +219,7 @@ Foam::label Foam::IOobjectList::append(const IOobjectList& other)
InfoInFunction << "Copy append " << iter.key() << nl;
}
set(iter.key(), new IOobject(*(iter.val())));
set(iter.key(), new IOobject(*iter.val()));
++count;
}
}
@ -307,6 +330,24 @@ Foam::label Foam::IOobjectList::count(const char* clsName) const
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted() const
{
return sorted<void>();
}
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted(const bool syncPar) const
{
return sorted<void>(syncPar);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::wordList Foam::IOobjectList::names() const
{
return HashPtrTable<IOobject>::toc();
@ -315,10 +356,7 @@ Foam::wordList Foam::IOobjectList::names() const
Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
{
wordList objNames(HashPtrTable<IOobject>::toc());
checkNames(objNames, syncPar);
return objNames;
return sortedNames(syncPar);
}
@ -336,7 +374,7 @@ Foam::wordList Foam::IOobjectList::names
) const
{
// No nullptr check - only called with string literals
return names(static_cast<word>(clsName), syncPar);
return sortedNames(static_cast<word>(clsName), syncPar);
}
@ -352,7 +390,7 @@ Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
{
wordList objNames(HashPtrTable<IOobject>::sortedToc());
checkNames(objNames, syncPar);
checkNameOrder(objNames, syncPar);
return objNames;
}
@ -371,7 +409,7 @@ Foam::wordList Foam::IOobjectList::sortedNames
) const
{
// No nullptr check - only called with string literals
return names(static_cast<word>(clsName), syncPar);
return sortedNames(static_cast<word>(clsName), syncPar);
}
@ -397,16 +435,14 @@ Foam::wordList Foam::IOobjectList::allNames() const
}
bool Foam::IOobjectList::checkNames(const bool syncPar) const
void Foam::IOobjectList::checkNames(const bool syncPar) const
{
if (syncPar && Pstream::parRun())
{
wordList objNames(HashPtrTable<IOobject>::toc());
wordList objNames(HashPtrTable<IOobject>::sortedToc());
return checkNames(objNames, syncPar);
checkNameOrder(objNames, syncPar);
}
return true;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,11 +36,12 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef IOobjectList_H
#define IOobjectList_H
#ifndef Foam_IOobjectList_H
#define Foam_IOobjectList_H
#include "HashPtrTable.H"
#include "HashSet.H"
#include "UPtrList.H"
#include "IOobject.H"
#include "wordRes.H"
@ -59,14 +60,23 @@ class IOobjectList
{
// Private Member Functions
//- Check name consistency on all processors
//- Check consistency of names and their order on all processors
//- (the input list is assumed to be pre-sorted).
//
// With syncPar = true, check that object names are the same on
// all processors. Trigger FatalError if not.
// With syncPar = true, check that object names are identical
// (content and order) on all processors. FatalError if not.
static void checkNameOrder(const wordList& objectNames, bool syncPar);
//- Check consistency of object names/order on all processors
//- (the input list is assumed to be pre-sorted).
//
// When syncPar is used, the object names are sorted as a side-effect,
// since this is required for consistent ordering across processors.
static bool checkNames(wordList& masterNames, const bool syncPar);
// With syncPar = true, check that object names are identical
// (content and order) on all processors. FatalError if not.
static void checkObjectOrder
(
const UPtrList<const IOobject>& objs,
bool syncPar
);
//- Combine names from all processors and sort
static void syncNames(wordList& objNames);
@ -116,6 +126,14 @@ class IOobjectList
const bool doSort
);
//- Templated implementation for sorted()
template<class Type, class MatchPredicate>
static UPtrList<const IOobject> objectsTypeImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
);
//- Templated implementation for lookup()
template<class MatchPredicate>
static IOobjectList lookupImpl
@ -383,6 +401,54 @@ public:
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
// Sorted access
//- The sorted list of IOobjects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const IOobject> sorted() const;
//- The sorted list of IOobjects with optional check for
//- parallel consistency.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const IOobject> sorted(const bool syncPar) const;
//- The sorted list of IOobjects with headerClassName == Type::typeName
//
// \note If \a Type is \c void, no headerClassName check is used
// (always true).
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<const IOobject> sorted() const;
//- The sorted names of the IOobjects with optional check for
//- parallel consistency.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<const IOobject> sorted(const bool syncPar) const;
//- The sorted list of IOobjects with headerClassName == Type::typeName
//- that also have a matching object name.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<const IOobject> sorted(const MatchPredicate& matchName) const;
//- The sorted list of IOobjects with headerClassName == Type::typeName
//- that also have a matching object name.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<const IOobject> sorted
(
const MatchPredicate& matchName,
const bool syncPar
) const;
// Number of items
//- The number of objects of the given headerClassName
@ -420,29 +486,36 @@ public:
// Summary of names
//- The names of the IOobjects
//- The unsorted names of the IOobjects
wordList names() const;
//- The names of the IOobjects.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of the IOobjects with optional check for
//- parallel consistency.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
wordList names(const bool syncPar) const;
//- The names of IOobjects with the given headerClassName
//- The unsorted names of IOobjects with the given headerClassName
wordList names(const char* clsName) const;
//- The names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of the IOobjects with the given headerClassName
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
wordList names(const char* clsName, const bool syncPar) const;
//- The names of IOobjects with the given headerClassName
//- The unsorted names of IOobjects with the given headerClassName
template<class MatchPredicate>
wordList names(const MatchPredicate& matchClass) const;
//- The names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of the IOobjects with the given headerClassName
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
template<class MatchPredicate>
wordList names
(
@ -450,7 +523,7 @@ public:
const bool syncPar
) const;
//- The names of IOobjects with the given headerClassName
//- The unsorted names of IOobjects with the given headerClassName
//- that also have a matching object name.
template<class MatchPredicate1, class MatchPredicate2>
wordList names
@ -459,10 +532,12 @@ public:
const MatchPredicate2& matchName
) const;
//- The names of the IOobjects with the given headerClassName
//- The sorted names of the IOobjects with the given headerClassName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
template<class MatchPredicate1, class MatchPredicate2>
wordList names
(
@ -472,27 +547,31 @@ public:
) const;
//- The names of objects with headerClassName == Type::typeName
//- The unsorted names of objects with
//- headerClassName == Type::typeName
template<class Type>
wordList names() const;
//- The names of objects with headerClassName == Type::typeName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of objects with
//- headerClassName == Type::typeName.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
template<class Type>
wordList names(bool syncPar) const;
//- The names of objects with headerClassName == Type::typeName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The unsorted names of objects with
//- headerClassName == Type::typeName and a matching object name.
template<class Type, class MatchPredicate>
wordList names(const MatchPredicate& matchName) const;
//- The names of objects with headerClassName == Type::typeName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of objects with
//- headerClassName == Type::typeName and a matching object name.
// FatalError if syncPar = true and names are not consistent on all
// processors.
// \note Output is always sorted - for consistent serial/parallel
// behaviour.
template<class Type, class MatchPredicate>
wordList names
(
@ -506,17 +585,18 @@ public:
//- The sorted names of the IOobjects
wordList sortedNames() const;
//- The sorted names of the IOobjects.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
//- The sorted names of the IOobjects with optional check for
//- parallel consistency.
// FatalError if syncPar = true and names are not consistent on all
// processors.
wordList sortedNames(const bool syncPar) const;
//- The sorted names of IOobjects with the given headerClassName
wordList sortedNames(const char* clsName) const;
//- The sorted names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
wordList sortedNames(const char* clsName, const bool syncPar) const;
//- The sorted names of IOobjects with the given headerClassName
@ -524,8 +604,8 @@ public:
wordList sortedNames(const MatchPredicate& matchClass) const;
//- The sorted names of the IOobjects with the given headerClassName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
template<class MatchPredicate>
wordList sortedNames
(
@ -544,8 +624,8 @@ public:
//- The sorted names of the IOobjects with the given headerClassName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
template<class MatchPredicate1, class MatchPredicate2>
wordList sortedNames
(
@ -560,22 +640,20 @@ public:
wordList sortedNames() const;
//- The sorted names of objects with headerClassName == Type::typeName
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
template<class Type>
wordList sortedNames(bool syncPar) const;
//- The sorted names of objects with headerClassName == Type::typeName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
template<class Type, class MatchPredicate>
wordList sortedNames(const MatchPredicate& matchName) const;
//- The sorted names of objects with headerClassName == Type::typeName
//- that also have a matching object name.
// With syncPar = true, sorts the names and triggers FatalError
// if the names are not consistent on all processors.
// FatalError if syncPar = true and names are not consistent on all
// processors.
template<class Type, class MatchPredicate>
wordList sortedNames
(
@ -620,9 +698,8 @@ public:
wordList allNames() const;
//- Verify that object names are synchronised across processors
// Triggers FatalError if the names are not consistent on all
// processors.
bool checkNames(const bool syncPar = true) const;
// FatalError if the names are not consistent on all processors.
void checkNames(const bool syncPar = true) const;
// Member Operators

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -96,7 +96,7 @@ Foam::label Foam::IOobjectList::countTypeImpl
{
const IOobject* io = iter.val();
if (io->isHeaderClassName<Type>() && matchName(io->name()))
if (io->isHeaderClass<Type>() && matchName(io->name()))
{
++count;
}
@ -159,7 +159,7 @@ Foam::wordList Foam::IOobjectList::namesTypeImpl
const word& key = iter.key();
const IOobject* io = iter.val();
if (io->isHeaderClassName<Type>() && matchName(key))
if (io->isHeaderClass<Type>() && matchName(key))
{
objNames[count] = key;
++count;
@ -177,6 +177,38 @@ Foam::wordList Foam::IOobjectList::namesTypeImpl
}
// Templated implementation for sorted()
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::objectsTypeImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
)
{
UPtrList<const IOobject> result(list.size());
label count = 0;
forAllConstIters(list, iter)
{
const word& key = iter.key();
const IOobject* io = iter.val();
if (io->isHeaderClass<Type>() && matchName(key))
{
result.set(count, io);
++count;
}
}
result.resize(count);
Foam::sort(result, nameOp<IOobject>()); // Sort by object name()
return result;
}
// Templated implementation for lookup()
template<class MatchPredicate>
Foam::IOobjectList Foam::IOobjectList::lookupImpl
@ -253,7 +285,7 @@ Foam::IOobjectList Foam::IOobjectList::lookupClassTypeImpl
const word& key = iter.key();
const IOobject* io = iter.val();
if (io->isHeaderClassName<Type>() && matchName(key))
if (io->isHeaderClass<Type>() && matchName(key))
{
if (IOobject::debug)
{
@ -282,7 +314,7 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject
{
const IOobject* io = iter.val();
if (io->isHeaderClassName<Type>())
if (io->isHeaderClass<Type>())
{
if (IOobject::debug)
{
@ -427,6 +459,62 @@ Foam::label Foam::IOobjectList::count
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted() const
{
return objectsTypeImpl<Type>(*this, predicates::always());
}
template<class Type>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted(const bool syncPar) const
{
UPtrList<const IOobject> list
(
objectsTypeImpl<Type>(*this, predicates::always())
);
checkObjectOrder(list, syncPar);
return list;
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted
(
const MatchPredicate& matchName
) const
{
return objectsTypeImpl<Type>(*this, matchName);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted
(
const MatchPredicate& matchName,
const bool syncPar
) const
{
UPtrList<const IOobject> list
(
objectsTypeImpl<Type>(*this, matchName)
);
checkObjectOrder(list, syncPar);
return list;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class MatchPredicate>
@ -446,13 +534,7 @@ Foam::wordList Foam::IOobjectList::names
const bool syncPar
) const
{
wordList objNames
(
namesImpl(*this, matchClass, predicates::always(), false)
);
checkNames(objNames, syncPar);
return objNames;
return sortedNames(matchClass, syncPar);
}
@ -475,10 +557,7 @@ Foam::wordList Foam::IOobjectList::names
const bool syncPar
) const
{
wordList objNames(namesImpl(*this, matchClass, matchName, false));
checkNames(objNames, syncPar);
return objNames;
return sortedNames(matchClass, matchName, syncPar);
}
@ -492,10 +571,7 @@ Foam::wordList Foam::IOobjectList::names() const
template<class Type>
Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
{
wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
checkNames(objNames, syncPar);
return objNames;
return sortedNames<Type>(syncPar);
}
@ -516,10 +592,7 @@ Foam::wordList Foam::IOobjectList::names
const bool syncPar
) const
{
wordList objNames(namesTypeImpl<Type>(*this, matchName, false));
checkNames(objNames, syncPar);
return objNames;
return sortedNames<Type>(matchName, syncPar);
}
@ -547,7 +620,7 @@ Foam::wordList Foam::IOobjectList::sortedNames
namesImpl(*this, matchClass, predicates::always(), true)
);
checkNames(objNames, syncPar);
checkNameOrder(objNames, syncPar);
return objNames;
}
@ -562,6 +635,7 @@ Foam::wordList Foam::IOobjectList::sortedNames
return namesImpl(*this, matchClass, matchName, true);
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::IOobjectList::sortedNames
(
@ -572,7 +646,7 @@ Foam::wordList Foam::IOobjectList::sortedNames
{
wordList objNames(namesImpl(*this, matchClass, matchName, true));
checkNames(objNames, syncPar);
checkNameOrder(objNames, syncPar);
return objNames;
}
@ -589,7 +663,7 @@ Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
{
wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
checkNames(objNames, syncPar);
checkNameOrder(objNames, syncPar);
return objNames;
}
@ -611,7 +685,10 @@ Foam::wordList Foam::IOobjectList::sortedNames
const bool syncPar
) const
{
return namesTypeImpl<Type>(*this, matchName, true, syncPar);
wordList objNames(namesTypeImpl<Type>(*this, matchName, true));
checkNameOrder(objNames, syncPar);
return objNames;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -144,6 +144,27 @@ Foam::label Foam::objectRegistry::count(const char* clsName) const
}
Foam::UPtrList<const Foam::regIOobject>
Foam::objectRegistry::csorted() const
{
return objectsTypeImpl<const regIOobject>(*this, predicates::always());
}
Foam::UPtrList<const Foam::regIOobject>
Foam::objectRegistry::sorted() const
{
return objectsTypeImpl<const regIOobject>(*this, predicates::always());
}
Foam::UPtrList<Foam::regIOobject>
Foam::objectRegistry::sorted()
{
return objectsTypeImpl<regIOobject>(*this, predicates::always());
}
Foam::wordList Foam::objectRegistry::names() const
{
return HashTable<regIOobject*>::toc();
@ -442,7 +463,7 @@ bool Foam::objectRegistry::modified() const
{
for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{
if ((*iter)->modified())
if (iter.val()->modified())
{
return true;
}
@ -463,7 +484,7 @@ void Foam::objectRegistry::readModifiedObjects()
<< iter.key() << endl;
}
(*iter)->readIfModified();
iter.val()->readIfModified();
}
}
@ -487,18 +508,19 @@ bool Foam::objectRegistry::writeObject
{
if (objectRegistry::debug)
{
const regIOobject& obj = *iter.val();
Pout<< "objectRegistry::write() : "
<< name() << " : Considering writing object "
<< iter.key()
<< " of type " << (*iter)->type()
<< " with writeOpt " << static_cast<int>((*iter)->writeOpt())
<< " to file " << (*iter)->objectPath()
<< endl;
<< iter.key() << " of type "
<< obj.type() << " with writeOpt "
<< static_cast<int>(obj.writeOpt())
<< " to file " << obj.objectRelPath() << endl;
}
if ((*iter)->writeOpt() != NO_WRITE)
if (iter.val()->writeOpt() != NO_WRITE)
{
ok = (*iter)->writeObject(streamOpt, valid) && ok;
ok = iter.val()->writeObject(streamOpt, valid) && ok;
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,11 +36,12 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef objectRegistry_H
#define objectRegistry_H
#ifndef Foam_objectRegistry_H
#define Foam_objectRegistry_H
#include "HashTable.H"
#include "HashSet.H"
#include "UPtrList.H"
#include "regIOobject.H"
#include "wordRes.H"
@ -130,6 +131,15 @@ class objectRegistry
const bool doSort
);
//- Templated implementation for sorted()
// Called with 'Type' or 'const Type'
template<class Type, class MatchPredicate>
static UPtrList<Type> objectsTypeImpl
(
const objectRegistry& list,
const MatchPredicate& matchName
);
//- No copy construct
objectRegistry(const objectRegistry&) = delete;
@ -203,6 +213,54 @@ public:
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
// Sorted access
//- Return sorted list of objects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const regIOobject> csorted() const;
//- Return sorted list of objects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const regIOobject> sorted() const;
//- Return sorted list of objects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<regIOobject> sorted();
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<const Type> csorted() const;
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<const Type> sorted() const;
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<Type> sorted();
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
//- that also have a matching object name.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<const Type> csorted(const MatchPredicate& matchName) const;
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
//- that also have a matching object name.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<const Type> sorted(const MatchPredicate& matchName) const;
//- Return sorted list of objects with a class satisfying \c isA\<Type\>
//- that also have a matching object name.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<Type> sorted(const MatchPredicate& matchName);
// Number of items
//- The number of objects of the given class name
@ -244,19 +302,19 @@ public:
// Summary of names
//- The names of all objects
//- The unsorted names of all objects
wordList names() const;
//- The names of objects with the given class name.
//- The unsorted names of objects with the given class name.
// \note uses the class type() method
wordList names(const char* clsName) const;
//- The names of objects with a matching class name
//- The unsorted names of objects with a matching class name
// \note uses the class type() method
template<class MatchPredicate>
wordList names(const MatchPredicate& matchClass) const;
//- The names of objects with a matching class name
//- The unsorted names of objects with a matching class name
//- that also have a matching object name.
// \note uses the class type() method
template<class MatchPredicate1, class MatchPredicate2>
@ -266,13 +324,13 @@ public:
const MatchPredicate2& matchName
) const;
//- The names of objects with a class satisfying \c isA\<Type\>.
//- The unsorted names of objects with a class satisfying \c isA\<Type\>.
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type>
wordList names() const;
//- The names of objects with a class satisfying \c isA\<Type\>
//- The unsorted names of objects with a class satisfying \c isA\<Type\>
//- that also have a matching object name.
//
// \note If \a Type is \c void, no isA check is used (always true).

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -99,7 +99,7 @@ Foam::label Foam::objectRegistry::countTypeImpl
if
(
(std::is_void<Type>::value || isA<Type>(*obj))
(std::is_void<Type>::value || Foam::isA<Type>(*obj))
&& matchName(obj->name())
)
{
@ -164,7 +164,7 @@ Foam::wordList Foam::objectRegistry::namesTypeImpl
if
(
(std::is_void<Type>::value || isA<Type>(*obj))
(std::is_void<Type>::value || Foam::isA<Type>(*obj))
&& matchName(obj->name())
)
{
@ -184,6 +184,39 @@ Foam::wordList Foam::objectRegistry::namesTypeImpl
}
// Templated implementation for sorted()
template<class Type, class MatchPredicate>
Foam::UPtrList<Type>
Foam::objectRegistry::objectsTypeImpl
(
const objectRegistry& list,
const MatchPredicate& matchName
)
{
typedef typename std::remove_cv<Type>::type BaseType;
UPtrList<Type> result(list.size());
label count = 0;
forAllConstIters(list, iter)
{
const BaseType* ptr = Foam::isA<BaseType>(*iter.val());
if (ptr && matchName(ptr->name()))
{
result.set(count, const_cast<BaseType*>(ptr));
++count;
}
}
result.resize(count);
Foam::sort(result, nameOp<Type>()); // Sort by object name()
return result;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class MatchPredicate>
@ -243,7 +276,12 @@ Foam::label Foam::objectRegistry::count
if
(
std::is_void<Type>::value
|| (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
||
(
strict
? bool(Foam::isType<Type>(*obj))
: bool(Foam::isA<Type>(*obj))
)
)
{
++nObjects;
@ -254,6 +292,66 @@ Foam::label Foam::objectRegistry::count
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
Foam::UPtrList<const Type>
Foam::objectRegistry::csorted() const
{
return objectsTypeImpl<const Type>(*this, predicates::always());
}
template<class Type>
Foam::UPtrList<const Type>
Foam::objectRegistry::sorted() const
{
return objectsTypeImpl<const Type>(*this, predicates::always());
}
template<class Type>
Foam::UPtrList<Type>
Foam::objectRegistry::sorted()
{
return objectsTypeImpl<Type>(*this, predicates::always());
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Type>
Foam::objectRegistry::csorted
(
const MatchPredicate& matchName
) const
{
return objectsTypeImpl<const Type>(*this, matchName);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Type>
Foam::objectRegistry::sorted
(
const MatchPredicate& matchName
) const
{
return objectsTypeImpl<const Type>(*this, matchName);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<Type>
Foam::objectRegistry::sorted
(
const MatchPredicate& matchName
)
{
return objectsTypeImpl<Type>(*this, matchName);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class MatchPredicate>
Foam::wordList Foam::objectRegistry::names
(
@ -342,7 +440,12 @@ Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
{
const regIOobject* obj = iter.val();
if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
if
(
strict
? bool(Foam::isType<Type>(*obj))
: bool(Foam::isA<Type>(*obj))
)
{
objectsOfClass.insert(obj->name(), dynamic_cast<const Type*>(obj));
}
@ -364,7 +467,12 @@ Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
{
regIOobject* obj = iter.val();
if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
if
(
strict
? bool(Foam::isType<Type>(*obj))
: bool(Foam::isA<Type>(*obj))
)
{
objectsOfClass.insert(obj->name(), dynamic_cast<Type*>(obj));
}

View File

@ -313,7 +313,7 @@ Foam::polyMesh::polyMesh(const IOobject& io, const bool doInit)
oldPointsPtr_(nullptr),
oldCellCentresPtr_(nullptr)
{
if (owner_.hasHeaderClassName())
if (owner_.hasHeaderClass())
{
initMesh();
}

View File

@ -240,7 +240,7 @@ Foam::polyMesh::readUpdateState Foam::polyMesh::readUpdate()
// Boundary is set so can use initMesh now (uses boundary_ to
// determine internal and active faces)
if (owner_.hasHeaderClassName())
if (owner_.hasHeaderClass())
{
initMesh();
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,8 +40,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef word_H
#define word_H
#ifndef Foam_word_H
#define Foam_word_H
#include "string.H"
@ -241,6 +241,12 @@ struct nameOp
{
return obj.name();
}
//- Less-compare two objects by their name() method - for sorting
bool operator()(const T& a, const T& b) const
{
return (a.name() < b.name());
}
};
@ -252,6 +258,12 @@ struct typeOp
{
return obj.type();
}
//- Less-compare two objects by their type() method - for sorting
bool operator()(const T& a, const T& b) const
{
return (a.type() < b.type());
}
};

View File

@ -192,7 +192,7 @@ Foam::label Foam::functionObjects::fvExpressionField::loadFields
const bool ok =
(
io.typeHeaderOk<regIOobject>(false) // Preload header info
&& io.hasHeaderClassName() // Extra safety
&& io.hasHeaderClass() // Extra safety
&&
(
loadField<scalar>(io)

View File

@ -104,7 +104,7 @@ bool Foam::functionObjects::readFields::execute()
const bool ok =
(
io.typeHeaderOk<regIOobject>(false) // Preload header info
&& io.hasHeaderClassName() // Extra safety
&& io.hasHeaderClass() // Extra safety
&&
(
loadField<scalar>(io)