ENH: explicitly handle empty name lookup (objectRegistry, IOobjectList)

- provides some additional safety and precludes any recursive searching

ENH: support local directory handling in regionProperties

- allows alternative locations. Eg, for finite-area (#3419)
This commit is contained in:
Mark Olesen
2025-08-18 14:27:58 +02:00
parent 1f5eb55eeb
commit 828693bc90
9 changed files with 102 additions and 92 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -101,8 +101,6 @@ Foam::IOobjectList::IOobjectList
const fileName& local, const fileName& local,
IOobjectOption ioOpt IOobjectOption ioOpt
) )
:
HashPtrTable<IOobject>()
{ {
word newInstance; word newInstance;
fileNameList objNames = fileHandler().readObjects fileNameList objNames = fileHandler().readObjects
@ -113,6 +111,8 @@ Foam::IOobjectList::IOobjectList
newInstance newInstance
); );
HashPtrTable<IOobject>::reserve(objNames.size());
for (const auto& objName : objNames) for (const auto& objName : objNames)
{ {
auto objectPtr = autoPtr<IOobject>::New auto objectPtr = autoPtr<IOobject>::New
@ -141,7 +141,7 @@ Foam::IOobjectList::IOobjectList
if (ok) if (ok)
{ {
insert(objectPtr->name(), objectPtr); insert(objectPtr->name(), std::move(objectPtr));
} }
} }
} }
@ -157,8 +157,11 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject
// Like HashPtrTable::get(), or lookup() with a nullptr // Like HashPtrTable::get(), or lookup() with a nullptr
const IOobject* io = nullptr; const IOobject* io = nullptr;
const const_iterator iter(cfind(objName)); if (objName.empty())
if (iter.good()) {
return nullptr;
}
else if (auto iter = cfind(objName); iter.good())
{ {
io = iter.val(); io = iter.val();
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2024 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -172,7 +172,7 @@ public:
IOobjectList() noexcept = default; IOobjectList() noexcept = default;
//- Construct empty without allocation (capacity=0) //- Construct empty without allocation (capacity=0)
inline explicit IOobjectList(const Foam::zero) noexcept; explicit IOobjectList(Foam::zero) noexcept {}
//- Construct empty with initial table capacity //- Construct empty with initial table capacity
inline explicit IOobjectList(const label initialCapacity); inline explicit IOobjectList(const label initialCapacity);
@ -181,7 +181,7 @@ public:
inline IOobjectList(const IOobjectList& list); inline IOobjectList(const IOobjectList& list);
//- Move construct //- Move construct
inline IOobjectList(IOobjectList&& list); inline IOobjectList(IOobjectList&& list) noexcept;
//- Construct from registry, instance, io options //- Construct from registry, instance, io options
inline IOobjectList inline IOobjectList

View File

@ -27,12 +27,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::IOobjectList::IOobjectList(const Foam::zero) noexcept
:
HashPtrTable<IOobject>(Foam::zero{})
{}
inline Foam::IOobjectList::IOobjectList(const label initialCapacity) inline Foam::IOobjectList::IOobjectList(const label initialCapacity)
: :
HashPtrTable<IOobject>(initialCapacity) HashPtrTable<IOobject>(initialCapacity)
@ -45,7 +39,7 @@ inline Foam::IOobjectList::IOobjectList(const IOobjectList& list)
{} {}
inline Foam::IOobjectList::IOobjectList(IOobjectList&& list) inline Foam::IOobjectList::IOobjectList(IOobjectList&& list) noexcept
: :
HashPtrTable<IOobject>(std::move(list)) HashPtrTable<IOobject>(std::move(list))
{} {}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2023 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -38,7 +38,8 @@ Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
const MatchPredicate& matchName const MatchPredicate& matchName
) )
{ {
HashTable<wordHashSet> summary(2*list.size()); HashTable<wordHashSet> summary;
summary.reserve(16); // Relatively few types
// Summary (key,val) = (class-name, object-names) // Summary (key,val) = (class-name, object-names)
forAllConstIters(list, iter) forAllConstIters(list, iter)
@ -315,8 +316,11 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject
// Like HashPtrTable::get(), or lookup() with a nullptr // Like HashPtrTable::get(), or lookup() with a nullptr
const IOobject* io = nullptr; const IOobject* io = nullptr;
const const_iterator iter(cfind(objName)); if (objName.empty())
if (iter.good()) {
return nullptr;
}
else if (auto iter = cfind(objName); iter.good())
{ {
io = iter.val(); io = iter.val();
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2019 OpenFOAM Foundation Copyright (C) 2011-2019 OpenFOAM Foundation
Copyright (C) 2015-2024 OpenCFD Ltd. Copyright (C) 2015-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -513,9 +513,11 @@ const Foam::regIOobject* Foam::objectRegistry::cfindIOobject
const bool recursive const bool recursive
) const ) const
{ {
const_iterator iter = cfind(name); if (name.empty())
{
if (iter.good()) return nullptr;
}
else if (auto iter = cfind(name); iter.good())
{ {
return iter.val(); return iter.val();
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2019 OpenFOAM Foundation Copyright (C) 2011-2019 OpenFOAM Foundation
Copyright (C) 2016-2024 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -457,14 +457,11 @@ public:
// Lookup // Lookup
//- Lookup and return a const sub-objectRegistry. //- Lookup and return a const sub-objectRegistry.
//
// \param forceCreate create it if it does not exist.
// \param recursive search parent registries.
const objectRegistry& subRegistry const objectRegistry& subRegistry
( (
const word& name, const word& name,
const bool forceCreate = false, const bool forceCreate = false, //!< Create if it does not exist
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
@ -492,101 +489,90 @@ public:
//- Return const pointer to the regIOobject. //- Return const pointer to the regIOobject.
// //
// \param recursive search parent registries // \return nullptr if the object was not found (or name == "")
//
// \return nullptr if the object was not found.
const regIOobject* cfindIOobject const regIOobject* cfindIOobject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Does the registry contain the regIOobject object (by name). //- Does the registry contain the regIOobject object (by name).
// // \return false if the object was not found (or name == "")
// \param name the object name bool contains
// \param recursive search parent registries (
bool contains(const word& name, const bool recursive = false) const; const word& name, //!< The object name
const bool recursive = false //!< Search parent registries
) const;
//- Is the named Type found? //- Contains the named Type?
// //
// \param recursive search parent registries // \return false if the object was not found (or name == "")
// or had incorrect type.
template<class Type> template<class Type>
bool foundObject bool foundObject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Return const pointer to the object of the given Type. //- Return const pointer to the object of the given Type.
// //
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type. // \return nullptr if the object was not found or had incorrect type.
template<class Type> template<class Type>
const Type* cfindObject const Type* cfindObject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Return const pointer to the object of the given Type. //- Return const pointer to the object of the given Type.
// //
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type. // \return nullptr if the object was not found or had incorrect type.
template<class Type> template<class Type>
const Type* findObject const Type* findObject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Return non-const pointer to the object of the given Type. //- Return non-const pointer to the object of the given Type.
// //
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type. // \return nullptr if the object was not found or had incorrect type.
template<class Type> template<class Type>
Type* findObject Type* findObject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
); );
//- Return non-const pointer to the object of the given Type, //- Return non-const pointer to the object of the given Type,
//- using a const-cast to have it behave like a mutable. //- using a const-cast to have it behave like a mutable.
// Exercise caution when using. // Exercise caution when using.
// //
// \param recursive search parent registries.
//
// \return nullptr if the object was not found or had incorrect type. // \return nullptr if the object was not found or had incorrect type.
template<class Type> template<class Type>
Type* getObjectPtr Type* getObjectPtr
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Lookup and return const reference to the object //- Lookup and return const reference to the object
//- of the given Type. Fatal if not found or the wrong type. //- of the given Type. Fatal if not found or the wrong type.
//
// \param recursive search parent registries.
template<class Type> template<class Type>
const Type& lookupObject const Type& lookupObject
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;
//- Lookup and return non-const reference to the object //- Lookup and return non-const reference to the object
//- of the given Type. Fatal if not found or the wrong type. //- of the given Type. Fatal if not found or the wrong type.
//
// \param recursive search parent registries.
template<class Type> template<class Type>
Type& lookupObjectRef Type& lookupObjectRef
( (
const word& name, const word& name, //!< The object name
const bool recursive = false const bool recursive = false //!< Search parent registries
) const; ) const;

View File

@ -40,7 +40,8 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
const MatchPredicate& matchName const MatchPredicate& matchName
) )
{ {
HashTable<wordHashSet> summary(2*list.size()); HashTable<wordHashSet> summary;
summary.reserve(16); // Relatively few types
// Summary (key,val) = (class-name, object-names) // Summary (key,val) = (class-name, object-names)
forAllConstIters(list, iter) forAllConstIters(list, iter)
@ -601,9 +602,7 @@ const Type& Foam::objectRegistry::lookupObject
const bool recursive const bool recursive
) const ) const
{ {
const_iterator iter = cfind(name); if (auto iter = cfind(name); iter.good())
if (iter.good())
{ {
const Type* ptr = dynamic_cast<const Type*>(iter.val()); const Type* ptr = dynamic_cast<const Type*>(iter.val());

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012 OpenFOAM Foundation Copyright (C) 2012 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -32,15 +32,10 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::regionProperties::regionProperties(const Time& runTime)
:
regionProperties(runTime, IOobject::MUST_READ_IF_MODIFIED)
{}
Foam::regionProperties::regionProperties Foam::regionProperties::regionProperties
( (
const Time& runTime, const Time& runTime,
const fileName& local,
IOobjectOption::readOption rOpt IOobjectOption::readOption rOpt
) )
{ {
@ -52,19 +47,28 @@ Foam::regionProperties::regionProperties
( (
"regionProperties", "regionProperties",
runTime.time().constant(), runTime.time().constant(),
local,
runTime.db(), runTime.db(),
rOpt, rOpt,
IOobjectOption::NO_WRITE IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
) )
); );
if (IOobjectOption::isReadRequired(rOpt) || iodict.size()) iodict.readEntry("regions", props, keyType::LITERAL, rOpt);
{
iodict.readEntry("regions", props);
}
} }
Foam::regionProperties::regionProperties
(
const Time& runTime,
IOobjectOption::readOption rOpt
)
:
regionProperties(runTime, fileName::null, rOpt)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::label Foam::regionProperties::count() const Foam::label Foam::regionProperties::count() const
@ -90,9 +94,9 @@ Foam::wordList Foam::regionProperties::names() const
const HashTable<wordList>& props = *this; const HashTable<wordList>& props = *this;
for (const word& grp : props.sortedToc()) for (const auto& iter : props.csorted())
{ {
for (const word& name : props[grp]) for (const word& name : iter.val())
{ {
list[n] = name; list[n] = name;
++n; ++n;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -34,6 +34,17 @@ Description
is no assumption on this level that a region should only have a single is no assumption on this level that a region should only have a single
set of physics. set of physics.
Uses the "regions" table from the constant/regionProperties file.
For example,
\verbatim
regions
(
fluid (air water)
solid (walls)
);
\endverbatim
SourceFiles SourceFiles
regionProperties.C regionProperties.C
@ -43,8 +54,9 @@ SourceFiles
#define Foam_regionProperties_H #define Foam_regionProperties_H
#include "HashTable.H" #include "HashTable.H"
#include "fileName.H"
#include "wordList.H" #include "wordList.H"
#include "IOobject.H" #include "IOobjectOption.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -66,14 +78,21 @@ public:
// Constructors // Constructors
//- Construct from Time
explicit regionProperties(const Time& runTime);
//- Construct from Time with specified read option //- Construct from Time with specified read option
//- (default: MUST_READ)
explicit regionProperties
(
const Time& runTime,
IOobjectOption::readOption rOpt = IOobjectOption::MUST_READ
);
//- Construct from Time and local (prefix) with specified read option
//- (default: MUST_READ)
regionProperties regionProperties
( (
const Time& runTime, const Time& runTime,
IOobjectOption::readOption rOpt const fileName& local,
IOobjectOption::readOption rOpt = IOobjectOption::MUST_READ
); );
@ -91,7 +110,6 @@ public:
//- The region names in sorted order. //- The region names in sorted order.
wordList sortedNames() const; wordList sortedNames() const;
}; };