ENH: define IOobjectList::csorted(), deprecate some sorted() const methods

- prefer csorted() method for const access since it ensures that the
  return values are also const pointers (for example) even if
  the object itself can be accessed as a non-const.

- the csorted() method already existed for HashTable and
  objectRegistry, but now added to IOobjectList for method name
  consistency (even although the IOobjectList only has a const-access
  version)

ENH: objectRegistry with templated strict lookup

- for lookupClass and csorted/sorted. Allows isType restriction as a
  compile-time specification.
This commit is contained in:
Mark Olesen
2023-07-07 12:25:34 +02:00
parent 0eb4354ee0
commit 129b738136
18 changed files with 418 additions and 220 deletions

View File

@ -85,7 +85,7 @@ Description
fileNameList procDirs
(
DirLister::dirs(".").sorted<fileName>(matchProcs)
DirLister::dirs(".").csorted<fileName>(matchProcs)
);
}
\endcode
@ -206,11 +206,11 @@ public:
//- Return a complete list of names, sorted in natural order
template<class StringType=Foam::word>
List<StringType> sorted() const;
List<StringType> csorted() const;
//- Return complete list of names, sorted in natural order
template<class StringType=Foam::word, class UnaryPredicate>
List<StringType> sorted
List<StringType> csorted
(
const UnaryPredicate& pred,
const bool prune = false

View File

@ -70,23 +70,23 @@ Foam::List<StringType> Foam::DirLister::list() const
template<class StringType, class UnaryPredicate>
Foam::List<StringType> Foam::DirLister::sorted
Foam::List<StringType> Foam::DirLister::csorted
(
const UnaryPredicate& pred,
const bool prune
) const
{
List<StringType> lst(list<StringType>(pred, prune));
sort(lst, stringOps::natural_sort());
List<StringType> list(list<StringType>(pred, prune));
Foam::sort(list, stringOps::natural_sort());
return lst;
return list;
}
template<class StringType>
Foam::List<StringType> Foam::DirLister::sorted() const
Foam::List<StringType> Foam::DirLister::csorted() const
{
return sorted<StringType>(predicates::always());
return csorted<StringType>(predicates::always());
}

View File

@ -162,7 +162,7 @@ int main(int argc, char *argv[])
Info<< "dirList: "
<< flatOutput
(
DirLister::dirs(".").sorted<fileName>(relist)
DirLister::dirs(".").csorted<fileName>(relist)
) << nl;
}

View File

@ -75,8 +75,8 @@ int main()
}
Info<< "\ntable1 sorted() :" << endl;
for (const auto& iter : table1.sorted())
Info<< "\ntable1 csorted() :" << endl;
for (const auto& iter : table1.csorted())
{
Info<< " " << iter.key() << " => " << iter.val() << nl;
}
@ -100,7 +100,7 @@ int main()
}
Info<< "\nInplace modified - via sorted() access :" << endl;
for (const auto& iter : table1.sorted())
for (const auto& iter : table1.csorted())
{
Info<< " " << iter.key() << " => " << iter.val() << nl;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -290,14 +290,14 @@ int main(int argc, char *argv[])
Info<< "Time: " << runTime.timeName() << nl;
report(objects);
report(objects.sorted());
report(objects.csorted());
report(objects.sorted<volScalarField>());
report(objects.sorted<volVectorField>());
report(objects.csorted<volScalarField>());
report(objects.csorted<volVectorField>());
// Extra checks
report<volScalarField>(objects.sorted<volScalarField>());
report<volScalarField>(objects.sorted<volVectorField>());
report<volScalarField>(objects.csorted<volScalarField>());
report<volScalarField>(objects.csorted<volVectorField>());
findObjectTest(objects);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2022 OpenCFD Ltd.
Copyright (C) 2022-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -139,7 +139,7 @@ void printInfo(const ensightMesh& mesh, int verbose = 0)
FixedList<label, 3> cellStats(Zero);
FixedList<label, 3> faceStats(Zero);
for (const auto& iter : mesh.cellZoneParts().sorted())
for (const auto& iter : mesh.cellZoneParts().csorted())
{
FixedList<label, 3> stats = printPartInfo(iter.val(), verbose);
@ -149,7 +149,7 @@ void printInfo(const ensightMesh& mesh, int verbose = 0)
}
}
for (const auto& iter : mesh.faceZoneParts().sorted())
for (const auto& iter : mesh.faceZoneParts().csorted())
{
FixedList<label, 3> stats = printPartInfo(iter.val(), verbose);
@ -159,7 +159,7 @@ void printInfo(const ensightMesh& mesh, int verbose = 0)
}
}
for (const auto& iter : mesh.boundaryParts().sorted())
for (const auto& iter : mesh.boundaryParts().csorted())
{
FixedList<label, 3> stats = printPartInfo(iter.val(), verbose);

View File

@ -57,7 +57,7 @@ int main(int argc, char *argv[])
Info<< "Found: " << objects << nl << endl;
for (const IOobject& io : objects.sorted<uniformDimensionedVectorField>())
for (const IOobject& io : objects.csorted<uniformDimensionedVectorField>())
{
if (io.name() == meshObjects::gravity::typeName)
{

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -78,8 +78,8 @@ void printRegistry
Foam::label indent
)
{
UPtrList<const regIOobject> objects(obr.sorted());
wordList regNames(obr.sortedNames<objectRegistry>());
const UPtrList<const regIOobject> objects(obr.csorted());
const wordList regNames(obr.sortedNames<objectRegistry>());
std::string prefix;
for (label i=indent; i; --i)

View File

@ -145,8 +145,8 @@ void printRegistry
Foam::label indent
)
{
UPtrList<const regIOobject> objects(obr.sorted());
wordList regNames(obr.sortedNames<objectRegistry>());
const UPtrList<const regIOobject> objects(obr.csorted());
const wordList regNames(obr.sortedNames<objectRegistry>());
std::string prefix;
for (label i=indent; i; --i)
@ -315,7 +315,7 @@ int main(int argc, char *argv[])
registryTests(mesh);
report(mesh.sorted<const volScalarField>());
report(mesh.csorted<volScalarField>());
report(mesh.csorted<volVectorField>());
Info<< nl;

View File

@ -192,14 +192,6 @@ Foam::HashTable<T, Key, Hash>::csorted() const
}
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();
}
template<class T, class Key, class Hash>
Foam::UPtrList<typename Foam::HashTable<T, Key, Hash>::node_type>
Foam::HashTable<T, Key, Hash>::sorted()

View File

@ -34,7 +34,7 @@ Description
depends on the method used to generate the hash key index, the
table capacity, insertion order etc. When the key order is
important, use the sortedToc() method to obtain a list of sorted
keys and use that for further access, or the sorted() method
keys and use that for further access, or the csorted()/sorted() methods
to obtain a UPtrList of entries to traverse in sorted order.
Internally the table uses closed addressing into a flat storage space
@ -351,11 +351,6 @@ public:
// 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!
@ -1006,6 +1001,11 @@ public:
//- Same as contains()
bool found(const Key& key) const { return this->contains(key); }
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const node_type> sorted() const { return this->csorted(); }
};

View File

@ -220,22 +220,6 @@ 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

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -129,12 +129,13 @@ class IOobjectList
const bool doSort
);
//- Templated implementation for sorted()
//- Templated implementation for csorted()/sorted()
template<class Type, class MatchPredicate>
static UPtrList<const IOobject> objectsTypeImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
const MatchPredicate& matchName,
const bool doSort
);
//- Templated implementation for lookup()
@ -421,26 +422,37 @@ public:
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
// Sorted access
// List-wise access (unsorted)
//- 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
//- The unsorted 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;
UPtrList<const IOobject> cobjects() const;
//- The unsorted 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> cobjects
(
const MatchPredicate& matchName
) const;
// List-wise access (sorted)
//- 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> csorted() const;
//- The sorted names of the IOobjects with optional check for
//- parallel consistency.
@ -448,13 +460,30 @@ public:
// processors.
// The lifetime of the returned content cannot exceed the parent!
template<class Type>
UPtrList<const IOobject> sorted(const bool syncPar) const;
UPtrList<const IOobject> csorted(const bool syncPar) const;
//- The sorted list of IOobjects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const IOobject> csorted() const
{
return csorted<void>();
}
//- 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> csorted(const bool syncPar) const
{
return csorted<void>(syncPar);
}
//- 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;
UPtrList<const IOobject> csorted(const MatchPredicate& matchName) const;
//- The sorted list of IOobjects with headerClassName == Type::typeName
//- that also have a matching object name.
@ -462,7 +491,7 @@ public:
// processors.
// The lifetime of the returned content cannot exceed the parent!
template<class Type, class MatchPredicate>
UPtrList<const IOobject> sorted
UPtrList<const IOobject> csorted
(
const MatchPredicate& matchName,
const bool syncPar
@ -769,10 +798,67 @@ public:
{
return getObject(objName);
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted() const
{
return csorted<Type>();
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted(const bool syncPar) const
{
return csorted<Type>(syncPar);
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted() const
{
return csorted<void>();
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted(const bool syncPar) const
{
return csorted<void>(syncPar);
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type, class MatchPredicate>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted(const MatchPredicate& matchName) const
{
return csorted<Type>(matchName);
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type, class MatchPredicate>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const IOobject> sorted
(
const MatchPredicate& matchName,
const bool syncPar
) const
{
return csorted<Type>(matchName, syncPar);
}
};
// Ostream operator
// Ostream Operator
Ostream& operator<<(Ostream& os, const IOobjectList& list);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -177,13 +177,14 @@ Foam::wordList Foam::IOobjectList::namesTypeImpl
}
// Templated implementation for sorted()
// Templated implementation for csorted(), csorted()
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::objectsTypeImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
const MatchPredicate& matchName,
const bool doSort
)
{
UPtrList<const IOobject> result(list.size());
@ -203,7 +204,10 @@ Foam::IOobjectList::objectsTypeImpl
result.resize(count);
if (doSort)
{
Foam::sort(result, nameOp<IOobject>()); // Sort by object name()
}
return result;
}
@ -472,19 +476,30 @@ Foam::label Foam::IOobjectList::count
template<class Type>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted() const
Foam::IOobjectList::cobjects() const
{
return objectsTypeImpl<Type>(*this, predicates::always());
// doSort = false
return objectsTypeImpl<Type>(*this, predicates::always(), false);
}
template<class Type>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted(const bool syncPar) const
Foam::IOobjectList::csorted() const
{
// doSort = true
return objectsTypeImpl<Type>(*this, predicates::always(), true);
}
template<class Type>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::csorted(const bool syncPar) const
{
UPtrList<const IOobject> list
(
objectsTypeImpl<Type>(*this, predicates::always())
// doSort = true
objectsTypeImpl<Type>(*this, predicates::always(), true)
);
checkObjectOrder(list, syncPar);
@ -495,18 +510,31 @@ Foam::IOobjectList::sorted(const bool syncPar) const
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted
Foam::IOobjectList::cobjects
(
const MatchPredicate& matchName
) const
{
return objectsTypeImpl<Type>(*this, matchName);
// doSort = false
return objectsTypeImpl<Type>(*this, matchName, false);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::sorted
Foam::IOobjectList::csorted
(
const MatchPredicate& matchName
) const
{
// doSort = true
return objectsTypeImpl<Type>(*this, matchName, true);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Foam::IOobject>
Foam::IOobjectList::csorted
(
const MatchPredicate& matchName,
const bool syncPar
@ -514,7 +542,8 @@ Foam::IOobjectList::sorted
{
UPtrList<const IOobject> list
(
objectsTypeImpl<Type>(*this, matchName)
// doSort = true
objectsTypeImpl<Type>(*this, matchName, true)
);
checkObjectOrder(list, syncPar);
@ -531,6 +560,7 @@ Foam::wordList Foam::IOobjectList::names
const MatchPredicate& matchClass
) const
{
// doSort = false
return namesImpl(*this, matchClass, predicates::always(), false);
}
@ -553,6 +583,7 @@ Foam::wordList Foam::IOobjectList::names
const MatchPredicate2& matchName
) const
{
// doSort = false
return namesImpl(*this, matchClass, matchName, false);
}
@ -572,6 +603,7 @@ Foam::wordList Foam::IOobjectList::names
template<class Type>
Foam::wordList Foam::IOobjectList::names() const
{
// doSort = false
return namesTypeImpl<Type>(*this, predicates::always(), false);
}
@ -589,6 +621,7 @@ Foam::wordList Foam::IOobjectList::names
const MatchPredicate& matchName
) const
{
// doSort = false
return namesTypeImpl<Type>(*this, matchName, false);
}

View File

@ -158,27 +158,6 @@ 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();

View File

@ -150,13 +150,24 @@ class objectRegistry
const bool doSort
);
//- Templated implementation for sorted()
//- Templated implementation for csorted()/sorted()
// Called with 'Type' or 'const Type'
template<class Type, class MatchPredicate>
static UPtrList<Type> objectsTypeImpl
(
const bool strict, // Check with isType<Type>
const objectRegistry& list,
const MatchPredicate& matchName
const MatchPredicate& matchName,
const bool doSort // Sort the list by name
);
//- Templated implementation for lookupClass()
// Called with 'Type' or 'const Type'
template<class Type>
static HashTable<Type*> lookupClassTypeImpl
(
const bool strict, // Check with isType<Type>
const objectRegistry& list
);
@ -240,49 +251,69 @@ public:
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
// Sorted access
// List-wise access (unsorted)
//- Return sorted list of objects
//- Return unsorted list of objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const regIOobject> csorted() const;
template<class Type, bool Strict=false>
UPtrList<const Type> cobjects() const;
//- Return sorted list of objects
//- Return unsorted list of objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const regIOobject> sorted() const;
template<class Type, bool Strict=false>
UPtrList<Type> objects();
//- Return sorted list of objects
//- Return unsorted 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!
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();
template<class Type, class MatchPredicate>
UPtrList<const Type> cobjects(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> objects(const MatchPredicate& matchName);
// List-wise access (sorted)
//- Return sorted list of objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
// The lifetime of the returned content cannot exceed the parent!
template<class Type, bool Strict=false>
UPtrList<const Type> csorted() const;
//- Return sorted list of objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
// The lifetime of the returned content cannot exceed the parent!
template<class Type, bool Strict=false>
UPtrList<Type> sorted();
//- Return sorted list of objects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<const regIOobject> csorted() const
{
return csorted<regIOobject>();
}
//- Return sorted list of objects
// The lifetime of the returned content cannot exceed the parent!
UPtrList<regIOobject> sorted()
{
return sorted<regIOobject>();
}
//- 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.
//- 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);
@ -351,7 +382,7 @@ public:
const MatchPredicate2& matchName
) const;
//- The unsorted 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>
@ -417,17 +448,27 @@ public:
) const;
//- Return all objects with a class satisfying \c isA\<Type\>
//
// \param strict use \c isType\<Type\> instead of \c isA\<Type\>
template<class Type>
HashTable<const Type*> lookupClass(const bool strict = false) const;
//- Return all objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
template<class Type, bool Strict=false>
HashTable<const Type*> lookupClass() const;
//- Return all objects with a class satisfying
//- \c isA\<Type\> or \c isType\<Type\> (with Strict)
template<class Type, bool Strict=false>
HashTable<Type*> lookupClass();
//- Return all objects with a class satisfying \c isA\<Type\>
//
// \param strict use \c isType\<Type\> instead of \c isA\<Type\>
template<class Type>
HashTable<Type*> lookupClass(const bool strict = false);
HashTable<const Type*> lookupClass(const bool strict) const;
//- Return all objects with a class satisfying \c isA\<Type\>
//
// \param strict use \c isType\<Type\> instead of \c isA\<Type\>
template<class Type>
HashTable<Type*> lookupClass(const bool strict);
//- Return const pointer to the regIOobject.
//
@ -673,6 +714,32 @@ public:
{
return this->getObjectPtr<Type>(name, recursive);
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const Type> sorted() const
{
return csorted<Type>();
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const regIOobject> sorted() const
{
return csorted<regIOobject>();
}
//- Deprecated(2023-07) use csorted() method
// \deprecated(2023-07) - use csorted() method
template<class Type, class MatchPredicate>
FOAM_DEPRECATED_FOR(2023-07, "csorted() method")
UPtrList<const Type> sorted(const MatchPredicate& matchName) const
{
return csorted<Type>(matchName);
}
};

View File

@ -184,13 +184,15 @@ Foam::wordList Foam::objectRegistry::namesTypeImpl
}
// Templated implementation for sorted()
// Templated implementation for cobjects()/objects(), csorted()/sorted()
template<class Type, class MatchPredicate>
Foam::UPtrList<Type>
Foam::objectRegistry::objectsTypeImpl
(
const bool strict,
const objectRegistry& list,
const MatchPredicate& matchName
const MatchPredicate& matchName,
const bool doSort
)
{
typedef typename std::remove_cv<Type>::type BaseType;
@ -200,9 +202,15 @@ Foam::objectRegistry::objectsTypeImpl
label count = 0;
forAllConstIters(list, iter)
{
const BaseType* ptr = Foam::isA<BaseType>(*iter.val());
const regIOobject* obj = iter.val();
const BaseType* ptr = dynamic_cast<const BaseType*>(obj);
if (ptr && matchName(ptr->name()))
if
(
ptr
&& (!strict || Foam::isType<BaseType>(*obj))
&& matchName(ptr->name())
)
{
result.set(count, const_cast<BaseType*>(ptr));
++count;
@ -211,7 +219,42 @@ Foam::objectRegistry::objectsTypeImpl
result.resize(count);
if (doSort)
{
Foam::sort(result, nameOp<Type>()); // Sort by object name()
}
return result;
}
// Templated implementation for lookupClass()
template<class Type>
Foam::HashTable<Type*>
Foam::objectRegistry::lookupClassTypeImpl
(
const bool strict,
const objectRegistry& list
)
{
typedef typename std::remove_cv<Type>::type BaseType;
HashTable<Type*> result(list.capacity());
forAllConstIters(list, iter)
{
const regIOobject* obj = iter.val();
const BaseType* ptr = dynamic_cast<const BaseType*>(obj);
if
(
ptr
&& (!strict || Foam::isType<BaseType>(*obj))
)
{
result.insert(obj->name(), const_cast<BaseType*>(ptr));
}
}
return result;
}
@ -294,27 +337,71 @@ Foam::label Foam::objectRegistry::count
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
template<class Type, bool Strict>
Foam::UPtrList<const Type>
Foam::objectRegistry::cobjects() const
{
return objectsTypeImpl<const Type>
(
Strict, *this, predicates::always(), false // doSort = false
);
}
template<class Type, bool Strict>
Foam::UPtrList<Type>
Foam::objectRegistry::objects()
{
return objectsTypeImpl<Type>
(
Strict, *this, predicates::always(), false // doSort = false
);
}
template<class Type, bool Strict>
Foam::UPtrList<const Type>
Foam::objectRegistry::csorted() const
{
return objectsTypeImpl<const Type>(*this, predicates::always());
return objectsTypeImpl<const Type>
(
Strict, *this, predicates::always(), true // doSort = true
);
}
template<class Type>
Foam::UPtrList<const Type>
Foam::objectRegistry::sorted() const
{
return objectsTypeImpl<const Type>(*this, predicates::always());
}
template<class Type>
template<class Type, bool Strict>
Foam::UPtrList<Type>
Foam::objectRegistry::sorted()
{
return objectsTypeImpl<Type>(*this, predicates::always());
return objectsTypeImpl<Type>
(
Strict, *this, predicates::always(), true // doSort = false
);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<const Type>
Foam::objectRegistry::cobjects
(
const MatchPredicate& matchName
) const
{
// doSort = false
return objectsTypeImpl<const Type>(false, *this, matchName, false);
}
template<class Type, class MatchPredicate>
Foam::UPtrList<Type>
Foam::objectRegistry::objects
(
const MatchPredicate& matchName
)
{
// doSort = false
return objectsTypeImpl<Type>(false, *this, matchName, false);
}
@ -325,20 +412,10 @@ Foam::objectRegistry::csorted
const MatchPredicate& matchName
) const
{
return objectsTypeImpl<const Type>(*this, matchName);
return objectsTypeImpl<const Type>(false, *this, matchName, true);
}
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
@ -346,7 +423,7 @@ Foam::objectRegistry::sorted
const MatchPredicate& matchName
)
{
return objectsTypeImpl<Type>(*this, matchName);
return objectsTypeImpl<Type>(false, *this, matchName, true);
}
@ -428,30 +505,27 @@ Foam::wordList Foam::objectRegistry::sortedNames
}
template<class Type, bool Strict>
Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass() const
{
return lookupClassTypeImpl<const Type>(Strict, *this);
}
template<class Type, bool Strict>
Foam::HashTable<Type*> Foam::objectRegistry::lookupClass()
{
return lookupClassTypeImpl<Type>(Strict, *this);
}
template<class Type>
Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
(
const bool strict
) const
{
HashTable<const Type*> objectsOfClass(size());
forAllConstIters(*this, iter)
{
const regIOobject* obj = iter.val();
if
(
strict
? bool(Foam::isType<Type>(*obj))
: bool(Foam::isA<Type>(*obj))
)
{
objectsOfClass.insert(obj->name(), dynamic_cast<const Type*>(obj));
}
}
return objectsOfClass;
return lookupClassTypeImpl<const Type>(strict, *this);
}
@ -461,24 +535,7 @@ Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
const bool strict
)
{
HashTable<Type*> objectsOfClass(size());
forAllIters(*this, iter)
{
regIOobject* obj = iter.val();
if
(
strict
? bool(Foam::isType<Type>(*obj))
: bool(Foam::isA<Type>(*obj))
)
{
objectsOfClass.insert(obj->name(), dynamic_cast<Type*>(obj));
}
}
return objectsOfClass;
return lookupClassTypeImpl<Type>(strict, *this);
}

View File

@ -206,7 +206,7 @@ void Foam::meshObject::updateMesh(objectRegistry& obr, const mapPolyMesh& mpm)
forAllIters(meshObjects, iter)
{
// isA<MoveableMeshObject<Mesh>>
// isA<UpdateableMeshObject<Mesh>>
auto* objectPtr = dynamic_cast<UpdateableMeshObject<Mesh>*>(*iter);
if (objectPtr)
@ -277,7 +277,7 @@ void Foam::meshObject::clearUpto(objectRegistry& obr)
forAllIters(meshObjects, iter)
{
// isA<ToType<Mesh>
// isA<ToType<Mesh>>
auto* objectPtr = dynamic_cast<ToType<Mesh>*>(*iter);
if (!objectPtr)