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 |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd.
Copyright (C) 2016-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -101,8 +101,6 @@ Foam::IOobjectList::IOobjectList
const fileName& local,
IOobjectOption ioOpt
)
:
HashPtrTable<IOobject>()
{
word newInstance;
fileNameList objNames = fileHandler().readObjects
@ -113,6 +111,8 @@ Foam::IOobjectList::IOobjectList
newInstance
);
HashPtrTable<IOobject>::reserve(objNames.size());
for (const auto& objName : objNames)
{
auto objectPtr = autoPtr<IOobject>::New
@ -141,7 +141,7 @@ Foam::IOobjectList::IOobjectList
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
const IOobject* io = nullptr;
const const_iterator iter(cfind(objName));
if (iter.good())
if (objName.empty())
{
return nullptr;
}
else if (auto iter = cfind(objName); iter.good())
{
io = iter.val();
}

View File

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

View File

@ -27,12 +27,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::IOobjectList::IOobjectList(const Foam::zero) noexcept
:
HashPtrTable<IOobject>(Foam::zero{})
{}
inline Foam::IOobjectList::IOobjectList(const label 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))
{}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2023 OpenCFD Ltd.
Copyright (C) 2018-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,7 +38,8 @@ Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
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)
forAllConstIters(list, iter)
@ -315,8 +316,11 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject
// Like HashPtrTable::get(), or lookup() with a nullptr
const IOobject* io = nullptr;
const const_iterator iter(cfind(objName));
if (iter.good())
if (objName.empty())
{
return nullptr;
}
else if (auto iter = cfind(objName); iter.good())
{
io = iter.val();
}

View File

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

View File

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

View File

@ -40,7 +40,8 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
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)
forAllConstIters(list, iter)
@ -601,9 +602,7 @@ const Type& Foam::objectRegistry::lookupObject
const bool recursive
) const
{
const_iterator iter = cfind(name);
if (iter.good())
if (auto iter = cfind(name); iter.good())
{
const Type* ptr = dynamic_cast<const Type*>(iter.val());

View File

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

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
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
set of physics.
Uses the "regions" table from the constant/regionProperties file.
For example,
\verbatim
regions
(
fluid (air water)
solid (walls)
);
\endverbatim
SourceFiles
regionProperties.C
@ -43,8 +54,9 @@ SourceFiles
#define Foam_regionProperties_H
#include "HashTable.H"
#include "fileName.H"
#include "wordList.H"
#include "IOobject.H"
#include "IOobjectOption.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -66,14 +78,21 @@ public:
// Constructors
//- Construct from Time
explicit regionProperties(const Time& runTime);
//- 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
(
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.
wordList sortedNames() const;
};